feat: runtime users and inbounds
This commit is contained in:
36 files changed
+2932
-83
No files matched your search
@@ -21,6 +21,8 @@ let
|
||||
userControlCfg
|
||||
proxyInboundsCfg
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsRuntimeEnabled
|
||||
proxyInboundsSpecFile
|
||||
amneziaWgProfileNamesFile
|
||||
awgRuntimeGlobal
|
||||
awgRuntimeOutbounds
|
||||
@@ -111,6 +113,11 @@ in
|
||||
INBOUNDS_SUBS_FILE = proxyInboundsSubscriptionsFile;
|
||||
INBOUNDS_SUB_BASE_URL =
|
||||
if proxyInboundsSubscriptionsBaseUrl == null then "" else proxyInboundsSubscriptionsBaseUrl;
|
||||
# inbounds.runtime: the users and listeners proxy-ctl adds go through this tool, which
|
||||
# reads what it may use from the spec.
|
||||
INBOUNDS_RUNTIME_ENABLED = flag proxyInboundsRuntimeEnabled;
|
||||
INBOUNDS_RUNTIME_TOOL = "${proxySuiteScriptsDir}/inbound_runtime.py";
|
||||
INBOUNDS_SPEC_FILE = "${proxyInboundsSpecFile}";
|
||||
# Only the system-wide instance reads the site lists; per-app zapret handles all traffic.
|
||||
ZAPRET_AUTO_ENABLED = flag (zapretEngine == "zapret2" && zapretGlobalEnabled);
|
||||
ZAPRET_STATE_DIR = constants.zapret2StateDir;
|
||||
|
||||
@@ -117,9 +117,17 @@ lib.mkMerge [
|
||||
# Inbound listeners are reached from outside, so their ports have to be open.
|
||||
firewall = lib.mkMerge [
|
||||
(lib.mkIf (proxyInboundsEnabled && cfg.inbounds.openFirewall) {
|
||||
allowedTCPPorts = proxyInboundFirewallPorts;
|
||||
allowedUDPPorts = proxyInboundFirewallUdpPorts;
|
||||
allowedTCPPorts = proxyInboundFirewallPorts ++ derived.proxyInboundRuntimeSinglePorts;
|
||||
allowedUDPPorts = proxyInboundFirewallUdpPorts ++ derived.proxyInboundRuntimeSinglePorts;
|
||||
})
|
||||
# Only the NixOS adapter forwards ranges.
|
||||
(lib.mkIf
|
||||
(proxyInboundsEnabled && cfg.inbounds.openFirewall && derived.proxyInboundRuntimePortRanges != [ ])
|
||||
{
|
||||
allowedTCPPortRanges = derived.proxyInboundRuntimePortRanges;
|
||||
allowedUDPPortRanges = derived.proxyInboundRuntimePortRanges;
|
||||
}
|
||||
)
|
||||
# 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 != { }) (
|
||||
@@ -194,6 +202,27 @@ lib.mkMerge [
|
||||
constants.runtimeSubscriptionsDir
|
||||
]
|
||||
))
|
||||
# inbounds.runtime: the users' and listeners' files hold their secrets, so the dir is
|
||||
# as closed as outbounds.d, and group-writable only with the inbounds scope.
|
||||
(lib.mkIf (cfg.enable && derived.proxyInboundsRuntimeEnabled) (
|
||||
map
|
||||
(
|
||||
dir:
|
||||
"d ${dir} ${
|
||||
if userControlAllows "inbounds" then
|
||||
"2770 root ${userControlCfg.group}"
|
||||
else if constants.privileged then
|
||||
"0700 root root"
|
||||
else
|
||||
"0700 - -"
|
||||
} -"
|
||||
)
|
||||
[
|
||||
constants.runtimeInboundsDir
|
||||
"${constants.runtimeInboundsDir}/users"
|
||||
"${constants.runtimeInboundsDir}/listeners"
|
||||
]
|
||||
))
|
||||
# Global AmneziaWG profiles added at runtime. Listable, so the tray and TUI show
|
||||
# their names to everyone; each file is 0600 (amneziawg_config.py writes it so).
|
||||
(lib.mkIf (cfg.enable && derived.awgRuntimeGlobal) [
|
||||
|
||||
@@ -29,6 +29,7 @@ let
|
||||
"proxy-suite-autoproxy-learn." = "autoProxy";
|
||||
"proxy-suite-zapret2-cutoff." = "zapret";
|
||||
"proxy-suite-inbound-stats." = "stats";
|
||||
"proxy-suite-inbounds-reload." = "inbounds";
|
||||
"proxy-suite-wb-" = "whitelistBypass";
|
||||
};
|
||||
templates = lib.filter (lib.hasSuffix "@") unitNames;
|
||||
|
||||
@@ -26,6 +26,7 @@ let
|
||||
proxyInboundsFile
|
||||
proxyInboundsSpecFile
|
||||
proxyInboundsSelfSources
|
||||
proxyInboundsRuntimeEnabled
|
||||
builders
|
||||
constants
|
||||
;
|
||||
@@ -40,7 +41,12 @@ let
|
||||
ip = "${pkgs.iproute2}/bin/ip";
|
||||
selfInterface = lib.escapeShellArg proxyInboundsCfg.routing.serverSource.interface;
|
||||
selfInterfaceDown = "${ip} link del ${selfInterface} 2>/dev/null || true";
|
||||
selfInterfaceUp = lib.optionalString (proxyInboundsSelfSources != [ ]) ''
|
||||
serverSource = proxyInboundsCfg.routing.serverSource;
|
||||
# With inbounds.runtime, runtime users may get addresses too, whatever the declared have.
|
||||
selfInterfaceWanted =
|
||||
proxyInboundsSelfSources != [ ]
|
||||
|| (proxyInboundsRuntimeEnabled && (serverSource.ipv4 != null || serverSource.ipv6 != null));
|
||||
selfInterfaceUp = lib.optionalString selfInterfaceWanted ''
|
||||
${selfInterfaceDown}
|
||||
${ip} link add ${selfInterface} type dummy
|
||||
${ip} link set ${selfInterface} up
|
||||
@@ -49,6 +55,14 @@ let
|
||||
lib.optionalString (s.ipv4 != null) "${ip} addr add ${s.ipv4}/32 dev ${selfInterface}\n"
|
||||
+ lib.optionalString (s.ipv6 != null) "${ip} -6 addr add ${s.ipv6}/128 dev ${selfInterface} nodad\n"
|
||||
) proxyInboundsSelfSources}
|
||||
${lib.optionalString proxyInboundsRuntimeEnabled ''
|
||||
while IFS=$'\t' read -r family address; do
|
||||
case "$family" in
|
||||
4) ${ip} addr add "$address/32" dev ${selfInterface} ;;
|
||||
6) ${ip} -6 addr add "$address/128" dev ${selfInterface} nodad ;;
|
||||
esac
|
||||
done < <(${jq} -r '.selfAddresses[]? | (select(.ipv4) | "4\t\(.ipv4)"), (select(.ipv6) | "6\t\(.ipv6)")' <<< "$RENDERED")
|
||||
''}
|
||||
'';
|
||||
# Only listeners, and for AmneziaWG listeners transparent sockets (IP_TRANSPARENT).
|
||||
runXray = constants.runAsServiceUser pkgs (
|
||||
@@ -173,7 +187,7 @@ let
|
||||
RENDERED=$(PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${buildInboundPy} \
|
||||
--spec ${proxyInboundsSpecFile} \
|
||||
--server-address "$SERVER_ADDRESS" \
|
||||
--onion-address "$ONION_ADDRESS")
|
||||
--onion-address "$ONION_ADDRESS"${lib.optionalString proxyInboundsRuntimeEnabled " \\\n --template ${proxyInboundsFile}"})
|
||||
|
||||
INBOUNDS_JSON=$(${jq} -c '.inbounds' <<< "$RENDERED")
|
||||
|
||||
@@ -184,6 +198,8 @@ let
|
||||
LOCAL_PROXY_PASSWORD="$(cat "${localProxyAuthPasswordSource}")"
|
||||
''}
|
||||
|
||||
# With inbounds.runtime, the routing rules are build-inbound.py's: the template's with the
|
||||
# runtime listeners and users in. Their serverSource outbounds come with them.
|
||||
${jq} \
|
||||
--slurpfile ibs <(printf '%s' "$INBOUNDS_JSON") \
|
||||
--slurpfile obs <(printf '%s' "$OUTBOUNDS_JSON") \
|
||||
@@ -195,8 +211,14 @@ let
|
||||
else
|
||||
"--arg password ''"
|
||||
} \
|
||||
--argjson runtime ${if proxyInboundsRuntimeEnabled then "true" else "false"} \
|
||||
--slurpfile rendered <(printf '%s' "$RENDERED") \
|
||||
'.inbounds += $ibs[0]
|
||||
| .outbounds = $obs[0] + .outbounds
|
||||
| if $runtime then
|
||||
.routing.rules = $rendered[0].rules
|
||||
| .outbounds += $rendered[0].selfOutbounds
|
||||
else . end
|
||||
| if $auth_enabled then
|
||||
(.outbounds[] | select(.tag == "proxy") | .settings.servers[0].users)
|
||||
= [{user:$user,pass:$password}]
|
||||
@@ -306,8 +328,22 @@ let
|
||||
chmod ${if userControlAllows "stats" then "640" else "600"} "$tmp"
|
||||
mv -f "$tmp" "$file"
|
||||
'';
|
||||
# inbounds.runtime: after proxy-ctl changes the spool. The AmneziaWG interfaces only when
|
||||
# their peers changed: restarting them cuts off every one of their clients for a moment.
|
||||
reloadInbounds = pkgs.writeShellScript "proxy-suite-inbounds" ''
|
||||
set -euo pipefail
|
||||
restart=$(PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${parserScriptsPythonPath}/inbound_runtime.py \
|
||||
--spec ${proxyInboundsSpecFile} check${
|
||||
lib.optionalString (
|
||||
proxyInboundsAwg != [ ]
|
||||
) " --awg-users ${constants.runtimeDir}/proxy-suite-inbounds-awg/users.json"
|
||||
})
|
||||
# shellcheck disable=SC2086
|
||||
${constants.systemctl} try-restart $restart proxy-suite-inbounds.service
|
||||
'';
|
||||
|
||||
stopInboundsInterface =
|
||||
if proxyInboundsSelfSources == [ ] then
|
||||
if !selfInterfaceWanted then
|
||||
null
|
||||
else
|
||||
pkgs.writeShellScript "proxy-suite-inbounds" selfInterfaceDown;
|
||||
@@ -317,6 +353,7 @@ in
|
||||
startInbounds
|
||||
stopInboundsInterface
|
||||
collectInboundStats
|
||||
reloadInbounds
|
||||
linksFile
|
||||
subscriptionsFile
|
||||
;
|
||||
|
||||
@@ -168,7 +168,12 @@ let
|
||||
inherit (startScripts) startSocks startTun startPerAppTun;
|
||||
|
||||
proxyInboundsScripts = import ./proxy-inbounds-scripts.nix { ctx = sctx; };
|
||||
inherit (proxyInboundsScripts) startInbounds stopInboundsInterface collectInboundStats;
|
||||
inherit (proxyInboundsScripts)
|
||||
startInbounds
|
||||
stopInboundsInterface
|
||||
collectInboundStats
|
||||
reloadInbounds
|
||||
;
|
||||
proxyInboundsLinksFile = proxyInboundsScripts.linksFile;
|
||||
proxyInboundsSubscriptionsFile = proxyInboundsScripts.subscriptionsFile;
|
||||
|
||||
@@ -206,6 +211,7 @@ in
|
||||
startInbounds
|
||||
stopInboundsInterface
|
||||
collectInboundStats
|
||||
reloadInbounds
|
||||
;
|
||||
inherit
|
||||
proxyInboundsLinksFile
|
||||
|
||||
@@ -22,6 +22,7 @@ let
|
||||
torOnionEnabled
|
||||
whitelistBypassJoiners
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsRuntimeEnabled
|
||||
proxyInboundsNeedLocalProxy
|
||||
scripts
|
||||
perAppRouting
|
||||
@@ -129,6 +130,16 @@ let
|
||||
};
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = proxyInboundsRuntimeEnabled;
|
||||
name = "proxy-suite-inbounds-reload";
|
||||
value = mkOneshotService {
|
||||
description = "Apply proxy-suite inbound users and listeners added at runtime";
|
||||
execStart = scripts.reloadInbounds;
|
||||
stateDirectory = "proxy-suite";
|
||||
extraServiceConfig.RemainAfterExit = false;
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = proxyInboundsEnabled;
|
||||
name = serviceNames.inboundStats;
|
||||
|
||||
Reference in new issue
Block a user