proxychains integration, proxy-ctl wrap

This commit is contained in:
FUFSoB committed 2026-04-14 14:49:32 +05:00
1 parent e6c7b1a817
commit 181f3355c1
3 files changed
+413

No files matched your search

+108
View File
@@ -284,6 +284,37 @@ let
};
};
appRoutingProfileType = types.submodule {
options = {
name = mkOption {
type = types.strMatching "^[a-z0-9][a-z0-9-]*$";
description = ''
Profile name used by `proxy-ctl wrap <name> -- <command>`.
Must be unique within appRouting.profiles.
'';
example = "steam-browser";
};
route = mkOption {
type = types.enum [
"direct"
"proxychains"
];
default = "proxychains";
description = ''
Per-app route backend used by proxy-ctl wrap.
- "direct": run the command unchanged.
- "proxychains": run the command through proxychains-ng using the
local proxy-suite mixed SOCKS endpoint.
Additional route backends may be added in the future.
'';
example = "proxychains";
};
};
};
in
{
options.services.proxy-suite = {
@@ -770,6 +801,83 @@ in
};
};
appRouting = {
enable = mkEnableOption "per-app routing helpers via proxy-ctl wrap";
createDefaultProfiles = mkOption {
type = types.bool;
default = false;
description = ''
Whether to automatically add curated appRouting profiles.
This is opt-in. Generated defaults are appended only when no
user-defined profile with the same name already exists.
Current curated defaults:
- `proxychains`: route = "proxychains"
This makes `proxy-ctl wrap proxychains -- <command>` available
without defining the profile manually.
'';
example = true;
};
profiles = mkOption {
type = types.listOf appRoutingProfileType;
default = [ ];
description = ''
Named per-app route profiles consumed by `proxy-ctl wrap`.
This initial implementation supports:
- "direct" for running a command unchanged
- "proxychains" for TCP apps that can use an LD_PRELOAD wrapper
instead of global TUN or TProxy interception
proxychains-based wrapping depends on the local proxy-suite mixed
proxy listener provided by sing-box.
When createDefaultProfiles = true, curated defaults are added on top
of this list unless a user-defined profile already uses the same
name.
'';
example = [
{
name = "steam-browser";
route = "proxychains";
}
{
name = "native-direct";
route = "direct";
}
];
};
proxychains = {
enable = mkEnableOption "proxychains-backed appRouting profiles";
quiet = mkOption {
type = types.bool;
default = true;
description = ''
Whether generated proxychains wrappers should suppress their normal
startup chatter. This maps to proxychains-ng quiet_mode.
'';
example = true;
};
proxyDns = mkOption {
type = types.bool;
default = true;
description = ''
Whether generated proxychains wrappers should resolve DNS through
the proxy instead of the local resolver. This maps to proxychains-ng
proxy_dns.
'';
example = true;
};
};
};
zapret = {
enable = mkEnableOption "zapret DPI bypass";
+113
View File
@@ -14,6 +14,7 @@
let
sb = cfg.singBox;
t = cfg.tgWsProxy;
ar = cfg.appRouting;
builtinTags = [
"proxy"
"direct"
@@ -29,6 +30,7 @@ let
jq = "${pkgs.jq}/bin/jq";
python3 = "${pkgs.python3}/bin/python3";
singBox = "${pkgs.sing-box}/bin/sing-box";
proxychains4 = "${pkgs.proxychains-ng}/bin/proxychains4";
fetchSubscriptionPy = ../../scripts/fetch-subscription.py;
@@ -236,18 +238,49 @@ let
# Subscription tags embedded at build time for proxy-ctl.
subscriptionTagsList = lib.concatStringsSep " " (map (sub: lib.escapeShellArg sub.tag) sb.subscriptions);
hasSubscriptions = sb.subscriptions != [ ];
appRoutingProfileNames = map (profile: profile.name) ar.profiles;
defaultAppRoutingProfiles = lib.optionals ar.createDefaultProfiles [
{
name = "proxychains";
route = "proxychains";
}
];
effectiveAppRoutingProfiles =
ar.profiles
++ builtins.filter (profile: !(builtins.elem profile.name appRoutingProfileNames)) defaultAppRoutingProfiles;
effectiveAppRoutingProfileNames = map (profile: profile.name) effectiveAppRoutingProfiles;
appRoutingProfilesFile = pkgs.writeText "proxy-suite-app-routing-profiles.json" (
builtins.toJSON effectiveAppRoutingProfiles
);
proxychainsConfigFile = pkgs.writeText "proxy-suite-proxychains.conf" ''
strict_chain
${lib.optionalString ar.proxychains.quiet "quiet_mode"}
${lib.optionalString ar.proxychains.proxyDns "proxy_dns"}
tcp_read_time_out 15000
tcp_connect_time_out 8000
[ProxyList]
socks5 ${sb.listenAddress} ${toString sb.port}
'';
proxychainsQuietArg = lib.optionalString ar.proxychains.quiet "-q ";
hasProxychainsProfiles = builtins.any (profile: profile.route == "proxychains") effectiveAppRoutingProfiles;
proxyCtl = pkgs.writeShellApplication {
name = "proxy-ctl";
runtimeInputs = with pkgs; [
curl
jq
proxychains-ng
];
text = ''
# Embedded at build time from module config
CLASH_API="${clashApi}"
SELECTION="${selection}"
SUB_TAGS=(${subscriptionTagsList})
APP_ROUTING_ENABLED="${if ar.enable then "1" else "0"}"
APP_ROUTING_PROXYCHAINS_ENABLED="${if ar.proxychains.enable then "1" else "0"}"
APP_ROUTING_PROFILES_FILE="${appRoutingProfilesFile}"
PROXYCHAINS_CONFIG="${proxychainsConfigFile}"
ALL_SERVICES=(
proxy-suite-socks
@@ -269,6 +302,8 @@ let
echo " logs [service] follow service logs (default: proxy-suite-socks)"
echo " outbounds list outbounds and current selection"
echo " select <tag> switch to a specific outbound (selector mode)"
echo " apps list configured per-app routing profiles"
echo " wrap <profile> -- <cmd> run a command via an appRouting profile"
echo " subscription list show subscriptions, cache age, and proxy count"
echo " subscription update force-refresh all subscription caches and restart"
exit 1
@@ -284,6 +319,18 @@ let
printf " %-44s %s\n" "$svc" "$state"
}
_ensure_app_routing() {
if [ "$APP_ROUTING_ENABLED" != "1" ]; then
echo "appRouting is not enabled in services.proxy-suite.appRouting."
exit 1
fi
}
_profile_route() {
local profile="$1"
${jq} -r --arg name "$profile" '.[] | select(.name == $name) | .route' "$APP_ROUTING_PROFILES_FILE"
}
cmd="''${1:-status}"
shift || true
@@ -357,6 +404,56 @@ let
fi
;;
apps)
_ensure_app_routing
if [ "$(${jq} 'length' "$APP_ROUTING_PROFILES_FILE")" -eq 0 ]; then
echo "No appRouting profiles configured."
else
printf " %-24s %s\n" "PROFILE" "ROUTE"
${jq} -r '.[] | " " + (.name | tostring) + "\t" + (.route | tostring)' \
"$APP_ROUTING_PROFILES_FILE" \
| while IFS=$'\t' read -r profile route; do
printf "%-26s %s\n" "$profile" "$route"
done
fi
;;
wrap)
profile="''${1:?Usage: proxy-ctl wrap <profile> -- <command> [args...]}"
shift || true
if [ "''${1:-}" = "--" ]; then
shift
fi
if [ "$#" -eq 0 ]; then
echo "Usage: proxy-ctl wrap <profile> -- <command> [args...]"
exit 1
fi
_ensure_app_routing
route="$(_profile_route "$profile")"
if [ -z "$route" ] || [ "$route" = "null" ]; then
echo "Unknown appRouting profile: $profile"
exit 1
fi
case "$route" in
direct)
exec "$@"
;;
proxychains)
if [ "$APP_ROUTING_PROXYCHAINS_ENABLED" != "1" ]; then
echo "Profile '$profile' uses route=proxychains, but appRouting.proxychains.enable is false."
exit 1
fi
exec ${proxychains4} ${proxychainsQuietArg}-f "$PROXYCHAINS_CONFIG" "$@"
;;
*)
echo "Route backend '$route' is not implemented in this build."
exit 1
;;
esac
;;
subscription)
subcmd="''${1:-list}"
shift || true
@@ -424,6 +521,22 @@ in
assertion = !(sb.tproxy.enable && sb.tproxy.autostart && sb.tun.enable && sb.tun.autostart);
message = "proxy-suite: singBox.tproxy.autostart and singBox.tun.autostart cannot both be enabled at the same time";
}
{
assertion = ar.enable || ar.profiles == [ ];
message = "proxy-suite: appRouting.profiles requires appRouting.enable = true";
}
{
assertion = !ar.proxychains.enable || ar.enable;
message = "proxy-suite: appRouting.proxychains.enable requires appRouting.enable = true";
}
{
assertion = builtins.length appRoutingProfileNames == builtins.length (lib.unique appRoutingProfileNames);
message = "proxy-suite: appRouting profile names must be unique";
}
{
assertion = !hasProxychainsProfiles || ar.proxychains.enable;
message = "proxy-suite: route=proxychains in appRouting.profiles requires appRouting.proxychains.enable = true";
}
{
assertion =
!t.enable
+192
View File
@@ -83,6 +83,21 @@ let
builtins.any (
pkg: builtins.match pattern (builtins.unsafeDiscardStringContext (toString pkg)) != null
) packages;
packageByPattern =
packages: pattern:
builtins.head (
builtins.filter (
pkg: builtins.match pattern (builtins.unsafeDiscardStringContext (toString pkg)) != null
) packages
);
lineByPrefix =
text: prefix:
builtins.head (
builtins.filter (line: pkgs.lib.hasPrefix prefix line) (pkgs.lib.splitString "\n" text)
);
quotedValueByPrefix =
text: prefix:
pkgs.lib.removeSuffix "\"" (pkgs.lib.removePrefix prefix (lineByPrefix text prefix));
baseModule = {
system.stateVersion = "26.05";
@@ -660,6 +675,116 @@ let
}
];
appRoutingProxychainsFixture = evalProxySuite [
baseModule
{
services.proxy-suite.appRouting = {
enable = true;
proxychains.enable = true;
profiles = [
{
name = "steam-browser";
route = "proxychains";
}
{
name = "native-direct";
route = "direct";
}
];
};
}
];
appRoutingProxychainsScript =
builtins.readFile (
"${packageByPattern appRoutingProxychainsFixture.config.environment.systemPackages
".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl"
);
appRoutingProxychainsConfig =
builtins.readFile (
quotedValueByPrefix appRoutingProxychainsScript "PROXYCHAINS_CONFIG=\""
);
appRoutingProxychainsProfiles =
builtins.fromJSON (
builtins.readFile (
quotedValueByPrefix appRoutingProxychainsScript "APP_ROUTING_PROFILES_FILE=\""
)
);
appRoutingDefaultProfilesFixture = evalProxySuite [
baseModule
{
services.proxy-suite.appRouting = {
enable = true;
createDefaultProfiles = true;
proxychains.enable = true;
};
}
];
appRoutingDefaultProfilesScript =
builtins.readFile (
"${packageByPattern appRoutingDefaultProfilesFixture.config.environment.systemPackages
".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl"
);
appRoutingDefaultProfiles =
builtins.fromJSON (
builtins.readFile (
quotedValueByPrefix appRoutingDefaultProfilesScript "APP_ROUTING_PROFILES_FILE=\""
)
);
appRoutingDefaultProfilesWithoutProxychainsEnable = forceEval (
(evalProxySuite [
baseModule
{
services.proxy-suite.appRouting = {
enable = true;
createDefaultProfiles = true;
};
}
]).config.system.build.toplevel.drvPath
);
duplicateAppRoutingProfiles = forceEval (
(evalProxySuite [
baseModule
{
services.proxy-suite.appRouting = {
enable = true;
profiles = [
{ name = "dup"; }
{ name = "dup"; }
];
};
}
]).config.system.build.toplevel.drvPath
);
appRoutingProfilesWithoutEnable = forceEval (
(evalProxySuite [
baseModule
{
services.proxy-suite.appRouting.profiles = [ { name = "oops"; } ];
}
]).config.system.build.toplevel.drvPath
);
appRoutingProxychainsWithoutEnable = forceEval (
(evalProxySuite [
baseModule
{
services.proxy-suite.appRouting = {
enable = true;
profiles = [
{
name = "steam-browser";
route = "proxychains";
}
];
};
}
]).config.system.build.toplevel.drvPath
);
# Validation failures
subscriptionBothSources = forceEval (
(evalProxySuite [
@@ -1088,6 +1213,73 @@ let
true
)
# -- appRouting: proxychains/direct profile config is accepted --
(
assert appRoutingProxychainsFixture.config.services.proxy-suite.appRouting.enable;
true
)
(
assert builtins.length appRoutingProxychainsFixture.config.services.proxy-suite.appRouting.profiles == 2;
true
)
(
assert builtins.length appRoutingProxychainsProfiles == 2;
true
)
# -- appRouting: createDefaultProfiles injects curated proxychains profile --
(
assert builtins.length appRoutingDefaultProfiles == 1;
assert (builtins.head appRoutingDefaultProfiles).name == "proxychains";
assert (builtins.head appRoutingDefaultProfiles).route == "proxychains";
true
)
# -- appRouting: proxy-ctl script embeds wrap/apps commands --
(
assert pkgs.lib.hasInfix "wrap <profile> -- <cmd>" appRoutingProxychainsScript;
assert pkgs.lib.hasInfix "apps" appRoutingProxychainsScript;
true
)
# -- appRouting: proxychains config is quiet and points at local SOCKS listener --
(
assert pkgs.lib.hasInfix "quiet_mode" appRoutingProxychainsConfig;
assert pkgs.lib.hasInfix "proxy_dns" appRoutingProxychainsConfig;
assert pkgs.lib.hasInfix "socks5 127.0.0.1 1080" appRoutingProxychainsConfig;
true
)
# -- appRouting: generated proxy-ctl script dispatches through proxychains4 --
(
assert pkgs.lib.hasInfix "proxychains4 -q -f \"$PROXYCHAINS_CONFIG\" \"$@\"" appRoutingProxychainsScript;
true
)
# -- appRouting: route=proxychains requires proxychains.enable --
(
assert appRoutingProxychainsWithoutEnable.success == false;
true
)
# -- appRouting: profiles require appRouting.enable --
(
assert appRoutingProfilesWithoutEnable.success == false;
true
)
# -- appRouting: default proxychains profile still requires proxychains.enable --
(
assert appRoutingDefaultProfilesWithoutProxychainsEnable.success == false;
true
)
# -- appRouting: profile names must be unique --
(
assert duplicateAppRoutingProfiles.success == false;
true
)
# -- subscription: no outbounds + no subscriptions fails --
(
assert noOutboundsNoSubscriptions.success == false;