Files
proxy-suite-flake/nix/checks/proxy-inbounds-runtime.nix

241 lines
11 KiB
Nix

{
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";
# Base64 of exactly 16 bytes, the 2022-blake3-aes-128-gcm key length (XRay 26.9.30 refuses others).
ssPassword = "dGhpcy1pcy0xNi1ieXRlcw==";
# Public addresses: blockPrivate would refuse the origin on a private one.
serverAddress = "11.0.0.1";
# XRay refuses plain VLESS to a public IP, so the client dials a private one.
serverPrivateAddress = "10.0.0.1";
# The inbounds' own XRay, for its API.
xrayApi = "${
import ../../pkgs/xray.nix { inherit pkgs; }
}/bin/xray api inbounduser --server=unix:///run/proxy-suite-inbounds/api/stats.sock -tag=vless-in";
network = public: private: {
networking.useDHCP = false;
networking.interfaces.eth1.ipv4.addresses = [
{
address = public;
prefixLength = 24;
}
{
address = private;
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 serverPrivateAddress)
];
services.proxy-suite = {
enable = true;
proxy.enable = false;
inbounds = {
enable = true;
inherit serverAddress;
# The origin is this host's own web server: through the tunnel only as a listed port.
serverPorts = [ 80 ];
routing.via = "direct";
openFirewall = true;
users = {
tester.uuidFile = "/run/inbound-secrets/uuid";
ss-user.passwordFile = "/run/inbound-secrets/password";
};
listeners.vless-in = {
type = "vless";
port = 8443;
users = [ "tester" ];
};
listeners.ss-in = {
type = "shadowsocks";
port = 8388;
method = "2022-blake3-aes-128-gcm";
users = [ "ss-user" ];
};
# Users and listeners proxy-ctl adds below.
runtime = {
enable = true;
ports = [ "9000-9010" ];
};
};
};
services.nginx = {
enable = true;
virtualHosts."origin".locations."/".return = "200 'served-from-origin'";
# On every address, kept from the internet by the firewall alone.
virtualHosts."internal" = {
listen = [
{
addr = "0.0.0.0";
port = 8080;
}
];
locations."/".return = "200 'internal-only'";
};
};
networking.firewall.allowedTCPPorts = [ 80 ];
# Written at boot, so the listener's spec carries only the paths.
systemd.tmpfiles.rules = [
"d /run/inbound-secrets 0700 root root -"
"f+ /run/inbound-secrets/uuid 0400 root root - ${uuid}"
"f+ /run/inbound-secrets/password 0400 root root - ${ssPassword}"
];
# The test script inspects the rendered config with jq.
environment.systemPackages = [ pkgs.jq ];
};
client =
{ ... }:
{
imports = [
proxySuiteModule
(network "11.0.0.2" "10.0.0.2")
];
services.proxy-suite = {
enable = true;
proxy = {
enable = true;
backend = "xray";
listener.port = 1080;
outbounds = [
{
tag = "server";
url = "vless://${uuid}@${serverPrivateAddress}: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"
# Plus api-in, the stats API's socket.
server.succeed(f"jq -e '[.inbounds[].tag] | sort == [\"api-in\", \"ss-in\", \"vless-in\"]' {cfg}")
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}) = 640")
server.fail("grep -r '${uuid}' /nix/store/*-proxy-suite-inbounds")
with subtest("safety rules are present and ordered first"):
cfg = "/run/proxy-suite-inbounds/config.json"
server.succeed(f"test $(jq -r '.routing.rules[1].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"
)
# Nor into a port of its own the firewall closes: "direct" would dial it over lo. The
# fence carries the addresses its interfaces had at start, the private one too.
cfg = "/run/proxy-suite-inbounds/config.json"
server.succeed(f"jq -e '[.outbounds[] | select(.tag == \"direct\") | .settings.finalRules[] | select(.ip and (.ip | index(\"${serverPrivateAddress}\")))] | length == 2' {cfg}")
server.succeed(f"jq -e '[.. | objects | select(has(\"_hostAddresses\"))] == []' {cfg}")
server.succeed("curl -sS --fail --max-time 5 http://${serverAddress}:8080/ | grep -q internal-only")
client.fail("curl -sS --fail --max-time 5 http://${serverAddress}:8080/")
client.fail(
"curl -sS --fail --max-time 15 --proxy socks5h://127.0.0.1:1080"
" http://${serverAddress}:8080/ 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 link vless-in --qr | 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)
with subtest("runtime users and listeners"):
cfg = "/run/proxy-suite-inbounds/config.json"
# A user comes through XRay's API: the same XRay, config.json kept in step with it.
pid = server.succeed("systemctl show -p MainPID --value proxy-suite-inbounds.service").strip()
server.succeed("proxy-ctl inbounds users add friend --listener vless-in")
server.wait_until_succeeds(f"jq -e '.inbounds[] | select(.tag == \"vless-in\") | .settings.clients | map(.email) == [\"tester\", \"friend\"]' {cfg}")
server.succeed("${xrayApi} | grep -q friend")
assert server.succeed("systemctl show -p MainPID --value proxy-suite-inbounds.service").strip() == pid
server.succeed("proxy-ctl inbounds add rt vless --port 9001 --user friend")
server.wait_for_open_port(9001)
server.succeed("proxy-ctl inbounds users | grep friend | grep -q rt")
# Refused, and nothing else disturbed. A declared user's uuid stays off a plain listener.
server.fail("proxy-ctl inbounds add far vless --port 30000 --user friend")
server.fail("proxy-ctl inbounds add clear vless --port 9002 --user tester")
server.fail("proxy-ctl inbounds bind tester rt")
server.fail("proxy-ctl inbounds bind tester vless-in")
server.succeed("test ! -e /var/lib/proxy-suite/inbounds.d/listeners/far.json")
server.succeed("test ! -e /var/lib/proxy-suite/inbounds.d/listeners/clear.json")
server.succeed("jq -e '.users | index(\"tester\") == null' /var/lib/proxy-suite/inbounds.d/listeners/rt.json")
# The runtime user's link works from the client, as a runtime outbound there. XRay
# refuses plain VLESS to a public IP, so it dials the private one.
link = server.succeed("proxy-ctl inbounds link rt friend").strip()
assert link.startswith("vless://") and ":9001" in link, link
link = link.replace("${serverAddress}", "${serverPrivateAddress}")
client.succeed(f"proxy-ctl proxy outbounds add rt-out '{link}'")
client.succeed("proxy-ctl proxy pin rt-out")
client.wait_until_succeeds(
"curl -sS --fail --max-time 20 --proxy socks5h://127.0.0.1:1080"
f" http://{'${serverAddress}'}/ | grep -q served-from-origin",
timeout=60,
)
# A spool file that no longer holds up is left out; the declared listeners stay.
server.succeed("echo '{' > /var/lib/proxy-suite/inbounds.d/listeners/rt.json")
server.succeed("systemctl start proxy-suite-inbounds-reload.service")
server.wait_for_open_port(8443)
server.succeed(f"jq -e '[.inbounds[].tag] | index(\"rt\") == null' {cfg}")
server.succeed("journalctl -u proxy-suite-inbounds | grep -q \"ignoring runtime listener 'rt'\"")
server.succeed("rm /var/lib/proxy-suite/inbounds.d/listeners/rt.json")
pid = server.succeed("systemctl show -p MainPID --value proxy-suite-inbounds.service").strip()
server.succeed("proxy-ctl inbounds users rm friend")
server.wait_until_succeeds(f"jq -e '.inbounds[] | select(.tag == \"vless-in\") | .settings.clients | map(.email) == [\"tester\"]' {cfg}")
server.fail("${xrayApi} | grep -q friend")
assert server.succeed("systemctl show -p MainPID --value proxy-suite-inbounds.service").strip() == pid
'';
}