This repository has been archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.c
179 lines (153 loc) · 4.9 KB
/
window.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <stdbool.h>
#include "lib/jsmn.c"
#include "lib/json.c"
#include "lib/buf.c"
#include "lib/log.c"
#include "lib/helpers.c"
#include "openvpn.c"
#include "config.c"
#define IPINFO_URL "http://ipinfo.io/json"
#define UI_FILE "gui/night_watchman.ui"
#define TOP_WINDOW "connect_window"
#define CURRENT_DIR "/home/dmix/vpn/"
GtkWidget *window;
GtkBuilder *builder;
GtkWidget *connect_button;
GtkWidget *disconnect_button;
GtkWidget *file_chooser_widget;
GtkFileChooser *file_chooser;
GtkStatusbar *status;
GtkStatusbar *location;
char *config_path;
void update_status(char *text)
{
gtk_statusbar_push(status,
0,
text);
}
void update_location(char *text)
{
gtk_statusbar_push(location,
0,
text);
}
/**
* Fetch ip/location info from ipinfo.org
*/
void *find_location()
{
printf ("Fetching location\n");
// Fetch current IP address
char *js;
js = json_fetch(IPINFO_URL);
jsmntok_t *tokens = json_tokenise(js);
// Parse JSON response
char *ip = "n/a";
char *city = "n/a";
char *state = "n/a";
char *country = "n/a";
char *org = "n/a";
char *str_to_ret;
ip = json_token_tostr(js, &tokens[2]);
city = json_token_tostr(js, &tokens[6]);
state = json_token_tostr(js, &tokens[8]);
country = json_token_tostr(js, &tokens[10]);
org = json_token_tostr(js, &tokens[14]);
// Update status information to gui
char status_text[200];
sprintf(status_text, "Location: %s, %s %s => %s", city, state, country, org);
update_location(status_text);
pthread_exit(NULL);
}
void run_location_finder()
{
pthread_t thread[1];
memset(thread, 0, sizeof(thread));
pthread_create(&thread[0], NULL, find_location, NULL);
}
void destroy_window(GtkWidget *widget, gpointer data)
{
printf("Exiting");
gtk_main_quit ();
}
/**
* Create GTK window
*/
static GtkWidget* create_window(void)
{
GError* error = NULL;
/* Load UI from file */
builder = gtk_builder_new ();
if (!gtk_builder_add_from_file (builder, UI_FILE, &error))
{
g_critical ("Couldn't load builder file: %s", error->message);
g_error_free (error);
}
/* Auto-connect signal handlers */
gtk_builder_connect_signals(builder, NULL);
/* Get the window object from the ui file */
window = GTK_WIDGET(gtk_builder_get_object(builder, TOP_WINDOW));
gtk_window_set_title(GTK_WINDOW (window), "Night Watchman VPN");
/* Exit when the window is closed */
g_signal_connect(window, "destroy", G_CALLBACK(destroy_window), NULL);
/* Assign button signals */
connect_button = (GtkWidget*)gtk_builder_get_object(builder, "connect");
disconnect_button = (GtkWidget*)gtk_builder_get_object(builder, "disconnect");
file_chooser_widget = (GtkWidget*)gtk_builder_get_object(builder, "config");
file_chooser = (GtkFileChooser*)gtk_builder_get_object(builder, "config");
gtk_file_chooser_set_filename(file_chooser, config_path);
status = (GtkStatusbar*)gtk_builder_get_object(builder, "status");
update_status ("Status: Disconnected");
location = (GtkStatusbar*)gtk_builder_get_object(builder, "location");
update_location("Location: Checking...");
/* fetch initial location */
run_location_finder();
g_object_unref(builder);
return window;
}
void message_alert(char text[])
{
GtkWidget *dialog;
GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
dialog = gtk_message_dialog_new(NULL,
flags,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
NULL);
gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG (dialog),
text);
gtk_dialog_run(GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
void connect_clicked(GtkButton *button, gpointer user_data)
{
bool is_connected;
printf("connected clicked\n");
update_status("Status: Connecting...");
is_connected = spawn_openvpn(config_path);
if (is_connected) {
gtk_widget_set_sensitive(file_chooser_widget, false);
gtk_widget_set_sensitive(connect_button, false);
gtk_widget_set_sensitive(disconnect_button, true);
}
else {
message_alert("Please select an OpenVPN config path");
}
}
void disconnect_clicked(GtkButton *button, gpointer user_data)
{
update_status("Status: Disconnected");
gtk_widget_set_sensitive(file_chooser_widget, true);
gtk_widget_set_sensitive(connect_button, true);
gtk_widget_set_sensitive(disconnect_button, false);
run_location_finder();
}
void config_selected(GtkFileChooser *button, gpointer user_data)
{
config_path = gtk_file_chooser_get_filename(file_chooser);
write_config_file(config_path);
printf("config selected: %s\n", config_path);
}