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";
58 Gdk.Pixbuf icon_connecting;
59 Gdk.Pixbuf icon_connected;
60 Gtk.Image icon_enabled;
61 Gtk.Image icon_disabled;
63 // ConIc, GConf and Osso context
66 ConIc.Connection conic;
77 TorControl.Connection tor_control;
81 * Update status area icon and status menu button value
83 private void update_status () {
84 if (tor_enabled && tor_connected && icon_connected == null) try {
85 var icon_theme = Gtk.IconTheme.get_default ();
86 var pixbuf = icon_theme.load_icon ("statusarea_tor_connected",
87 STATUS_AREA_ICON_SIZE,
88 Gtk.IconLookupFlags.NO_SVG);
89 icon_connected = pixbuf;
93 if (tor_enabled && !tor_connected && icon_connecting == null) try {
94 var icon_theme = Gtk.IconTheme.get_default ();
95 var pixbuf = icon_theme.load_icon ("statusarea_tor_connecting",
96 STATUS_AREA_ICON_SIZE,
97 Gtk.IconLookupFlags.NO_SVG);
98 icon_connecting = pixbuf;
102 if (tor_enabled && icon_enabled == null) try {
103 var icon_theme = Gtk.IconTheme.get_default();
104 var pixbuf = icon_theme.load_icon ("statusarea_tor_enabled",
105 STATUS_MENU_ICON_SIZE,
106 Gtk.IconLookupFlags.NO_SVG);
107 icon_enabled = new Gtk.Image.from_pixbuf (pixbuf);
111 if (!tor_enabled && icon_disabled == null) try {
112 var icon_theme = Gtk.IconTheme.get_default();
113 var pixbuf = icon_theme.load_icon ("statusarea_tor_disabled",
114 STATUS_MENU_ICON_SIZE,
115 Gtk.IconLookupFlags.NO_SVG);
116 icon_disabled = new Gtk.Image.from_pixbuf (pixbuf);
121 if (conic_connected && tor_enabled) {
122 set_status_area_icon (tor_connected ? icon_connected : icon_connecting);
123 button.set_value (tor_connected ? _("Connected") : _("Connecting ..."));
125 set_status_area_icon (null);
126 button.set_value (tor_enabled ? _("Disconnected") : _("Disabled"));
128 button.set_image (tor_enabled ? icon_enabled : icon_disabled);
132 * Callback for Tor daemon line output
134 private bool tor_io_func (IOChannel source, IOCondition condition) {
136 if ((condition & (IOCondition.IN | IOCondition.PRI)) != 0) {
140 /* var status = */ source.read_line (out line, out length, null);
143 if (log_label != null)
144 log_label.label = tor_log;
146 if ("[notice]" in line) {
147 if ("Bootstrapped 100%" in line) {
148 tor_connected = true;
152 if ("Opening Control listener on 127.0.0.1:9051" in line) {
153 tor_control = new TorControl.Connection ();
154 tor_control_auth.begin ();
158 Hildon.Banner.show_information (null, null, "DEBUG: %s".printf (line));
162 Hildon.Banner.show_information (null, null, "Error: %s".printf (e.message));
165 if ((condition & (IOCondition.ERR | IOCondition.HUP | IOCondition.NVAL)) != 0) {
172 * Authenticate with Tor on the control channel
174 private async void tor_control_auth () throws Error {
175 yield tor_control.authenticate_async (password);
177 var bridges = new SList<string> ();
179 bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
181 error ("Error loading bridges: %s", e.message);
185 if (bridges.length () <= 0)
188 // Enable bridge relays
189 tor_control.set_conf_list ("Bridge", bridges);
190 tor_control.set_conf_bool ("UseBridges", true);
192 bool use = yield tor_control.get_conf_bool_async ("UseBridges");
194 Hildon.Banner.show_information (null, null,
195 "Failed to set up bridge relays");
200 * Start Tor and setup proxy settings
202 private void start_tor () {
204 if (tor_pid == (Pid) 0) {
205 string[] tor_hash_argv = {
207 "--hash-password", "",
211 Random.set_seed ((uint32) tv.tv_usec);
212 password = "tor-status-%8x".printf (Random.next_int ());
213 tor_hash_argv[2] = password;
215 Process.spawn_sync ("/tmp", tor_hash_argv, null, 0, null, out hash);
216 hash = hash.str ("16:").replace ("\n", "");
219 Hildon.Banner.show_information (null, null,
220 "Failed to get hash");
224 string[] tor_argv = {
226 "--ControlPort", "9051",
227 "--HashedControlPassword", "",
231 Process.spawn_async_with_pipes ("/tmp",
234 SpawnFlags.SEARCH_PATH,
240 var channel = new IOChannel.unix_new (tor_stdout);
241 channel.add_watch (IOCondition.IN | IOCondition.PRI | IOCondition.ERR | IOCondition.HUP | IOCondition.NVAL, tor_io_func);
243 if (polipo_pid == (Pid) 0) {
244 Process.spawn_async_with_pipes ("/tmp",
245 { "/usr/bin/polipo" },
247 SpawnFlags.SEARCH_PATH,
252 /* --> proxy settings and will be set up and tor_connected will
253 * be set to true once Tor signals 100%
255 } catch (SpawnError e) {
256 Hildon.Banner.show_information (null, null, "DEBUG: Failed to spawn polipo and tor: %s".printf (e.message));
261 if (log_label != null)
262 log_label.label = tor_log;
267 * Stop Tor and revert proxy settings
269 private void stop_tor () {
271 tor_connected = false;
272 if (polipo_pid != (Pid) 0) {
273 Process.close_pid (polipo_pid);
274 Posix.kill ((Posix.pid_t) polipo_pid, Posix.SIGKILL);
275 polipo_pid = (Pid) 0;
277 if (tor_pid != (Pid) 0) {
278 Process.close_pid (tor_pid);
279 Posix.kill ((Posix.pid_t) tor_pid, Posix.SIGKILL);
287 * Setup proxy settings to route through the Tor network
289 private void proxy_setup () {
290 if (backup == null) try {
291 backup = new ProxyBackup ();
292 backup.use_http_proxy = gconf.get_bool (GCONF_KEY_PROXY_HTTP_ENABLED);
294 backup.http_host = gconf.get_string (GCONF_KEY_PROXY_HTTP_HOST);
295 backup.socks_host = gconf.get_string (GCONF_KEY_PROXY_SOCKS_HOST);
296 backup.secure_host = gconf.get_string (GCONF_KEY_PROXY_SECURE_HOST);
297 backup.http_port = gconf.get_int (GCONF_KEY_PROXY_HTTP_PORT);
298 backup.socks_port = gconf.get_int (GCONF_KEY_PROXY_SOCKS_PORT);
299 backup.secure_port = gconf.get_int (GCONF_KEY_PROXY_SECURE_PORT);
301 backup.mode = gconf.get_string (GCONF_KEY_PROXY_MODE);
303 error ("Error saving proxy settings: %s", e.message);
304 backup = new ProxyBackup ();
305 backup.use_http_proxy = false;
307 backup.http_host = "";
308 backup.socks_host = "";
309 backup.secure_host = "";
310 backup.http_port = 8080;
311 backup.socks_port = 0;
312 backup.secure_port = 0;
314 backup.mode = "none";
317 // Hildon.Banner.show_information (null, null, "DEBUG: Proxy setup");
318 gconf.set_bool (GCONF_KEY_PROXY_HTTP_ENABLED, true);
320 gconf.set_string (GCONF_KEY_PROXY_HTTP_HOST, "127.0.0.1");
321 gconf.set_string (GCONF_KEY_PROXY_SOCKS_HOST, "127.0.0.1");
322 gconf.set_string (GCONF_KEY_PROXY_SECURE_HOST, "127.0.0.1");
323 gconf.set_int (GCONF_KEY_PROXY_HTTP_PORT, 8118);
324 gconf.set_int (GCONF_KEY_PROXY_SOCKS_PORT, 9050);
325 gconf.set_int (GCONF_KEY_PROXY_SECURE_PORT, 8118);
327 gconf.set_string (GCONF_KEY_PROXY_MODE, "manual");
329 error ("Error changing proxy settings: %s", e.message);
334 * Revert proxy settings
336 private void proxy_restore () {
337 if (backup != null) try {
338 // Hildon.Banner.show_information (null, null, "DEBUG: Restoring proxy settings");
339 gconf.set_bool (GCONF_KEY_PROXY_HTTP_ENABLED, backup.use_http_proxy);
341 gconf.set_string (GCONF_KEY_PROXY_HTTP_HOST, backup.http_host);
342 gconf.set_string (GCONF_KEY_PROXY_SOCKS_HOST, backup.socks_host);
343 gconf.set_string (GCONF_KEY_PROXY_SECURE_HOST, backup.secure_host);
344 gconf.set_int (GCONF_KEY_PROXY_HTTP_PORT, backup.http_port);
345 gconf.set_int (GCONF_KEY_PROXY_SOCKS_PORT, backup.socks_port);
346 gconf.set_int (GCONF_KEY_PROXY_SECURE_PORT, backup.secure_port);
348 gconf.set_string (GCONF_KEY_PROXY_MODE, backup.mode);
351 error ("Error restoring proxy: %s", e.message);
356 * Show the bridge relay configuration dialog
358 private const int RESPONSE_NEW = 1;
359 private void bridges_clicked_cb () {
360 var dialog = new Gtk.Dialog ();
361 var content = (Gtk.VBox) dialog.get_content_area ();
362 content.set_size_request (-1, 5*70);
364 dialog.set_title (_("Bridge relays"));
366 var bridges = new SList<string> ();
368 bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
370 Hildon.Banner.show_information (null, null, "Error loading bridges: %s".printf (e.message));
373 var list_store = new Gtk.ListStore (1, typeof (string));
375 foreach (string bridge in bridges) {
376 list_store.append (out iter);
377 list_store.@set (iter, 0, bridge);
380 var pannable_area = new Hildon.PannableArea ();
381 var tree_view = new Gtk.TreeView.with_model (list_store);
382 var renderer = new Gtk.CellRendererText ();
383 var column = new Gtk.TreeViewColumn.with_attributes ("IP", renderer, "text", 0);
384 tree_view.append_column (column);
385 pannable_area.add (tree_view);
386 content.pack_start (pannable_area, true, true, 0);
388 tree_view.row_activated.connect ((path, column) => {
389 bridge_edit_dialog (list_store, path);
392 dialog.add_button (_("New"), RESPONSE_NEW);
393 dialog.response.connect ((response_id) => {
394 if (response_id == RESPONSE_NEW) {
395 bridge_edit_dialog (list_store, null);
403 * Show the bridge relay edit dialog
405 private const int RESPONSE_DELETE = 1;
406 private void bridge_edit_dialog (Gtk.ListStore store, Gtk.TreePath? path) {
407 var dialog = new Gtk.Dialog ();
408 var content = (Gtk.VBox) dialog.get_content_area ();
411 dialog.set_title (_("New bridge relay"));
413 dialog.set_title (_("Edit bridge relay"));
415 var size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.HORIZONTAL);
417 var hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
418 var label = new Gtk.Label (_("IP address"));
419 label.set_alignment (0, 0.5f);
420 size_group.add_widget (label);
421 hbox.pack_start (label, false, false, 0);
422 var ip_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
423 ip_entry.set ("hildon-input-mode", Hildon.GtkInputMode.NUMERIC |
424 Hildon.GtkInputMode.SPECIAL);
425 hbox.pack_start (ip_entry, true, true, 0);
426 content.pack_start (hbox, false, false, 0);
428 hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
429 label = new Gtk.Label (_("Port"));
430 label.set_alignment (0, 0.5f);
431 size_group.add_widget (label);
432 hbox.pack_start (label, false, false, 0);
433 var port_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
434 port_entry.set ("hildon-input-mode", Hildon.GtkInputMode.NUMERIC);
435 hbox.pack_start (port_entry, true, true, 0);
436 content.pack_start (hbox, true, true, 0);
438 hbox = new Gtk.HBox (false, Hildon.MARGIN_DOUBLE);
439 label = new Gtk.Label (_("Fingerprint"));
440 label.set_alignment (0, 0.5f);
441 size_group.add_widget (label);
442 hbox.pack_start (label, false, false, 0);
443 var fingerprint_entry = new Hildon.Entry (Hildon.SizeType.FINGER_HEIGHT);
444 fingerprint_entry.set ("hildon-input-mode", Hildon.GtkInputMode.HEXA);
445 hbox.pack_start (fingerprint_entry, true, true, 0);
446 content.pack_start (hbox, true, true, 0);
448 var iter = Gtk.TreeIter ();
450 port_entry.set_text ("443");
451 } else if (store.get_iter (out iter, path)) {
453 store.@get (iter, 0, out tmp);
454 string[] ip_port = tmp.split (":");
455 if (ip_port.length == 2) {
456 ip_entry.set_text (ip_port[0]);
457 port_entry.set_text (ip_port[1]);
460 dialog.add_button (_("Delete"), RESPONSE_DELETE);
462 dialog.add_button (_("Save"), Gtk.ResponseType.OK);
463 dialog.response.connect ((response_id) => {
464 var bridges = new SList<string> ();
466 if (response_id == RESPONSE_DELETE) {
470 if (store.get_iter_first (out iter)) do {
471 store.@get (iter, 0, out bridge);
472 bridges.append (bridge);
473 } while (store.iter_next (ref iter));
475 gconf.set_list (GCONF_KEY_BRIDGES,
476 GConf.ValueType.STRING,
479 Hildon.Banner.show_information (dialog, null,
480 "Failed to save bridge relay list: %s".printf (e.message));
485 if (response_id == Gtk.ResponseType.OK) {
486 if (!is_valid_ip_address (ip_entry.get_text ())) {
487 Hildon.Banner.show_information (dialog, null,
488 _("Invalid IP address"));
491 int port = port_entry.get_text ().to_int ();
492 if (port < 0 || port > 65565) {
493 Hildon.Banner.show_information (dialog, null,
494 _("Invalid port number"));
498 store.append (out iter);
500 store.@set (iter, 0, "%s:%d".printf (ip_entry.get_text (), port));
502 bridges = gconf.get_list (GCONF_KEY_BRIDGES,
503 GConf.ValueType.STRING);
505 Hildon.Banner.show_information (null, null,
506 "Error loading bridges: %s".printf (e.message));
509 bridges.append ("%s:%d".printf (ip_entry.get_text (), port));
513 if (store.get_iter_first (out iter)) do {
514 store.@get (iter, 0, out bridge);
515 bridges.append (bridge);
516 } while (store.iter_next (ref iter));
519 gconf.set_list (GCONF_KEY_BRIDGES,
520 GConf.ValueType.STRING,
523 Hildon.Banner.show_information (dialog, null,
524 "Failed to save bridge relay list: %s".printf (e.message));
535 * Check whether the IP address consists of four numbers in the 0..255 range
537 bool is_valid_ip_address (string address) {
538 string[] ip = address.split (".");
543 for (int i = 0; i < ip.length; i++) {
544 int n = ip[i].to_int ();
545 if (n < 0 || n > 255)
553 * Show the Tor log dialog
555 private void show_tor_log () {
556 var dialog = new Gtk.Dialog ();
557 var content = (Gtk.VBox) dialog.get_content_area ();
558 content.set_size_request (-1, 5*70);
560 dialog.set_title (_("Log"));
562 var pannable = new Hildon.PannableArea ();
563 pannable.mov_mode = Hildon.MovementMode.BOTH;
564 log_label = new Gtk.Label (tor_log);
565 log_label.set_alignment (0, 0);
566 pannable.add_with_viewport (log_label);
567 content.pack_start (pannable, true, true, 0);
569 dialog.response.connect (() => {
577 * Callback for the status menu button clicked signal
579 private const int RESPONSE_LOG = 1;
580 private void button_clicked_cb () {
581 var dialog = new Gtk.Dialog ();
582 var content = (Gtk.VBox) dialog.get_content_area ();
583 content.set_size_request (-1, 2*70);
585 dialog.set_title (_("Tor: anonymity online"));
587 var check = new Hildon.CheckButton (Hildon.SizeType.FINGER_HEIGHT);
588 check.set_label (_("Enable onion routing"));
589 check.set_active (tor_enabled);
590 content.pack_start (check, true, true, 0);
592 var button = new Hildon.Button.with_text (Hildon.SizeType.FINGER_HEIGHT,
593 Hildon.ButtonArrangement.VERTICAL,
596 button.set_style (Hildon.ButtonStyle.PICKER);
597 button.set_alignment (0, 0.5f, 0, 0.5f);
598 button.clicked.connect (bridges_clicked_cb);
599 content.pack_start (button, true, true, 0);
601 dialog.add_button (_("Log"), RESPONSE_LOG);
603 dialog.add_button (_("Save"), Gtk.ResponseType.ACCEPT);
604 dialog.response.connect ((response_id) => {
605 if (response_id == RESPONSE_LOG) {
609 if (response_id == Gtk.ResponseType.ACCEPT) {
610 if (!tor_enabled && check.get_active ()) {
613 if (conic_connected) {
616 conic.connect (ConIc.ConnectFlags.NONE);
618 } else if (tor_enabled && !check.get_active ()) {
632 private string get_bridge_list () {
634 var bridges = new SList<string> ();
636 bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
638 error ("Error loading bridges: %s", e.message);
640 foreach (string bridge in bridges) {
644 list += ", " + bridge;
653 * Callback for the ConIc connection-event signal
655 private void conic_connection_event_cb (ConIc.Connection conic, ConIc.ConnectionEvent event) {
656 var status = event.get_status ();
658 case ConIc.ConnectionStatus.CONNECTED:
659 conic_connected = true;
666 case ConIc.ConnectionStatus.DISCONNECTING:
667 conic_connected = false;
670 case ConIc.ConnectionStatus.DISCONNECTED:
671 case ConIc.ConnectionStatus.NETWORK_UP:
676 var error = event.get_error ();
678 case ConIc.ConnectionError.CONNECTION_FAILED:
679 Hildon.Banner.show_information (null, null, "DEBUG: ConIc connection failed");
681 case ConIc.ConnectionError.USER_CANCELED:
682 Hildon.Banner.show_information (null, null, "DEBUG: ConIc user canceled");
684 case ConIc.ConnectionError.NONE:
685 case ConIc.ConnectionError.INVALID_IAP:
691 private void create_widgets () {
692 // Status menu button
693 button = new Hildon.Button.with_text (Hildon.SizeType.FINGER_HEIGHT,
694 Hildon.ButtonArrangement.VERTICAL,
695 _("The Onion Router"),
696 tor_enabled ? _("Enabled") : _("Disabled"));
697 button.set_alignment (0.0f, 0.5f, 1.0f, 1.0f);
698 button.set_style (Hildon.ButtonStyle.PICKER);
699 button.clicked.connect (button_clicked_cb);
713 Intl.setlocale (LocaleCategory.ALL, "");
714 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
715 Intl.textdomain (Config.GETTEXT_PACKAGE);
718 gconf = GConf.Client.get_default ();
720 tor_enabled = gconf.get_bool (GCONF_KEY_TOR_ENABLED);
722 error ("Failed to get GConf setting: %s", e.message);
724 tor_connected = false;
727 conic = new ConIc.Connection ();
729 Hildon.Banner.show_information (null, null, "DEBUG: ConIc hook-up failed");
731 conic_connected = false;
732 conic.automatic_connection_events = true;
734 conic.connect (ConIc.ConnectFlags.AUTOMATICALLY_TRIGGERED);
735 conic.connection_event.connect (conic_connection_event_cb);
738 osso = new Osso.Context (STATUSMENU_TOR_LIBOSSO_SERVICE_NAME,
748 * Vala code can't use the HD_DEFINE_PLUGIN_MODULE macro, but it handles
749 * most of the class registration issues itself. Only this code from
750 * HD_PLUGIN_MODULE_SYMBOLS_CODE has to be has to be included manually
751 * to register with hildon-desktop:
754 public void hd_plugin_module_load (TypeModule plugin) {
755 // [ModuleInit] registers types automatically
756 ((HD.PluginModule) plugin).add_type (typeof (TorStatusMenuItem));
759 public void hd_plugin_module_unload (HD.PluginModule plugin) {