feat/wip/fix: add inbounds support, fix ssh-proxy

This commit is contained in:
fufsob committed 2026-09-09 16:38:52 +00:00
1 parent ea7916dffb
commit 2a319d1f88
39 files changed
+5093 -48

No files matched your search

+4
View File
@@ -44,6 +44,8 @@ let
mkTProxyConfig = fixture: mkProxyConfig fixture "tproxyFile";
mkTunConfig = fixture: mkProxyConfig fixture "tunFile";
mkPerAppTunConfig = fixture: mkProxyConfig fixture "perAppTunFile";
mkInboundsConfig = fixture: mkProxyConfig fixture "proxyInboundsFile";
mkInboundsSpec = fixture: mkProxyConfig fixture "proxyInboundsSpecFile";
mkTProxyNftRules =
fixture:
let
@@ -227,6 +229,8 @@ in
mkTProxyConfig
mkTunConfig
mkPerAppTunConfig
mkInboundsConfig
mkInboundsSpec
mkTProxyNftRules
mkPerAppZapretNftRules
mkPerAppUserRules
+13
View File
@@ -11,6 +11,8 @@ let
mkTProxyConfig
mkTunConfig
mkPerAppTunConfig
mkInboundsConfig
mkInboundsSpec
mkTProxyNftRules
mkPerAppZapretNftRules
mkPerAppUserRules
@@ -115,6 +117,16 @@ let
;
};
proxyInboundsChecks = import ./proxy-inbounds.nix {
inherit
pkgs
evalProxySuite
baseModule
mkInboundsConfig
mkInboundsSpec
;
};
perAppRoutingChecks = import ./per-app-routing.nix {
inherit
pkgs
@@ -215,6 +227,7 @@ let
++ coreProxyChecks.assertions
++ localProxyAuthChecks.assertions
++ sshProxyChecks.assertions
++ proxyInboundsChecks.assertions
++ tgWsProxyChecks.assertions
++ xrayBackendChecks.assertions
++ outboundValidationChecks.assertions
+9
View File
@@ -10,6 +10,15 @@
touch "$out"
'';
build-inbound-renderer =
pkgs.runCommand "build-inbound-renderer-check" { nativeBuildInputs = [ pkgs.python3 ]; }
''
export PYTHONDONTWRITEBYTECODE=1
export PYTHONPATH=${../../scripts}:$PYTHONPATH
python ${../../scripts/test-build-inbound.py}
touch "$out"
'';
fetch-subscription-parser =
pkgs.runCommand "fetch-subscription-parser-check" { nativeBuildInputs = [ pkgs.python3 ]; }
''
+148
View File
@@ -0,0 +1,148 @@
{
pkgs,
proxySuiteModule,
}:
let
# Fixed so the client can be configured against it at build time. Real
# deployments keep these in a secret manager.
uuid = "b831381d-6324-4d53-ad4f-8cda48b30811";
ssPassword = "dGhpcy1pcy1hLTE2Ynl0ZS1rZXk=";
serverAddress = "192.168.0.1";
network = address: {
networking.useDHCP = false;
networking.interfaces.eth1.ipv4.addresses = [
{
inherit address;
prefixLength = 24;
}
];
};
in
pkgs.testers.runNixOSTest {
name = "proxy-suite-inbounds-runtime";
nodes = {
# A pure server: no client proxy stack at all.
server =
{ ... }:
{
imports = [
proxySuiteModule
(network serverAddress)
];
services.proxy-suite = {
enable = true;
proxy.enable = false;
proxyInbounds = {
enable = true;
inherit serverAddress;
via = "direct";
openFirewall = true;
listeners.vless-in = {
type = "vless";
port = 8443;
users = [
{
name = "tester";
inherit uuid;
}
];
};
listeners.ss-in = {
type = "shadowsocks";
port = 8388;
method = "2022-blake3-aes-128-gcm";
users = [ { password = ssPassword; } ];
};
};
};
services.nginx = {
enable = true;
virtualHosts."origin".locations."/".return = "200 'served-from-origin'";
};
networking.firewall.allowedTCPPorts = [ 80 ];
};
client =
{ ... }:
{
imports = [
proxySuiteModule
(network "192.168.0.2")
];
services.proxy-suite = {
enable = true;
proxy = {
enable = true;
xray.enable = true;
port = 1080;
outbounds = [
{
tag = "server";
url = "vless://${uuid}@${serverAddress}:8443?type=tcp&security=none";
}
];
};
};
};
};
testScript = ''
start_all()
server.wait_for_unit("proxy-suite-inbounds.service")
server.wait_for_unit("nginx.service")
server.wait_for_open_port(8443)
server.wait_for_open_port(8388)
with subtest("the rendered config carries both listeners"):
cfg = "/run/proxy-suite-inbounds/config.json"
server.succeed(f"test $(jq -r '.inbounds | length' {cfg}) = 2")
server.succeed(f"jq -e '.inbounds[] | select(.tag == \"vless-in\" and .port == 8443)' {cfg}")
server.succeed(f"jq -e '.inbounds[] | select(.tag == \"ss-in\" and .port == 8388)' {cfg}")
# Credentials live only in the runtime config, never in the store.
server.succeed(f"test $(stat -c %a {cfg}) = 600")
server.fail(f"grep -r '${uuid}' /nix/store/*proxy-suite-inbounds-*.json")
with subtest("safety rules are present and ordered first"):
cfg = "/run/proxy-suite-inbounds/config.json"
server.succeed(f"test $(jq -r '.routing.rules[0].ruleTag' {cfg}) = inbound-block-private")
server.succeed(f"jq -e '.routing.rules[] | select(.ruleTag == \"inbound-block-ru-domain\")' {cfg}")
server.succeed(f"test $(jq -r '.routing.rules[-1].outboundTag' {cfg}) = direct")
with subtest("a client reaches the origin through the inbound"):
client.wait_for_unit("proxy-suite-socks.service")
client.wait_for_open_port(1080)
client.succeed(
"curl -sS --fail --max-time 20 --proxy socks5h://127.0.0.1:1080"
f" http://{'${serverAddress}'}/ | grep -q served-from-origin"
)
with subtest("blocked destinations are refused"):
# The inbound must not become a way into the server's own loopback.
server.succeed("jq -e '.routing.rules[] | select(.ruleTag == \"inbound-block-private\") | select(.outboundTag == \"block\")' /run/proxy-suite-inbounds/config.json")
client.fail(
"curl -sS --fail --max-time 15 --proxy socks5h://127.0.0.1:1080"
" http://127.0.0.1/ 2>&1"
)
with subtest("share links are generated and readable by proxy-ctl"):
links = "/run/proxy-suite-inbounds/links.json"
server.succeed(f"test $(stat -c %a {links}) = 600")
server.succeed(f"test $(jq -r 'length' {links}) = 2")
link = server.succeed("proxy-ctl inbounds link vless-in").strip()
assert link.startswith("vless://${uuid}@${serverAddress}:8443"), link
assert "#tester" in link, link
server.succeed("proxy-ctl inbounds | grep -q vless-in")
server.succeed("proxy-ctl inbounds qr vless-in | head -c 1")
server.succeed("proxy-ctl status | grep -q proxy-suite-inbounds")
with subtest("a listener keeps serving after a restart"):
server.succeed("systemctl restart proxy-suite-inbounds.service")
server.wait_for_open_port(8443)
'';
}
+448
View File
@@ -0,0 +1,448 @@
{
pkgs,
evalProxySuite,
baseModule,
mkInboundsConfig,
mkInboundsSpec,
}:
let
inherit (pkgs) lib;
realityListener = {
type = "vless";
port = 443;
users = [ { uuidFile = "/run/secrets/uuid"; } ];
flow = "xtls-rprx-vision";
reality = {
enable = true;
serverNames = [ "www.microsoft.com" ];
privateKeyFile = "/run/secrets/reality-key";
publicKey = "public-key";
shortIds = [ "0123abcd" ];
};
};
mkInbounds =
proxyInbounds:
evalProxySuite [
baseModule
{ services.proxy-suite.proxyInbounds = { enable = true; } // proxyInbounds; }
];
relayFixture = mkInbounds { listeners.vless-in = realityListener; };
relayConfig = mkInboundsConfig relayFixture;
relaySpec = mkInboundsSpec relayFixture;
exitFixture = mkInbounds {
via = "direct";
listeners.vless-in = realityListener;
};
exitConfig = mkInboundsConfig exitFixture;
mixedFixture = mkInbounds {
listeners.relayed = realityListener;
listeners.local-exit = realityListener // {
port = 8443;
via = "direct";
};
};
mixedConfig = mkInboundsConfig mixedFixture;
# Two listeners, two different servers.
pinnedFixture = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
proxy = {
enable = true;
singBox.enable = true;
outbounds = [
{
tag = "nl-vps";
url = "http://nl.example.com:8080";
}
{
tag = "de-vps";
url = "http://de.example.com:8080";
}
{
tag = "unused";
singBoxJson = {
type = "tuic";
};
}
];
};
proxyInbounds = {
enable = true;
via = "nl-vps";
listeners.through-nl = realityListener;
listeners.through-de = realityListener // {
port = 8443;
via = "de-vps";
};
};
};
}
];
pinnedConfig = mkInboundsConfig pinnedFixture;
# A .ru serverAddress is exactly what blockRu would otherwise blackhole.
namedFixture = mkInbounds {
serverAddress = "vpn.example.ru";
listeners.vless-in = realityListener;
};
namedConfig = mkInboundsConfig namedFixture;
# The same trap in its literal-IP form.
ipNamedFixture = mkInbounds {
serverAddress = "82.146.44.102";
listeners.vless-in = realityListener;
};
ipNamedConfig = mkInboundsConfig ipNamedFixture;
unguardedFixture = mkInbounds {
blockRu = false;
blockPrivate = false;
listeners.vless-in = realityListener;
};
unguardedConfig = mkInboundsConfig unguardedFixture;
firewallFixture = mkInbounds {
listeners.vless-in = realityListener;
listeners.ss-in = {
type = "shadowsocks";
port = 8388;
users = [ { passwordFile = "/run/secrets/ss"; } ];
};
# Fronted by something else on this host, so its port stays closed even
# though the share link advertises the public one.
listeners.ws-in = {
type = "vless";
port = 10002;
sharePort = 443;
listenAddress = "127.0.0.1";
users = [ { uuidFile = "/run/secrets/ws"; } ];
transport = {
type = "ws";
path = "/vpnjantit";
};
tls = {
enable = true;
certificateFile = "/run/acme/fullchain.pem";
keyFile = "/run/acme/key.pem";
};
};
};
firewallSpec = mkInboundsSpec firewallFixture;
wsListener = lib.head (builtins.filter (l: l.tag == "ws-in") firewallSpec.listeners);
noFirewallFixture = mkInbounds {
openFirewall = false;
listeners.vless-in = realityListener;
};
ruleTags = config: map (rule: rule.ruleTag) config.routing.rules;
ruleByTag =
config: tag: builtins.head (builtins.filter (rule: rule.ruleTag == tag) config.routing.rules);
outboundTags = config: map (ob: ob.tag) config.outbounds;
hasOutbound = config: tag: builtins.elem tag (outboundTags config);
service = fixture: fixture.config.systemd.services."proxy-suite-inbounds";
# Bad configurations are checked by looking at config.assertions directly
# rather than by forcing system.build.toplevel: it is far cheaper, and it
# pins which assertion fired instead of just "something failed".
baseProxy = {
enable = true;
proxy.enable = true;
proxy.singBox.enable = true;
proxy.outbounds = [
{
tag = "primary";
url = "http://proxy.example.com:8080";
}
];
};
mkRejects =
proxySuiteConfig: fragment:
let
fixture = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = proxySuiteConfig;
}
];
failed = builtins.filter (a: !a.assertion) fixture.config.assertions;
in
if lib.any (a: lib.hasInfix fragment a.message) failed then
true
else
# Name what was expected and what actually failed, so a regression here
# points at the assertion instead of just "something evaluated wrong".
throw "proxy-inbounds check: expected an assertion matching '${fragment}', got: ${builtins.toJSON (map (a: a.message) failed)}";
failing = [
(mkRejects (
baseProxy
// {
proxyInbounds = {
enable = true;
listeners.bad = {
type = "vless";
xrayJson.protocol = "vless";
};
};
}
) "set exactly one of type, xrayJson, or jsonFile")
(mkRejects (
baseProxy
// {
proxyInbounds = {
enable = true;
listeners.a = realityListener;
listeners.b = realityListener;
};
}
) "must each use a distinct port")
(mkRejects {
enable = true;
proxy.enable = false;
proxyInbounds = {
enable = true;
via = "proxy";
listeners.vless-in = realityListener;
};
} "requires proxy.enable = true")
(mkRejects (
baseProxy
// {
proxyInbounds = {
enable = true;
listeners.vless-in = realityListener // {
reality = realityListener.reality // {
publicKey = null;
};
};
};
}
) "reality.publicKey is required")
(mkRejects (
baseProxy
// {
proxyInbounds = {
enable = true;
listeners.tls-in = {
type = "vless";
port = 8443;
users = [ { uuidFile = "/run/secrets/uuid"; } ];
tls.enable = true;
};
};
}
) "needs both tls.certificateFile and tls.keyFile")
(mkRejects (
baseProxy
// {
proxyInbounds = {
enable = true;
listeners.ws-in = realityListener // {
transport.type = "ws";
};
};
}
) "flow is only valid on a vless listener")
(mkRejects (
baseProxy
// {
proxyInbounds = {
enable = true;
via = "no-such-outbound";
listeners.vless-in = realityListener;
};
}
) "are not defined in proxy.outbounds")
(mkRejects {
enable = true;
proxy.enable = true;
proxy.singBox.enable = true;
proxy.outbounds = [
{
tag = "sb-only";
singBoxJson.type = "tuic";
}
];
proxyInbounds = {
enable = true;
via = "sb-only";
listeners.vless-in = realityListener;
};
} "targets a sing-box-only outbound")
(mkRejects (baseProxy // { proxyInbounds.enable = true; })
"requires at least one entry in proxyInbounds.listeners"
)
# recursiveUpdate, not //: this one nests into proxy, which baseProxy sets.
(mkRejects
(lib.recursiveUpdate baseProxy {
proxy.port = 1080;
proxyInbounds = {
enable = true;
listeners.clash = realityListener // {
port = 1080;
};
};
})
"collides with proxy.port"
)
];
assertions = [
# The template carries no listeners; they are rendered at start time so no
# credential reaches the Nix store.
(assert relayConfig.inbounds == [ ]; true)
# Relaying goes through the client stack's local SOCKS listener rather than
# a second copy of the outbound machinery.
(assert hasOutbound relayConfig "proxy"; true)
(assert (ruleByTag relayConfig "inbound-final").outboundTag == "proxy"; true)
(
assert
(builtins.head (builtins.filter (ob: ob.tag == "proxy") relayConfig.outbounds)).protocol
== "socks";
true
)
(
assert
(builtins.head (builtins.filter (ob: ob.tag == "proxy") relayConfig.outbounds))
.settings.servers
== [
{
address = "127.0.0.1";
port = 1080;
}
];
true
)
# A pure exit node needs no proxy outbound at all.
(assert !hasOutbound exitConfig "proxy"; true)
(assert (ruleByTag exitConfig "inbound-final").outboundTag == "direct"; true)
# Safety rules are on by default and come before anything else, so an
# inbound credential cannot reach the LAN or loopback.
(assert builtins.head (ruleTags relayConfig) == "inbound-block-private"; true)
(assert (ruleByTag relayConfig "inbound-block-private").outboundTag == "block"; true)
(assert (ruleByTag relayConfig "inbound-block-ru-domain").domain == [ "geosite:category-ru" ]; true)
(assert (ruleByTag relayConfig "inbound-block-ru-ip").ip == [ "geoip:ru" ]; true)
(assert !builtins.elem "inbound-block-private" (ruleTags unguardedConfig); true)
# The relay must not blackhole the address clients dial to reach it, and the
# exemption has to sit after blockPrivate but ahead of the country blocks.
(
assert
lib.take 3 (ruleTags namedConfig) == [
"inbound-block-private"
"inbound-server-address-direct"
"inbound-block-ru-domain"
];
true
)
(
assert
(ruleByTag namedConfig "inbound-server-address-direct").domain == [ "full:vpn.example.ru" ];
true
)
# No address to exempt when it is detected at runtime instead.
(assert !builtins.elem "inbound-server-address-direct" (ruleTags relayConfig); true)
(
assert
(ruleByTag ipNamedConfig "inbound-server-address-direct").ip == [ "82.146.44.102" ];
true
)
(assert !builtins.elem "inbound-block-ru-domain" (ruleTags unguardedConfig); true)
# A per-listener via becomes its own rule; listeners on the default do not.
(assert builtins.elem "inbound-via-local-exit" (ruleTags mixedConfig); true)
(assert !builtins.elem "inbound-via-relayed" (ruleTags mixedConfig); true)
(assert (ruleByTag mixedConfig "inbound-via-local-exit").inboundTag == [ "local-exit" ]; true)
(assert (ruleByTag mixedConfig "inbound-via-local-exit").outboundTag == "direct"; true)
# Two listeners can leave through two different servers: the pinned one
# gets its own rule, the default one rides the final rule.
(assert (ruleByTag pinnedConfig "inbound-via-through-de").outboundTag == "de-vps"; true)
(assert (ruleByTag pinnedConfig "inbound-final").outboundTag == "nl-vps"; true)
# Pinned outbounds are injected at start time, not baked into the template,
# and chaining through the local proxy is not needed for either of them.
(assert !hasOutbound pinnedConfig "proxy"; true)
(assert outboundTags pinnedConfig == [ "direct" "block" ]; true)
# Only referenced outbounds are rendered, so the sing-box-only one is fine.
(
assert
map (ob: ob.tag) pinnedFixture.config.services.proxy-suite.proxy.outbounds == [
"nl-vps"
"de-vps"
"unused"
];
true
)
# The final rule always comes last.
(assert lib.last (ruleTags relayConfig) == "inbound-final"; true)
(assert lib.last (ruleTags mixedConfig) == "inbound-final"; true)
# Without zapret running there is nothing to route around it.
(assert !builtins.elem "inbound-zapret-direct-domain" (ruleTags relayConfig); true)
# The spec carries secret paths, never their contents.
(assert builtins.length relaySpec.listeners == 1; true)
(assert (builtins.head relaySpec.listeners).tag == "vless-in"; true)
(assert (builtins.head relaySpec.listeners).listen == "::"; true)
(
assert
(builtins.head (builtins.head relaySpec.listeners).users).uuidFile == "/run/secrets/uuid";
true
)
(assert (builtins.head relaySpec.listeners).reality.privateKeyFile == "/run/secrets/reality-key"; true)
(assert relaySpec.shareLinks; true)
# Firewall: TCP for every listener, UDP only where the protocol uses it.
(
assert
lib.sort lib.lessThan firewallFixture.config.networking.firewall.allowedTCPPorts == [
443
8388
];
true
)
(assert firewallFixture.config.networking.firewall.allowedUDPPorts == [ 8388 ]; true)
# A loopback-bound listener is fronted on this host; opening its port would
# expose what the arrangement hides.
(assert wsListener.port == 10002 && wsListener.sharePort == 443; true)
(assert noFirewallFixture.config.networking.firewall.allowedTCPPorts == [ ]; true)
# The unit exists, and only waits on the client stack when it relays.
(assert (service relayFixture).wantedBy == [ "multi-user.target" ]; true)
(assert builtins.elem "proxy-suite-socks.service" (service relayFixture).after; true)
(assert !builtins.elem "proxy-suite-socks.service" (service exitFixture).after; true)
# Not `requires`: direct-routed listeners keep serving if the client is down.
(assert (service relayFixture).requires == [ ]; true)
]
++ failing;
in
{
inherit assertions;
}
+258 -22
View File
@@ -10,7 +10,23 @@
let
generated = import ./read-generated.nix;
sshOnlySingBox = evalProxySuite [
testHostKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
# The socks start script always passes proxy.tproxy.proxyMark as the routing
# mark, so an expected outbound has to carry it to match byte for byte.
markOf = fixture: fixture.config.services.proxy-suite.proxy.tproxy.proxyMark;
# writeText paths are content-addressed, so asserting the store path appears
# in the start script pins the exact generated JSON without reading it back.
# The context has to go, or the check derivation refuses to mention the path.
expectedOutboundFile =
backend: value:
builtins.unsafeDiscardStringContext "${
pkgs.writeText "proxy-suite-ob-ssh-proxy-${backend}.json" (builtins.toJSON value)
}";
# SingBox dials SSH natively: no unit, no local SOCKS listener.
sshNativeSingBox = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
@@ -20,13 +36,8 @@ let
user = "root";
host = "ssh.example.com";
asOutbound = true;
serviceUser = "proxy";
identityFile = "/run/secrets/ssh-key";
knownHostsFile = "/run/secrets/ssh-known-hosts";
extraArgs = [
"-o"
"ServerAliveInterval=30"
];
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
@@ -35,17 +46,92 @@ let
};
}
];
sshOnlySingBoxStart = generated.readDerivation (
sshOnlySingBox.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
sshNativeSingBoxStart = generated.readDerivation (
sshNativeSingBox.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
expectedNativeSingBoxOutbound = expectedOutboundFile "sing-box" {
type = "ssh";
tag = "ssh-proxy";
server = "ssh.example.com";
server_port = 22;
user = "root";
private_key_path = "/run/secrets/ssh-key";
host_key = [ testHostKey ];
routing_mark = markOf sshNativeSingBox;
};
# hostKeyFile is read at start time, so the keys must be injected by the
# start script rather than baked into the outbound JSON.
sshHostKeyFile = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
sshProxy = {
enable = true;
user = "root";
host = "ssh.example.com";
sshPort = 2222;
asOutbound = true;
hostKeyFile = "/run/secrets/ssh-known-hosts";
};
proxy = {
enable = true;
singBox.enable = true;
};
};
}
];
sshHostKeyFileStart = generated.readDerivation (
sshHostKeyFile.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
# asOutbound = false is a standalone tunnel no backend knows about, so the
# OpenSSH unit is still created even on SingBox.
sshStandalone = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
sshProxy = {
enable = true;
user = "root";
host = "ssh.example.com";
asOutbound = false;
serviceUser = "proxy";
identityFile = "/run/secrets/ssh-key";
knownHostsFile = "/run/secrets/ssh-known-hosts";
extraArgs = [
"-o"
"IPQoS=throughput"
];
};
proxy = {
enable = true;
singBox.enable = true;
outbounds = [
{
tag = "static";
url = "http://proxy.example.com:8080";
}
];
};
};
}
];
standaloneService = sshStandalone.config.systemd.services."proxy-suite-ssh-proxy";
standaloneStartScript = generated.readDerivation standaloneService.serviceConfig.ExecStart;
sshStandaloneStart = generated.readDerivation (
sshStandalone.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
);
# XRay has no SSH outbound, so it keeps the unit and proxies through its
# local SOCKS5 listener, with domainStrategy mapped onto sockopt.
sshXray = evalProxySuite [
{
system.stateVersion = "26.05";
@@ -56,6 +142,7 @@ let
user = "proxy";
host = "ssh.example.com";
asOutbound = true;
domainStrategy = "prefer_ipv4";
};
proxy = {
enable = true;
@@ -67,6 +154,20 @@ let
sshXrayStart = generated.readDerivation (
sshXray.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
xrayService = sshXray.config.systemd.services."proxy-suite-ssh-proxy";
xrayStartScript = generated.readDerivation xrayService.serviceConfig.ExecStart;
expectedXrayOutbound = expectedOutboundFile "xray" {
protocol = "socks";
tag = "ssh-proxy";
settings = {
address = "127.0.0.1";
port = 1091;
};
streamSettings.sockopt = {
mark = markOf sshXray;
domainStrategy = "UseIPv4v6";
};
};
sshHybrid = evalProxySuite [
{
@@ -78,6 +179,7 @@ let
user = "proxy";
host = "ssh.example.com";
asOutbound = true;
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
@@ -90,6 +192,15 @@ let
sshHybridStart = generated.readDerivation (
sshHybrid.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
expectedHybridOutbound = expectedOutboundFile "hybrid" {
type = "ssh";
tag = "ssh-proxy";
server = "ssh.example.com";
server_port = 22;
user = "proxy";
host_key = [ testHostKey ];
routing_mark = markOf sshHybrid;
};
sshSelector = evalProxySuite [
{
@@ -101,6 +212,7 @@ let
user = "proxy";
host = "ssh.example.com";
asOutbound = true;
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
@@ -159,6 +271,7 @@ let
user = "proxy";
host = "ssh.example.com";
asOutbound = true;
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
@@ -183,6 +296,7 @@ let
user = "proxy";
host = "ssh.example.com";
asOutbound = true;
hostKey = [ testHostKey ];
};
}
{
@@ -190,6 +304,7 @@ let
sshProxy = {
enable = true;
asOutbound = true;
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
@@ -203,6 +318,7 @@ let
user = "proxy";
host = "ssh.example.com";
asOutbound = true;
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
@@ -215,45 +331,127 @@ let
];
};
}
# SingBox verifies host keys by value, so an empty hostKey would silently
# accept any key.
{
enable = true;
sshProxy = {
enable = true;
user = "proxy";
host = "ssh.example.com";
asOutbound = true;
};
proxy = {
enable = true;
singBox.enable = true;
};
}
];
assertions = [
(
assert sshOnlySingBox.config.services.proxy-suite.proxy.outbounds == [ ];
assert sshNativeSingBox.config.services.proxy-suite.proxy.outbounds == [ ];
true
)
# No OpenSSH unit and no ordering against one: sing-box dials SSH in-process.
(
assert sshOnlySingBox.config.systemd.services ? "proxy-suite-ssh-proxy";
assert !(sshNativeSingBox.config.systemd.services ? "proxy-suite-ssh-proxy");
true
)
(
assert
sshOnlySingBox.config.systemd.services."proxy-suite-socks".after == [
sshNativeSingBox.config.systemd.services."proxy-suite-socks".after == [
"network-online.target"
"proxy-suite-ssh-proxy.service"
];
true
)
(
assert pkgs.lib.hasInfix "ssh-proxy" sshOnlySingBoxStart;
assert
sshNativeSingBox.config.systemd.services."proxy-suite-socks".wants == [
"network-online.target"
];
true
)
(
assert pkgs.lib.hasInfix "proxy-suite-ob-ssh-proxy-sing-box.json" sshOnlySingBoxStart;
assert pkgs.lib.hasInfix expectedNativeSingBoxOutbound sshNativeSingBoxStart;
true
)
# The global local-resolve route rule is gone; the native outbound resolves.
(
assert !(pkgs.lib.hasInfix "\"resolve\"" sshNativeSingBoxStart);
true
)
# Non-default port has to be looked up as "[host]:port" in known_hosts.
(
assert pkgs.lib.hasInfix "ssh-keygen -F '[ssh.example.com]:2222'" sshHostKeyFileStart;
true
)
(
assert pkgs.lib.hasInfix "ServerAliveInterval=30" sshStartScript;
assert pkgs.lib.hasInfix "'.host_key = \$hk'" sshHostKeyFileStart;
true
)
(
assert sshService.serviceConfig.User == "proxy";
assert pkgs.lib.hasInfix "/run/secrets/ssh-known-hosts" sshHostKeyFileStart;
true
)
# The keys must not be baked into the store-resident outbound JSON.
(
assert !(pkgs.lib.hasInfix "host_key" (builtins.toJSON sshHostKeyFile.config.services.proxy-suite.sshProxy.hostKey));
true
)
# Standalone tunnel: unit is created, but nothing is wired as an outbound.
(
assert sshStandalone.config.systemd.services ? "proxy-suite-ssh-proxy";
true
)
(
assert !(sshService.serviceConfig ? SocketMark);
assert !(pkgs.lib.hasInfix "ssh-proxy" sshStandaloneStart);
true
)
(
assert standaloneService.serviceConfig.User == "proxy";
true
)
(
assert !(standaloneService.serviceConfig ? SocketMark);
true
)
# Hardening: a half-open tunnel must be detected and always restarted.
(
assert standaloneService.serviceConfig.Restart == "always";
true
)
(
assert standaloneService.serviceConfig.LimitNOFILE == 65536;
true
)
(
assert standaloneService.startLimitIntervalSec == 0;
true
)
(
assert pkgs.lib.hasInfix "ServerAliveInterval=15" standaloneStartScript;
true
)
(
assert pkgs.lib.hasInfix "ServerAliveCountMax=3" standaloneStartScript;
true
)
(
assert pkgs.lib.hasInfix "ConnectTimeout=10" standaloneStartScript;
true
)
(
assert pkgs.lib.hasInfix "IPQoS=throughput" standaloneStartScript;
true
)
(
assert pkgs.lib.hasInfix "UserKnownHostsFile=/run/secrets/ssh-known-hosts" standaloneStartScript;
true
)
(
assert !(unchanged.config.systemd.services ? "proxy-suite-ssh-proxy");
true
@@ -262,18 +460,56 @@ let
assert !(pkgs.lib.hasInfix "ssh-proxy" unchangedStart);
true
)
# XRay keeps the unit and orders the backend behind it.
(
assert sshXray.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart != null;
assert sshXray.config.systemd.services ? "proxy-suite-ssh-proxy";
true
)
(
assert pkgs.lib.hasInfix "proxy-suite-ob-ssh-proxy-xray.json" sshXrayStart;
assert
sshXray.config.systemd.services."proxy-suite-socks".after == [
"network-online.target"
"proxy-suite-ssh-proxy.service"
];
true
)
(
assert
sshXray.config.systemd.services."proxy-suite-socks".wants == [
"network-online.target"
"proxy-suite-ssh-proxy.service"
];
true
)
(
assert xrayService.serviceConfig.Restart == "always";
true
)
(
assert pkgs.lib.hasInfix "ServerAliveInterval=15" xrayStartScript;
true
)
# prefer_ipv4 maps onto XRay's "IPv4 first, fall back to IPv6".
(
assert pkgs.lib.hasInfix expectedXrayOutbound sshXrayStart;
true
)
# Hybrid routes the tunnel through sing-box, so it gets the native shape.
(
assert !(sshHybrid.config.systemd.services ? "proxy-suite-ssh-proxy");
true
)
(
assert pkgs.lib.hasInfix "_proxy_suite_add_sing_box_ob" sshHybridStart;
true
)
(
assert pkgs.lib.hasInfix expectedHybridOutbound sshHybridStart;
true
)
(
assert pkgs.lib.hasInfix "selector" sshSelectorStart;
true
@@ -15,6 +15,7 @@ let
pureXrayEnabled = true;
singBoxCfg = null;
proxyCfg = null;
sshProxyCfg = null;
hybridEnabled = null;
collapseNamedOutbounds = null;
selectionMode = null;