fix: behavior and docs fixes

This commit is contained in:
FUFSoB committed 2026-09-26 18:35:32 +05:00
1 parent e4458dcae8
commit 2255e1f9f0
42 files changed
+320 -161

No files matched your search

+23 -31
View File
@@ -94,38 +94,30 @@ in
inherit (assembly) context;
})
(lib.mkIf
(
cfg.zapret.engine == "zapret-discord-youtube"
&& (cfg.zapret.enable || cfg.perAppRouting.zapret.enable)
)
(
import ./zapret.nix {
inherit
lib
pkgs
cfg
zapret
;
inherit (nftr) perAppZapretRulesFile nft;
}
)
)
(lib.mkIf (cfg.zapret.engine == "zapret-discord-youtube" && cfg.zapret.enable) (
import ./zapret.nix {
inherit
lib
pkgs
cfg
zapret
;
inherit (nftr) perAppZapretRulesFile nft;
}
))
(lib.mkIf (cfg.zapret.engine == "zapret2" && (cfg.zapret.enable || cfg.perAppRouting.zapret.enable))
(
import ./zapret2.nix {
inherit
lib
pkgs
cfg
packages
;
inherit (nftr) perAppZapretRulesFile nft;
zapret2Sources = { inherit nfqws2-keenetic z2k; };
}
)
)
(lib.mkIf (cfg.zapret.engine == "zapret2" && cfg.zapret.enable) (
import ./zapret2.nix {
inherit
lib
pkgs
cfg
packages
;
inherit (nftr) perAppZapretRulesFile nft;
zapret2Sources = { inherit nfqws2-keenetic z2k; };
}
))
(lib.mkIf cfg.tgWsProxy.enable (
(import ./tg-ws-proxy.nix {
+6 -4
View File
@@ -321,10 +321,10 @@ let
# Always on with sing-box: `proxy-ctl proxy outbounds test` needs it in every selection mode.
clashApiEnabled = singBoxEnabled;
perAppZapretEnabled = perAppZapretCfg.enable;
# The system-wide instance; per-app zapret runs its own.
zapretGlobalEnabled = zapretCfg.enable && zapretCfg.global.enable;
zapretCutoffEnabled =
zapretEngine == "zapret2"
&& (zapretCfg.enable || perAppZapretEnabled)
&& zapretCfg.zapret2.cutoff.enable;
zapretEngine == "zapret2" && zapretCfg.enable && zapretCfg.zapret2.cutoff.enable;
# The cut-off networks no whitelisted name fixes go through the proxy outbound.
zapretCutoffProxyFallback =
zapretCutoffEnabled && zapretCfg.zapret2.cutoff.proxyFallback && hasAvailableOutbounds;
@@ -464,7 +464,8 @@ let
perAppTun = 18535;
};
xraySidecarBasePorts = {
# One loopback VLESS inbound per stack's XRay sidecar, however many outbounds it carries.
xraySidecarPorts = {
socks = 33080;
tun = 33180;
perAppTun = 33280;
@@ -509,6 +510,7 @@ in
zapretEngine
perAppZapretCfg
perAppZapretEnabled
zapretGlobalEnabled
zapretCutoffEnabled
zapretCutoffProxyFallback
userControlCfg
+7 -1
View File
@@ -7,7 +7,13 @@ let
in
{
options.services.proxy-suite.zapret = {
enable = mkEnableOption "zapret DPI bypass";
enable = mkEnableOption "zapret DPI bypass. Needed by `perAppRouting.zapret` too";
global.enable = bool true ''
Run zapret system-wide. Turn off to keep zapret to apps started with
`proxy-ctl apps run zapret` (`perAppRouting.zapret`); `directSync`, `cidrExemption` and
zapret2's learned sites then do nothing.
'';
engine = mkOption {
type = types.enum [
+5 -3
View File
@@ -25,9 +25,11 @@ let
subtractItems = items: exclusions: builtins.filter (item: !(builtins.elem item exclusions)) items;
syncZapretDirectDomains = zapretCfg.enable && zapretCfg.directSync.enable;
syncZapretDirectUpstreamIps = zapretCfg.enable && zapretCfg.directSync.upstreamIps;
syncZapretDirectUserIps = zapretCfg.enable && zapretCfg.directSync.userIps;
# Only the system-wide instance handles traffic the proxy would send direct.
globalEnabled = zapretCfg.enable && zapretCfg.global.enable;
syncZapretDirectDomains = globalEnabled && zapretCfg.directSync.enable;
syncZapretDirectUpstreamIps = globalEnabled && zapretCfg.directSync.upstreamIps;
syncZapretDirectUserIps = globalEnabled && zapretCfg.directSync.userIps;
syncZapretDirectAnyIps = syncZapretDirectUpstreamIps || syncZapretDirectUserIps;
zapretDefaultDomainFiles = [
+8 -2
View File
@@ -69,6 +69,7 @@ let
reservedLoopbackPorts =
builtins.attrValues constants.xrayDnsBridgePorts
++ [ constants.outboundTestPort ]
++ lib.optionals hybridEnabled (builtins.attrValues constants.xraySidecarPorts)
++ lib.optionals derived.warpOutboundEnabled [
derived.warpCfg.tunnelPort
derived.warpCfg.directPort
@@ -334,6 +335,9 @@ let
(requires perAppZapretCfg.enable perAppRoutingCfg.enable "perAppRouting.zapret.enable"
"perAppRouting.enable"
)
(mkAssertion (!perAppZapretCfg.enable || zapretCfg.enable) ''
proxy-suite: perAppRouting.zapret.enable requires zapret.enable = true. For per-app zapret
alone, also set zapret.global.enable = false.'')
(requires hasZapretProfiles perAppZapretCfg.enable "route=zapret in perAppRouting.profiles"
"perAppRouting.zapret.enable"
)
@@ -455,9 +459,10 @@ let
|| !lib.any (port: builtins.elem port derived.proxyInboundPorts) (
map (listener: listener.internalPort) derived.proxyInboundsAwg
++ builtins.attrValues derived.constants.xrayDnsBridgePorts
++ lib.optionals hybridEnabled (builtins.attrValues derived.constants.xraySidecarPorts)
)
)
"proxy-suite: a proxyInbounds listener port collides with a port proxy-suite uses internally (the AmneziaWG listeners' loopback inbounds from 18700, or 18533-18535)"
"proxy-suite: a proxyInbounds listener port collides with a port proxy-suite uses internally (the AmneziaWG listeners' loopback inbounds from 18700, 18533-18535, or the hybrid XRay sidecar's 33080, 33180 and 33280)"
)
# The prober opens one loopback listener per exit from probeBasePort; the WARP
# and AmneziaWG tunnels open two each from their own bases. They all bind
@@ -858,7 +863,8 @@ let
(distinct perAppTunWithTproxy perAppRoutingTun.routeTable globalTproxy.routeTable
"perAppRouting.tun.routeTable must differ from proxy.tproxy.routeTable when global TProxy is enabled"
)
(distinct (perAppZapretCfg.enable && zapretCfg.enable) perAppZapretCfg.qnum globalZapretQnum
(distinct (perAppZapretCfg.enable && derived.zapretGlobalEnabled) perAppZapretCfg.qnum
globalZapretQnum
"perAppRouting.zapret.qnum must differ from the global zapret instance's NFQUEUE ${toString globalZapretQnum}"
)
(distinct perAppRoutingTproxy.enable perAppRoutingTproxy.fwmark globalTproxy.fwmark
+3 -1
View File
@@ -14,6 +14,7 @@ let
perAppRoutingTproxy
perAppZapretCfg
zapretEngine
zapretGlobalEnabled
zapretCutoffEnabled
constants
selectionMode
@@ -100,7 +101,8 @@ in
INBOUNDS_SUBS_FILE = proxyInboundsSubscriptionsFile;
INBOUNDS_SUB_BASE_URL =
if proxyInboundsSubscriptionsBaseUrl == null then "" else proxyInboundsSubscriptionsBaseUrl;
ZAPRET_AUTO_ENABLED = flag (zapretEngine == "zapret2");
# 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;
ZAPRET_CUTOFF_ENABLED = flag zapretCutoffEnabled;
OUTBOUND_INVENTORY_FILE = constants.outboundInventoryFile;
@@ -15,7 +15,7 @@ let
inherit (constants) runtimeDir;
perAppRoutingProfileNames = map (profile: profile.name) perAppRoutingCfg.profiles;
defaultPerAppRoutingProfiles = lib.optionals perAppRoutingCfg.createDefaultProfiles (
[
lib.optionals perAppRoutingCfg.proxychains.enable [
{
name = "proxychains";
route = "proxychains";
@@ -9,14 +9,18 @@ let
;
in
routingMark: xraySidecarBasePort: xrayDnsBridgePort:
routingMark: xraySidecarPort: xrayDnsBridgePort:
lib.optionalString hybridEnabled ''
XRAY_OUTBOUNDS_JSON='[]'
XRAY_INBOUNDS_JSON='[]'
XRAY_CLIENTS_JSON='[]'
XRAY_ROUTE_RULES_JSON='[]'
XRAY_SIDECAR_NEXT_PORT=${toString xraySidecarBasePort}
XRAY_SIDECAR_PORT=${toString xraySidecarPort}
XRAY_SIDECAR_DNS_PORT=${toString xrayDnsBridgePort}
# Client ids: this run's random prefix plus a counter, so every sidecar outbound has its
# own and no other local user can guess one.
XRAY_SIDECAR_SECRET=$(cat /proc/sys/kernel/random/uuid)
XRAY_SIDECAR_NEXT_ID=0
_proxy_suite_add_sing_box_ob() {
local ob="$1"
@@ -24,37 +28,42 @@ lib.optionalString hybridEnabled ''
}
# $1 a JSON array of XRay outbounds, each tagged. One jq pass for the lot: each gets a
# loopback SOCKS inbound on the sidecar and stands in sing-box as a hop to it.
# client on the sidecar's one loopback VLESS inbound, routed to it by its email, and
# stands in sing-box as a hop to it. VLESS rather than SOCKS: UDP rides the stream
# (XUDP) with the client id, where SOCKS UDP datagrams carry no user to route by.
_proxy_suite_add_xray_sidecar_obs() {
local batch
batch=$(${jq} -c \
--argjson base "$XRAY_SIDECAR_NEXT_PORT" \
--argjson next "$XRAY_SIDECAR_NEXT_ID" \
--arg secret "$XRAY_SIDECAR_SECRET" \
--argjson port "$XRAY_SIDECAR_PORT" \
--argjson mark ${
if xraySidecarRoutingMark == null then "null" else toString xraySidecarRoutingMark
} '
def hex12: [range(11; -1; -1) as $i | (. / pow(16; $i) | floor) % 16 | "0123456789abcdef"[.:. + 1]] | add;
to_entries | map(
($base + .key) as $port
| ("proxy-suite-xray-" + ($port | tostring)) as $auth
($secret[:24] + ($next + .key | hex12)) as $id
| .value.tag as $tag
| {outbound: (.value
${lib.optionalString (
xraySidecarRoutingMark != null
) "| .streamSettings.sockopt.mark = $mark"}
| .streamSettings.sockopt.domainStrategy = (.streamSettings.sockopt.domainStrategy // "UseIP")),
inbound: {tag: ($tag + "-inbound"), listen: "127.0.0.1", port: $port, protocol: "socks",
settings: {auth: "password", udp: true, accounts: [{user: $auth, pass: $auth}]}},
rule: {type: "field", inboundTag: [$tag + "-inbound"], outboundTag: $tag},
hop: ({type: "socks", tag: $tag, server: "127.0.0.1", server_port: $port, version: "5",
username: $auth, password: $auth}${
client: {id: $id, email: $tag},
rule: {type: "field", inboundTag: ["sidecar-in"], user: [$tag], outboundTag: $tag},
hop: ({type: "vless", tag: $tag, server: "127.0.0.1", server_port: $port, uuid: $id,
packet_encoding: "xudp"}${
lib.optionalString (routingMark != null) " + {routing_mark: ${toString routingMark}}"
})})
| {outbounds: map(.outbound), inbounds: map(.inbound), rules: map(.rule), hops: map(.hop)}
| {outbounds: map(.outbound), clients: map(.client), rules: map(.rule), hops: map(.hop)}
' <<< "$1")
XRAY_SIDECAR_NEXT_PORT=$((XRAY_SIDECAR_NEXT_PORT + $(${jq} '.outbounds | length' <<< "$batch")))
XRAY_OUTBOUNDS_JSON=$(${jq} -c --argjson b "$batch" '. + $b.outbounds' <<< "$XRAY_OUTBOUNDS_JSON")
XRAY_INBOUNDS_JSON=$(${jq} -c --argjson b "$batch" '. + $b.inbounds' <<< "$XRAY_INBOUNDS_JSON")
XRAY_ROUTE_RULES_JSON=$(${jq} -c --argjson b "$batch" '. + $b.rules' <<< "$XRAY_ROUTE_RULES_JSON")
OUTBOUNDS_JSON=$(${jq} -c --argjson b "$batch" '. + $b.hops' <<< "$OUTBOUNDS_JSON")
XRAY_SIDECAR_NEXT_ID=$((XRAY_SIDECAR_NEXT_ID + $(${jq} '.outbounds | length' <<< "$batch")))
# Through a file, not --argjson: a big subscription's batch outgrows the kernel's
# 128 KiB limit on one argument.
XRAY_OUTBOUNDS_JSON=$(${jq} -c --slurpfile b <(printf '%s' "$batch") '. + $b[0].outbounds' <<< "$XRAY_OUTBOUNDS_JSON")
XRAY_CLIENTS_JSON=$(${jq} -c --slurpfile b <(printf '%s' "$batch") '. + $b[0].clients' <<< "$XRAY_CLIENTS_JSON")
XRAY_ROUTE_RULES_JSON=$(${jq} -c --slurpfile b <(printf '%s' "$batch") '. + $b[0].rules' <<< "$XRAY_ROUTE_RULES_JSON")
OUTBOUNDS_JSON=$(${jq} -c --slurpfile b <(printf '%s' "$batch") '. + $b[0].hops' <<< "$OUTBOUNDS_JSON")
}
# $1 one XRay outbound, $2 its tag.
@@ -69,9 +78,10 @@ lib.optionalString hybridEnabled ''
${jq} -n \
--arg loglevel "$XRAY_LOGLEVEL" \
--argjson dns_port "$XRAY_SIDECAR_DNS_PORT" \
--argjson inbounds "$XRAY_INBOUNDS_JSON" \
--argjson outbounds "$XRAY_OUTBOUNDS_JSON" \
--argjson route_rules "$XRAY_ROUTE_RULES_JSON" \
--argjson port "$XRAY_SIDECAR_PORT" \
--slurpfile clients <(printf '%s' "$XRAY_CLIENTS_JSON") \
--slurpfile outbounds <(printf '%s' "$XRAY_OUTBOUNDS_JSON") \
--slurpfile route_rules <(printf '%s' "$XRAY_ROUTE_RULES_JSON") \
'
{
log: {
@@ -88,11 +98,19 @@ lib.optionalString hybridEnabled ''
}
]
},
inbounds: $inbounds,
outbounds: ($outbounds + [{protocol:"freedom",tag:"direct"}]),
inbounds: [
{
tag: "sidecar-in",
listen: "127.0.0.1",
port: $port,
protocol: "vless",
settings: {clients: $clients[0], decryption: "none"}
}
],
outbounds: ($outbounds[0] + [{protocol:"freedom",tag:"direct"}]),
routing: {
domainStrategy: "AsIs",
rules: ($route_rules + [{type:"field",ip:["127.0.0.1"],port:$dns_port,outboundTag:"direct"}])
rules: ($route_rules[0] + [{type:"field",ip:["127.0.0.1"],port:$dns_port,outboundTag:"direct"}])
}
}' > "$RUNTIME_DIR/xray-sidecar.json"
}
@@ -38,7 +38,7 @@ let
autoProxyStateDir
outboundTestPort
xrayDnsBridgePorts
xraySidecarBasePorts
xraySidecarPorts
;
autoProxyRender = import ../autoproxy-render.nix {
@@ -175,7 +175,7 @@ let
routingMark ? null,
enableLocalProxyAuth ? false,
xrayTunDnsRuntime ? false,
xraySidecarBasePort ? xraySidecarBasePorts.socks,
xraySidecarPort ? xraySidecarPorts.socks,
xrayDnsBridgePort ? xrayDnsBridgePorts.socks,
enableAutoProxy ? false,
enableOutboundTest ? false,
@@ -197,7 +197,7 @@ let
ROUTE_RULES_JSON='[]'
ROUTE_MODE_ACTIVE=false
CLEAR_DNS_RULES=false
${hybridRuntimeHelpersBlock routingMark xraySidecarBasePort xrayDnsBridgePort}
${hybridRuntimeHelpersBlock routingMark xraySidecarPort xrayDnsBridgePort}
${subscriptionCacheHelpersBlock}
${lib.optionalString enableLocalProxyAuth ''
LOCAL_PROXY_PASSWORD="$(cat "${localProxyAuthPasswordSource}")"
@@ -447,7 +447,7 @@ let
# TProxy takes the system resolver's own upstream queries too, so a proxy server's name
# has to resolve inside XRay, as under the TUN.
xrayTunDnsRuntime = pureXrayEnabled && globalTproxy.enable;
xraySidecarBasePort = xraySidecarBasePorts.socks;
xraySidecarPort = xraySidecarPorts.socks;
xrayDnsBridgePort = xrayDnsBridgePorts.socks;
# Only the socks unit: relayed traffic reaches it, and the prober needs one home.
enableAutoProxy = proxyCfg.autoProxy.enable && !pureXrayEnabled;
@@ -460,7 +460,7 @@ let
configFile = tunFile;
routingMark = if pureXrayEnabled then globalTproxy.proxyMark else null;
xrayTunDnsRuntime = pureXrayEnabled;
xraySidecarBasePort = xraySidecarBasePorts.tun;
xraySidecarPort = xraySidecarPorts.tun;
xrayDnsBridgePort = xrayDnsBridgePorts.tun;
# Pure XRay's TUN routes by ip rules instead: see xrayTunUpScript.
excludeServiceUserFromTun = !pureXrayEnabled;
@@ -471,7 +471,7 @@ let
configFile = perAppTunFile;
routingMark = if xrayEnabled || globalTproxy.enable then globalTproxy.proxyMark else null;
xrayTunDnsRuntime = pureXrayEnabled;
xraySidecarBasePort = xraySidecarBasePorts.perAppTun;
xraySidecarPort = xraySidecarPorts.perAppTun;
xrayDnsBridgePort = xrayDnsBridgePorts.perAppTun;
};
in
+4 -3
View File
@@ -14,6 +14,7 @@ let
zapretCfg = cfg.zapret;
perAppZapretCfg = cfg.perAppRouting.zapret;
inherit (import ./derived.nix { inherit lib cfg; }) zapretGlobalEnabled;
inherit
(import ./zapret/common.nix {
inherit
@@ -80,12 +81,12 @@ in
assertions = zapretPackages.assertions;
services.proxy-suite.internal.earlyPackages = (
lib.optionals zapretCfg.enable [ globalZapretPackage ]
lib.optionals zapretGlobalEnabled [ globalZapretPackage ]
++ lib.optionals perAppZapretCfg.enable [ perAppZapretPackage ]
);
services.proxy-suite.internal.services.proxy-suite-zapret =
lib.mkIf zapretCfg.enable
lib.mkIf zapretGlobalEnabled
(mkOneshotService {
description = "zapret DPI bypass";
after = [ "network-online.target" ];
@@ -119,7 +120,7 @@ in
});
services.proxy-suite.internal.services.proxy-suite-zapret-vm-exempt =
lib.mkIf (zapretCfg.enable && zapretCfg.cidrExemption.enable)
lib.mkIf (zapretGlobalEnabled && zapretCfg.cidrExemption.enable)
(
mkOneshotService {
description = "Exempt CIDRs from zapret NFQUEUE";
+6 -2
View File
@@ -16,7 +16,11 @@ let
zapretCfg = cfg.zapret;
perAppZapretCfg = cfg.perAppRouting.zapret;
cutoffCfg = zapretCfg.zapret2.cutoff;
inherit (import ./derived.nix { inherit lib cfg; }) constants userControlAllows;
inherit (import ./derived.nix { inherit lib cfg; })
constants
userControlAllows
zapretGlobalEnabled
;
inherit
(import ./zapret/common.nix {
inherit
@@ -117,7 +121,7 @@ in
lib.mkIf cutoffCfg.enable cutoff.timer;
services.proxy-suite.internal.services.proxy-suite-zapret =
lib.mkIf zapretCfg.enable
lib.mkIf zapretGlobalEnabled
(mkRestartingService {
description = "zapret2 DPI bypass";
after = [ "network-online.target" ];
+6 -2
View File
@@ -12,7 +12,11 @@
}:
let
inherit (import ../derived.nix { inherit lib cfg; }) constants userControlAllows;
inherit (import ../derived.nix { inherit lib cfg; })
constants
userControlAllows
zapretGlobalEnabled
;
z2k = zapret2Sources.z2k;
lists = "${z2k}/files/lists";
dir = constants.zapret2CutoffDir;
@@ -20,7 +24,7 @@ let
table = "proxy_suite_cutoff_probe";
# try-restart fails on a unit that is not installed, so only name the ones built.
restartUnits =
lib.optional cfg.zapret.enable "proxy-suite-zapret.service"
lib.optional zapretGlobalEnabled "proxy-suite-zapret.service"
++ lib.optional cfg.perAppRouting.zapret.enable "proxy-suite-per-app-zapret.service";
z2kDetect = pkgs.buildGoModule {