feat: tproxy lan gateway, killswitch; fix: zapret2 fixes, various other
This commit is contained in:
54 files changed
+1880
-143
No files matched your search
@@ -14,6 +14,7 @@
|
||||
perAppTproxyRulesFile,
|
||||
perAppZapretRulesFile,
|
||||
nftablesRulesFile,
|
||||
killSwitchRulesFile,
|
||||
ip,
|
||||
nft,
|
||||
}:
|
||||
@@ -96,6 +97,7 @@ let
|
||||
perAppTproxyRulesFile
|
||||
perAppZapretRulesFile
|
||||
nftablesRulesFile
|
||||
killSwitchRulesFile
|
||||
ip
|
||||
nft
|
||||
;
|
||||
|
||||
@@ -21,6 +21,8 @@ let
|
||||
proxyInboundsCfg
|
||||
proxyInboundsEnabled
|
||||
amneziaWgProfileNamesFile
|
||||
ruleSets
|
||||
pkgs
|
||||
;
|
||||
inherit (ctx.scripts)
|
||||
subscriptionTagsFile
|
||||
@@ -54,6 +56,11 @@ in
|
||||
SELECTION = selectionMode;
|
||||
SUB_TAGS_FILE = toString subscriptionTagsFile;
|
||||
SUB_CACHE_DIR = subscriptionCacheDir;
|
||||
RULE_SETS_FILE = toString (
|
||||
pkgs.writeText "proxy-suite-control" (
|
||||
builtins.toJSON (map (rs: { inherit (rs) name path; }) ruleSets)
|
||||
)
|
||||
);
|
||||
PER_APP_ROUTING_ENABLED = flag perAppRoutingCfg.enable;
|
||||
PER_APP_ROUTING_PROXYCHAINS_ENABLED = flag perAppRoutingCfg.proxychains.enable;
|
||||
PER_APP_ROUTING_TUN_ENABLED = flag perAppRoutingTun.enable;
|
||||
@@ -69,7 +76,7 @@ in
|
||||
INBOUNDS_LINKS_FILE = proxyInboundsLinksFile;
|
||||
INBOUNDS_STATS_FILE = constants.inboundStatsFile;
|
||||
INBOUNDS_XRAY = proxyInboundsXray;
|
||||
INBOUNDS_API = "127.0.0.1:${toString constants.inboundStatsApiPort}";
|
||||
INBOUNDS_API = "unix://${constants.inboundStatsApiSocket}";
|
||||
INBOUNDS_SUBS_FILE = proxyInboundsSubscriptionsFile;
|
||||
INBOUNDS_SUB_BASE_URL =
|
||||
if proxyInboundsSubscriptionsBaseUrl == null then "" else proxyInboundsSubscriptionsBaseUrl;
|
||||
|
||||
@@ -26,6 +26,7 @@ let
|
||||
pureXrayEnabled
|
||||
globalTun
|
||||
globalTproxy
|
||||
tproxyLanSysctl
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
userControlCfg
|
||||
@@ -98,10 +99,26 @@ lib.mkMerge [
|
||||
) true;
|
||||
|
||||
# Inbound listeners are reached from outside, so their ports have to be open.
|
||||
firewall = lib.mkIf (proxyInboundsEnabled && cfg.inbounds.openFirewall) {
|
||||
allowedTCPPorts = proxyInboundFirewallPorts;
|
||||
allowedUDPPorts = proxyInboundFirewallUdpPorts;
|
||||
};
|
||||
firewall = lib.mkMerge [
|
||||
(lib.mkIf (proxyInboundsEnabled && cfg.inbounds.openFirewall) {
|
||||
allowedTCPPorts = proxyInboundFirewallPorts;
|
||||
allowedUDPPorts = proxyInboundFirewallUdpPorts;
|
||||
})
|
||||
# Gateway clients' diverted packets reach input with their original destination, and
|
||||
# carry a mark whose table has no route back. Only those: the LAN is not trusted.
|
||||
(lib.mkIf (tproxyLanSysctl != { }) (
|
||||
let
|
||||
rules = lib.concatMapStrings (interface: ''
|
||||
iifname "${interface}" meta mark ${toString globalTproxy.fwmark} accept
|
||||
'') globalTproxy.lanInterfaces;
|
||||
in
|
||||
{
|
||||
extraInputRules = rules;
|
||||
extraReversePathFilterRules = rules;
|
||||
}
|
||||
))
|
||||
];
|
||||
sysctl = tproxyLanSysctl;
|
||||
|
||||
groups = lib.mkIf (cfg.enable && (userControlEnabled || localProxyAuthEnabled)) [
|
||||
userControlCfg.group
|
||||
|
||||
@@ -173,7 +173,7 @@ let
|
||||
--argjson auth_enabled ${if needsLocalProxyAuth then "true" else "false"} \
|
||||
--arg user ${if needsLocalProxyAuth then lib.escapeShellArg localProxyAuth.username else "''"} \
|
||||
--arg password ${if needsLocalProxyAuth then "\"$LOCAL_PROXY_PASSWORD\"" else "''"} \
|
||||
'.inbounds = $ibs
|
||||
'.inbounds += $ibs
|
||||
| .outbounds = $obs + .outbounds
|
||||
| if $auth_enabled then
|
||||
(.outbounds[] | select(.tag == "proxy") | .settings.servers[0].users)
|
||||
@@ -220,6 +220,20 @@ let
|
||||
}
|
||||
mv "$RUNTIME_DIR/config.json.tmp" "$RUNTIME_DIR/config.json"
|
||||
|
||||
# The stats API's socket takes this setgid directory's group: XRay cannot chown it.
|
||||
API_DIR=$(dirname ${lib.escapeShellArg constants.inboundStatsApiSocket})
|
||||
rm -rf "$API_DIR"
|
||||
${
|
||||
if !constants.privileged then
|
||||
''mkdir -m 0700 "$API_DIR"''
|
||||
else if userControlAllows "stats" then
|
||||
''
|
||||
install -d -m 2750 -o ${constants.serviceUser} -g ${lib.escapeShellArg userControlCfg.group} "$API_DIR" ||
|
||||
install -d -m 2750 -o ${constants.serviceUser} -g ${constants.serviceUser} "$API_DIR"''
|
||||
else
|
||||
''install -d -m 2750 -o ${constants.serviceUser} -g ${constants.serviceUser} "$API_DIR"''
|
||||
}
|
||||
|
||||
${writeLinksBlock}
|
||||
${writeSubscriptionsBlock}
|
||||
|
||||
@@ -231,7 +245,7 @@ let
|
||||
collectInboundStats = pkgs.writeShellScript "proxy-suite-inbounds" ''
|
||||
set -euo pipefail
|
||||
file=${lib.escapeShellArg constants.inboundStatsFile}
|
||||
api=--server=127.0.0.1:${toString constants.inboundStatsApiPort}
|
||||
api=--server=unix://${constants.inboundStatsApiSocket}
|
||||
# A timer run and a stop can overlap: each reads and resets part, and both rewrite the file.
|
||||
exec 9> "$file.lock"
|
||||
${pkgs.util-linux}/bin/flock 9
|
||||
|
||||
@@ -8,9 +8,11 @@ let
|
||||
ip
|
||||
nft
|
||||
nftablesRulesFile
|
||||
killSwitchRulesFile
|
||||
constants
|
||||
globalTun
|
||||
globalTproxy
|
||||
tproxyLanSysctl
|
||||
perAppRoutingTun
|
||||
proxyCfg
|
||||
;
|
||||
@@ -62,8 +64,24 @@ let
|
||||
family = "inet";
|
||||
table = xrayTunReplyTable;
|
||||
};
|
||||
deleteKillSwitchTable = builders.mkNftDeleteTable {
|
||||
inherit nft;
|
||||
family = "inet";
|
||||
table = "proxy_suite_killswitch";
|
||||
};
|
||||
in
|
||||
{
|
||||
killSwitchUpScript = pkgs.writeShellScript "proxy-suite-routing" ''
|
||||
set -euo pipefail
|
||||
${deleteKillSwitchTable}
|
||||
${nft} -f ${killSwitchRulesFile}
|
||||
'';
|
||||
|
||||
killSwitchDownScript = pkgs.writeShellScript "proxy-suite-routing" ''
|
||||
set +e
|
||||
${deleteKillSwitchTable}
|
||||
'';
|
||||
|
||||
xrayTunUpScript = pkgs.writeShellScript "proxy-suite-routing" ''
|
||||
set -euo pipefail
|
||||
|
||||
@@ -139,6 +157,12 @@ in
|
||||
}}
|
||||
|
||||
${nft} -f ${nftablesRulesFile}
|
||||
# Set on NixOS already; system-manager hosts only get them here.
|
||||
${lib.concatStrings (
|
||||
lib.mapAttrsToList (name: value: ''
|
||||
${pkgs.procps}/bin/sysctl -q -w ${name}=${toString value}
|
||||
'') tproxyLanSysctl
|
||||
)}
|
||||
${builders.mkTproxyRoutingUp {
|
||||
inherit ip;
|
||||
inherit ipv6;
|
||||
|
||||
@@ -11,6 +11,8 @@ let
|
||||
pureXrayEnabled
|
||||
globalTun
|
||||
globalTproxy
|
||||
killSwitchEnabled
|
||||
awgGlobalProfiles
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
perAppZapretEnabled
|
||||
@@ -37,6 +39,8 @@ let
|
||||
xrayTunUpScript
|
||||
tproxyUpScript
|
||||
tproxyDownScript
|
||||
killSwitchUpScript
|
||||
killSwitchDownScript
|
||||
tunCleanupScript
|
||||
;
|
||||
|
||||
@@ -44,6 +48,7 @@ let
|
||||
socks = "proxy-suite-socks";
|
||||
tproxy = "proxy-suite-tproxy";
|
||||
tun = "proxy-suite-tun";
|
||||
killSwitch = "proxy-suite-killswitch";
|
||||
perAppTun = "proxy-suite-per-app-tun";
|
||||
perAppTproxy = "proxy-suite-per-app-tproxy";
|
||||
perAppZapret = "proxy-suite-per-app-zapret";
|
||||
@@ -132,6 +137,23 @@ let
|
||||
extraServiceConfig.RemainAfterExit = false;
|
||||
};
|
||||
}
|
||||
# Pulled in by every global tunnel and left up when it stops or crashes: only
|
||||
# proxy-ctl's off verbs take it down. After them, so a first start still fetches
|
||||
# subscriptions and resolves endpoints directly.
|
||||
{
|
||||
enable = killSwitchEnabled;
|
||||
name = serviceNames.killSwitch;
|
||||
value = mkOneshotService {
|
||||
description = "proxy-suite kill switch - reject traffic outside the global tunnel";
|
||||
after = [
|
||||
"${serviceNames.socks}.service"
|
||||
"${serviceNames.tun}.service"
|
||||
]
|
||||
++ map (name: "proxy-suite-awg-${name}.service") (builtins.attrNames awgGlobalProfiles);
|
||||
execStart = killSwitchUpScript;
|
||||
execStop = killSwitchDownScript;
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = proxyEnabled && globalTproxy.enable;
|
||||
name = serviceNames.tproxy;
|
||||
@@ -143,6 +165,7 @@ let
|
||||
];
|
||||
wantedBy = lib.optionals (proxyCfg.autostart == "tproxy") [ "multi-user.target" ];
|
||||
requires = [ "${serviceNames.socks}.service" ];
|
||||
wants = lib.optional killSwitchEnabled "${serviceNames.killSwitch}.service";
|
||||
conflicts = [
|
||||
"${serviceNames.tun}.service"
|
||||
"${serviceNames.perAppTproxy}.service"
|
||||
@@ -157,7 +180,10 @@ let
|
||||
value = mkRestartingService {
|
||||
description = "${backendDescription} TUN proxy client";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
wants = [
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional killSwitchEnabled "${serviceNames.killSwitch}.service";
|
||||
wantedBy = lib.optionals (proxyCfg.autostart == "tun") [ "multi-user.target" ];
|
||||
conflicts = [ "${serviceNames.tproxy}.service" ];
|
||||
execStartPre = tunCleanupScript;
|
||||
|
||||
Reference in new issue
Block a user