feat/wip: ssh-proxy
This commit is contained in:
16 files changed
+990
-22
No files matched your search
@@ -86,6 +86,16 @@ in
|
||||
}
|
||||
))
|
||||
|
||||
(lib.mkIf cfg.sshProxy.enable (
|
||||
import ./ssh-proxy.nix {
|
||||
inherit
|
||||
lib
|
||||
pkgs
|
||||
cfg
|
||||
;
|
||||
}
|
||||
))
|
||||
|
||||
(lib.mkIf cfg.amneziaWg.enable (
|
||||
import ./amnezia-wg.nix {
|
||||
inherit
|
||||
|
||||
@@ -36,6 +36,9 @@ let
|
||||
perAppRoutingTproxy = proxyCfg.tproxy.perApp;
|
||||
perAppZapretCfg = cfg.zapret.perApp;
|
||||
userControlCfg = cfg.userControl;
|
||||
sshProxyCfg = cfg.sshProxy;
|
||||
sshProxyOutboundTag = "ssh-proxy";
|
||||
sshProxyOutboundEnabled = sshProxyCfg.enable && sshProxyCfg.asOutbound;
|
||||
|
||||
selectionMode = proxyCfg.selection;
|
||||
builtinTags = [
|
||||
@@ -44,10 +47,12 @@ let
|
||||
"block"
|
||||
];
|
||||
outboundTags = map (ob: ob.tag) proxyCfg.outbounds;
|
||||
effectiveOutboundTags = outboundTags ++ lib.optional sshProxyOutboundEnabled sshProxyOutboundTag;
|
||||
subscriptionTags = map (sub: sub.tag) proxyCfg.subscriptions;
|
||||
|
||||
hasStaticOutbounds = proxyCfg.outbounds != [ ];
|
||||
hasSubscriptions = proxyCfg.subscriptions != [ ];
|
||||
hasAvailableOutbounds = hasStaticOutbounds || hasSubscriptions || sshProxyOutboundEnabled;
|
||||
collapseNamedOutbounds = selectionMode == "first";
|
||||
clashApiEnabled = (singBoxEnabled || hybridEnabled) && selectionMode != "first";
|
||||
perAppZapretEnabled = perAppZapretCfg.enable;
|
||||
@@ -79,7 +84,7 @@ let
|
||||
invalidRoutingTargets = lib.unique (
|
||||
map (rule: rule.outbound) (
|
||||
builtins.filter (
|
||||
rule: !builtins.elem rule.outbound (builtinTags ++ outboundTags)
|
||||
rule: !builtins.elem rule.outbound (builtinTags ++ effectiveOutboundTags)
|
||||
) proxyCfg.routing.rules
|
||||
)
|
||||
);
|
||||
@@ -105,13 +110,18 @@ in
|
||||
perAppZapretEnabled
|
||||
userControlCfg
|
||||
userControlEnabled
|
||||
sshProxyCfg
|
||||
sshProxyOutboundTag
|
||||
sshProxyOutboundEnabled
|
||||
constants
|
||||
selectionMode
|
||||
builtinTags
|
||||
outboundTags
|
||||
effectiveOutboundTags
|
||||
subscriptionTags
|
||||
hasStaticOutbounds
|
||||
hasSubscriptions
|
||||
hasAvailableOutbounds
|
||||
collapseNamedOutbounds
|
||||
clashApiEnabled
|
||||
invalidRoutingTargets
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
{
|
||||
imports = [
|
||||
./other.nix
|
||||
./ssh-proxy.nix
|
||||
./amnezia-wg.nix
|
||||
./legacy-sing-box.nix
|
||||
./sing-box.nix
|
||||
|
||||
@@ -20,13 +20,13 @@ in
|
||||
|
||||
port = mkOption {
|
||||
type = types.port;
|
||||
default = 1081;
|
||||
default = 1085;
|
||||
description = ''
|
||||
Local listen port for the proxy backend's TProxy inbound.
|
||||
nftables redirection created by proxy-suite-tproxy sends intercepted
|
||||
TCP/UDP traffic to this port.
|
||||
'';
|
||||
example = 1081;
|
||||
example = 1085;
|
||||
};
|
||||
|
||||
fwmark = mkOption {
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
# SSH dynamic SOCKS5 proxy options.
|
||||
{ lib, ... }:
|
||||
|
||||
let
|
||||
inherit (lib) mkEnableOption mkOption types;
|
||||
in
|
||||
{
|
||||
options.services.proxy-suite.sshProxy = {
|
||||
enable = mkEnableOption "SSH dynamic SOCKS5 proxy";
|
||||
|
||||
user = mkOption {
|
||||
type = types.nullOr (types.strMatching "[^[:space:]]+");
|
||||
default = null;
|
||||
description = ''
|
||||
SSH login user. Required when sshProxy.enable is true.
|
||||
'';
|
||||
example = "root";
|
||||
};
|
||||
|
||||
host = mkOption {
|
||||
type = types.nullOr (types.strMatching "[^[:space:]]+");
|
||||
default = null;
|
||||
description = ''
|
||||
SSH server hostname or address. Required when sshProxy.enable is true.
|
||||
'';
|
||||
example = "ssh.example.com";
|
||||
};
|
||||
|
||||
sshPort = mkOption {
|
||||
type = types.port;
|
||||
default = 22;
|
||||
description = "Remote SSH server port.";
|
||||
example = 22;
|
||||
};
|
||||
|
||||
listenAddress = mkOption {
|
||||
type = types.strMatching "[^[:space:]]+";
|
||||
default = "127.0.0.1";
|
||||
description = "Local address for the SSH-created SOCKS5 listener.";
|
||||
example = "127.0.0.1";
|
||||
};
|
||||
|
||||
listenPort = mkOption {
|
||||
type = types.port;
|
||||
default = 1085;
|
||||
description = "Local port for the SSH-created SOCKS5 listener.";
|
||||
example = 1085;
|
||||
};
|
||||
|
||||
identityFile = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
description = ''
|
||||
Runtime path to the SSH private key. Leave unset to use the SSH agent
|
||||
or OpenSSH's normal identity-file lookup.
|
||||
'';
|
||||
example = "/run/secrets/proxy-suite-ssh-key";
|
||||
};
|
||||
|
||||
knownHostsFile = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
description = ''
|
||||
Optional runtime path to the known-hosts file passed to OpenSSH.
|
||||
'';
|
||||
example = "/run/secrets/proxy-suite-ssh-known-hosts";
|
||||
};
|
||||
|
||||
strictHostKeyChecking = mkOption {
|
||||
type = types.enum [
|
||||
"yes"
|
||||
"accept-new"
|
||||
"no"
|
||||
];
|
||||
default = "accept-new";
|
||||
description = ''
|
||||
OpenSSH host-key verification policy. Use "yes" with pinned
|
||||
knownHostsFile contents for strict verification.
|
||||
'';
|
||||
example = "yes";
|
||||
};
|
||||
|
||||
extraArgs = mkOption {
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
description = ''
|
||||
Additional arguments passed to OpenSSH before the destination.
|
||||
This can be used for options such as jump hosts or agent forwarding.
|
||||
'';
|
||||
example = [
|
||||
"-o"
|
||||
"ServerAliveInterval=30"
|
||||
];
|
||||
};
|
||||
|
||||
asOutbound = mkOption {
|
||||
type = types.bool;
|
||||
default = false;
|
||||
description = ''
|
||||
Add the local SSH SOCKS5 listener as a proxy-suite outbound tagged
|
||||
"ssh-proxy". Requires proxy.enable and an active SingBox or XRay
|
||||
backend.
|
||||
'';
|
||||
example = true;
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
tgWsProxyCfg,
|
||||
builtinTags,
|
||||
outboundTags,
|
||||
effectiveOutboundTags ? outboundTags,
|
||||
subscriptionTags,
|
||||
invalidRoutingTargets,
|
||||
effectivePerAppRoutingProfileNames,
|
||||
@@ -63,16 +64,24 @@ let
|
||||
(mkAssertion (!proxyEnabled || proxyCfg.singBox.enable || proxyCfg.xray.enable)
|
||||
"proxy-suite: at least one of proxy.singBox.enable or proxy.xray.enable must be true when proxy.enable = true"
|
||||
)
|
||||
(requireAvailable proxyEnabled (
|
||||
proxyCfg.outbounds != [ ] || proxyCfg.subscriptions != [ ]
|
||||
(requireAvailable proxyEnabled (derived.hasAvailableOutbounds
|
||||
) "proxy-suite: at least one outbound or subscription is required when proxy.enable = true")
|
||||
(uniqueValues proxyEnabled outboundTags "proxy-suite: outbound tags must be unique")
|
||||
(uniqueValues proxyEnabled effectiveOutboundTags "proxy-suite: outbound tags must be unique")
|
||||
(uniqueValues proxyEnabled subscriptionTags
|
||||
"proxy-suite: subscription tags must be unique because they are used as cache keys and outbound tag prefixes"
|
||||
)
|
||||
(mkAssertion (
|
||||
!proxyEnabled || builtins.all (tag: !builtins.elem tag builtinTags) outboundTags
|
||||
!proxyEnabled || builtins.all (tag: !builtins.elem tag builtinTags) effectiveOutboundTags
|
||||
) "proxy-suite: outbound tags must not use reserved names: proxy, direct, block")
|
||||
(requireEnabled cfg.sshProxy.asOutbound cfg.sshProxy.enable
|
||||
"proxy-suite: sshProxy.asOutbound requires sshProxy.enable = true"
|
||||
)
|
||||
(requireEnabled cfg.sshProxy.asOutbound proxyEnabled
|
||||
"proxy-suite: sshProxy.asOutbound requires proxy.enable = true"
|
||||
)
|
||||
(mkAssertion (
|
||||
!cfg.sshProxy.enable || (cfg.sshProxy.user != null && cfg.sshProxy.host != null)
|
||||
) "proxy-suite: sshProxy.user and sshProxy.host are required when sshProxy.enable = true")
|
||||
(mkAssertion (!proxyEnabled || invalidRoutingTargets == [ ])
|
||||
"proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}"
|
||||
)
|
||||
|
||||
@@ -42,6 +42,8 @@ let
|
||||
invalidRoutingTargets
|
||||
collapseNamedOutbounds
|
||||
hasSubscriptions
|
||||
sshProxyCfg
|
||||
sshProxyOutboundEnabled
|
||||
;
|
||||
|
||||
# Tool paths – defined once here and passed into sub-modules as needed.
|
||||
@@ -74,6 +76,7 @@ let
|
||||
pkgs
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
sshProxyCfg
|
||||
xrayEnabled
|
||||
hybridEnabled
|
||||
pureXrayEnabled
|
||||
|
||||
@@ -60,7 +60,9 @@ let
|
||||
userControlEnabled
|
||||
perAppZapretEnabled
|
||||
hasSubscriptions
|
||||
sshProxyOutboundEnabled
|
||||
outboundTags
|
||||
effectiveOutboundTags
|
||||
subscriptionTags
|
||||
invalidRoutingTargets
|
||||
builtinTags
|
||||
@@ -96,6 +98,7 @@ let
|
||||
perAppRoutingTproxy
|
||||
perAppZapretEnabled
|
||||
hasSubscriptions
|
||||
sshProxyOutboundEnabled
|
||||
scripts
|
||||
perAppRouting
|
||||
routingScripts
|
||||
@@ -153,6 +156,7 @@ in
|
||||
inherit
|
||||
builtinTags
|
||||
outboundTags
|
||||
effectiveOutboundTags
|
||||
subscriptionTags
|
||||
invalidRoutingTargets
|
||||
;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
pkgs,
|
||||
singBoxCfg,
|
||||
proxyCfg,
|
||||
sshProxyCfg,
|
||||
pureXrayEnabled,
|
||||
hybridEnabled,
|
||||
collapseNamedOutbounds,
|
||||
@@ -19,6 +20,8 @@
|
||||
}:
|
||||
|
||||
let
|
||||
sshProxyTag = "ssh-proxy";
|
||||
|
||||
rawOutboundJson =
|
||||
ob: tag: routingMark:
|
||||
let
|
||||
@@ -163,6 +166,47 @@ let
|
||||
fi
|
||||
'';
|
||||
|
||||
mkSshProxyOutboundBlock =
|
||||
routingMark:
|
||||
let
|
||||
singBoxOutbound = {
|
||||
type = "socks";
|
||||
tag = sshProxyTag;
|
||||
server = sshProxyCfg.listenAddress;
|
||||
server_port = sshProxyCfg.listenPort;
|
||||
version = "5";
|
||||
}
|
||||
// lib.optionalAttrs (routingMark != null) {
|
||||
routing_mark = routingMark;
|
||||
};
|
||||
xrayOutbound = {
|
||||
protocol = "socks";
|
||||
tag = sshProxyTag;
|
||||
settings = {
|
||||
address = sshProxyCfg.listenAddress;
|
||||
port = sshProxyCfg.listenPort;
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs (routingMark != null) {
|
||||
streamSettings.sockopt.mark = routingMark;
|
||||
};
|
||||
outbound = if pureXrayEnabled then xrayOutbound else singBoxOutbound;
|
||||
outboundJson = builtins.toJSON outbound;
|
||||
jsonFile = pkgs.writeText "proxy-suite-ob-ssh-proxy-${backend}.json" outboundJson;
|
||||
in
|
||||
if hybridEnabled then
|
||||
''
|
||||
# outbound: ${sshProxyTag} (SSH SOCKS5)
|
||||
OB_JSON=$(cat "${jsonFile}")
|
||||
_proxy_suite_add_sing_box_ob "$OB_JSON"
|
||||
''
|
||||
else
|
||||
''
|
||||
# outbound: ${sshProxyTag} (SSH SOCKS5)
|
||||
OB_JSON=$(cat "${jsonFile}")
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson ob "$OB_JSON" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
|
||||
mkBackendOutboundBlock = if hybridEnabled then mkHybridOutboundBlock else mkOutboundBlock;
|
||||
|
||||
requireOutboundsBlock = ''
|
||||
@@ -185,6 +229,8 @@ let
|
||||
sub: mkSubscriptionBlock sub routingMark
|
||||
) proxyCfg.subscriptions;
|
||||
|
||||
sshProxyBlock = lib.optionalString sshProxyCfg.asOutbound (mkSshProxyOutboundBlock routingMark);
|
||||
|
||||
wrapperBlock =
|
||||
if pureXrayEnabled && selectionMode == "urltest" then
|
||||
''
|
||||
@@ -194,13 +240,15 @@ let
|
||||
fi
|
||||
''
|
||||
else if collapseNamedOutbounds then
|
||||
lib.optionalString (proxyCfg.outbounds == [ ] && proxyCfg.subscriptions != [ ]) ''
|
||||
FIRST_TAG=$(${jq} -r 'if length > 0 then .[0].tag else "" end' <<< "$OUTBOUNDS_JSON")
|
||||
if [ -n "$FIRST_TAG" ]; then
|
||||
OUTBOUNDS_JSON=$(${jq} --arg t "$FIRST_TAG" \
|
||||
'map(if .tag == $t then .tag = "proxy" else . end)' <<< "$OUTBOUNDS_JSON")
|
||||
fi
|
||||
''
|
||||
lib.optionalString
|
||||
(proxyCfg.outbounds == [ ] && (proxyCfg.subscriptions != [ ] || sshProxyCfg.asOutbound))
|
||||
''
|
||||
FIRST_TAG=$(${jq} -r 'if length > 0 then .[0].tag else "" end' <<< "$OUTBOUNDS_JSON")
|
||||
if [ -n "$FIRST_TAG" ]; then
|
||||
OUTBOUNDS_JSON=$(${jq} --arg t "$FIRST_TAG" \
|
||||
'map(if .tag == $t then .tag = "proxy" else . end)' <<< "$OUTBOUNDS_JSON")
|
||||
fi
|
||||
''
|
||||
else if selectionMode == "selector" then
|
||||
''
|
||||
TAGS=$(${jq} '[.[].tag]' <<< "$OUTBOUNDS_JSON")
|
||||
@@ -223,7 +271,7 @@ let
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
in
|
||||
outboundBlocks + subscriptionBlocks + requireOutboundsBlock + wrapperBlock;
|
||||
outboundBlocks + subscriptionBlocks + sshProxyBlock + requireOutboundsBlock + wrapperBlock;
|
||||
in
|
||||
{
|
||||
inherit mkOutboundScript rawOutboundJson;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
pkgs,
|
||||
singBoxCfg,
|
||||
proxyCfg,
|
||||
sshProxyCfg,
|
||||
xrayEnabled,
|
||||
hybridEnabled,
|
||||
pureXrayEnabled,
|
||||
@@ -88,6 +89,7 @@ let
|
||||
pkgs
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
sshProxyCfg
|
||||
pureXrayEnabled
|
||||
hybridEnabled
|
||||
collapseNamedOutbounds
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
perAppRoutingTproxy,
|
||||
perAppZapretEnabled,
|
||||
hasSubscriptions,
|
||||
sshProxyOutboundEnabled,
|
||||
scripts,
|
||||
perAppRouting,
|
||||
routingScripts,
|
||||
@@ -60,7 +61,10 @@ let
|
||||
name = serviceNames.socks;
|
||||
value = mkRestartingService {
|
||||
description = "${backendDescription} proxy client (SOCKS + TProxy-ready)";
|
||||
after = [ "network-online.target" ];
|
||||
after = [
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional sshProxyOutboundEnabled "proxy-suite-ssh-proxy.service";
|
||||
wants = [ "network-online.target" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
execStart = scripts.startSocks;
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# Native OpenSSH dynamic SOCKS5 proxy service.
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
cfg,
|
||||
}:
|
||||
|
||||
let
|
||||
s = cfg.sshProxy;
|
||||
destination = if s.user == null then s.host else "${s.user}@${s.host}";
|
||||
extraArgs = lib.concatMapStrings (arg: " args+=(${lib.escapeShellArg arg})\n") s.extraArgs;
|
||||
startScript = pkgs.writeShellScript "proxy-suite-ssh-proxy-start" ''
|
||||
set -euo pipefail
|
||||
|
||||
args=(
|
||||
-N
|
||||
-o BatchMode=yes
|
||||
-o ExitOnForwardFailure=yes
|
||||
-o StrictHostKeyChecking=${lib.escapeShellArg s.strictHostKeyChecking}
|
||||
-D ${lib.escapeShellArg "${s.listenAddress}:${toString s.listenPort}"}
|
||||
)
|
||||
${lib.optionalString (s.sshPort != 22) "args+=(-p ${toString s.sshPort})"}
|
||||
${lib.optionalString (s.identityFile != null) "args+=(-i ${lib.escapeShellArg s.identityFile})"}
|
||||
${lib.optionalString (
|
||||
s.knownHostsFile != null
|
||||
) "args+=(-o ${lib.escapeShellArg "UserKnownHostsFile=${s.knownHostsFile}"})"}
|
||||
${extraArgs}
|
||||
exec ${pkgs.openssh}/bin/ssh "''${args[@]}" ${lib.escapeShellArg destination}
|
||||
'';
|
||||
in
|
||||
{
|
||||
systemd.services.proxy-suite-ssh-proxy = {
|
||||
description = "SSH dynamic SOCKS5 proxy";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
serviceConfig = {
|
||||
ExecStart = startScript;
|
||||
Restart = "on-failure";
|
||||
RestartSec = 5;
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user