refactor: remove duplication

This commit is contained in:
FUFSoB committed 2026-09-18 21:39:18 +05:00
1 parent f7036f6075
commit 8a4ba2db6a
109 files changed
+2206 -3078

No files matched your search

+101 -227
View File
@@ -1,4 +1,5 @@
{
checkLib,
pkgs,
evalProxySuite,
baseModule,
@@ -8,6 +9,7 @@
}:
let
inherit (checkLib) ok mkProxySuite startScript;
generated = import ./read-generated.nix;
testHostKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
@@ -22,29 +24,22 @@ let
builtins.unsafeDiscardStringContext "${pkgs.writeText "proxy-suite-core" (builtins.toJSON value)}";
# SingBox dials SSH natively: no unit, no local SOCKS listener.
sshNativeSingBox = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
sshProxy = {
enable = true;
server.user = "root";
server.host = "ssh.example.com";
asOutbound = true;
identityFile = "/run/secrets/ssh-key";
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
backend = "sing-box";
};
};
}
];
sshNativeSingBoxStart = generated.readDerivation (
sshNativeSingBox.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
sshNativeSingBox = mkProxySuite {
enable = true;
sshProxy = {
enable = true;
server.user = "root";
server.host = "ssh.example.com";
asOutbound = true;
identityFile = "/run/secrets/ssh-key";
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
backend = "sing-box";
};
};
sshNativeSingBoxStart = startScript sshNativeSingBox;
expectedNativeSingBoxOutbound = expectedOutboundFile {
type = "ssh";
tag = "ssh-proxy";
@@ -56,29 +51,22 @@ let
};
# hostKeyFile keys are injected at start, not baked into the JSON.
sshHostKeyFile = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
sshProxy = {
enable = true;
server.user = "root";
server.host = "ssh.example.com";
server.port = 2222;
asOutbound = true;
hostKeyFile = "/run/secrets/ssh-known-hosts";
};
proxy = {
enable = true;
backend = "sing-box";
};
};
}
];
sshHostKeyFileStart = generated.readDerivation (
sshHostKeyFile.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
sshHostKeyFile = mkProxySuite {
enable = true;
sshProxy = {
enable = true;
server.user = "root";
server.host = "ssh.example.com";
server.port = 2222;
asOutbound = true;
hostKeyFile = "/run/secrets/ssh-known-hosts";
};
proxy = {
enable = true;
backend = "sing-box";
};
};
sshHostKeyFileStart = startScript sshHostKeyFile;
# asOutbound = false: a standalone tunnel, so the OpenSSH unit exists even on SingBox.
sshStandalone = evalProxySuite [
@@ -114,38 +102,27 @@ let
];
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
);
sshStandaloneStart = startScript sshStandalone;
unchanged = evalProxySuite [ baseModule ];
unchangedStart = generated.readDerivation (
unchanged.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
unchangedStart = startScript unchanged;
# XRay keeps the unit and proxies through its listener, domainStrategy on sockopt.
sshXray = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
sshProxy = {
enable = true;
server.user = "proxy";
server.host = "ssh.example.com";
asOutbound = true;
domainStrategy = "prefer_ipv4";
};
proxy = {
enable = true;
backend = "xray";
};
};
}
];
sshXrayStart = generated.readDerivation (
sshXray.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
sshXray = mkProxySuite {
enable = true;
sshProxy = {
enable = true;
server.user = "proxy";
server.host = "ssh.example.com";
asOutbound = true;
domainStrategy = "prefer_ipv4";
};
proxy = {
enable = true;
backend = "xray";
};
};
sshXrayStart = startScript sshXray;
xrayService = sshXray.config.systemd.services."proxy-suite-ssh-proxy";
xrayStartScript = generated.readDerivation xrayService.serviceConfig.ExecStart;
expectedXrayOutbound = expectedOutboundFile {
@@ -161,28 +138,21 @@ let
};
};
sshHybrid = evalProxySuite [
{
system.stateVersion = "26.05";
services.proxy-suite = {
enable = true;
sshProxy = {
enable = true;
server.user = "proxy";
server.host = "ssh.example.com";
asOutbound = true;
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
backend = "hybrid";
};
};
}
];
sshHybridStart = generated.readDerivation (
sshHybrid.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
sshHybrid = mkProxySuite {
enable = true;
sshProxy = {
enable = true;
server.user = "proxy";
server.host = "ssh.example.com";
asOutbound = true;
hostKey = [ testHostKey ];
};
proxy = {
enable = true;
backend = "hybrid";
};
};
sshHybridStart = startScript sshHybrid;
expectedHybridOutbound = expectedOutboundFile {
type = "ssh";
tag = "ssh-proxy";
@@ -219,9 +189,7 @@ let
};
}
];
sshSelectorStart = generated.readDerivation (
sshSelector.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
sshSelectorStart = startScript sshSelector;
sshUrltest = evalProxySuite [
{
@@ -248,9 +216,7 @@ let
};
}
];
sshUrltestStart = generated.readDerivation (
sshUrltest.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart
);
sshUrltestStart = startScript sshUrltest;
sshRouting = evalProxySuite [
{
@@ -339,15 +305,9 @@ let
];
assertions = [
(
assert sshNativeSingBox.config.services.proxy-suite.proxy.outbounds == [ ];
true
)
(ok (sshNativeSingBox.config.services.proxy-suite.proxy.outbounds == [ ]))
# No OpenSSH unit and no ordering against one: sing-box dials SSH in-process.
(
assert !(sshNativeSingBox.config.systemd.services ? "proxy-suite-ssh-proxy");
true
)
(ok (!(sshNativeSingBox.config.systemd.services ? "proxy-suite-ssh-proxy")))
(
assert
sshNativeSingBox.config.systemd.services."proxy-suite-socks".after == [
@@ -372,24 +332,12 @@ let
true
)
# The global local-resolve route rule is gone; the native outbound resolves.
(
assert !(pkgs.lib.hasInfix "\"resolve\"" sshNativeSingBoxStart);
true
)
(ok (!(pkgs.lib.hasInfix "\"resolve\"" sshNativeSingBoxStart)))
# 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 "'.host_key = \$hk'" sshHostKeyFileStart;
true
)
(
assert pkgs.lib.hasInfix "/run/secrets/ssh-known-hosts" sshHostKeyFileStart;
true
)
(ok (pkgs.lib.hasInfix "ssh-keygen -F '[ssh.example.com]:2222'" sshHostKeyFileStart))
(ok (pkgs.lib.hasInfix "'.host_key = \$hk'" sshHostKeyFileStart))
(ok (pkgs.lib.hasInfix "/run/secrets/ssh-known-hosts" sshHostKeyFileStart))
# The keys must not be baked into the store-resident outbound JSON.
(
assert
@@ -400,56 +348,21 @@ let
)
# Standalone tunnel: unit is created, but nothing is wired as an outbound.
(
assert sshStandalone.config.systemd.services ? "proxy-suite-ssh-proxy";
true
)
(
assert !(pkgs.lib.hasInfix "ssh-proxy" sshStandaloneStart);
true
)
(
assert standaloneService.serviceConfig.User == "proxy";
true
)
(
assert !(standaloneService.serviceConfig ? SocketMark);
true
)
(ok (sshStandalone.config.systemd.services ? "proxy-suite-ssh-proxy"))
(ok (!(pkgs.lib.hasInfix "ssh-proxy" sshStandaloneStart)))
(ok (standaloneService.serviceConfig.User == "proxy"))
(ok (!(standaloneService.serviceConfig ? SocketMark)))
# 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="$CREDENTIALS_DIRECTORY/known_hosts"''
standaloneStartScript;
true
)
(ok (standaloneService.serviceConfig.Restart == "always"))
(ok (standaloneService.serviceConfig.LimitNOFILE == 65536))
(ok (standaloneService.startLimitIntervalSec == 0))
(ok (pkgs.lib.hasInfix "ServerAliveInterval=15" standaloneStartScript))
(ok (pkgs.lib.hasInfix "ServerAliveCountMax=3" standaloneStartScript))
(ok (pkgs.lib.hasInfix "ConnectTimeout=10" standaloneStartScript))
(ok (pkgs.lib.hasInfix "IPQoS=throughput" standaloneStartScript))
(ok (
pkgs.lib.hasInfix ''UserKnownHostsFile="$CREDENTIALS_DIRECTORY/known_hosts"'' standaloneStartScript
))
# Secrets reach the unprivileged user as credentials.
(
assert
@@ -460,20 +373,11 @@ let
true
)
(
assert !(unchanged.config.systemd.services ? "proxy-suite-ssh-proxy");
true
)
(
assert !(pkgs.lib.hasInfix "ssh-proxy" unchangedStart);
true
)
(ok (!(unchanged.config.systemd.services ? "proxy-suite-ssh-proxy")))
(ok (!(pkgs.lib.hasInfix "ssh-proxy" unchangedStart)))
# XRay keeps the unit and orders the backend behind it.
(
assert sshXray.config.systemd.services ? "proxy-suite-ssh-proxy";
true
)
(ok (sshXray.config.systemd.services ? "proxy-suite-ssh-proxy"))
(
assert
sshXray.config.systemd.services."proxy-suite-socks".after == [
@@ -490,50 +394,20 @@ let
];
true
)
(
assert xrayService.serviceConfig.Restart == "always";
true
)
(
assert pkgs.lib.hasInfix "ServerAliveInterval=15" xrayStartScript;
true
)
(ok (xrayService.serviceConfig.Restart == "always"))
(ok (pkgs.lib.hasInfix "ServerAliveInterval=15" xrayStartScript))
# prefer_ipv4 maps onto XRay's "IPv4 first, fall back to IPv6".
(
assert pkgs.lib.hasInfix expectedXrayOutbound sshXrayStart;
true
)
(ok (pkgs.lib.hasInfix expectedXrayOutbound sshXrayStart))
# 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
)
(ok (!(sshHybrid.config.systemd.services ? "proxy-suite-ssh-proxy")))
(ok (pkgs.lib.hasInfix "_proxy_suite_add_sing_box_ob" sshHybridStart))
(ok (pkgs.lib.hasInfix expectedHybridOutbound sshHybridStart))
(
assert pkgs.lib.hasInfix "selector" sshSelectorStart;
true
)
(
assert pkgs.lib.hasInfix "ssh-proxy" sshSelectorStart;
true
)
(
assert pkgs.lib.hasInfix "# outbound: ssh-proxy (OpenSSH SOCKS5 listener)" sshUrltestStart;
true
)
(
assert builtins.any (rule: (rule.outbound or null) == "ssh-proxy") (mkRoutingRules sshRouting);
true
)
(ok (pkgs.lib.hasInfix "selector" sshSelectorStart))
(ok (pkgs.lib.hasInfix "ssh-proxy" sshSelectorStart))
(ok (pkgs.lib.hasInfix "# outbound: ssh-proxy (OpenSSH SOCKS5 listener)" sshUrltestStart))
(ok (builtins.any (rule: (rule.outbound or null) == "ssh-proxy") (mkRoutingRules sshRouting)))
]
++ invalidAssertions;
in