diff --git a/README.md b/README.md index 741a1a3..eff85ab 100644 --- a/README.md +++ b/README.md @@ -341,3 +341,38 @@ services.proxy-suite.singBox.outbounds = [{ # Create /run/proxy-url with your URL, ensure it's readable by root urlFile = "/run/proxy-url"; ``` + +--- + +## System tray + +Optional tray indicator for desktop environments. Shows proxy status and provides quick toggles. + +```nix +services.proxy-suite = { + tray = { + enable = true; + autostart = true; # Install XDG autostart for all graphical users (default) + pollInterval = 5; # Status refresh interval in seconds + }; +}; +``` + +Features: +- **Status icon**: `network-vpn` (active), `network-vpn-acquiring` (partial), `network-vpn-disconnected` (stopped) +- **Toggle TProxy/TUN**: Only shown if the service is enabled in your config +- **Restart services**: Restarts `proxy-suite-socks`, and `proxy-suite-tun` if it is active +- **Polkit authentication**: Prompts for password when toggling services + +When enabled, the tray app is installed system-wide and autostarts for all graphical users via XDG autostart unless you set `tray.autostart = false;`. + +The tray uses libayatana-appindicator and requires StatusNotifier/AppIndicator support. KDE generally works out of the box; GNOME may need shell support or an extension that exposes AppIndicators. + +To enable TProxy/TUN toggles in the tray, you need to enable the services in your config: + +```nix +services.proxy-suite.singBox = { + tproxy.enable = true; # Show TProxy toggle + tun.enable = true; # Show TUN toggle +}; +``` diff --git a/flake.nix b/flake.nix index e356c63..de2320b 100644 --- a/flake.nix +++ b/flake.nix @@ -29,10 +29,12 @@ overlays.default = final: prev: { tg-ws-proxy = import ./pkgs/tg-ws-proxy.nix { pkgs = final; }; + proxy-suite-tray = import ./pkgs/proxy-suite-tray.nix { pkgs = final; }; }; packages = forAll (system: { tg-ws-proxy = import ./pkgs/tg-ws-proxy.nix { pkgs = pkgsFor system; }; + proxy-suite-tray = import ./pkgs/proxy-suite-tray.nix { pkgs = pkgsFor system; }; }); checks = forAll (system: import ./nix/checks.nix { diff --git a/modules/proxy-suite/default.nix b/modules/proxy-suite/default.nix index 440bb97..f8160e4 100644 --- a/modules/proxy-suite/default.nix +++ b/modules/proxy-suite/default.nix @@ -36,5 +36,9 @@ in (lib.mkIf cfg.tgWsProxy.enable ( import ./tg-ws-proxy.nix { inherit lib pkgs cfg; } )) + + (lib.mkIf cfg.tray.enable ( + import ./tray.nix { inherit config lib pkgs cfg; } + )) ]); } diff --git a/modules/proxy-suite/options.nix b/modules/proxy-suite/options.nix index faa3b98..c8f16ac 100644 --- a/modules/proxy-suite/options.nix +++ b/modules/proxy-suite/options.nix @@ -393,6 +393,22 @@ in }; }; + tray = { + enable = mkEnableOption "system tray indicator for proxy-suite"; + + pollInterval = mkOption { + type = types.int; + default = 5; + description = "Status polling interval in seconds."; + }; + + autostart = mkOption { + type = types.bool; + default = true; + description = "Whether to autostart the tray application for all graphical users via XDG desktop autostart."; + }; + }; + tgWsProxy = { enable = mkEnableOption "Telegram MTProto WebSocket proxy"; diff --git a/modules/proxy-suite/tray.nix b/modules/proxy-suite/tray.nix new file mode 100644 index 0000000..f1fe37f --- /dev/null +++ b/modules/proxy-suite/tray.nix @@ -0,0 +1,55 @@ +# Tray indicator module for proxy-suite +{ + config, + lib, + pkgs, + cfg, +}: + +let + tray = cfg.tray; + + proxyTray = import ../../pkgs/proxy-suite-tray.nix { + inherit pkgs; + pollInterval = tray.pollInterval; + }; + + desktopEntryText = '' + [Desktop Entry] + Type=Application + Version=1.0 + Name=Proxy Suite Tray + Comment=System tray indicator for proxy-suite + Exec=${proxyTray}/bin/proxy-suite-tray + Icon=network-vpn + Categories=Network;System; + StartupNotify=false + Terminal=false + ''; + + desktopEntry = pkgs.writeTextFile { + name = "proxy-suite-tray.desktop"; + destination = "/share/applications/proxy-suite-tray.desktop"; + text = desktopEntryText; + }; + + autostartEntry = pkgs.writeTextFile { + name = "proxy-suite-tray-autostart.desktop"; + destination = "/share/xdg/autostart/proxy-suite-tray.desktop"; + text = '' + ${desktopEntryText} + X-GNOME-Autostart-enabled=true + ''; + }; +in +{ + environment.systemPackages = [ + proxyTray + desktopEntry + ]; + + environment.etc = lib.mkIf tray.autostart { + "xdg/autostart/proxy-suite-tray.desktop".source = + "${autostartEntry}/share/xdg/autostart/proxy-suite-tray.desktop"; + }; +} diff --git a/nix/checks.nix b/nix/checks.nix index d698cb6..f01890f 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -54,6 +54,11 @@ let dnsHasRuleSet = dnsRules: ruleSet: builtins.any (rule: (rule ? rule_set) && builtins.elem ruleSet rule.rule_set) dnsRules; + packagePathMatches = + packages: pattern: + builtins.any ( + pkg: builtins.match pattern (builtins.unsafeDiscardStringContext (toString pkg)) != null + ) packages; baseModule = { system.stateVersion = "26.05"; @@ -261,6 +266,23 @@ let ]; proxyDirectConfig = mkTProxyConfig proxyDirectFixture; + trayAutostartFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.tray.enable = true; + } + ]; + + trayManualFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.tray = { + enable = true; + autostart = false; + }; + } + ]; + blockGeoFixture = evalProxySuite [ baseModule { @@ -409,6 +431,30 @@ let assert ruDefaultConfig.dns.final == "remote"; true ) + ( + assert packagePathMatches trayAutostartFixture.config.environment.systemPackages ".*/[^/]*proxy-suite-tray(-[0-9.]+)?$"; + true + ) + ( + assert packagePathMatches trayAutostartFixture.config.environment.systemPackages ".*/[^/]*proxy-suite-tray\\.desktop$"; + true + ) + ( + assert + builtins.match + ".*/share/xdg/autostart/proxy-suite-tray\\.desktop$" + (builtins.unsafeDiscardStringContext trayAutostartFixture.config.environment.etc."xdg/autostart/proxy-suite-tray.desktop".source) + != null; + true + ) + ( + assert !(trayManualFixture.config.environment.etc ? "xdg/autostart/proxy-suite-tray.desktop"); + true + ) + ( + assert packagePathMatches trayManualFixture.config.environment.systemPackages ".*/[^/]*proxy-suite-tray\\.desktop$"; + true + ) ]; in { diff --git a/pkgs/proxy-suite-tray.nix b/pkgs/proxy-suite-tray.nix new file mode 100644 index 0000000..9140fe6 --- /dev/null +++ b/pkgs/proxy-suite-tray.nix @@ -0,0 +1,33 @@ +{ + pkgs, + pollInterval ? 5, +}: + +pkgs.stdenv.mkDerivation { + pname = "proxy-suite-tray"; + version = "0.1.0"; + + src = ./proxy-suite-tray; + + nativeBuildInputs = [ pkgs.pkg-config ]; + buildInputs = [ pkgs.gtk3 pkgs.libayatana-appindicator ]; + + buildPhase = '' + $CC -o proxy-suite-tray main.c \ + $(pkg-config --cflags --libs gtk+-3.0 ayatana-appindicator3-0.1) \ + -DSYSTEMCTL_BIN='"${pkgs.systemd}/bin/systemctl"' \ + -DPKEXEC_BIN='"/run/wrappers/bin/pkexec"' \ + -DPOLL_INTERVAL=${toString pollInterval} \ + -O2 -Wall + ''; + + installPhase = '' + mkdir -p $out/bin + cp proxy-suite-tray $out/bin/ + ''; + + meta = { + description = "System tray indicator for proxy-suite"; + mainProgram = "proxy-suite-tray"; + }; +} diff --git a/pkgs/proxy-suite-tray/main.c b/pkgs/proxy-suite-tray/main.c new file mode 100644 index 0000000..ac7065b --- /dev/null +++ b/pkgs/proxy-suite-tray/main.c @@ -0,0 +1,241 @@ +#include +#include +#include +#include +#include + +#ifndef POLL_INTERVAL +#define POLL_INTERVAL 5 +#endif +#ifndef SYSTEMCTL_BIN +#define SYSTEMCTL_BIN "/run/current-system/sw/bin/systemctl" +#endif +#ifndef PKEXEC_BIN +#define PKEXEC_BIN "/run/current-system/sw/bin/pkexec" +#endif + +static AppIndicator *indicator; +static GtkWidget *menu; +static GtkWidget *status_item; +static GtkWidget *tproxy_item; +static GtkWidget *tun_item; + +static gboolean command_succeeds(gchar **argv) { + GError *error = NULL; + gint wait_status = 0; + + if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, NULL, NULL, &wait_status, &error)) { + g_clear_error(&error); + return FALSE; + } + + if (!g_spawn_check_wait_status(wait_status, &error)) { + g_clear_error(&error); + return FALSE; + } + + return TRUE; +} + +static gboolean command_stdout(gchar **argv, gchar **stdout_text) { + GError *error = NULL; + gint wait_status = 0; + + *stdout_text = NULL; + if (!g_spawn_sync(NULL, argv, NULL, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, stdout_text, NULL, &wait_status, &error)) { + g_clear_error(&error); + return FALSE; + } + + if (!g_spawn_check_wait_status(wait_status, &error)) { + g_clear_error(&error); + g_clear_pointer(stdout_text, g_free); + return FALSE; + } + + return TRUE; +} + +static gboolean service_exists(const char *name) { + gchar *load_state = NULL; + gboolean exists = FALSE; + gchar *argv[] = { + (gchar *) SYSTEMCTL_BIN, + (gchar *) "show", + (gchar *) "--value", + (gchar *) "--property=LoadState", + NULL, + NULL + }; + + argv[4] = g_strdup_printf("%s.service", name); + if (command_stdout(argv, &load_state)) { + g_strstrip(load_state); + exists = load_state[0] != '\0' && strcmp(load_state, "not-found") != 0; + } + + g_free(argv[4]); + g_free(load_state); + return exists; +} + +static gboolean service_active(const char *name) { + gchar *argv[] = { + (gchar *) SYSTEMCTL_BIN, + (gchar *) "is-active", + (gchar *) "--quiet", + NULL, + NULL + }; + + argv[3] = g_strdup_printf("%s.service", name); + gboolean active = command_succeeds(argv); + g_free(argv[3]); + return active; +} + +static gboolean run_privileged(const char *action, const char *service) { + char cmd[512]; + + snprintf( + cmd, + sizeof(cmd), + "%s %s %s %s.service", + PKEXEC_BIN, + SYSTEMCTL_BIN, + action, + service + ); + + return system(cmd) == 0; +} + +static void update_status(void) { + gboolean socks = service_active("proxy-suite-socks"); + gboolean tproxy = service_active("proxy-suite-tproxy"); + gboolean tun = service_active("proxy-suite-tun"); + + if (socks) { + if (tproxy || tun) { + app_indicator_set_icon(indicator, "network-vpn"); + } else { + app_indicator_set_icon(indicator, "network-vpn-acquiring"); + } + } else { + app_indicator_set_icon(indicator, "network-vpn-disconnected"); + } + + gtk_menu_item_set_label(GTK_MENU_ITEM(status_item), + socks ? "SOCKS Proxy: Running" : "SOCKS Proxy: Stopped"); + + if (tproxy_item) { + gtk_menu_item_set_label(GTK_MENU_ITEM(tproxy_item), + tproxy ? "Disable TProxy" : "Enable TProxy"); + } + if (tun_item) { + gtk_menu_item_set_label(GTK_MENU_ITEM(tun_item), + tun ? "Disable TUN" : "Enable TUN"); + } +} + +static void on_tproxy_toggle(GtkMenuItem *item, gpointer data) { + (void)item; (void)data; + if (service_active("proxy-suite-tproxy")) { + run_privileged("stop", "proxy-suite-tproxy"); + } else { + run_privileged("start", "proxy-suite-tproxy"); + } + update_status(); +} + +static void on_tun_toggle(GtkMenuItem *item, gpointer data) { + (void)item; (void)data; + if (service_active("proxy-suite-tun")) { + run_privileged("stop", "proxy-suite-tun"); + } else { + run_privileged("start", "proxy-suite-tun"); + } + update_status(); +} + +static void on_restart(GtkMenuItem *item, gpointer data) { + (void)item; (void)data; + gboolean tun_was_active = service_active("proxy-suite-tun"); + + if (run_privileged("restart", "proxy-suite-socks") && tun_was_active) { + run_privileged("restart", "proxy-suite-tun"); + } + + update_status(); +} + +static void on_quit(GtkMenuItem *item, gpointer data) { + (void)item; (void)data; + gtk_main_quit(); +} + +static gboolean on_timer(gpointer data) { + (void)data; + update_status(); + return TRUE; +} + +static void build_menu(void) { + menu = gtk_menu_new(); + + status_item = gtk_menu_item_new_with_label("SOCKS Proxy: Unknown"); + gtk_widget_set_sensitive(status_item, FALSE); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), status_item); + + gtk_menu_shell_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); + + if (service_exists("proxy-suite-tproxy")) { + tproxy_item = gtk_menu_item_new_with_label("Enable TProxy"); + g_signal_connect(tproxy_item, "activate", G_CALLBACK(on_tproxy_toggle), NULL); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), tproxy_item); + } + + if (service_exists("proxy-suite-tun")) { + tun_item = gtk_menu_item_new_with_label("Enable TUN"); + g_signal_connect(tun_item, "activate", G_CALLBACK(on_tun_toggle), NULL); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), tun_item); + } + + gtk_menu_shell_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); + + GtkWidget *restart_item = gtk_menu_item_new_with_label("Restart Services"); + g_signal_connect(restart_item, "activate", G_CALLBACK(on_restart), NULL); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), restart_item); + + gtk_menu_shell_append(GTK_MENU_SHELL(menu), gtk_separator_menu_item_new()); + + GtkWidget *quit_item = gtk_menu_item_new_with_label("Exit"); + g_signal_connect(quit_item, "activate", G_CALLBACK(on_quit), NULL); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), quit_item); + + gtk_widget_show_all(menu); +} + +int main(int argc, char *argv[]) { + if (!gtk_init_check(&argc, &argv)) { + fprintf(stderr, "proxy-suite-tray: failed to initialize GTK\n"); + return 1; + } + + indicator = app_indicator_new( + "proxy-suite-tray", + "network-vpn-disconnected", + APP_INDICATOR_CATEGORY_SYSTEM_SERVICES + ); + app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE); + + build_menu(); + app_indicator_set_menu(indicator, GTK_MENU(menu)); + + update_status(); + + g_timeout_add_seconds(POLL_INTERVAL, on_timer, NULL); + + gtk_main(); + return 0; +}