feat/wip/fix: add inbounds support, fix ssh-proxy
This commit is contained in:
39 files changed
+5093
-48
No files matched your search
@@ -8,6 +8,8 @@
|
||||
tunFile,
|
||||
perAppTunFile,
|
||||
routeModeRulesFile,
|
||||
proxyInboundsFile,
|
||||
proxyInboundsSpecFile,
|
||||
perAppTunChainFile,
|
||||
perAppTproxyRulesFile,
|
||||
perAppZapretRulesFile,
|
||||
@@ -44,6 +46,12 @@ let
|
||||
hasSubscriptions
|
||||
sshProxyCfg
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
proxyInboundsCfg
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundViaOutbounds
|
||||
userControlEnabled
|
||||
;
|
||||
|
||||
# Tool paths – defined once here and passed into sub-modules as needed.
|
||||
@@ -64,8 +72,11 @@ let
|
||||
proxySuiteScriptsDir = ../../../scripts;
|
||||
parserScriptsPythonPath = proxySuiteScriptsDir;
|
||||
buildOutboundPy = "${proxySuiteScriptsDir}/build-outbound.py";
|
||||
buildInboundPy = "${proxySuiteScriptsDir}/build-inbound.py";
|
||||
fetchSubscriptionPy = "${proxySuiteScriptsDir}/fetch-subscription.py";
|
||||
|
||||
builders = import ./builders.nix { inherit lib pkgs; };
|
||||
|
||||
polkit = import ./polkit.nix {
|
||||
inherit lib cfg userControlCfg;
|
||||
};
|
||||
@@ -95,6 +106,7 @@ let
|
||||
xray
|
||||
parserScriptsPythonPath
|
||||
buildOutboundPy
|
||||
buildInboundPy
|
||||
fetchSubscriptionPy
|
||||
;
|
||||
inherit
|
||||
@@ -102,6 +114,15 @@ let
|
||||
tunFile
|
||||
perAppTunFile
|
||||
routeModeRulesFile
|
||||
proxyInboundsFile
|
||||
proxyInboundsSpecFile
|
||||
;
|
||||
inherit
|
||||
proxyInboundsCfg
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundViaOutbounds
|
||||
userControlEnabled
|
||||
builders
|
||||
;
|
||||
};
|
||||
|
||||
@@ -144,6 +165,8 @@ let
|
||||
;
|
||||
inherit (scripts) subscriptionTagsFile subscriptionCacheDir;
|
||||
inherit (scripts) routeModeStateFile;
|
||||
inherit proxyInboundsEnabled;
|
||||
inherit (scripts) proxyInboundsLinksFile;
|
||||
inherit amneziaWgProfileNamesFile;
|
||||
inherit (perAppRouting)
|
||||
perAppRoutingProfilesFile
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
proxychainsQuietArg,
|
||||
routeModeStateFile,
|
||||
amneziaWgProfileNamesFile,
|
||||
proxyInboundsEnabled,
|
||||
proxyInboundsLinksFile,
|
||||
}:
|
||||
{
|
||||
proxyCtl = packages.mkProxyCtl {
|
||||
@@ -35,5 +37,7 @@
|
||||
perAppRoutingTunEnabled = if perAppRoutingTun.enable then "1" else "0";
|
||||
perAppRoutingTproxyEnabled = if perAppRoutingTproxy.enable then "1" else "0";
|
||||
perAppRoutingZapretEnabled = if perAppZapretCfg.enable then "1" else "0";
|
||||
inboundsEnabled = if proxyInboundsEnabled then "1" else "0";
|
||||
inboundsLinksFile = proxyInboundsLinksFile;
|
||||
};
|
||||
}
|
||||
@@ -9,6 +9,8 @@
|
||||
tunFile,
|
||||
perAppTunFile,
|
||||
routeModeRulesFile,
|
||||
proxyInboundsFile,
|
||||
proxyInboundsSpecFile,
|
||||
nftablesRulesFile,
|
||||
perAppTproxyRulesFile,
|
||||
perAppZapretRulesFile,
|
||||
@@ -28,6 +30,8 @@ let
|
||||
tunFile
|
||||
perAppTunFile
|
||||
routeModeRulesFile
|
||||
proxyInboundsFile
|
||||
proxyInboundsSpecFile
|
||||
perAppTunChainFile
|
||||
perAppTproxyRulesFile
|
||||
perAppZapretRulesFile
|
||||
@@ -61,6 +65,11 @@ let
|
||||
perAppZapretEnabled
|
||||
hasSubscriptions
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundFirewallPorts
|
||||
proxyInboundFirewallUdpPorts
|
||||
outboundTags
|
||||
effectiveOutboundTags
|
||||
subscriptionTags
|
||||
@@ -99,6 +108,9 @@ let
|
||||
perAppZapretEnabled
|
||||
hasSubscriptions
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsNeedLocalProxy
|
||||
scripts
|
||||
perAppRouting
|
||||
routingScripts
|
||||
@@ -124,6 +136,12 @@ in
|
||||
|| perAppZapretEnabled
|
||||
) (lib.mkDefault true);
|
||||
|
||||
# Inbound listeners are reached from outside, so their ports have to be open.
|
||||
networking.firewall = lib.mkIf (proxyInboundsEnabled && cfg.proxyInbounds.openFirewall) {
|
||||
allowedTCPPorts = proxyInboundFirewallPorts;
|
||||
allowedUDPPorts = proxyInboundFirewallUdpPorts;
|
||||
};
|
||||
|
||||
users.groups = lib.mkIf (cfg.enable && (userControlEnabled || localProxyAuthEnabled)) {
|
||||
"${userControlCfg.group}" = { };
|
||||
};
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
# Startup script for the server-side inbound service.
|
||||
#
|
||||
# Much smaller than the client start script: no route-mode, no TUN/TProxy, no
|
||||
# hybrid sidecar. It renders the listeners (reading their secrets), merges them
|
||||
# into the build-time template, and hands the result to XRay.
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
proxyCfg,
|
||||
proxyInboundsCfg,
|
||||
proxyInboundsNeedLocalProxy,
|
||||
proxyInboundViaOutbounds,
|
||||
userControlCfg,
|
||||
userControlEnabled,
|
||||
localProxyAuth,
|
||||
localProxyAuthEnabled,
|
||||
localProxyAuthPasswordSource,
|
||||
jq,
|
||||
python3,
|
||||
parserScriptsPythonPath,
|
||||
buildInboundPy,
|
||||
buildOutboundPy,
|
||||
proxyInboundsFile,
|
||||
proxyInboundsSpecFile,
|
||||
builders,
|
||||
}:
|
||||
|
||||
let
|
||||
runtimeDir = "/run/proxy-suite-inbounds";
|
||||
linksFile = "${runtimeDir}/links.json";
|
||||
# This service's own package, not the client backend's.
|
||||
xray = "${proxyInboundsCfg.package}/bin/xray";
|
||||
|
||||
# Only the local proxy needs credentials injected; the listeners' own secrets
|
||||
# are resolved by build-inbound.py.
|
||||
needsLocalProxyAuth = proxyInboundsNeedLocalProxy && localProxyAuthEnabled;
|
||||
|
||||
serverAddressBlock =
|
||||
if proxyInboundsCfg.serverAddress != null then
|
||||
''SERVER_ADDRESS=${lib.escapeShellArg proxyInboundsCfg.serverAddress}''
|
||||
else if proxyInboundsCfg.shareLinks then
|
||||
''
|
||||
${builders.mkDefaultUplinkIPv4Source {
|
||||
ip = "${pkgs.iproute2}/bin/ip";
|
||||
awk = "${pkgs.gawk}/bin/awk";
|
||||
errorMessage = "proxy-suite: could not determine this host's uplink address for inbound share links; set proxyInbounds.serverAddress";
|
||||
}}
|
||||
SERVER_ADDRESS="$uplink_addr"
|
||||
''
|
||||
else
|
||||
''SERVER_ADDRESS=""'';
|
||||
|
||||
# Outbounds a listener pins itself to with `via = "<tag>"`. Rendered here, at
|
||||
# start time, so a urlFile's contents stay out of the Nix store. Subscriptions
|
||||
# are deliberately not duplicated into this service: their cache is refreshed
|
||||
# for the client backend only, and `via = "proxy"` already reaches them.
|
||||
mkViaOutboundBlock =
|
||||
ob:
|
||||
let
|
||||
urlSource =
|
||||
if ob.urlFile != null then
|
||||
ob.urlFile
|
||||
else if ob.url != null then
|
||||
pkgs.writeText "proxy-suite-inbound-via-url-${ob.tag}" ob.url
|
||||
else
|
||||
null;
|
||||
in
|
||||
if urlSource == null then
|
||||
''
|
||||
# via outbound: ${ob.tag} (static xray json)
|
||||
OB_JSON=$(cat ${
|
||||
pkgs.writeText "proxy-suite-inbound-via-${ob.tag}.json" (
|
||||
builtins.toJSON (ob.xrayJson // { inherit (ob) tag; })
|
||||
)
|
||||
})
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson ob "$OB_JSON" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
|
||||
''
|
||||
else
|
||||
''
|
||||
# via outbound: ${ob.tag}
|
||||
URL=$(cat ${lib.escapeShellArg urlSource})
|
||||
OB_JSON=$(printf '%s' "$URL" | PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${buildOutboundPy} \
|
||||
--backend xray --tag ${lib.escapeShellArg ob.tag})
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson ob "$OB_JSON" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
|
||||
viaOutboundsBlock = lib.concatMapStrings mkViaOutboundBlock proxyInboundViaOutbounds;
|
||||
|
||||
# Share links carry the listener credentials, so they are no more public than
|
||||
# the config itself.
|
||||
writeLinksBlock = lib.optionalString proxyInboundsCfg.shareLinks ''
|
||||
${jq} -c '.links' <<< "$RENDERED" > "${linksFile}"
|
||||
${lib.optionalString userControlEnabled ''
|
||||
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${linksFile}"
|
||||
''}
|
||||
chmod ${if userControlEnabled then "640" else "600"} "${linksFile}"
|
||||
'';
|
||||
|
||||
startInbounds = pkgs.writeShellScript "proxy-suite-start-inbounds" ''
|
||||
set -euo pipefail
|
||||
umask 077
|
||||
RUNTIME_DIR="${runtimeDir}"
|
||||
mkdir -p "$RUNTIME_DIR"
|
||||
|
||||
${serverAddressBlock}
|
||||
|
||||
RENDERED=$(PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${buildInboundPy} \
|
||||
--spec ${proxyInboundsSpecFile} \
|
||||
--server-address "$SERVER_ADDRESS")
|
||||
|
||||
INBOUNDS_JSON=$(${jq} -c '.inbounds' <<< "$RENDERED")
|
||||
|
||||
OUTBOUNDS_JSON='[]'
|
||||
${viaOutboundsBlock}
|
||||
|
||||
${lib.optionalString needsLocalProxyAuth ''
|
||||
LOCAL_PROXY_PASSWORD="$(cat "${localProxyAuthPasswordSource}")"
|
||||
''}
|
||||
|
||||
${jq} \
|
||||
--argjson ibs "$INBOUNDS_JSON" \
|
||||
--argjson obs "$OUTBOUNDS_JSON" \
|
||||
--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
|
||||
| .outbounds = $obs + .outbounds
|
||||
| if $auth_enabled then
|
||||
(.outbounds[] | select(.tag == "proxy") | .settings.servers[0].users)
|
||||
= [{user:$user,pass:$password}]
|
||||
else . end' \
|
||||
${proxyInboundsFile} > "$RUNTIME_DIR/config.json"
|
||||
chmod 600 "$RUNTIME_DIR/config.json"
|
||||
|
||||
${writeLinksBlock}
|
||||
|
||||
exec ${xray} run -c "$RUNTIME_DIR/config.json"
|
||||
'';
|
||||
in
|
||||
{
|
||||
inherit startInbounds linksFile;
|
||||
}
|
||||
@@ -22,6 +22,15 @@
|
||||
let
|
||||
sshProxyTag = "ssh-proxy";
|
||||
|
||||
# sing-box domain-strategy names mapped onto XRay's sockopt.domainStrategy.
|
||||
# XRay's "UseIPv4v6" means "IPv4 first, fall back to IPv6", i.e. prefer_ipv4.
|
||||
xrayDomainStrategies = {
|
||||
prefer_ipv4 = "UseIPv4v6";
|
||||
prefer_ipv6 = "UseIPv6v4";
|
||||
ipv4_only = "UseIPv4";
|
||||
ipv6_only = "UseIPv6";
|
||||
};
|
||||
|
||||
rawOutboundJson =
|
||||
ob: tag: routingMark:
|
||||
let
|
||||
@@ -169,16 +178,33 @@ let
|
||||
mkSshProxyOutboundBlock =
|
||||
routingMark:
|
||||
let
|
||||
# sing-box dials SSH itself, so there is no OpenSSH unit and no local
|
||||
# SOCKS5 hop. It resolves destinations natively, which is why this shape
|
||||
# takes no domainStrategy.
|
||||
singBoxOutbound = {
|
||||
type = "socks";
|
||||
type = "ssh";
|
||||
tag = sshProxyTag;
|
||||
server = sshProxyCfg.listenAddress;
|
||||
server_port = sshProxyCfg.listenPort;
|
||||
version = "5";
|
||||
server = sshProxyCfg.host;
|
||||
server_port = sshProxyCfg.sshPort;
|
||||
user = sshProxyCfg.user;
|
||||
}
|
||||
// lib.optionalAttrs (sshProxyCfg.identityFile != null) {
|
||||
private_key_path = sshProxyCfg.identityFile;
|
||||
}
|
||||
// lib.optionalAttrs (sshProxyCfg.hostKey != [ ]) {
|
||||
host_key = sshProxyCfg.hostKey;
|
||||
}
|
||||
// lib.optionalAttrs (routingMark != null) {
|
||||
routing_mark = routingMark;
|
||||
};
|
||||
# XRay has no SSH outbound, so it proxies through the listener the
|
||||
# OpenSSH unit creates. sockopt.domainStrategy is what makes XRay resolve
|
||||
# destinations here instead of handing the domain to the remote sshd.
|
||||
xraySockopt =
|
||||
lib.optionalAttrs (routingMark != null) { mark = routingMark; }
|
||||
// lib.optionalAttrs (sshProxyCfg.domainStrategy != null) {
|
||||
domainStrategy = xrayDomainStrategies.${sshProxyCfg.domainStrategy};
|
||||
};
|
||||
xrayOutbound = {
|
||||
protocol = "socks";
|
||||
tag = sshProxyTag;
|
||||
@@ -187,23 +213,45 @@ let
|
||||
port = sshProxyCfg.listenPort;
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (routingMark != null) {
|
||||
streamSettings.sockopt.mark = routingMark;
|
||||
// lib.optionalAttrs (xraySockopt != { }) {
|
||||
streamSettings.sockopt = xraySockopt;
|
||||
};
|
||||
outbound = if pureXrayEnabled then xrayOutbound else singBoxOutbound;
|
||||
outboundJson = builtins.toJSON outbound;
|
||||
jsonFile = pkgs.writeText "proxy-suite-ob-ssh-proxy-${backend}.json" outboundJson;
|
||||
|
||||
# Host keys come from a known-hosts file at start time so the file stays
|
||||
# out of the Nix store. ssh-keygen -F also resolves hashed entries.
|
||||
knownHostsTarget =
|
||||
if sshProxyCfg.sshPort == 22 then
|
||||
sshProxyCfg.host
|
||||
else
|
||||
"[${sshProxyCfg.host}]:${toString sshProxyCfg.sshPort}";
|
||||
hostKeyFileBlock =
|
||||
lib.optionalString (!pureXrayEnabled && sshProxyCfg.hostKeyFile != null)
|
||||
''
|
||||
SSH_HOST_KEYS=$(${pkgs.openssh}/bin/ssh-keygen -F ${lib.escapeShellArg knownHostsTarget} \
|
||||
-f ${lib.escapeShellArg sshProxyCfg.hostKeyFile} \
|
||||
| ${jq} -R -s '[splits("\n")] | map(select(length > 0 and (startswith("#") | not))) | map(sub("^\\S+\\s+"; ""))')
|
||||
if [ "$(${jq} 'length' <<< "$SSH_HOST_KEYS")" -eq 0 ]; then
|
||||
echo "proxy-suite: no host keys for ${knownHostsTarget} in ${sshProxyCfg.hostKeyFile}" >&2
|
||||
exit 1
|
||||
fi
|
||||
OB_JSON=$(${jq} --argjson hk "$SSH_HOST_KEYS" '.host_key = $hk' <<< "$OB_JSON")
|
||||
'';
|
||||
in
|
||||
if hybridEnabled then
|
||||
''
|
||||
# outbound: ${sshProxyTag} (SSH SOCKS5)
|
||||
# outbound: ${sshProxyTag} (${if pureXrayEnabled then "OpenSSH SOCKS5 listener" else "native SSH"})
|
||||
OB_JSON=$(cat "${jsonFile}")
|
||||
${hostKeyFileBlock}
|
||||
_proxy_suite_add_sing_box_ob "$OB_JSON"
|
||||
''
|
||||
else
|
||||
''
|
||||
# outbound: ${sshProxyTag} (SSH SOCKS5)
|
||||
# outbound: ${sshProxyTag} (${if pureXrayEnabled then "OpenSSH SOCKS5 listener" else "native SSH"})
|
||||
OB_JSON=$(cat "${jsonFile}")
|
||||
${hostKeyFileBlock}
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson ob "$OB_JSON" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
|
||||
|
||||
@@ -26,6 +26,14 @@
|
||||
tunFile,
|
||||
perAppTunFile,
|
||||
routeModeRulesFile,
|
||||
proxyInboundsCfg,
|
||||
proxyInboundsNeedLocalProxy,
|
||||
proxyInboundViaOutbounds,
|
||||
userControlEnabled,
|
||||
buildInboundPy,
|
||||
proxyInboundsFile,
|
||||
proxyInboundsSpecFile,
|
||||
builders,
|
||||
}:
|
||||
|
||||
let
|
||||
@@ -157,6 +165,32 @@ let
|
||||
};
|
||||
inherit (startScripts) startSocks startTun startPerAppTun;
|
||||
|
||||
proxyInboundsScripts = import ./proxy-inbounds-scripts.nix {
|
||||
inherit
|
||||
lib
|
||||
pkgs
|
||||
proxyCfg
|
||||
proxyInboundsCfg
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundViaOutbounds
|
||||
userControlCfg
|
||||
userControlEnabled
|
||||
localProxyAuth
|
||||
localProxyAuthEnabled
|
||||
localProxyAuthPasswordSource
|
||||
jq
|
||||
python3
|
||||
parserScriptsPythonPath
|
||||
buildInboundPy
|
||||
buildOutboundPy
|
||||
proxyInboundsFile
|
||||
proxyInboundsSpecFile
|
||||
builders
|
||||
;
|
||||
};
|
||||
inherit (proxyInboundsScripts) startInbounds;
|
||||
proxyInboundsLinksFile = proxyInboundsScripts.linksFile;
|
||||
|
||||
controlScripts = import ./control-scripts.nix {
|
||||
inherit
|
||||
lib
|
||||
@@ -175,8 +209,9 @@ let
|
||||
|
||||
in
|
||||
{
|
||||
inherit startSocks startTun startPerAppTun;
|
||||
inherit startSocks startTun startPerAppTun startInbounds;
|
||||
inherit
|
||||
proxyInboundsLinksFile
|
||||
routeModeStateFile
|
||||
setRouteModeScript
|
||||
subscriptionUpdateScript
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
perAppZapretEnabled,
|
||||
hasSubscriptions,
|
||||
sshProxyOutboundEnabled,
|
||||
sshProxyUnitEnabled,
|
||||
proxyInboundsEnabled,
|
||||
proxyInboundsNeedLocalProxy,
|
||||
scripts,
|
||||
perAppRouting,
|
||||
routingScripts,
|
||||
@@ -41,6 +44,7 @@ let
|
||||
perAppTproxy = "proxy-suite-per-app-tproxy";
|
||||
perAppZapret = "proxy-suite-per-app-zapret";
|
||||
subscriptionUpdate = "proxy-suite-subscription-update";
|
||||
inbounds = "proxy-suite-inbounds";
|
||||
};
|
||||
|
||||
backendDescription =
|
||||
@@ -61,17 +65,43 @@ let
|
||||
name = serviceNames.socks;
|
||||
value = mkRestartingService {
|
||||
description = "${backendDescription} proxy client (SOCKS + TProxy-ready)";
|
||||
# Only the XRay backend proxies through the OpenSSH unit's listener;
|
||||
# SingBox dials SSH inside its own process and needs no ordering.
|
||||
after = [
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional sshProxyOutboundEnabled "proxy-suite-ssh-proxy.service";
|
||||
wants = [ "network-online.target" ];
|
||||
++ lib.optional (sshProxyOutboundEnabled && sshProxyUnitEnabled) "proxy-suite-ssh-proxy.service";
|
||||
wants = [
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional (sshProxyOutboundEnabled && sshProxyUnitEnabled) "proxy-suite-ssh-proxy.service";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
execStart = scripts.startSocks;
|
||||
runtimeDirectory = serviceNames.socks;
|
||||
stateDirectory = "proxy-suite";
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = proxyInboundsEnabled;
|
||||
name = serviceNames.inbounds;
|
||||
value = mkRestartingService {
|
||||
description = "XRay server inbounds (accept connections from outside)";
|
||||
after = [
|
||||
"network-online.target"
|
||||
]
|
||||
# Relayed traffic leaves through the client stack's SOCKS listener, so
|
||||
# start after it. Not `requires`: the listeners should keep serving
|
||||
# direct-routed traffic even if the client stack is down.
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service";
|
||||
wants = [
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
execStart = scripts.startInbounds;
|
||||
runtimeDirectory = serviceNames.inbounds;
|
||||
};
|
||||
}
|
||||
{
|
||||
enable = proxyEnabled && globalTproxy.enable;
|
||||
name = serviceNames.tproxy;
|
||||
|
||||
Reference in new issue
Block a user