1 /* This file is part of status-area-applet-tor.
3 * Copyright (C) 2010 Philipp Zabel
5 * status-area-applet-tor is free software: you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * status-area-applet-tor is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with status-area-applet-tor. If not, see <http://www.gnu.org/licenses/>.
21 public bool use_http_proxy;
22 public string http_host;
23 public string socks_host;
24 public string secure_host;
26 public int socks_port;
27 public int secure_port;
31 class TorStatusMenuItem : HD.StatusMenuItem {
32 private const string STATUSMENU_TOR_LIBOSSO_SERVICE_NAME = "tor_status_menu_item";
34 private const int STATUS_MENU_ICON_SIZE = 48;
35 private const int STATUS_AREA_ICON_SIZE = 18;
37 private const string GCONF_DIR_TOR = "/apps/maemo/tor";
38 private const string GCONF_KEY_TOR_ENABLED = GCONF_DIR_TOR + "/enabled";
39 private const string GCONF_KEY_BRIDGES = GCONF_DIR_TOR + "/bridges";
41 private const string GCONF_DIR_PROXY_HTTP = "/system/http_proxy";
42 private const string GCONF_KEY_PROXY_HTTP_ENABLED = GCONF_DIR_PROXY_HTTP + "/use_http_proxy";
43 private const string GCONF_KEY_PROXY_HTTP_HOST = GCONF_DIR_PROXY_HTTP + "/host";
44 private const string GCONF_KEY_PROXY_HTTP_PORT = GCONF_DIR_PROXY_HTTP + "/port";
46 private const string GCONF_DIR_PROXY = "/system/proxy";
47 private const string GCONF_KEY_PROXY_MODE = GCONF_DIR_PROXY + "/mode";
48 private const string GCONF_KEY_PROXY_SOCKS_HOST = GCONF_DIR_PROXY + "/socks_host";
49 private const string GCONF_KEY_PROXY_SOCKS_PORT = GCONF_DIR_PROXY + "/socks_port";
50 private const string GCONF_KEY_PROXY_SECURE_HOST = GCONF_DIR_PROXY + "/secure_host";
51 private const string GCONF_KEY_PROXY_SECURE_PORT = GCONF_DIR_PROXY + "/secure_port";
57 Gdk.Pixbuf icon_connecting;
58 Gdk.Pixbuf icon_connected;
59 Gtk.Image icon_enabled;
60 Gtk.Image icon_disabled;
62 // ConIc, GConf and Osso context
65 ConIc.Connection conic;
78 * Update status area icon and status menu button value
80 private void update_status () {
81 if (tor_enabled && tor_connected && icon_connected == null) try {
82 var icon_theme = Gtk.IconTheme.get_default ();
83 var pixbuf = icon_theme.load_icon ("statusarea_tor_connected",
84 STATUS_AREA_ICON_SIZE,
85 Gtk.IconLookupFlags.NO_SVG);
86 icon_connected = pixbuf;
90 if (tor_enabled && !tor_connected && icon_connecting == null) try {
91 var icon_theme = Gtk.IconTheme.get_default ();
92 var pixbuf = icon_theme.load_icon ("statusarea_tor_connecting",
93 STATUS_AREA_ICON_SIZE,
94 Gtk.IconLookupFlags.NO_SVG);
95 icon_connecting = pixbuf;
99 if (tor_enabled && icon_enabled == null) try {
100 var icon_theme = Gtk.IconTheme.get_default();
101 var pixbuf = icon_theme.load_icon ("statusarea_tor_enabled",
102 STATUS_MENU_ICON_SIZE,
103 Gtk.IconLookupFlags.NO_SVG);
104 icon_enabled = new Gtk.Image.from_pixbuf (pixbuf);
108 if (!tor_enabled && icon_disabled == null) try {
109 var icon_theme = Gtk.IconTheme.get_default();
110 var pixbuf = icon_theme.load_icon ("statusarea_tor_disabled",
111 STATUS_MENU_ICON_SIZE,
112 Gtk.IconLookupFlags.NO_SVG);
113 icon_disabled = new Gtk.Image.from_pixbuf (pixbuf);
118 if (conic_connected && tor_enabled) {
119 set_status_area_icon (tor_connected ? icon_connected : icon_connecting);
120 button.set_value (tor_connected ? _("Connected") : _("Connecting ..."));
122 set_status_area_icon (null);
123 button.set_value (tor_enabled ? _("Disconnected") : _("Disabled"));
125 button.set_image (tor_enabled ? icon_enabled : icon_disabled);
129 * Callback for Tor daemon line output
131 private bool tor_io_func (IOChannel source, IOCondition condition) {
133 if ((condition & (IOCondition.IN | IOCondition.PRI)) != 0) {
137 /* var status = */ source.read_line (out line, out length, null);
140 if ("[notice]" in line) {
141 if ("Bootstrapped 100%" in line) {
142 tor_connected = true;
148 Hildon.Banner.show_information (null, null, "DEBUG: %s".printf (line));
152 Hildon.Banner.show_information (null, null, "Error: %s".printf (e.message));
155 if ((condition & (IOCondition.ERR | IOCondition.HUP | IOCondition.NVAL)) != 0) {
162 * Start Tor and setup proxy settings
164 private void start_tor () {
166 if (tor_pid == (Pid) 0) {
167 Process.spawn_async_with_pipes ("/tmp",
170 SpawnFlags.SEARCH_PATH,
176 var channel = new IOChannel.unix_new (tor_stdout);
177 channel.add_watch (IOCondition.IN | IOCondition.PRI | IOCondition.ERR | IOCondition.HUP | IOCondition.NVAL, tor_io_func);
179 if (polipo_pid == (Pid) 0) {
180 Process.spawn_async_with_pipes ("/tmp",
181 { "/usr/bin/polipo" },
183 SpawnFlags.SEARCH_PATH,
188 /* --> proxy settings and will be set up and tor_connected will
189 * be set to true once Tor signals 100%
191 } catch (SpawnError e) {
192 Hildon.Banner.show_information (null, null, "DEBUG: Failed to spawn polipo and tor: %s".printf (e.message));
201 * Stop Tor and revert proxy settings
203 private void stop_tor () {
205 tor_connected = false;
206 if (polipo_pid != (Pid) 0) {
207 Process.close_pid (polipo_pid);
208 Posix.kill ((Posix.pid_t) polipo_pid, Posix.SIGKILL);
209 polipo_pid = (Pid) 0;
211 if (tor_pid != (Pid) 0) {
212 Process.close_pid (tor_pid);
213 Posix.kill ((Posix.pid_t) tor_pid, Posix.SIGKILL);
221 * Setup proxy settings to route through the Tor network
223 private void proxy_setup () {
224 if (backup == null) try {
225 backup = new ProxyBackup ();
226 backup.use_http_proxy = gconf.get_bool (GCONF_KEY_PROXY_HTTP_ENABLED);
228 backup.http_host = gconf.get_string (GCONF_KEY_PROXY_HTTP_HOST);
229 backup.socks_host = gconf.get_string (GCONF_KEY_PROXY_SOCKS_HOST);
230 backup.secure_host = gconf.get_string (GCONF_KEY_PROXY_SECURE_HOST);
231 backup.http_port = gconf.get_int (GCONF_KEY_PROXY_HTTP_PORT);
232 backup.socks_port = gconf.get_int (GCONF_KEY_PROXY_SOCKS_PORT);
233 backup.secure_port = gconf.get_int (GCONF_KEY_PROXY_SECURE_PORT);
235 backup.mode = gconf.get_string (GCONF_KEY_PROXY_MODE);
237 error ("Error saving proxy settings: %s", e.message);
238 backup = new ProxyBackup ();
239 backup.use_http_proxy = false;
241 backup.http_host = "";
242 backup.socks_host = "";
243 backup.secure_host = "";
244 backup.http_port = 8080;
245 backup.socks_port = 0;
246 backup.secure_port = 0;
248 backup.mode = "none";
251 // Hildon.Banner.show_information (null, null, "DEBUG: Proxy setup");
252 gconf.set_bool (GCONF_KEY_PROXY_HTTP_ENABLED, true);
254 gconf.set_string (GCONF_KEY_PROXY_HTTP_HOST, "127.0.0.1");
255 gconf.set_string (GCONF_KEY_PROXY_SOCKS_HOST, "127.0.0.1");
256 gconf.set_string (GCONF_KEY_PROXY_SECURE_HOST, "127.0.0.1");
257 gconf.set_int (GCONF_KEY_PROXY_HTTP_PORT, 8118);
258 gconf.set_int (GCONF_KEY_PROXY_SOCKS_PORT, 9050);
259 gconf.set_int (GCONF_KEY_PROXY_SECURE_PORT, 8118);
261 gconf.set_string (GCONF_KEY_PROXY_MODE, "manual");
263 error ("Error changing proxy settings: %s", e.message);
268 * Revert proxy settings
270 private void proxy_restore () {
271 if (backup != null) try {
272 // Hildon.Banner.show_information (null, null, "DEBUG: Restoring proxy settings");
273 gconf.set_bool (GCONF_KEY_PROXY_HTTP_ENABLED, backup.use_http_proxy);
275 gconf.set_string (GCONF_KEY_PROXY_HTTP_HOST, backup.http_host);
276 gconf.set_string (GCONF_KEY_PROXY_SOCKS_HOST, backup.socks_host);
277 gconf.set_string (GCONF_KEY_PROXY_SECURE_HOST, backup.secure_host);
278 gconf.set_int (GCONF_KEY_PROXY_HTTP_PORT, backup.http_port);
279 gconf.set_int (GCONF_KEY_PROXY_SOCKS_PORT, backup.socks_port);
280 gconf.set_int (GCONF_KEY_PROXY_SECURE_PORT, backup.secure_port);
282 gconf.set_string (GCONF_KEY_PROXY_MODE, backup.mode);
285 error ("Error restoring proxy: %s", e.message);
290 * Show the bridge relay configuration dialog
292 private const int RESPONSE_NEW = 1;
293 private void bridges_clicked_cb () {
294 var dialog = new Gtk.Dialog ();
295 var content = (Gtk.VBox) dialog.get_content_area ();
296 content.set_size_request (-1, 5*70);
298 dialog.set_title (_("Bridge relays"));
300 var bridges = new SList<string> ();
302 bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
304 error ("Error loading bridges: %s", e.message);
307 var list_store = new Gtk.ListStore (1, typeof (string));
309 foreach (string bridge in bridges) {
310 list_store.append (out iter);
311 list_store.@set (iter, 0, bridge);
314 var pannable_area = new Hildon.PannableArea ();
315 var tree_view = new Gtk.TreeView.with_model (list_store);
316 var renderer = new Gtk.CellRendererText ();
317 var column = new Gtk.TreeViewColumn.with_attributes ("IP", renderer, "text", 0);
318 tree_view.append_column (column);
319 pannable_area.add (tree_view);
320 content.pack_start (pannable_area, true, true, 0);
322 tree_view.row_activated.connect ((path, column) => {
323 bridge_edit_dialog (list_store, path);
326 dialog.add_button (_("New"), RESPONSE_NEW);
327 dialog.response.connect ((response_id) => {
328 if (response_id == RESPONSE_NEW) {
329 bridge_edit_dialog (list_store, null);
337 * Show the bridge relay edit dialog
339 private const int RESPONSE_DELETE = 1;
340 private void bridge_edit_dialog (Gtk.ListStore store, Gtk.TreePath? path) {
341 var dialog = new Gtk.Dialog ();
342 var content = (Gtk.VBox) dialog.get_content_area ();
345 dialog.set_title (_("New bridge relay"));
347 dialog.set_title (_("Edit bridge relay"));
349 var size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
351 var hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
352 var label = new Gtk.Label (_("IP address"));
353 label.set_alignment (0, 0.5f);
354 size_group.add_widget (label);
355 hbox.pack_start (label, false, false, 0);
356 var ip_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
357 Hildon.gtk_entry_set_input_mode (ip_entry, Hildon.GtkInputMode.NUMERIC |
358 Hildon.GtkInputMode.SPECIAL);
359 hbox.pack_start (ip_entry, true, true, 0);
360 content.pack_start (hbox, false, false, 0);
362 hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
363 label = new Gtk.Label (_("Port"));
364 label.set_alignment (0, 0.5f);
365 size_group.add_widget (label);
366 hbox.pack_start (label, false, false, 0);
367 var port_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
368 Hildon.gtk_entry_set_input_mode (port_entry, Hildon.GtkInputMode.NUMERIC);
369 hbox.pack_start (port_entry, true, true, 0);
370 content.pack_start (hbox, true, true, 0);
372 hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
373 label = new Gtk.Label (_("Fingerprint"));
374 label.set_alignment (0, 0.5f);
375 size_group.add_widget (label);
376 hbox.pack_start (label, false, false, 0);
377 var fingerprint_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
378 Hildon.gtk_entry_set_input_mode (fingerprint_entry, Hildon.GtkInputMode.HEXA);
379 hbox.pack_start (fingerprint_entry, true, true, 0);
380 content.pack_start (hbox, true, true, 0);
382 var iter = Gtk.TreeIter ();
384 port_entry.set_text ("443");
385 } else if (store.get_iter (out iter, path)) {
387 store.@get (iter, 0, out tmp);
388 string[] ip_port = tmp.split (":");
389 if (ip_port.length == 2) {
390 ip_entry.set_text (ip_port[0]);
391 port_entry.set_text (ip_port[1]);
394 dialog.add_button (_("Delete"), RESPONSE_DELETE);
396 dialog.add_button (_("Save"), Gtk.ResponseType.OK);
397 dialog.response.connect ((response_id) => {
398 var bridges = new SList<string> ();
400 if (response_id == RESPONSE_DELETE) {
404 if (store.get_iter_first (out iter)) do {
405 store.@get (iter, 0, out bridge);
406 bridges.append (bridge);
407 } while (store.iter_next (ref iter));
408 gconf.set_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING,
413 if (response_id == Gtk.ResponseType.OK) {
414 if (!is_valid_ip_address (ip_entry.get_text ())) {
415 Hildon.Banner.show_information (dialog, null,
416 _("Invalid IP address"));
419 int port = port_entry.get_text ().to_int ();
420 if (port < 0 || port > 65565) {
421 Hildon.Banner.show_information (dialog, null,
422 _("Invalid port number"));
426 store.append (out iter);
428 store.@set (iter, 0, "%s:%d".printf (ip_entry.get_text (), port));
430 bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
432 bridges.append ("%s:%d".printf (ip_entry.get_text (), port));
436 if (store.get_iter_first (out iter)) do {
437 store.@get (iter, 0, out bridge);
438 bridges.append (bridge);
439 } while (store.iter_next (ref iter));
441 gconf.set_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING, bridges);
451 * Check whether the IP address consists of four numbers in the 0..255 range
453 bool is_valid_ip_address (string address) {
454 string[] ip = address.split (".");
459 for (int i = 0; i < ip.length; i++) {
460 int n = ip[i].to_int ();
461 if (n < 0 || n > 255)
469 * Show the Tor log dialog
471 private void show_tor_log () {
472 var dialog = new Gtk.Dialog ();
473 var content = (Gtk.VBox) dialog.get_content_area ();
474 content.set_size_request (-1, 5*70);
476 dialog.set_title (_("Log"));
478 var pannable = new Hildon.PannableArea ();
479 var label = new Gtk.Label (tor_log);
480 pannable.add_with_viewport (label);
481 content.pack_start (pannable, true, true, 0);
487 * Callback for the status menu button clicked signal
489 private const int RESPONSE_LOG = 1;
490 private void button_clicked_cb () {
491 var dialog = new Gtk.Dialog ();
492 var content = (Gtk.VBox) dialog.get_content_area ();
493 content.set_size_request (-1, 2*70);
495 dialog.set_title (_("Tor: anonymity online"));
497 var check = new Hildon.CheckButton (Hildon.SizeType.FINGER_HEIGHT);
498 check.set_label (_("Enable onion routing"));
499 check.set_active (tor_enabled);
500 content.pack_start (check, true, true, 0);
502 var button = new Hildon.Button.with_text (Hildon.SizeType.FINGER_HEIGHT,
503 Hildon.ButtonArrangement.VERTICAL,
506 button.set_style (Hildon.ButtonStyle.PICKER);
507 button.set_alignment (0, 0.5f, 0, 0.5f);
508 button.clicked.connect (bridges_clicked_cb);
509 content.pack_start (button, true, true, 0);
511 dialog.add_button (_("Log"), RESPONSE_LOG);
513 dialog.add_button (_("Save"), Gtk.ResponseType.ACCEPT);
514 dialog.response.connect ((response_id) => {
515 if (response_id == RESPONSE_LOG) {
519 if (response_id == Gtk.ResponseType.ACCEPT) {
520 if (!tor_enabled && check.get_active ()) {
523 if (conic_connected) {
526 conic.connect (ConIc.ConnectFlags.NONE);
528 } else if (tor_enabled && !check.get_active ()) {
542 private string get_bridge_list () {
544 var bridges = new SList<string> ();
546 bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
548 error ("Error loading bridges: %s", e.message);
550 foreach (string bridge in bridges) {
554 list += ", " + bridge;
563 * Callback for the ConIc connection-event signal
565 private void conic_connection_event_cb (ConIc.Connection conic, ConIc.ConnectionEvent event) {
566 var status = event.get_status ();
568 case ConIc.ConnectionStatus.CONNECTED:
569 conic_connected = true;
576 case ConIc.ConnectionStatus.DISCONNECTING:
577 conic_connected = false;
580 case ConIc.ConnectionStatus.DISCONNECTED:
581 case ConIc.ConnectionStatus.NETWORK_UP:
586 var error = event.get_error ();
588 case ConIc.ConnectionError.CONNECTION_FAILED:
589 Hildon.Banner.show_information (null, null, "DEBUG: ConIc connection failed");
591 case ConIc.ConnectionError.USER_CANCELED:
592 Hildon.Banner.show_information (null, null, "DEBUG: ConIc user canceled");
594 case ConIc.ConnectionError.NONE:
595 case ConIc.ConnectionError.INVALID_IAP:
601 private void create_widgets () {
602 // Status menu button
603 button = new Hildon.Button.with_text (Hildon.SizeType.FINGER_HEIGHT,
604 Hildon.ButtonArrangement.VERTICAL,
605 _("The Onion Router"),
606 tor_enabled ? _("Enabled") : _("Disabled"));
607 button.set_alignment (0.0f, 0.5f, 1.0f, 1.0f);
608 button.set_style (Hildon.ButtonStyle.PICKER);
609 button.clicked.connect (button_clicked_cb);
621 Intl.setlocale (LocaleCategory.ALL, "");
622 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
623 Intl.textdomain (Config.GETTEXT_PACKAGE);
626 gconf = GConf.Client.get_default ();
628 tor_enabled = gconf.get_bool (GCONF_KEY_TOR_ENABLED);
630 error ("Failed to get GConf setting: %s", e.message);
632 tor_connected = false;
635 conic = new ConIc.Connection ();
637 Hildon.Banner.show_information (null, null, "DEBUG: ConIc hook-up failed");
639 conic_connected = false;
640 conic.automatic_connection_events = true;
642 conic.connect (ConIc.ConnectFlags.AUTOMATICALLY_TRIGGERED);
643 conic.connection_event.connect (conic_connection_event_cb);
646 osso = new Osso.Context (STATUSMENU_TOR_LIBOSSO_SERVICE_NAME,
656 * Vala code can't use the HD_DEFINE_PLUGIN_MODULE macro, but it handles
657 * most of the class registration issues itself. Only this code from
658 * HD_PLUGIN_MODULE_SYMBOLS_CODE has to be has to be included manually
659 * to register with hildon-desktop:
662 public void hd_plugin_module_load (TypeModule plugin) {
663 // [ModuleInit] registers types automatically
664 ((HD.PluginModule) plugin).add_type (typeof (TorStatusMenuItem));
667 public void hd_plugin_module_unload (HD.PluginModule plugin) {