feat: improve warp, outbounds reputation
This commit is contained in:
18 files changed
+533
-224
No files matched your search
@@ -1,4 +1,5 @@
|
||||
# Cloudflare WARP: the AmneziaWG profile and, without a configFile, wgcf registration.
|
||||
# Cloudflare WARP: the tunnel behind the "warp" outbound, the AmneziaWG profile and, without a
|
||||
# configFile, wgcf registration.
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
@@ -54,44 +55,110 @@ let
|
||||
userinfo=
|
||||
${userinfo}
|
||||
HTTPS_PROXY="socks5://''${userinfo}${hostPart}:${toString listener.port}" register || register
|
||||
${lib.optionalString w.asOutbound "${pkgs.systemd}/bin/systemctl --no-block try-restart proxy-suite-socks.service"}
|
||||
''
|
||||
else
|
||||
"register"
|
||||
}
|
||||
'';
|
||||
|
||||
# sing-box's "local" server has no upstream on hosts behind systemd-resolved, so the tunnel
|
||||
# resolves like the main config: the peer hostname and destinations go to proxy.dns.local.
|
||||
dnsServer = {
|
||||
tag = "local";
|
||||
type = cfg.proxy.dns.local.type;
|
||||
server = cfg.proxy.dns.local.address;
|
||||
server_port = cfg.proxy.dns.local.port;
|
||||
};
|
||||
|
||||
# Some lines drop a share of fresh WARP handshakes for good, and sing-box retries on the
|
||||
# same source port forever. A new process binds a new port, so the probe exits after
|
||||
# repeated failures and systemd starts the tunnel again.
|
||||
tunnelScript = pkgs.writeShellScript "proxy-suite-warp-tunnel" ''
|
||||
set -euo pipefail
|
||||
profile=${lib.escapeShellArg w.profilePath}
|
||||
if [ ! -s "$profile" ]; then
|
||||
echo "proxy-suite: waiting for the WARP profile at $profile" >&2
|
||||
until [ -s "$profile" ]; do sleep 5; done
|
||||
fi
|
||||
|
||||
# The profile is a secret, so it is converted here rather than baked into the store.
|
||||
endpoint=$(${pkgs.python3}/bin/python3 ${../../scripts/warp_outbound.py} --tag warp --routing-mark ${toString cfg.proxy.tproxy.proxyMark} < "$profile")
|
||||
${pkgs.jq}/bin/jq -n --argjson ep "$endpoint" '{
|
||||
log: {level: "warn"},
|
||||
dns: {servers: [${builtins.toJSON dnsServer}]},
|
||||
route: {default_domain_resolver: "local", final: "warp"},
|
||||
inbounds: [{type: "socks", tag: "socks-in", listen: "127.0.0.1", listen_port: ${toString w.tunnelPort}}],
|
||||
endpoints: [$ep]
|
||||
}' > "$RUNTIME_DIRECTORY/config.json"
|
||||
|
||||
${cfg.proxy.singBox.package}/bin/sing-box run -c "$RUNTIME_DIRECTORY/config.json" &
|
||||
singbox=$!
|
||||
|
||||
failures=0
|
||||
while sleep 10; do
|
||||
kill -0 "$singbox" 2>/dev/null || { wait "$singbox"; exit 1; }
|
||||
if ${pkgs.curl}/bin/curl -sf --noproxy "" -x socks5h://127.0.0.1:${toString w.tunnelPort} -m 10 -o /dev/null ${lib.escapeShellArg cfg.proxy.urlTest.url}; then
|
||||
failures=0
|
||||
elif (( ++failures >= 3 )); then
|
||||
echo "proxy-suite: WARP stopped answering; restarting the tunnel on a new source port" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
'';
|
||||
in
|
||||
{
|
||||
services.proxy-suite.amneziaWg.profiles = lib.mkIf w.asAmneziaWg {
|
||||
warp.configFile = w.profilePath;
|
||||
};
|
||||
|
||||
systemd.services = lib.mkIf w.autoRegister {
|
||||
proxy-suite-warp = {
|
||||
description = "proxy-suite - register a Cloudflare WARP device with wgcf";
|
||||
after = [ "network-online.target" ] ++ lib.optional cfg.proxy.enable "proxy-suite-socks.service";
|
||||
wants = [ "network-online.target" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
path = [ pkgs.wgcf ];
|
||||
# Retried until it registers. Not a oneshot: a slow or failed registration must not
|
||||
# hold up or fail a switch.
|
||||
startLimitIntervalSec = 0;
|
||||
serviceConfig = {
|
||||
Type = "simple";
|
||||
RemainAfterExit = true;
|
||||
Restart = "on-failure";
|
||||
RestartSec = 30;
|
||||
StateDirectory = "proxy-suite/warp";
|
||||
StateDirectoryMode = "0700";
|
||||
WorkingDirectory = "/var/lib/proxy-suite/warp";
|
||||
UMask = "0077";
|
||||
ExecStart = registerScript;
|
||||
systemd.services = lib.mkMerge [
|
||||
(lib.mkIf w.asOutbound {
|
||||
proxy-suite-warp-tunnel = {
|
||||
description = "proxy-suite - Cloudflare WARP tunnel behind the warp outbound";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
# Probe-triggered restarts must not trip the start limit.
|
||||
startLimitIntervalSec = 0;
|
||||
serviceConfig = {
|
||||
Type = "simple";
|
||||
ExecStart = tunnelScript;
|
||||
Restart = "always";
|
||||
RestartSec = 2;
|
||||
RuntimeDirectory = "proxy-suite-warp-tunnel";
|
||||
RuntimeDirectoryMode = "0700";
|
||||
UMask = "0077";
|
||||
};
|
||||
};
|
||||
};
|
||||
})
|
||||
|
||||
# Only pulls registration in: a simple unit gives the profile no ordering guarantee.
|
||||
proxy-suite-awg-warp = lib.mkIf w.asAmneziaWg {
|
||||
wants = [ "proxy-suite-warp.service" ];
|
||||
};
|
||||
};
|
||||
(lib.mkIf w.autoRegister {
|
||||
proxy-suite-warp = {
|
||||
description = "proxy-suite - register a Cloudflare WARP device with wgcf";
|
||||
after = [ "network-online.target" ] ++ lib.optional cfg.proxy.enable "proxy-suite-socks.service";
|
||||
wants = [ "network-online.target" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
path = [ pkgs.wgcf ];
|
||||
# Retried until it registers. Not a oneshot: a slow or failed registration must not
|
||||
# hold up or fail a switch.
|
||||
startLimitIntervalSec = 0;
|
||||
serviceConfig = {
|
||||
Type = "simple";
|
||||
RemainAfterExit = true;
|
||||
Restart = "on-failure";
|
||||
RestartSec = 30;
|
||||
StateDirectory = "proxy-suite/warp";
|
||||
StateDirectoryMode = "0700";
|
||||
WorkingDirectory = "/var/lib/proxy-suite/warp";
|
||||
UMask = "0077";
|
||||
ExecStart = registerScript;
|
||||
};
|
||||
};
|
||||
|
||||
# Only pulls registration in: a simple unit gives the profile no ordering guarantee.
|
||||
proxy-suite-awg-warp = lib.mkIf w.asAmneziaWg {
|
||||
wants = [ "proxy-suite-warp.service" ];
|
||||
};
|
||||
})
|
||||
];
|
||||
}
|
||||
Reference in new issue
Block a user