From 7a106326eacb5037cf39fcaa3f607a4952bfcfa8 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Tue, 8 Sep 2026 23:33:52 +0500 Subject: [PATCH] feat/wip: ssh-proxy --- README.md | 12 + docs/options.md | 422 +++++++++++++++++- modules/proxy-suite/default.nix | 10 + modules/proxy-suite/derived.nix | 12 +- modules/proxy-suite/options/default.nix | 1 + .../proxy-suite/options/sing-box-tproxy.nix | 4 +- modules/proxy-suite/options/ssh-proxy.nix | 107 +++++ modules/proxy-suite/service-assertions.nix | 17 +- modules/proxy-suite/service/context.nix | 3 + modules/proxy-suite/service/default.nix | 4 + .../service/script-blocks/outbounds.nix | 64 ++- modules/proxy-suite/service/scripts.nix | 2 + modules/proxy-suite/service/units.nix | 6 +- modules/proxy-suite/ssh-proxy.nix | 43 ++ nix/checks/module-suite.nix | 12 + nix/checks/ssh-proxy.nix | 293 ++++++++++++ 16 files changed, 990 insertions(+), 22 deletions(-) create mode 100644 modules/proxy-suite/options/ssh-proxy.nix create mode 100644 modules/proxy-suite/ssh-proxy.nix create mode 100644 nix/checks/ssh-proxy.nix diff --git a/README.md b/README.md index b8eca3d..c9a5107 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ The goal is to replace GUI clients like v2rayN and throne – you configure your - **`proxy-ctl`** – control script for managing services, switching outbounds, and following logs - **DPI bypass** via zapret – handles YouTube, Discord, and other sites defined by the project - **Telegram proxy** – running local MTProto WebSocket proxy using tg-ws-proxy +- **SSH SOCKS5 proxy** – create a local SOCKS5 listener from an SSH connection --- @@ -91,6 +92,17 @@ services.proxy-suite = { }; }; + # Optional SSH dynamic SOCKS5 proxy. Set asOutbound = true to make the + # local SSH SOCKS listener available as an ordinary proxy outbound. + sshProxy = { + enable = true; + user = "root"; + host = "ssh.example.com"; + asOutbound = false; + identityFile = "/run/secrets/proxy-suite-ssh-key"; + knownHostsFile = "/run/secrets/proxy-suite-ssh-known-hosts"; + }; + # Native AmneziaWG profiles are independent of the SingBox/XRay backend. # Only one AWG, TUN, or TProxy global tunnel can be active at once; # zapret is mutually exclusive with AWG while its service is running. diff --git a/docs/options.md b/docs/options.md index 1395612..ea50bbf 100644 --- a/docs/options.md +++ b/docs/options.md @@ -181,6 +181,18 @@ Update module option docs there instead of editing this file by hand. - xray - [enable](#services-proxy-suite-proxy-xray-enable) - [package](#services-proxy-suite-proxy-xray-package) + - sshProxy + - [enable](#services-proxy-suite-sshproxy-enable) + - [asOutbound](#services-proxy-suite-sshproxy-asoutbound) + - [extraArgs](#services-proxy-suite-sshproxy-extraargs) + - [host](#services-proxy-suite-sshproxy-host) + - [identityFile](#services-proxy-suite-sshproxy-identityfile) + - [knownHostsFile](#services-proxy-suite-sshproxy-knownhostsfile) + - [listenAddress](#services-proxy-suite-sshproxy-listenaddress) + - [listenPort](#services-proxy-suite-sshproxy-listenport) + - [sshPort](#services-proxy-suite-sshproxy-sshport) + - [strictHostKeyChecking](#services-proxy-suite-sshproxy-stricthostkeychecking) + - [user](#services-proxy-suite-sshproxy-user) - tgWsProxy - [enable](#services-proxy-suite-tgwsproxy-enable) - [bufKb](#services-proxy-suite-tgwsproxy-bufkb) @@ -336,7 +348,7 @@ services.proxy-suite = { ]; routeTable = 102; }; - port = 1081; + port = 1085; proxyMark = 2; routeTable = 100; }; @@ -367,6 +379,19 @@ services.proxy-suite = { package = pkgs.xray; }; }; + sshProxy = { + asOutbound = false; + enable = false; + extraArgs = [ ]; + host = null; + identityFile = null; + knownHostsFile = null; + listenAddress = "127.0.0.1"; + listenPort = 1085; + sshPort = 22; + strictHostKeyChecking = "accept-new"; + user = null; + }; tgWsProxy = { bufKb = 256; bypassTransparentProxy = true; @@ -602,7 +627,7 @@ services.proxy-suite = { ]; routeTable = 102; }; - port = 1081; + port = 1085; proxyMark = 2; routeTable = 100; }; @@ -634,6 +659,22 @@ services.proxy-suite = { package = pkgs.xray; }; }; + sshProxy = { + asOutbound = true; + enable = true; + extraArgs = [ + "-o" + "ServerAliveInterval=30" + ]; + host = "ssh.example.com"; + identityFile = "/run/secrets/proxy-suite-ssh-key"; + knownHostsFile = "/run/secrets/proxy-suite-ssh-known-hosts"; + listenAddress = "127.0.0.1"; + listenPort = 1085; + sshPort = 22; + strictHostKeyChecking = "yes"; + user = "root"; + }; tgWsProxy = { bufKb = 512; bypassTransparentProxy = true; @@ -4891,7 +4932,7 @@ TCP/UDP traffic to this port\. *Default:* ```nix -1081 +1085 ``` @@ -4899,7 +4940,7 @@ TCP/UDP traffic to this port\. *Example:* ```nix -1081 +1085 ``` *Declared by:* @@ -5527,6 +5568,377 @@ pkgs.xray + +## services\.proxy-suite\.sshProxy\.enable + + + +Whether to enable SSH dynamic SOCKS5 proxy\. + + + +*Type:* +boolean + + + +*Default:* + +```nix +false +``` + + + +*Example:* + +```nix +true +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.asOutbound + + + +Add the local SSH SOCKS5 listener as a proxy-suite outbound tagged +“ssh-proxy”\. Requires proxy\.enable and an active SingBox or XRay +backend\. + + + +*Type:* +boolean + + + +*Default:* + +```nix +false +``` + + + +*Example:* + +```nix +true +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.extraArgs + + + +Additional arguments passed to OpenSSH before the destination\. +This can be used for options such as jump hosts or agent forwarding\. + + + +*Type:* +list of string + + + +*Default:* + +```nix +[ ] +``` + + + +*Example:* + +```nix +[ + "-o" + "ServerAliveInterval=30" +] +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.host + + + +SSH server hostname or address\. Required when sshProxy\.enable is true\. + + + +*Type:* +null or string matching the pattern \[^\[:space:]]+ + + + +*Default:* + +```nix +null +``` + + + +*Example:* + +```nix +"ssh.example.com" +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.identityFile + + + +Runtime path to the SSH private key\. Leave unset to use the SSH agent +or OpenSSH’s normal identity-file lookup\. + + + +*Type:* +null or string + + + +*Default:* + +```nix +null +``` + + + +*Example:* + +```nix +"/run/secrets/proxy-suite-ssh-key" +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.knownHostsFile + + + +Optional runtime path to the known-hosts file passed to OpenSSH\. + + + +*Type:* +null or string + + + +*Default:* + +```nix +null +``` + + + +*Example:* + +```nix +"/run/secrets/proxy-suite-ssh-known-hosts" +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.listenAddress + + + +Local address for the SSH-created SOCKS5 listener\. + + + +*Type:* +string matching the pattern \[^\[:space:]]+ + + + +*Default:* + +```nix +"127.0.0.1" +``` + + + +*Example:* + +```nix +"127.0.0.1" +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.listenPort + + + +Local port for the SSH-created SOCKS5 listener\. + + + +*Type:* +16 bit unsigned integer; between 0 and 65535 (both inclusive) + + + +*Default:* + +```nix +1085 +``` + + + +*Example:* + +```nix +1085 +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.sshPort + + + +Remote SSH server port\. + + + +*Type:* +16 bit unsigned integer; between 0 and 65535 (both inclusive) + + + +*Default:* + +```nix +22 +``` + + + +*Example:* + +```nix +22 +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.strictHostKeyChecking + + + +OpenSSH host-key verification policy\. Use “yes” with pinned +knownHostsFile contents for strict verification\. + + + +*Type:* +one of “yes”, “accept-new”, “no” + + + +*Default:* + +```nix +"accept-new" +``` + + + +*Example:* + +```nix +"yes" +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + + +## services\.proxy-suite\.sshProxy\.user + + + +SSH login user\. Required when sshProxy\.enable is true\. + + + +*Type:* +null or string matching the pattern \[^\[:space:]]+ + + + +*Default:* + +```nix +null +``` + + + +*Example:* + +```nix +"root" +``` + +*Declared by:* + - [modules/proxy-suite/options/ssh-proxy\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/ssh-proxy.nix) + + + ## services\.proxy-suite\.tgWsProxy\.enable @@ -7052,8 +7464,6 @@ list of string ## services\.proxy-suite\.zapret\.ipsetExclude - - IPs/CIDRs to exclude from zapret’s ipset\. Also excluded from zapret-derived proxy direct IP routing when syncDirectRoutingUserIps = true\. diff --git a/modules/proxy-suite/default.nix b/modules/proxy-suite/default.nix index 33d15d4..d980360 100644 --- a/modules/proxy-suite/default.nix +++ b/modules/proxy-suite/default.nix @@ -86,6 +86,16 @@ in } )) + (lib.mkIf cfg.sshProxy.enable ( + import ./ssh-proxy.nix { + inherit + lib + pkgs + cfg + ; + } + )) + (lib.mkIf cfg.amneziaWg.enable ( import ./amnezia-wg.nix { inherit diff --git a/modules/proxy-suite/derived.nix b/modules/proxy-suite/derived.nix index 19daeb6..a1f7693 100644 --- a/modules/proxy-suite/derived.nix +++ b/modules/proxy-suite/derived.nix @@ -36,6 +36,9 @@ let perAppRoutingTproxy = proxyCfg.tproxy.perApp; perAppZapretCfg = cfg.zapret.perApp; userControlCfg = cfg.userControl; + sshProxyCfg = cfg.sshProxy; + sshProxyOutboundTag = "ssh-proxy"; + sshProxyOutboundEnabled = sshProxyCfg.enable && sshProxyCfg.asOutbound; selectionMode = proxyCfg.selection; builtinTags = [ @@ -44,10 +47,12 @@ let "block" ]; outboundTags = map (ob: ob.tag) proxyCfg.outbounds; + effectiveOutboundTags = outboundTags ++ lib.optional sshProxyOutboundEnabled sshProxyOutboundTag; subscriptionTags = map (sub: sub.tag) proxyCfg.subscriptions; hasStaticOutbounds = proxyCfg.outbounds != [ ]; hasSubscriptions = proxyCfg.subscriptions != [ ]; + hasAvailableOutbounds = hasStaticOutbounds || hasSubscriptions || sshProxyOutboundEnabled; collapseNamedOutbounds = selectionMode == "first"; clashApiEnabled = (singBoxEnabled || hybridEnabled) && selectionMode != "first"; perAppZapretEnabled = perAppZapretCfg.enable; @@ -79,7 +84,7 @@ let invalidRoutingTargets = lib.unique ( map (rule: rule.outbound) ( builtins.filter ( - rule: !builtins.elem rule.outbound (builtinTags ++ outboundTags) + rule: !builtins.elem rule.outbound (builtinTags ++ effectiveOutboundTags) ) proxyCfg.routing.rules ) ); @@ -105,13 +110,18 @@ in perAppZapretEnabled userControlCfg userControlEnabled + sshProxyCfg + sshProxyOutboundTag + sshProxyOutboundEnabled constants selectionMode builtinTags outboundTags + effectiveOutboundTags subscriptionTags hasStaticOutbounds hasSubscriptions + hasAvailableOutbounds collapseNamedOutbounds clashApiEnabled invalidRoutingTargets diff --git a/modules/proxy-suite/options/default.nix b/modules/proxy-suite/options/default.nix index 3df334a..53a21f7 100644 --- a/modules/proxy-suite/options/default.nix +++ b/modules/proxy-suite/options/default.nix @@ -3,6 +3,7 @@ { imports = [ ./other.nix + ./ssh-proxy.nix ./amnezia-wg.nix ./legacy-sing-box.nix ./sing-box.nix diff --git a/modules/proxy-suite/options/sing-box-tproxy.nix b/modules/proxy-suite/options/sing-box-tproxy.nix index 710dc4d..91ca132 100644 --- a/modules/proxy-suite/options/sing-box-tproxy.nix +++ b/modules/proxy-suite/options/sing-box-tproxy.nix @@ -20,13 +20,13 @@ in port = mkOption { type = types.port; - default = 1081; + default = 1085; description = '' Local listen port for the proxy backend's TProxy inbound. nftables redirection created by proxy-suite-tproxy sends intercepted TCP/UDP traffic to this port. ''; - example = 1081; + example = 1085; }; fwmark = mkOption { diff --git a/modules/proxy-suite/options/ssh-proxy.nix b/modules/proxy-suite/options/ssh-proxy.nix new file mode 100644 index 0000000..4e11bd7 --- /dev/null +++ b/modules/proxy-suite/options/ssh-proxy.nix @@ -0,0 +1,107 @@ +# SSH dynamic SOCKS5 proxy options. +{ lib, ... }: + +let + inherit (lib) mkEnableOption mkOption types; +in +{ + options.services.proxy-suite.sshProxy = { + enable = mkEnableOption "SSH dynamic SOCKS5 proxy"; + + user = mkOption { + type = types.nullOr (types.strMatching "[^[:space:]]+"); + default = null; + description = '' + SSH login user. Required when sshProxy.enable is true. + ''; + example = "root"; + }; + + host = mkOption { + type = types.nullOr (types.strMatching "[^[:space:]]+"); + default = null; + description = '' + SSH server hostname or address. Required when sshProxy.enable is true. + ''; + example = "ssh.example.com"; + }; + + sshPort = mkOption { + type = types.port; + default = 22; + description = "Remote SSH server port."; + example = 22; + }; + + listenAddress = mkOption { + type = types.strMatching "[^[:space:]]+"; + default = "127.0.0.1"; + description = "Local address for the SSH-created SOCKS5 listener."; + example = "127.0.0.1"; + }; + + listenPort = mkOption { + type = types.port; + default = 1085; + description = "Local port for the SSH-created SOCKS5 listener."; + example = 1085; + }; + + identityFile = mkOption { + type = types.nullOr types.str; + default = null; + description = '' + Runtime path to the SSH private key. Leave unset to use the SSH agent + or OpenSSH's normal identity-file lookup. + ''; + example = "/run/secrets/proxy-suite-ssh-key"; + }; + + knownHostsFile = mkOption { + type = types.nullOr types.str; + default = null; + description = '' + Optional runtime path to the known-hosts file passed to OpenSSH. + ''; + example = "/run/secrets/proxy-suite-ssh-known-hosts"; + }; + + strictHostKeyChecking = mkOption { + type = types.enum [ + "yes" + "accept-new" + "no" + ]; + default = "accept-new"; + description = '' + OpenSSH host-key verification policy. Use "yes" with pinned + knownHostsFile contents for strict verification. + ''; + example = "yes"; + }; + + extraArgs = mkOption { + type = types.listOf types.str; + default = [ ]; + description = '' + Additional arguments passed to OpenSSH before the destination. + This can be used for options such as jump hosts or agent forwarding. + ''; + example = [ + "-o" + "ServerAliveInterval=30" + ]; + }; + + asOutbound = mkOption { + type = types.bool; + default = false; + description = '' + Add the local SSH SOCKS5 listener as a proxy-suite outbound tagged + "ssh-proxy". Requires proxy.enable and an active SingBox or XRay + backend. + ''; + example = true; + }; + }; +} diff --git a/modules/proxy-suite/service-assertions.nix b/modules/proxy-suite/service-assertions.nix index 3ebb2ba..c534c0b 100644 --- a/modules/proxy-suite/service-assertions.nix +++ b/modules/proxy-suite/service-assertions.nix @@ -5,6 +5,7 @@ tgWsProxyCfg, builtinTags, outboundTags, + effectiveOutboundTags ? outboundTags, subscriptionTags, invalidRoutingTargets, effectivePerAppRoutingProfileNames, @@ -63,16 +64,24 @@ let (mkAssertion (!proxyEnabled || proxyCfg.singBox.enable || proxyCfg.xray.enable) "proxy-suite: at least one of proxy.singBox.enable or proxy.xray.enable must be true when proxy.enable = true" ) - (requireAvailable proxyEnabled ( - proxyCfg.outbounds != [ ] || proxyCfg.subscriptions != [ ] + (requireAvailable proxyEnabled (derived.hasAvailableOutbounds ) "proxy-suite: at least one outbound or subscription is required when proxy.enable = true") - (uniqueValues proxyEnabled outboundTags "proxy-suite: outbound tags must be unique") + (uniqueValues proxyEnabled effectiveOutboundTags "proxy-suite: outbound tags must be unique") (uniqueValues proxyEnabled subscriptionTags "proxy-suite: subscription tags must be unique because they are used as cache keys and outbound tag prefixes" ) (mkAssertion ( - !proxyEnabled || builtins.all (tag: !builtins.elem tag builtinTags) outboundTags + !proxyEnabled || builtins.all (tag: !builtins.elem tag builtinTags) effectiveOutboundTags ) "proxy-suite: outbound tags must not use reserved names: proxy, direct, block") + (requireEnabled cfg.sshProxy.asOutbound cfg.sshProxy.enable + "proxy-suite: sshProxy.asOutbound requires sshProxy.enable = true" + ) + (requireEnabled cfg.sshProxy.asOutbound proxyEnabled + "proxy-suite: sshProxy.asOutbound requires proxy.enable = true" + ) + (mkAssertion ( + !cfg.sshProxy.enable || (cfg.sshProxy.user != null && cfg.sshProxy.host != null) + ) "proxy-suite: sshProxy.user and sshProxy.host are required when sshProxy.enable = true") (mkAssertion (!proxyEnabled || invalidRoutingTargets == [ ]) "proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}" ) diff --git a/modules/proxy-suite/service/context.nix b/modules/proxy-suite/service/context.nix index 3a887e4..a8f7c19 100644 --- a/modules/proxy-suite/service/context.nix +++ b/modules/proxy-suite/service/context.nix @@ -42,6 +42,8 @@ let invalidRoutingTargets collapseNamedOutbounds hasSubscriptions + sshProxyCfg + sshProxyOutboundEnabled ; # Tool paths – defined once here and passed into sub-modules as needed. @@ -74,6 +76,7 @@ let pkgs singBoxCfg proxyCfg + sshProxyCfg xrayEnabled hybridEnabled pureXrayEnabled diff --git a/modules/proxy-suite/service/default.nix b/modules/proxy-suite/service/default.nix index 689f936..87282f4 100644 --- a/modules/proxy-suite/service/default.nix +++ b/modules/proxy-suite/service/default.nix @@ -60,7 +60,9 @@ let userControlEnabled perAppZapretEnabled hasSubscriptions + sshProxyOutboundEnabled outboundTags + effectiveOutboundTags subscriptionTags invalidRoutingTargets builtinTags @@ -96,6 +98,7 @@ let perAppRoutingTproxy perAppZapretEnabled hasSubscriptions + sshProxyOutboundEnabled scripts perAppRouting routingScripts @@ -153,6 +156,7 @@ in inherit builtinTags outboundTags + effectiveOutboundTags subscriptionTags invalidRoutingTargets ; diff --git a/modules/proxy-suite/service/script-blocks/outbounds.nix b/modules/proxy-suite/service/script-blocks/outbounds.nix index b9e7532..e039697 100644 --- a/modules/proxy-suite/service/script-blocks/outbounds.nix +++ b/modules/proxy-suite/service/script-blocks/outbounds.nix @@ -4,6 +4,7 @@ pkgs, singBoxCfg, proxyCfg, + sshProxyCfg, pureXrayEnabled, hybridEnabled, collapseNamedOutbounds, @@ -19,6 +20,8 @@ }: let + sshProxyTag = "ssh-proxy"; + rawOutboundJson = ob: tag: routingMark: let @@ -163,6 +166,47 @@ let fi ''; + mkSshProxyOutboundBlock = + routingMark: + let + singBoxOutbound = { + type = "socks"; + tag = sshProxyTag; + server = sshProxyCfg.listenAddress; + server_port = sshProxyCfg.listenPort; + version = "5"; + } + // lib.optionalAttrs (routingMark != null) { + routing_mark = routingMark; + }; + xrayOutbound = { + protocol = "socks"; + tag = sshProxyTag; + settings = { + address = sshProxyCfg.listenAddress; + port = sshProxyCfg.listenPort; + }; + } + // lib.optionalAttrs (routingMark != null) { + streamSettings.sockopt.mark = routingMark; + }; + outbound = if pureXrayEnabled then xrayOutbound else singBoxOutbound; + outboundJson = builtins.toJSON outbound; + jsonFile = pkgs.writeText "proxy-suite-ob-ssh-proxy-${backend}.json" outboundJson; + in + if hybridEnabled then + '' + # outbound: ${sshProxyTag} (SSH SOCKS5) + OB_JSON=$(cat "${jsonFile}") + _proxy_suite_add_sing_box_ob "$OB_JSON" + '' + else + '' + # outbound: ${sshProxyTag} (SSH SOCKS5) + OB_JSON=$(cat "${jsonFile}") + OUTBOUNDS_JSON=$(${jq} --argjson ob "$OB_JSON" '. + [$ob]' <<< "$OUTBOUNDS_JSON") + ''; + mkBackendOutboundBlock = if hybridEnabled then mkHybridOutboundBlock else mkOutboundBlock; requireOutboundsBlock = '' @@ -185,6 +229,8 @@ let sub: mkSubscriptionBlock sub routingMark ) proxyCfg.subscriptions; + sshProxyBlock = lib.optionalString sshProxyCfg.asOutbound (mkSshProxyOutboundBlock routingMark); + wrapperBlock = if pureXrayEnabled && selectionMode == "urltest" then '' @@ -194,13 +240,15 @@ let fi '' else if collapseNamedOutbounds then - lib.optionalString (proxyCfg.outbounds == [ ] && proxyCfg.subscriptions != [ ]) '' - FIRST_TAG=$(${jq} -r 'if length > 0 then .[0].tag else "" end' <<< "$OUTBOUNDS_JSON") - if [ -n "$FIRST_TAG" ]; then - OUTBOUNDS_JSON=$(${jq} --arg t "$FIRST_TAG" \ - 'map(if .tag == $t then .tag = "proxy" else . end)' <<< "$OUTBOUNDS_JSON") - fi - '' + lib.optionalString + (proxyCfg.outbounds == [ ] && (proxyCfg.subscriptions != [ ] || sshProxyCfg.asOutbound)) + '' + FIRST_TAG=$(${jq} -r 'if length > 0 then .[0].tag else "" end' <<< "$OUTBOUNDS_JSON") + if [ -n "$FIRST_TAG" ]; then + OUTBOUNDS_JSON=$(${jq} --arg t "$FIRST_TAG" \ + 'map(if .tag == $t then .tag = "proxy" else . end)' <<< "$OUTBOUNDS_JSON") + fi + '' else if selectionMode == "selector" then '' TAGS=$(${jq} '[.[].tag]' <<< "$OUTBOUNDS_JSON") @@ -223,7 +271,7 @@ let OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON") ''; in - outboundBlocks + subscriptionBlocks + requireOutboundsBlock + wrapperBlock; + outboundBlocks + subscriptionBlocks + sshProxyBlock + requireOutboundsBlock + wrapperBlock; in { inherit mkOutboundScript rawOutboundJson; diff --git a/modules/proxy-suite/service/scripts.nix b/modules/proxy-suite/service/scripts.nix index 4535e49..fa2305c 100644 --- a/modules/proxy-suite/service/scripts.nix +++ b/modules/proxy-suite/service/scripts.nix @@ -4,6 +4,7 @@ pkgs, singBoxCfg, proxyCfg, + sshProxyCfg, xrayEnabled, hybridEnabled, pureXrayEnabled, @@ -88,6 +89,7 @@ let pkgs singBoxCfg proxyCfg + sshProxyCfg pureXrayEnabled hybridEnabled collapseNamedOutbounds diff --git a/modules/proxy-suite/service/units.nix b/modules/proxy-suite/service/units.nix index fc4b514..ff59b6c 100644 --- a/modules/proxy-suite/service/units.nix +++ b/modules/proxy-suite/service/units.nix @@ -12,6 +12,7 @@ perAppRoutingTproxy, perAppZapretEnabled, hasSubscriptions, + sshProxyOutboundEnabled, scripts, perAppRouting, routingScripts, @@ -60,7 +61,10 @@ let name = serviceNames.socks; value = mkRestartingService { description = "${backendDescription} proxy client (SOCKS + TProxy-ready)"; - after = [ "network-online.target" ]; + after = [ + "network-online.target" + ] + ++ lib.optional sshProxyOutboundEnabled "proxy-suite-ssh-proxy.service"; wants = [ "network-online.target" ]; wantedBy = [ "multi-user.target" ]; execStart = scripts.startSocks; diff --git a/modules/proxy-suite/ssh-proxy.nix b/modules/proxy-suite/ssh-proxy.nix new file mode 100644 index 0000000..113ce8d --- /dev/null +++ b/modules/proxy-suite/ssh-proxy.nix @@ -0,0 +1,43 @@ +# Native OpenSSH dynamic SOCKS5 proxy service. +{ + lib, + pkgs, + cfg, +}: + +let + s = cfg.sshProxy; + destination = if s.user == null then s.host else "${s.user}@${s.host}"; + extraArgs = lib.concatMapStrings (arg: " args+=(${lib.escapeShellArg arg})\n") s.extraArgs; + startScript = pkgs.writeShellScript "proxy-suite-ssh-proxy-start" '' + set -euo pipefail + + args=( + -N + -o BatchMode=yes + -o ExitOnForwardFailure=yes + -o StrictHostKeyChecking=${lib.escapeShellArg s.strictHostKeyChecking} + -D ${lib.escapeShellArg "${s.listenAddress}:${toString s.listenPort}"} + ) + ${lib.optionalString (s.sshPort != 22) "args+=(-p ${toString s.sshPort})"} + ${lib.optionalString (s.identityFile != null) "args+=(-i ${lib.escapeShellArg s.identityFile})"} + ${lib.optionalString ( + s.knownHostsFile != null + ) "args+=(-o ${lib.escapeShellArg "UserKnownHostsFile=${s.knownHostsFile}"})"} + ${extraArgs} + exec ${pkgs.openssh}/bin/ssh "''${args[@]}" ${lib.escapeShellArg destination} + ''; +in +{ + systemd.services.proxy-suite-ssh-proxy = { + description = "SSH dynamic SOCKS5 proxy"; + after = [ "network-online.target" ]; + wants = [ "network-online.target" ]; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + ExecStart = startScript; + Restart = "on-failure"; + RestartSec = 5; + }; + }; +} diff --git a/nix/checks/module-suite.nix b/nix/checks/module-suite.nix index 614e32c..db94a6e 100644 --- a/nix/checks/module-suite.nix +++ b/nix/checks/module-suite.nix @@ -104,6 +104,17 @@ let ; }; + sshProxyChecks = import ./ssh-proxy.nix { + inherit + pkgs + evalProxySuite + baseModule + mkBadProxySuiteFixture + mkFailingAssertions + mkRoutingRules + ; + }; + perAppRoutingChecks = import ./per-app-routing.nix { inherit pkgs @@ -203,6 +214,7 @@ let ] ++ coreProxyChecks.assertions ++ localProxyAuthChecks.assertions + ++ sshProxyChecks.assertions ++ tgWsProxyChecks.assertions ++ xrayBackendChecks.assertions ++ outboundValidationChecks.assertions diff --git a/nix/checks/ssh-proxy.nix b/nix/checks/ssh-proxy.nix new file mode 100644 index 0000000..e380afa --- /dev/null +++ b/nix/checks/ssh-proxy.nix @@ -0,0 +1,293 @@ +{ + pkgs, + evalProxySuite, + baseModule, + mkRoutingRules, + mkBadProxySuiteFixture, + mkFailingAssertions, +}: + +let + generated = import ./read-generated.nix; + + sshOnlySingBox = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + sshProxy = { + enable = true; + user = "root"; + host = "ssh.example.com"; + asOutbound = true; + identityFile = "/run/secrets/ssh-key"; + knownHostsFile = "/run/secrets/ssh-known-hosts"; + extraArgs = [ + "-o" + "ServerAliveInterval=30" + ]; + }; + proxy = { + enable = true; + singBox.enable = true; + }; + }; + } + ]; + sshOnlySingBoxStart = generated.readDerivation ( + sshOnlySingBox.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart + ); + sshService = sshOnlySingBox.config.systemd.services."proxy-suite-ssh-proxy"; + sshStartScript = generated.readDerivation sshService.serviceConfig.ExecStart; + + unchanged = evalProxySuite [ baseModule ]; + unchangedStart = generated.readDerivation ( + unchanged.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart + ); + + sshXray = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + sshProxy = { + enable = true; + user = "proxy"; + host = "ssh.example.com"; + asOutbound = true; + }; + proxy = { + enable = true; + xray.enable = true; + }; + }; + } + ]; + sshXrayStart = generated.readDerivation ( + sshXray.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart + ); + + sshHybrid = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + sshProxy = { + enable = true; + user = "proxy"; + host = "ssh.example.com"; + asOutbound = true; + }; + proxy = { + enable = true; + singBox.enable = true; + xray.enable = true; + }; + }; + } + ]; + sshHybridStart = generated.readDerivation ( + sshHybrid.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart + ); + + sshSelector = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + sshProxy = { + enable = true; + user = "proxy"; + host = "ssh.example.com"; + asOutbound = true; + }; + proxy = { + enable = true; + singBox.enable = true; + selection = "selector"; + outbounds = [ + { + tag = "static"; + url = "http://proxy.example.com:8080"; + } + ]; + }; + }; + } + ]; + sshSelectorStart = generated.readDerivation ( + sshSelector.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart + ); + + sshUrltest = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + sshProxy = { + enable = true; + user = "proxy"; + host = "ssh.example.com"; + asOutbound = true; + }; + proxy = { + enable = true; + xray.enable = true; + selection = "urltest"; + outbounds = [ + { + tag = "static"; + url = "http://proxy.example.com:8080"; + } + ]; + }; + }; + } + ]; + sshUrltestStart = generated.readDerivation ( + sshUrltest.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart + ); + + sshRouting = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + sshProxy = { + enable = true; + user = "proxy"; + host = "ssh.example.com"; + asOutbound = true; + }; + proxy = { + enable = true; + singBox.enable = true; + selection = "selector"; + routing.rules = [ + { + outbound = "ssh-proxy"; + domains = [ "ssh-only.example.com" ]; + } + ]; + }; + }; + } + ]; + + invalidAssertions = mkFailingAssertions mkBadProxySuiteFixture [ + { + enable = true; + sshProxy = { + enable = true; + user = "proxy"; + host = "ssh.example.com"; + asOutbound = true; + }; + } + { + enable = true; + sshProxy = { + enable = true; + asOutbound = true; + }; + proxy = { + enable = true; + singBox.enable = true; + }; + } + { + enable = true; + sshProxy = { + enable = true; + user = "proxy"; + host = "ssh.example.com"; + asOutbound = true; + }; + proxy = { + enable = true; + singBox.enable = true; + outbounds = [ + { + tag = "ssh-proxy"; + url = "http://proxy.example.com:8080"; + } + ]; + }; + } + ]; + + assertions = [ + ( + assert sshOnlySingBox.config.services.proxy-suite.proxy.outbounds == [ ]; + true + ) + ( + assert sshOnlySingBox.config.systemd.services ? "proxy-suite-ssh-proxy"; + true + ) + ( + assert + sshOnlySingBox.config.systemd.services."proxy-suite-socks".after == [ + "network-online.target" + "proxy-suite-ssh-proxy.service" + ]; + true + ) + ( + assert pkgs.lib.hasInfix "ssh-proxy" sshOnlySingBoxStart; + true + ) + ( + assert pkgs.lib.hasInfix "proxy-suite-ob-ssh-proxy-sing-box.json" sshOnlySingBoxStart; + true + ) + ( + assert pkgs.lib.hasInfix "ServerAliveInterval=30" sshStartScript; + true + ) + ( + assert !(sshService.serviceConfig ? SocketMark); + true + ) + ( + assert !(unchanged.config.systemd.services ? "proxy-suite-ssh-proxy"); + true + ) + ( + assert !(pkgs.lib.hasInfix "ssh-proxy" unchangedStart); + true + ) + ( + assert sshXray.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart != null; + true + ) + ( + assert pkgs.lib.hasInfix "proxy-suite-ob-ssh-proxy-xray.json" sshXrayStart; + true + ) + ( + assert pkgs.lib.hasInfix "_proxy_suite_add_sing_box_ob" sshHybridStart; + true + ) + ( + assert pkgs.lib.hasInfix "selector" sshSelectorStart; + true + ) + ( + assert pkgs.lib.hasInfix "ssh-proxy" sshSelectorStart; + true + ) + ( + assert pkgs.lib.hasInfix "proxy-suite-ob-ssh-proxy-xray.json" sshUrltestStart; + true + ) + ( + assert builtins.any (rule: (rule.outbound or null) == "ssh-proxy") (mkRoutingRules sshRouting); + true + ) + ] + ++ invalidAssertions; +in +{ + inherit assertions; +}