feat: select zapret2 strategies source
This commit is contained in:
14 files changed
+566
-208
No files matched your search
@@ -4,6 +4,7 @@
|
||||
pkgs,
|
||||
cfg,
|
||||
packages,
|
||||
zapret2Sources,
|
||||
}:
|
||||
|
||||
let
|
||||
@@ -19,6 +20,7 @@ let
|
||||
autoHostlistFile = "${stateDir}/zapret-hosts-auto.txt";
|
||||
userHostlistFile = "${stateDir}/zapret-hosts-user.txt";
|
||||
excludeHostlistFile = "${stateDir}/zapret-hosts-user-exclude.txt";
|
||||
circularStateDir = "${stateDir}/circular";
|
||||
|
||||
runtimeDeps = lib.attrValues {
|
||||
inherit (pkgs)
|
||||
@@ -38,9 +40,17 @@ let
|
||||
};
|
||||
|
||||
toHex = value: "0x" + lib.toHexString value;
|
||||
orSource = value: fallback: if value != null then value else fallback;
|
||||
withSpaces = lib.concatMapStrings (arg: " " + arg);
|
||||
|
||||
sources = import ./sources.nix {
|
||||
inherit lib pkgs;
|
||||
sources = zapret2Sources;
|
||||
};
|
||||
source = sources.${zapret2Cfg.strategySource};
|
||||
isZ2k = zapret2Cfg.strategySource == "z2k";
|
||||
|
||||
blobPath = file: if lib.hasPrefix "/" file then file else "${zapretBase}/files/fake/${file}";
|
||||
blobArgs = lib.mapAttrsToList (name: file: "--blob=${name}:@${blobPath file}") zapret2Cfg.blobs;
|
||||
|
||||
hostlistMarkers = [
|
||||
"<HOSTLIST>"
|
||||
@@ -52,29 +62,65 @@ let
|
||||
lib.optional (
|
||||
zapret2Cfg.domains != [ ]
|
||||
) "--hostlist-domains=${lib.concatStringsSep "," zapret2Cfg.domains}"
|
||||
++ lib.optional (
|
||||
zapret2Cfg.excludeDomains != [ ]
|
||||
) "--hostlist-exclude-domains=${lib.concatStringsSep "," zapret2Cfg.excludeDomains}";
|
||||
++ excludeDomainArgs;
|
||||
excludeDomainArgs = lib.optional (
|
||||
zapret2Cfg.excludeDomains != [ ]
|
||||
) "--hostlist-exclude-domains=${lib.concatStringsSep "," zapret2Cfg.excludeDomains}";
|
||||
|
||||
renderProfile =
|
||||
profile:
|
||||
let
|
||||
suffix = lib.concatMapStrings (arg: " " + arg) staticListArgs;
|
||||
in
|
||||
lib.replaceStrings hostlistMarkers (map (marker: marker + suffix) hostlistMarkers) (
|
||||
lib.trim profile
|
||||
);
|
||||
lib.replaceStrings hostlistMarkers (map (
|
||||
marker: marker + withSpaces staticListArgs
|
||||
) hostlistMarkers) (lib.trim profile);
|
||||
|
||||
nfqwsOpt = lib.concatStringsSep " " (
|
||||
blobArgs ++ [ (lib.concatStringsSep " --new " (map renderProfile zapret2Cfg.profiles)) ]
|
||||
z2kGenerated = sources.z2k.mkProfiles {
|
||||
hostlistSuffix = withSpaces staticListArgs;
|
||||
# z2k's category profiles filter on their own lists, not on a marker.
|
||||
excludeSuffix = withSpaces ([ "--hostlist-exclude=${excludeHostlistFile}" ] ++ excludeDomainArgs);
|
||||
};
|
||||
|
||||
profilesFile =
|
||||
if zapret2Cfg.profiles == null && isZ2k then
|
||||
"${z2kGenerated}/profiles"
|
||||
else
|
||||
pkgs.writeText "proxy-suite-zapret2-profiles" (
|
||||
lib.concatStringsSep " --new " (map renderProfile (orSource zapret2Cfg.profiles source.profiles))
|
||||
);
|
||||
|
||||
blobsFile =
|
||||
if isZ2k then
|
||||
"${z2kGenerated}/blobs"
|
||||
else
|
||||
pkgs.writeText "proxy-suite-zapret2-blobs" (lib.concatStringsSep " " source.blobArgs);
|
||||
|
||||
# The state layer wraps circular, so it loads after the source's Lua.
|
||||
optPrefix = lib.concatStringsSep " " (
|
||||
map (file: "--lua-init=@${file}") (
|
||||
source.luaInit ++ [ "${zapret2Sources.z2k}/files/lua/z2k-state-persist.lua" ]
|
||||
)
|
||||
++ lib.mapAttrsToList (name: file: "--blob=${name}:@${blobPath file}") zapret2Cfg.blobs
|
||||
);
|
||||
|
||||
# The NFQUEUE window must exceed the retransmission threshold it counts.
|
||||
# Detector args a circular instance leaves unset come from autoHostlist, so
|
||||
# learning and rotation agree; each source keeps its own fails and time.
|
||||
circularFill = lib.concatStringsSep " " (
|
||||
[
|
||||
"retrans=${toString autoCfg.retransThreshold}"
|
||||
"maxseq=${toString autoCfg.retransMaxseq}"
|
||||
"inseq=${toString autoCfg.incomingMaxseq}"
|
||||
"udp_out=${toString autoCfg.udpOut}"
|
||||
"udp_in=${toString autoCfg.udpIn}"
|
||||
]
|
||||
++ lib.optional autoCfg.retransReset "reset"
|
||||
);
|
||||
|
||||
# The NFQUEUE window must exceed the retransmission threshold it counts, and
|
||||
# cover what the source's detectors need.
|
||||
packetWindow = {
|
||||
tcpOut = 6 + autoCfg.retransThreshold;
|
||||
tcpOut = lib.max (6 + autoCfg.retransThreshold) (source.window.tcpOut or 0);
|
||||
tcpIn = 15;
|
||||
udpOut = 6 + autoCfg.retransThreshold;
|
||||
udpIn = 3;
|
||||
udpOut = lib.max (6 + autoCfg.retransThreshold) (source.window.udpOut or 0);
|
||||
udpIn = source.window.udpIn or 3;
|
||||
};
|
||||
|
||||
mkConfig =
|
||||
@@ -86,43 +132,74 @@ let
|
||||
filterMark ? null,
|
||||
modeFilter,
|
||||
}:
|
||||
pkgs.writeText "proxy-suite-zapret2-config" (
|
||||
lib.concatStringsSep "\n" (
|
||||
[
|
||||
"# Generated by proxy-suite. Do not edit."
|
||||
"WS_USER=root"
|
||||
"FWTYPE=nftables"
|
||||
"INIT_APPLY_FW=1"
|
||||
"QNUM=${toString qnum}"
|
||||
"DESYNC_MARK=${toHex desyncMark}"
|
||||
"DESYNC_MARK_POSTNAT=${toHex desyncMarkPostnat}"
|
||||
"ZAPRET_NFT_TABLE=${nftTable}"
|
||||
]
|
||||
++ lib.optional (filterMark != null) "FILTER_MARK=${toHex filterMark}"
|
||||
++ lib.optional (!zapret2Cfg.ipv6) "DISABLE_IPV6=1"
|
||||
++ [
|
||||
"NFQWS2_ENABLE=1"
|
||||
"NFQWS2_PORTS_TCP=${zapret2Cfg.ports.tcp}"
|
||||
"NFQWS2_PORTS_UDP=${zapret2Cfg.ports.udp}"
|
||||
"NFQWS2_TCP_PKT_OUT=${toString packetWindow.tcpOut}"
|
||||
"NFQWS2_TCP_PKT_IN=${toString packetWindow.tcpIn}"
|
||||
"NFQWS2_UDP_PKT_OUT=${toString packetWindow.udpOut}"
|
||||
"NFQWS2_UDP_PKT_IN=${toString packetWindow.udpIn}"
|
||||
"MODE_FILTER=${modeFilter}"
|
||||
"AUTOHOSTLIST_FAIL_THRESHOLD=${toString autoCfg.failThreshold}"
|
||||
"AUTOHOSTLIST_FAIL_TIME=${toString autoCfg.failTime}"
|
||||
"AUTOHOSTLIST_RETRANS_THRESHOLD=${toString autoCfg.retransThreshold}"
|
||||
"AUTOHOSTLIST_RETRANS_RESET=${if autoCfg.retransReset then "1" else "0"}"
|
||||
"AUTOHOSTLIST_RETRANS_MAXSEQ=${toString autoCfg.retransMaxseq}"
|
||||
"AUTOHOSTLIST_INCOMING_MAXSEQ=${toString autoCfg.incomingMaxseq}"
|
||||
"AUTOHOSTLIST_UDP_OUT=${toString autoCfg.udpOut}"
|
||||
"AUTOHOSTLIST_UDP_IN=${toString autoCfg.udpIn}"
|
||||
"AUTOHOSTLIST_DEBUGLOG=${if autoCfg.debugLog then "1" else "0"}"
|
||||
"NFQWS2_OPT=${lib.escapeShellArg nfqwsOpt}"
|
||||
""
|
||||
]
|
||||
)
|
||||
);
|
||||
let
|
||||
header = pkgs.writeText "proxy-suite-zapret2-config-header" (
|
||||
lib.concatStringsSep "\n" (
|
||||
[
|
||||
"# Generated by proxy-suite. Do not edit."
|
||||
"WS_USER=root"
|
||||
"FWTYPE=nftables"
|
||||
"INIT_APPLY_FW=1"
|
||||
"QNUM=${toString qnum}"
|
||||
"DESYNC_MARK=${toHex desyncMark}"
|
||||
"DESYNC_MARK_POSTNAT=${toHex desyncMarkPostnat}"
|
||||
"ZAPRET_NFT_TABLE=${nftTable}"
|
||||
]
|
||||
++ lib.optional (filterMark != null) "FILTER_MARK=${toHex filterMark}"
|
||||
++ lib.optional (!zapret2Cfg.ipv6) "DISABLE_IPV6=1"
|
||||
++ [
|
||||
"NFQWS2_ENABLE=1"
|
||||
"NFQWS2_PORTS_TCP=${orSource zapret2Cfg.ports.tcp source.ports.tcp}"
|
||||
"NFQWS2_PORTS_UDP=${orSource zapret2Cfg.ports.udp source.ports.udp}"
|
||||
"NFQWS2_TCP_PKT_OUT=${toString packetWindow.tcpOut}"
|
||||
"NFQWS2_TCP_PKT_IN=${toString packetWindow.tcpIn}"
|
||||
"NFQWS2_UDP_PKT_OUT=${toString packetWindow.udpOut}"
|
||||
"NFQWS2_UDP_PKT_IN=${toString packetWindow.udpIn}"
|
||||
"MODE_FILTER=${modeFilter}"
|
||||
"AUTOHOSTLIST_FAIL_THRESHOLD=${toString autoCfg.failThreshold}"
|
||||
"AUTOHOSTLIST_FAIL_TIME=${toString autoCfg.failTime}"
|
||||
"AUTOHOSTLIST_RETRANS_THRESHOLD=${toString autoCfg.retransThreshold}"
|
||||
"AUTOHOSTLIST_RETRANS_RESET=${if autoCfg.retransReset then "1" else "0"}"
|
||||
"AUTOHOSTLIST_RETRANS_MAXSEQ=${toString autoCfg.retransMaxseq}"
|
||||
"AUTOHOSTLIST_INCOMING_MAXSEQ=${toString autoCfg.incomingMaxseq}"
|
||||
"AUTOHOSTLIST_UDP_OUT=${toString autoCfg.udpOut}"
|
||||
"AUTOHOSTLIST_UDP_IN=${toString autoCfg.udpIn}"
|
||||
"AUTOHOSTLIST_DEBUGLOG=${if autoCfg.debugLog then "1" else "0"}"
|
||||
""
|
||||
]
|
||||
)
|
||||
);
|
||||
in
|
||||
pkgs.runCommand "proxy-suite-zapret2-config"
|
||||
{
|
||||
nativeBuildInputs = [ pkgs.gawk ];
|
||||
inherit circularFill optPrefix;
|
||||
}
|
||||
''
|
||||
opt="$optPrefix $(cat ${blobsFile}) $(cat ${profilesFile})"
|
||||
opt=$(printf '%s\n' "$opt" | awk -v fill="$circularFill" '
|
||||
{
|
||||
for (i = 1; i <= NF; i++) {
|
||||
t = $i
|
||||
if (t ~ /^--lua-desync=circular(:|$)/) {
|
||||
n = split(fill, f, " ")
|
||||
for (j = 1; j <= n; j++) {
|
||||
k = f[j]
|
||||
sub(/=.*/, "", k)
|
||||
if (t !~ (":" k "(=|:|$)")) t = t ":" f[j]
|
||||
}
|
||||
}
|
||||
printf "%s%s", (i > 1 ? " " : ""), t
|
||||
}
|
||||
}')
|
||||
case "$opt" in
|
||||
*"'"*)
|
||||
echo "nfqws2 options must not contain a single quote" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
{ cat ${header}; printf "NFQWS2_OPT='%s'\n" "$opt"; } >"$out"
|
||||
'';
|
||||
|
||||
# Keep desync off TUN interfaces (it breaks proxied handshakes), off per-app
|
||||
# traffic, and off exempted subnets (e.g. NATed VMs whose traffic it would break).
|
||||
@@ -168,8 +245,7 @@ let
|
||||
interface: mkRule chain.name "${chain.direction} '\"${interface}\"'" "proxy-suite TUN bypass"
|
||||
) tunInterfaces
|
||||
++ map (
|
||||
cidr:
|
||||
mkRule chain.name "${nftFamily cidr} ${chain.ipMatch} ${cidr}" "proxy-suite CIDR exemption"
|
||||
cidr: mkRule chain.name "${nftFamily cidr} ${chain.ipMatch} ${cidr}" "proxy-suite CIDR exemption"
|
||||
) exemptCidrs
|
||||
) chains;
|
||||
in
|
||||
@@ -214,6 +290,9 @@ let
|
||||
"HOSTLIST_BASE=${stateDir}"
|
||||
"PIDDIR=${pidDir}"
|
||||
"PATH=${lib.makeBinPath runtimeDeps}"
|
||||
# Read by z2k-state-persist.lua inside nfqws2, which inherits the unit's environment.
|
||||
"Z2K_STATE_DIR_OVERRIDE=${circularStateDir}"
|
||||
"Z2K_AUTOCIRCULAR_FALLBACK_OVERRIDE=${pidDir}"
|
||||
];
|
||||
in
|
||||
{
|
||||
@@ -223,6 +302,7 @@ in
|
||||
autoHostlistFile
|
||||
userHostlistFile
|
||||
excludeHostlistFile
|
||||
circularStateDir
|
||||
mkCustomScript
|
||||
mkRuntime
|
||||
mkEnv
|
||||
|
||||
Reference in new issue
Block a user