feat/wip: ssh-proxy

This commit is contained in:
FUFSoB committed 2026-09-08 23:36:47 +05:00
1 parent 9a1604554d
commit 7a106326ea
16 files changed
+984 -16

No files matched your search

+12
View File
@@ -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 - **`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 - **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 - **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. # Native AmneziaWG profiles are independent of the SingBox/XRay backend.
# Only one AWG, TUN, or TProxy global tunnel can be active at once; # Only one AWG, TUN, or TProxy global tunnel can be active at once;
# zapret is mutually exclusive with AWG while its service is running. # zapret is mutually exclusive with AWG while its service is running.
+416 -6
View File
@@ -181,6 +181,18 @@ Update module option docs there instead of editing this file by hand.
- xray - xray
- [enable](#services-proxy-suite-proxy-xray-enable) - [enable](#services-proxy-suite-proxy-xray-enable)
- [package](#services-proxy-suite-proxy-xray-package) - [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 - tgWsProxy
- [enable](#services-proxy-suite-tgwsproxy-enable) - [enable](#services-proxy-suite-tgwsproxy-enable)
- [bufKb](#services-proxy-suite-tgwsproxy-bufkb) - [bufKb](#services-proxy-suite-tgwsproxy-bufkb)
@@ -336,7 +348,7 @@ services.proxy-suite = {
]; ];
routeTable = 102; routeTable = 102;
}; };
port = 1081; port = 1085;
proxyMark = 2; proxyMark = 2;
routeTable = 100; routeTable = 100;
}; };
@@ -367,6 +379,19 @@ services.proxy-suite = {
package = pkgs.xray; 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 = { tgWsProxy = {
bufKb = 256; bufKb = 256;
bypassTransparentProxy = true; bypassTransparentProxy = true;
@@ -602,7 +627,7 @@ services.proxy-suite = {
]; ];
routeTable = 102; routeTable = 102;
}; };
port = 1081; port = 1085;
proxyMark = 2; proxyMark = 2;
routeTable = 100; routeTable = 100;
}; };
@@ -634,6 +659,22 @@ services.proxy-suite = {
package = pkgs.xray; 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 = { tgWsProxy = {
bufKb = 512; bufKb = 512;
bypassTransparentProxy = true; bypassTransparentProxy = true;
@@ -4891,7 +4932,7 @@ TCP/UDP traffic to this port\.
*Default:* *Default:*
```nix ```nix
1081 1085
``` ```
@@ -4899,7 +4940,7 @@ TCP/UDP traffic to this port\.
*Example:* *Example:*
```nix ```nix
1081 1085
``` ```
*Declared by:* *Declared by:*
@@ -5527,6 +5568,377 @@ pkgs.xray
<a id="services-proxy-suite-sshproxy-enable"></a>
## 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)
<a id="services-proxy-suite-sshproxy-asoutbound"></a>
## 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)
<a id="services-proxy-suite-sshproxy-extraargs"></a>
## 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)
<a id="services-proxy-suite-sshproxy-host"></a>
## 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)
<a id="services-proxy-suite-sshproxy-identityfile"></a>
## 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)
<a id="services-proxy-suite-sshproxy-knownhostsfile"></a>
## 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)
<a id="services-proxy-suite-sshproxy-listenaddress"></a>
## 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)
<a id="services-proxy-suite-sshproxy-listenport"></a>
## 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)
<a id="services-proxy-suite-sshproxy-sshport"></a>
## 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)
<a id="services-proxy-suite-sshproxy-stricthostkeychecking"></a>
## 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)
<a id="services-proxy-suite-sshproxy-user"></a>
## 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)
<a id="services-proxy-suite-tgwsproxy-enable"></a> <a id="services-proxy-suite-tgwsproxy-enable"></a>
## services\.proxy-suite\.tgWsProxy\.enable ## services\.proxy-suite\.tgWsProxy\.enable
@@ -7052,8 +7464,6 @@ list of string
<a id="services-proxy-suite-zapret-ipsetexclude"></a> <a id="services-proxy-suite-zapret-ipsetexclude"></a>
## services\.proxy-suite\.zapret\.ipsetExclude ## services\.proxy-suite\.zapret\.ipsetExclude
IPs/CIDRs to exclude from zapret’s ipset\. IPs/CIDRs to exclude from zapret’s ipset\.
Also excluded from zapret-derived proxy direct IP routing when Also excluded from zapret-derived proxy direct IP routing when
syncDirectRoutingUserIps = true\. syncDirectRoutingUserIps = true\.
+10
View File
@@ -86,6 +86,16 @@ in
} }
)) ))
(lib.mkIf cfg.sshProxy.enable (
import ./ssh-proxy.nix {
inherit
lib
pkgs
cfg
;
}
))
(lib.mkIf cfg.amneziaWg.enable ( (lib.mkIf cfg.amneziaWg.enable (
import ./amnezia-wg.nix { import ./amnezia-wg.nix {
inherit inherit
+11 -1
View File
@@ -36,6 +36,9 @@ let
perAppRoutingTproxy = proxyCfg.tproxy.perApp; perAppRoutingTproxy = proxyCfg.tproxy.perApp;
perAppZapretCfg = cfg.zapret.perApp; perAppZapretCfg = cfg.zapret.perApp;
userControlCfg = cfg.userControl; userControlCfg = cfg.userControl;
sshProxyCfg = cfg.sshProxy;
sshProxyOutboundTag = "ssh-proxy";
sshProxyOutboundEnabled = sshProxyCfg.enable && sshProxyCfg.asOutbound;
selectionMode = proxyCfg.selection; selectionMode = proxyCfg.selection;
builtinTags = [ builtinTags = [
@@ -44,10 +47,12 @@ let
"block" "block"
]; ];
outboundTags = map (ob: ob.tag) proxyCfg.outbounds; outboundTags = map (ob: ob.tag) proxyCfg.outbounds;
effectiveOutboundTags = outboundTags ++ lib.optional sshProxyOutboundEnabled sshProxyOutboundTag;
subscriptionTags = map (sub: sub.tag) proxyCfg.subscriptions; subscriptionTags = map (sub: sub.tag) proxyCfg.subscriptions;
hasStaticOutbounds = proxyCfg.outbounds != [ ]; hasStaticOutbounds = proxyCfg.outbounds != [ ];
hasSubscriptions = proxyCfg.subscriptions != [ ]; hasSubscriptions = proxyCfg.subscriptions != [ ];
hasAvailableOutbounds = hasStaticOutbounds || hasSubscriptions || sshProxyOutboundEnabled;
collapseNamedOutbounds = selectionMode == "first"; collapseNamedOutbounds = selectionMode == "first";
clashApiEnabled = (singBoxEnabled || hybridEnabled) && selectionMode != "first"; clashApiEnabled = (singBoxEnabled || hybridEnabled) && selectionMode != "first";
perAppZapretEnabled = perAppZapretCfg.enable; perAppZapretEnabled = perAppZapretCfg.enable;
@@ -79,7 +84,7 @@ let
invalidRoutingTargets = lib.unique ( invalidRoutingTargets = lib.unique (
map (rule: rule.outbound) ( map (rule: rule.outbound) (
builtins.filter ( builtins.filter (
rule: !builtins.elem rule.outbound (builtinTags ++ outboundTags) rule: !builtins.elem rule.outbound (builtinTags ++ effectiveOutboundTags)
) proxyCfg.routing.rules ) proxyCfg.routing.rules
) )
); );
@@ -105,13 +110,18 @@ in
perAppZapretEnabled perAppZapretEnabled
userControlCfg userControlCfg
userControlEnabled userControlEnabled
sshProxyCfg
sshProxyOutboundTag
sshProxyOutboundEnabled
constants constants
selectionMode selectionMode
builtinTags builtinTags
outboundTags outboundTags
effectiveOutboundTags
subscriptionTags subscriptionTags
hasStaticOutbounds hasStaticOutbounds
hasSubscriptions hasSubscriptions
hasAvailableOutbounds
collapseNamedOutbounds collapseNamedOutbounds
clashApiEnabled clashApiEnabled
invalidRoutingTargets invalidRoutingTargets
+1
View File
@@ -3,6 +3,7 @@
{ {
imports = [ imports = [
./other.nix ./other.nix
./ssh-proxy.nix
./amnezia-wg.nix ./amnezia-wg.nix
./legacy-sing-box.nix ./legacy-sing-box.nix
./sing-box.nix ./sing-box.nix
@@ -20,13 +20,13 @@ in
port = mkOption { port = mkOption {
type = types.port; type = types.port;
default = 1081; default = 1085;
description = '' description = ''
Local listen port for the proxy backend's TProxy inbound. Local listen port for the proxy backend's TProxy inbound.
nftables redirection created by proxy-suite-tproxy sends intercepted nftables redirection created by proxy-suite-tproxy sends intercepted
TCP/UDP traffic to this port. TCP/UDP traffic to this port.
''; '';
example = 1081; example = 1085;
}; };
fwmark = mkOption { fwmark = mkOption {
+107
View File
@@ -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;
};
};
}
+13 -4
View File
@@ -5,6 +5,7 @@
tgWsProxyCfg, tgWsProxyCfg,
builtinTags, builtinTags,
outboundTags, outboundTags,
effectiveOutboundTags ? outboundTags,
subscriptionTags, subscriptionTags,
invalidRoutingTargets, invalidRoutingTargets,
effectivePerAppRoutingProfileNames, effectivePerAppRoutingProfileNames,
@@ -63,16 +64,24 @@ let
(mkAssertion (!proxyEnabled || proxyCfg.singBox.enable || proxyCfg.xray.enable) (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" "proxy-suite: at least one of proxy.singBox.enable or proxy.xray.enable must be true when proxy.enable = true"
) )
(requireAvailable proxyEnabled ( (requireAvailable proxyEnabled (derived.hasAvailableOutbounds
proxyCfg.outbounds != [ ] || proxyCfg.subscriptions != [ ]
) "proxy-suite: at least one outbound or subscription is required when proxy.enable = true") ) "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 (uniqueValues proxyEnabled subscriptionTags
"proxy-suite: subscription tags must be unique because they are used as cache keys and outbound tag prefixes" "proxy-suite: subscription tags must be unique because they are used as cache keys and outbound tag prefixes"
) )
(mkAssertion ( (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") ) "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 == [ ]) (mkAssertion (!proxyEnabled || invalidRoutingTargets == [ ])
"proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}" "proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}"
) )
+3
View File
@@ -42,6 +42,8 @@ let
invalidRoutingTargets invalidRoutingTargets
collapseNamedOutbounds collapseNamedOutbounds
hasSubscriptions hasSubscriptions
sshProxyCfg
sshProxyOutboundEnabled
; ;
# Tool paths – defined once here and passed into sub-modules as needed. # Tool paths – defined once here and passed into sub-modules as needed.
@@ -74,6 +76,7 @@ let
pkgs pkgs
singBoxCfg singBoxCfg
proxyCfg proxyCfg
sshProxyCfg
xrayEnabled xrayEnabled
hybridEnabled hybridEnabled
pureXrayEnabled pureXrayEnabled
+4
View File
@@ -60,7 +60,9 @@ let
userControlEnabled userControlEnabled
perAppZapretEnabled perAppZapretEnabled
hasSubscriptions hasSubscriptions
sshProxyOutboundEnabled
outboundTags outboundTags
effectiveOutboundTags
subscriptionTags subscriptionTags
invalidRoutingTargets invalidRoutingTargets
builtinTags builtinTags
@@ -96,6 +98,7 @@ let
perAppRoutingTproxy perAppRoutingTproxy
perAppZapretEnabled perAppZapretEnabled
hasSubscriptions hasSubscriptions
sshProxyOutboundEnabled
scripts scripts
perAppRouting perAppRouting
routingScripts routingScripts
@@ -153,6 +156,7 @@ in
inherit inherit
builtinTags builtinTags
outboundTags outboundTags
effectiveOutboundTags
subscriptionTags subscriptionTags
invalidRoutingTargets invalidRoutingTargets
; ;
@@ -4,6 +4,7 @@
pkgs, pkgs,
singBoxCfg, singBoxCfg,
proxyCfg, proxyCfg,
sshProxyCfg,
pureXrayEnabled, pureXrayEnabled,
hybridEnabled, hybridEnabled,
collapseNamedOutbounds, collapseNamedOutbounds,
@@ -19,6 +20,8 @@
}: }:
let let
sshProxyTag = "ssh-proxy";
rawOutboundJson = rawOutboundJson =
ob: tag: routingMark: ob: tag: routingMark:
let let
@@ -163,6 +166,47 @@ let
fi 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; mkBackendOutboundBlock = if hybridEnabled then mkHybridOutboundBlock else mkOutboundBlock;
requireOutboundsBlock = '' requireOutboundsBlock = ''
@@ -185,6 +229,8 @@ let
sub: mkSubscriptionBlock sub routingMark sub: mkSubscriptionBlock sub routingMark
) proxyCfg.subscriptions; ) proxyCfg.subscriptions;
sshProxyBlock = lib.optionalString sshProxyCfg.asOutbound (mkSshProxyOutboundBlock routingMark);
wrapperBlock = wrapperBlock =
if pureXrayEnabled && selectionMode == "urltest" then if pureXrayEnabled && selectionMode == "urltest" then
'' ''
@@ -194,7 +240,9 @@ let
fi fi
'' ''
else if collapseNamedOutbounds then else if collapseNamedOutbounds then
lib.optionalString (proxyCfg.outbounds == [ ] && proxyCfg.subscriptions != [ ]) '' lib.optionalString
(proxyCfg.outbounds == [ ] && (proxyCfg.subscriptions != [ ] || sshProxyCfg.asOutbound))
''
FIRST_TAG=$(${jq} -r 'if length > 0 then .[0].tag else "" end' <<< "$OUTBOUNDS_JSON") FIRST_TAG=$(${jq} -r 'if length > 0 then .[0].tag else "" end' <<< "$OUTBOUNDS_JSON")
if [ -n "$FIRST_TAG" ]; then if [ -n "$FIRST_TAG" ]; then
OUTBOUNDS_JSON=$(${jq} --arg t "$FIRST_TAG" \ OUTBOUNDS_JSON=$(${jq} --arg t "$FIRST_TAG" \
@@ -223,7 +271,7 @@ let
OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON") OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON")
''; '';
in in
outboundBlocks + subscriptionBlocks + requireOutboundsBlock + wrapperBlock; outboundBlocks + subscriptionBlocks + sshProxyBlock + requireOutboundsBlock + wrapperBlock;
in in
{ {
inherit mkOutboundScript rawOutboundJson; inherit mkOutboundScript rawOutboundJson;
+2
View File
@@ -4,6 +4,7 @@
pkgs, pkgs,
singBoxCfg, singBoxCfg,
proxyCfg, proxyCfg,
sshProxyCfg,
xrayEnabled, xrayEnabled,
hybridEnabled, hybridEnabled,
pureXrayEnabled, pureXrayEnabled,
@@ -88,6 +89,7 @@ let
pkgs pkgs
singBoxCfg singBoxCfg
proxyCfg proxyCfg
sshProxyCfg
pureXrayEnabled pureXrayEnabled
hybridEnabled hybridEnabled
collapseNamedOutbounds collapseNamedOutbounds
+5 -1
View File
@@ -12,6 +12,7 @@
perAppRoutingTproxy, perAppRoutingTproxy,
perAppZapretEnabled, perAppZapretEnabled,
hasSubscriptions, hasSubscriptions,
sshProxyOutboundEnabled,
scripts, scripts,
perAppRouting, perAppRouting,
routingScripts, routingScripts,
@@ -60,7 +61,10 @@ let
name = serviceNames.socks; name = serviceNames.socks;
value = mkRestartingService { value = mkRestartingService {
description = "${backendDescription} proxy client (SOCKS + TProxy-ready)"; 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" ]; wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
execStart = scripts.startSocks; execStart = scripts.startSocks;
+43
View File
@@ -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;
};
};
}
+12
View File
@@ -104,6 +104,17 @@ let
; ;
}; };
sshProxyChecks = import ./ssh-proxy.nix {
inherit
pkgs
evalProxySuite
baseModule
mkBadProxySuiteFixture
mkFailingAssertions
mkRoutingRules
;
};
perAppRoutingChecks = import ./per-app-routing.nix { perAppRoutingChecks = import ./per-app-routing.nix {
inherit inherit
pkgs pkgs
@@ -203,6 +214,7 @@ let
] ]
++ coreProxyChecks.assertions ++ coreProxyChecks.assertions
++ localProxyAuthChecks.assertions ++ localProxyAuthChecks.assertions
++ sshProxyChecks.assertions
++ tgWsProxyChecks.assertions ++ tgWsProxyChecks.assertions
++ xrayBackendChecks.assertions ++ xrayBackendChecks.assertions
++ outboundValidationChecks.assertions ++ outboundValidationChecks.assertions
+293
View File
@@ -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;
}