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);
178 var bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
180 if (bridges.length () > 0) {
181 // Enable bridge relays
182 tor_control.set_conf_list ("Bridge", bridges);
183 tor_control.set_conf_bool ("UseBridges", true);
185 bool use = yield tor_control.get_conf_bool_async ("UseBridges");
187 Hildon.Banner.show_information (null, null,
188 "Failed to set up bridge relays");
192 error ("Error loading bridges: %s", e.message);
197 * Start Tor and setup proxy settings
199 private void start_tor () {
201 if (tor_pid == (Pid) 0) {
202 string[] tor_hash_argv = {
204 "--hash-password", "",
208 Random.set_seed ((uint32) tv.tv_usec);
209 password = "tor-status-%8x".printf (Random.next_int ());
210 tor_hash_argv[2] = password;
212 Process.spawn_sync ("/tmp", tor_hash_argv, null, 0, null, out hash);
213 hash = hash.str ("16:").replace ("\n", "");
216 Hildon.Banner.show_information (null, null,
217 "Failed to get hash");
221 string[] tor_argv = {
223 "--ControlPort", "9051",
224 "--HashedControlPassword", "",
228 Process.spawn_async_with_pipes ("/tmp",
231 SpawnFlags.SEARCH_PATH,
237 var channel = new IOChannel.unix_new (tor_stdout);
238 channel.add_watch (IOCondition.IN | IOCondition.PRI | IOCondition.ERR | IOCondition.HUP | IOCondition.NVAL, tor_io_func);
240 if (polipo_pid == (Pid) 0) {
241 Process.spawn_async_with_pipes ("/tmp",
242 { "/usr/bin/polipo" },
244 SpawnFlags.SEARCH_PATH,
249 /* --> proxy settings and will be set up and tor_connected will
250 * be set to true once Tor signals 100%
252 } catch (SpawnError e) {
253 Hildon.Banner.show_information (null, null, "DEBUG: Failed to spawn polipo and tor: %s".printf (e.message));
258 if (log_label != null)
259 log_label.label = tor_log;
264 * Stop Tor and revert proxy settings
266 private void stop_tor () {
268 tor_connected = false;
269 if (polipo_pid != (Pid) 0) {
270 Process.close_pid (polipo_pid);
271 Posix.kill ((Posix.pid_t) polipo_pid, Posix.SIGKILL);
272 polipo_pid = (Pid) 0;
274 if (tor_pid != (Pid) 0) {
275 Process.close_pid (tor_pid);
276 Posix.kill ((Posix.pid_t) tor_pid, Posix.SIGKILL);
284 * Setup proxy settings to route through the Tor network
286 private void proxy_setup () {
287 if (backup == null) try {
288 backup = new ProxyBackup ();
289 backup.use_http_proxy = gconf.get_bool (GCONF_KEY_PROXY_HTTP_ENABLED);
291 backup.http_host = gconf.get_string (GCONF_KEY_PROXY_HTTP_HOST);
292 backup.socks_host = gconf.get_string (GCONF_KEY_PROXY_SOCKS_HOST);
293 backup.secure_host = gconf.get_string (GCONF_KEY_PROXY_SECURE_HOST);
294 backup.http_port = gconf.get_int (GCONF_KEY_PROXY_HTTP_PORT);
295 backup.socks_port = gconf.get_int (GCONF_KEY_PROXY_SOCKS_PORT);
296 backup.secure_port = gconf.get_int (GCONF_KEY_PROXY_SECURE_PORT);
298 backup.mode = gconf.get_string (GCONF_KEY_PROXY_MODE);
300 error ("Error saving proxy settings: %s", e.message);
301 backup = new ProxyBackup ();
302 backup.use_http_proxy = false;
304 backup.http_host = "";
305 backup.socks_host = "";
306 backup.secure_host = "";
307 backup.http_port = 8080;
308 backup.socks_port = 0;
309 backup.secure_port = 0;
311 backup.mode = "none";
314 // Hildon.Banner.show_information (null, null, "DEBUG: Proxy setup");
315 gconf.set_bool (GCONF_KEY_PROXY_HTTP_ENABLED, true);
317 gconf.set_string (GCONF_KEY_PROXY_HTTP_HOST, "127.0.0.1");
318 gconf.set_string (GCONF_KEY_PROXY_SOCKS_HOST, "127.0.0.1");
319 gconf.set_string (GCONF_KEY_PROXY_SECURE_HOST, "127.0.0.1");
320 gconf.set_int (GCONF_KEY_PROXY_HTTP_PORT, 8118);
321 gconf.set_int (GCONF_KEY_PROXY_SOCKS_PORT, 9050);
322 gconf.set_int (GCONF_KEY_PROXY_SECURE_PORT, 8118);
324 gconf.set_string (GCONF_KEY_PROXY_MODE, "manual");
326 error ("Error changing proxy settings: %s", e.message);
331 * Revert proxy settings
333 private void proxy_restore () {
334 if (backup != null) try {
335 // Hildon.Banner.show_information (null, null, "DEBUG: Restoring proxy settings");
336 gconf.set_bool (GCONF_KEY_PROXY_HTTP_ENABLED, backup.use_http_proxy);
338 gconf.set_string (GCONF_KEY_PROXY_HTTP_HOST, backup.http_host);
339 gconf.set_string (GCONF_KEY_PROXY_SOCKS_HOST, backup.socks_host);
340 gconf.set_string (GCONF_KEY_PROXY_SECURE_HOST, backup.secure_host);
341 gconf.set_int (GCONF_KEY_PROXY_HTTP_PORT, backup.http_port);
342 gconf.set_int (GCONF_KEY_PROXY_SOCKS_PORT, backup.socks_port);
343 gconf.set_int (GCONF_KEY_PROXY_SECURE_PORT, backup.secure_port);
345 gconf.set_string (GCONF_KEY_PROXY_MODE, backup.mode);
348 error ("Error restoring proxy: %s", e.message);
353 * Show the bridge relay configuration dialog
355 private void bridges_clicked_cb () {
356 var dialog = new BridgeDialog ();
361 * Check whether the IP address consists of four numbers in the 0..255 range
363 bool is_valid_ip_address (string address) {
364 string[] ip = address.split (".");
369 for (int i = 0; i < ip.length; i++) {
370 int n = ip[i].to_int ();
371 if (n < 0 || n > 255)
379 * Show the Tor log dialog
381 private void show_tor_log () {
382 var dialog = new Gtk.Dialog ();
383 var content = (Gtk.VBox) dialog.get_content_area ();
384 content.set_size_request (-1, 5*70);
386 dialog.set_title (_("Log"));
388 var pannable = new Hildon.PannableArea ();
389 pannable.mov_mode = Hildon.MovementMode.BOTH;
390 log_label = new Gtk.Label (tor_log);
391 log_label.set_alignment (0, 0);
392 pannable.add_with_viewport (log_label);
393 content.pack_start (pannable, true, true, 0);
395 dialog.response.connect (() => {
403 * Callback for the status menu button clicked signal
405 private const int RESPONSE_LOG = 1;
406 private void button_clicked_cb () {
407 var dialog = new Gtk.Dialog ();
408 var content = (Gtk.VBox) dialog.get_content_area ();
409 content.set_size_request (-1, 2*70);
411 dialog.set_title (_("Tor: anonymity online"));
413 var check = new Hildon.CheckButton (Hildon.SizeType.FINGER_HEIGHT);
414 check.set_label (_("Enable onion routing"));
415 check.set_active (tor_enabled);
416 content.pack_start (check, true, true, 0);
418 var button = new Hildon.Button.with_text (Hildon.SizeType.FINGER_HEIGHT,
419 Hildon.ButtonArrangement.VERTICAL,
422 button.set_style (Hildon.ButtonStyle.PICKER);
423 button.set_alignment (0, 0.5f, 0, 0.5f);
424 button.clicked.connect (bridges_clicked_cb);
425 content.pack_start (button, true, true, 0);
427 dialog.add_button (_("Log"), RESPONSE_LOG);
429 dialog.add_button (_("Save"), Gtk.ResponseType.ACCEPT);
430 dialog.response.connect ((response_id) => {
431 if (response_id == RESPONSE_LOG) {
435 if (response_id == Gtk.ResponseType.ACCEPT) {
436 if (!tor_enabled && check.get_active ()) {
439 if (conic_connected) {
442 conic.connect (ConIc.ConnectFlags.NONE);
444 } else if (tor_enabled && !check.get_active ()) {
458 private string get_bridge_list () {
460 var bridges = new SList<string> ();
462 bridges = gconf.get_list (GCONF_KEY_BRIDGES, GConf.ValueType.STRING);
464 error ("Error loading bridges: %s", e.message);
466 foreach (string bridge in bridges) {
470 list += ", " + bridge;
479 * Callback for the ConIc connection-event signal
481 private void conic_connection_event_cb (ConIc.Connection conic, ConIc.ConnectionEvent event) {
482 var status = event.get_status ();
484 case ConIc.ConnectionStatus.CONNECTED:
485 conic_connected = true;
492 case ConIc.ConnectionStatus.DISCONNECTING:
493 conic_connected = false;
496 case ConIc.ConnectionStatus.DISCONNECTED:
497 case ConIc.ConnectionStatus.NETWORK_UP:
502 var error = event.get_error ();
504 case ConIc.ConnectionError.CONNECTION_FAILED:
505 Hildon.Banner.show_information (null, null, "DEBUG: ConIc connection failed");
507 case ConIc.ConnectionError.USER_CANCELED:
508 Hildon.Banner.show_information (null, null, "DEBUG: ConIc user canceled");
510 case ConIc.ConnectionError.NONE:
511 case ConIc.ConnectionError.INVALID_IAP:
517 private void create_widgets () {
518 // Status menu button
519 button = new Hildon.Button.with_text (Hildon.SizeType.FINGER_HEIGHT,
520 Hildon.ButtonArrangement.VERTICAL,
521 _("The Onion Router"),
522 tor_enabled ? _("Enabled") : _("Disabled"));
523 button.set_alignment (0.0f, 0.5f, 1.0f, 1.0f);
524 button.set_style (Hildon.ButtonStyle.PICKER);
525 button.clicked.connect (button_clicked_cb);
539 Intl.setlocale (LocaleCategory.ALL, "");
540 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
541 Intl.textdomain (Config.GETTEXT_PACKAGE);
544 gconf = GConf.Client.get_default ();
546 tor_enabled = gconf.get_bool (GCONF_KEY_TOR_ENABLED);
548 error ("Failed to get GConf setting: %s", e.message);
550 tor_connected = false;
553 conic = new ConIc.Connection ();
555 Hildon.Banner.show_information (null, null, "DEBUG: ConIc hook-up failed");
557 conic_connected = false;
558 conic.automatic_connection_events = true;
560 conic.connect (ConIc.ConnectFlags.AUTOMATICALLY_TRIGGERED);
561 conic.connection_event.connect (conic_connection_event_cb);
564 osso = new Osso.Context (STATUSMENU_TOR_LIBOSSO_SERVICE_NAME,
574 * Vala code can't use the HD_DEFINE_PLUGIN_MODULE macro, but it handles
575 * most of the class registration issues itself. Only this code from
576 * HD_PLUGIN_MODULE_SYMBOLS_CODE has to be has to be included manually
577 * to register with hildon-desktop:
580 public void hd_plugin_module_load (TypeModule plugin) {
581 // [ModuleInit] registers types automatically
582 ((HD.PluginModule) plugin).add_type (typeof (TorStatusMenuItem));
585 public void hd_plugin_module_unload (HD.PluginModule plugin) {