Files
proxy-suite-flake/modules/proxy-suite/zapret2/runtime.nix
T

484 lines
18 KiB
Nix

# Per-instance zapret2 runtime: a generated config and custom.d hook, no package copy.
{
lib,
pkgs,
cfg,
packages,
zapret2Sources,
}:
let
fillTemplate = import ../lib/fill-template.nix;
zapret2Cfg = cfg.zapret.zapret2;
autoCfg = zapret2Cfg.autoHostlist;
package = packages.zapret2;
zapretBase = "${package}/opt/zapret2";
# Shared by both instances: what the global one learns applies per-app too.
inherit (import ../derived.nix { inherit lib cfg; }) constants;
stateDir = constants.zapret2StateDir;
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)
coreutils
findutils
gawk
gnugrep
gnused
iproute2
iptables
ipset
kmod
nftables
procps
util-linux
;
};
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}";
hostlistMarkers = [
"<HOSTLIST>"
"<HOSTLIST_NOAUTO>"
];
# Static domain filters join every profile that uses a hostlist marker.
staticListArgs =
lib.optional (
zapret2Cfg.domains != [ ]
) "--hostlist-domains=${lib.concatStringsSep "," zapret2Cfg.domains}"
++ excludeDomainArgs;
excludeDomainArgs = lib.optional (
zapret2Cfg.excludeDomains != [ ]
) "--hostlist-exclude-domains=${lib.concatStringsSep "," zapret2Cfg.excludeDomains}";
# lib.trim drops string context: without it the source's list paths are no build input,
# so lazy trees never materialize them and the closure omits them.
# <STALLWATCH>: where the source's profile for learned sites takes stallWatch.
renderProfile =
profile:
lib.replaceStrings (hostlistMarkers ++ [ "<STALLWATCH> " ]) (
map (marker: marker + withSpaces staticListArgs) hostlistMarkers
++ [ (lib.optionalString (stallWatch != "") (stallWatch + " ")) ]
) (lib.addContextFrom profile (lib.trim profile));
# detect.lua learns in place of nfqws2's autohostlist, in a last TLS profile of its
# own: z2k-profiles.template.sh puts it there, tailProfiles for nfqws2-keenetic.
learnLua = autoCfg.enable && autoCfg.extendedDetection && zapret2Cfg.profiles == null;
learnArgs = lib.optionalString learnLua (
lib.concatStringsSep ":" (
[
"auto=${autoHostlistFile}"
"exclude=${lib.concatStringsSep "," (source.learnExclude ++ [ excludeHostlistFile ])}"
"fails=${toString autoCfg.failThreshold}"
"time=${toString autoCfg.failTime}"
"inseq=${toString autoCfg.incomingMaxseq}"
"retrans=${toString autoCfg.retransThreshold}"
"win=${toString packetWindow.tcpIn}"
]
++ lib.optional (
zapret2Cfg.excludeDomains != [ ]
) "exclude_domains=${lib.concatStringsSep "," zapret2Cfg.excludeDomains}"
++ lib.optional zapret2Cfg.strategyLog "log"
)
);
# detect.lua's ps_watch, ahead of every other instance of the profile that takes the
# learned sites, so rotation (an orchestrator: it runs the instances after it) leaves it
# be; it sees the server's bytes past what rotation judges a success on, and the
# profile's filters are back at nfqws2's defaults after it.
stallWatch = lib.optionalString learnLua (
"--in-range=-s65536 --payload=all --lua-desync=ps_watch:"
+ lib.concatStringsSep ":" (
[
"fails=${toString autoCfg.failThreshold}"
"time=${toString autoCfg.failTime}"
"win=${toString packetWindow.tcpIn}"
]
++ lib.optional zapret2Cfg.strategyLog "log"
)
+ " --in-range=x --payload=all"
);
z2kGenerated = sources.z2k.mkProfiles {
inherit learnArgs stallWatch;
hostlistSuffix = withSpaces staticListArgs;
# z2k's category profiles filter on their own lists, not on a marker.
excludeSuffix = withSpaces ([ "--hostlist-exclude=${excludeHostlistFile}" ] ++ excludeDomainArgs);
};
# With detect.lua learning, the source's own learning profile only reads the auto list.
sourceProfiles =
if learnLua then
map (lib.replaceStrings [ "<HOSTLIST>" ] [ "<HOSTLIST_NOAUTO>" ]) source.profiles
else
source.profiles;
profilesFile =
if zapret2Cfg.profiles == null && isZ2k then
"${z2kGenerated}/profiles"
else
pkgs.writeText "proxy-suite-zapret2" (
lib.concatStringsSep " --new " (map renderProfile (orSource zapret2Cfg.profiles sourceProfiles))
);
# After every source profile, so each only takes what none of them did. ps_learn: the
# TLS sites in no list (z2k's template places its own). ps_syn: every new TCP connection
# until its first data names the protocol, to tell an address blocked outright.
tailProfiles = lib.concatMapStrings (profile: " --new " + profile) (
lib.optional (learnLua && !isZ2k) (
"--filter-tcp=443 --filter-l7=tls --in-range=-s65536 --out-range=-s32768 --payload=all"
+ " --lua-desync=ps_learn:${learnArgs}"
)
++ lib.optional zapret2Cfg.proxyFallback (
"--filter-tcp=* --out-range=<n2 --in-range=<n2 --payload=empty --lua-desync=ps_syn:"
+ lib.concatStringsSep ":" (
[
"fails=${toString autoCfg.failThreshold}"
"time=${toString autoCfg.failTime}"
]
++ lib.optional zapret2Cfg.strategyLog "log"
)
)
);
blobsFile =
if isZ2k then
"${z2kGenerated}/blobs"
else
pkgs.writeText "proxy-suite-zapret2" (lib.concatStringsSep " " source.blobArgs);
# The state layer undoes a rotation when the host succeeded within 30 s, unless
# a failure came later; only z2k's own detector stamps that failure. Without the
# stamp any success on a busy host (all of googlevideo.com) undoes every rotation.
failStamp = pkgs.writeText "proxy-suite-zapret2-fail-stamp.lua" ''
local count = automate_failure_counter
function automate_failure_counter(hrec, crec, fails, maxtime)
if not (crec and crec.failure) then
hrec.z2k_last_fail_ts = type(clock_getfloattime) == "function" and clock_getfloattime() or os.time()
end
return count(hrec, crec, fails, maxtime)
end
'';
publicSuffixHostkey = pkgs.writeText "proxy-suite-zapret2-public-suffix-hostkey.lua" (
fillTemplate ./public-suffix-hostkey.lua {
suffixes =
pkgs.runCommand "proxy-suite-zapret2-public-suffixes"
{
nativeBuildInputs = [ pkgs.python3 ];
}
"python3 ${./public-suffixes.py} ${pkgs.publicsuffix-list}/share/publicsuffix/public_suffix_list.dat >$out";
}
);
# The state layer wraps circular, so it loads after the source's Lua; the writes it
# skips are retried after it, and the log wraps them all, to report the switch that stuck.
#
# --bind-fix: a fake leaves by the interface its connection does. Under policy routing
# (an app or a domain sent through a tunnel) it would otherwise take the default route,
# away from the connection it was meant to precede; without such routing it is a no-op.
optPrefix = lib.concatStringsSep " " (
lib.optional cfg.debug.zapret2.packets "--debug=1"
++ [
"--bind-fix4"
"--bind-fix6"
]
++ source.daemonArgs
++ map (file: "--lua-init=@${file}") (
source.luaInit
++ [
publicSuffixHostkey
failStamp
"${zapret2Sources.z2k}/files/lua/z2k-state-persist.lua"
./persist-every.lua
# After z2k-alert.lua, whose detector it wraps.
./detect.lua
]
++ lib.optional zapret2Cfg.strategyLog ./strategy-log.lua
)
++ lib.mapAttrsToList (name: file: "--blob=${name}:@${blobPath file}") zapret2Cfg.blobs
);
portList = lib.concatMapStringsSep "," toString;
# 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 = lib.max (6 + autoCfg.retransThreshold) (source.window.tcpOut or 0);
# The learner tells a stalled transfer from a finished one only while it still sees
# the server's packets; the 16 KB cutoff falls at about 12 of them.
tcpIn = if learnLua then 32 else 15;
udpOut = lib.max (6 + autoCfg.retransThreshold) (source.window.udpOut or 0);
udpIn = source.window.udpIn or 3;
};
mkConfig =
{
qnum,
desyncMark,
desyncMarkPostnat,
nftTable,
filterMark ? null,
modeFilter,
}:
let
header = pkgs.writeText "proxy-suite-zapret2" (
lib.concatStringsSep "\n" (
[
"# Generated by proxy-suite. Do not edit."
"WS_USER=root"
# /proc/sys is read-only in the units' sandbox: the unit sets autohostlist's
# nf_conntrack_tcp_be_liberal itself, outside it.
"SKIP_CONNTRACK_LIBERAL_MODE=1"
"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_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 cfg.debug.zapret2.autoHostlist then "1" else "0"}"
""
]
)
);
in
pkgs.runCommand "proxy-suite-zapret2"
{
nativeBuildInputs = [ pkgs.gawk ];
inherit circularFill optPrefix tailProfiles;
basePortsTcp = orSource zapret2Cfg.ports.tcp source.ports.tcp;
basePortsUdp = orSource zapret2Cfg.ports.udp source.ports.udp;
extraPortsTcp = portList zapret2Cfg.ports.extraTcp;
extraPortsUdp = portList zapret2Cfg.ports.extraUdp;
}
''
opt="$optPrefix $(cat ${blobsFile}) $(cat ${profilesFile})$tailProfiles"
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
}
}')
opt=$(printf '%s\n' "$opt" | awk -v btcp="$basePortsTcp" -v budp="$basePortsUdp" \
-v xtcp="$extraPortsTcp" -v xudp="$extraPortsUdp" -v ports=ports -f ${./ports.awk})
case "$opt" in
*"'"*)
echo "nfqws2 options must not contain a single quote" >&2
exit 1
;;
esac
{ cat ${header} ports; printf "NFQWS2_OPT='%s'\n" "$opt"; } >"$out"
'';
# {circular key: {strategy: [its desync instances]}}, for proxy-ctl to name the
# strategy state.tsv remembers. A circular without key= is keyed by its instance,
# <func>_<profile>_<nth desync of the profile>, counting profiles from 1 and
# templates apart; a profile that imports a template rotates over its strategies.
mkStrategies =
config:
pkgs.runCommand "proxy-suite-zapret2-strategies.json" { nativeBuildInputs = [ pkgs.gawk ]; } (
fillTemplate ./strategies.template.sh { inherit config; }
);
# 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).
mkCustomScript =
{
tunInterfaces ? [ ],
excludeMark ? null,
exemptCidrs ? [ ],
probeCtMark ? null,
}:
let
chains = [
{
name = "postrouting";
direction = "oifname";
ipMatch = "daddr";
}
{
name = "postnat";
direction = "oifname";
ipMatch = "daddr";
}
{
name = "prerouting";
direction = "iifname";
ipMatch = "saddr";
}
{
name = "prenat";
direction = "iifname";
ipMatch = "saddr";
}
];
nftFamily = cidr: if lib.hasInfix ":" cidr then "ip6" else "ip";
mkRule =
chain: match: comment:
" nft insert rule inet $ZAPRET_NFT_TABLE ${chain} ${match} return comment '\"${comment}\"'";
rules = lib.concatMap (
chain:
lib.optional (excludeMark != null) (
mkRule chain.name "mark and ${toHex excludeMark} != 0" "proxy-suite per-app-zapret bypass"
)
# Replies carry no socket, so the cutoff probe is matched by conntrack.
++ lib.optional (probeCtMark != null) (
mkRule chain.name "ct mark and ${toHex probeCtMark} != 0" "proxy-suite cutoff probe bypass"
)
++ map (
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"
) exemptCidrs
) chains;
in
pkgs.writeText "proxy-suite-zapret2" (
lib.concatStringsSep "\n" (
[ "zapret_custom_firewall_nft() {" ]
++ rules
++ [
" return 0"
"}"
""
]
)
);
mkRuntime =
{
name,
customScript ? null,
...
}@args:
let
config = mkConfig (
builtins.removeAttrs args [
"name"
"customScript"
]
);
in
pkgs.runCommand name { } ''
mkdir -p "$out/init.d/sysv/custom.d"
cp ${config} "$out/config"
cp ${mkStrategies config} "$out/strategies.json"
${lib.optionalString (customScript != null) ''
cp ${customScript} "$out/init.d/sysv/custom.d/50-proxy-suite-custom.sh"
''}
'';
mkEnv =
{ runtime, pidDir }:
[
"ZAPRET_BASE=${zapretBase}"
"ZAPRET_RW=${runtime}"
"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}"
# detect.lua's verdicts: what zapret2 fixes, and what the proxy has to carry.
"PROXY_SUITE_ZAPRET2_VERDICTS=${stateDir}/verdicts.tsv"
"Z2K_AUTOCIRCULAR_FALLBACK_OVERRIDE=${pidDir}"
]
# The cutoff probe's maps, read by z2k's tcp16 name step; pin.txt forces one name for the line.
++ lib.optionals (zapret2Cfg.cutoff.enable && isZ2k) [
"Z2K_TCP16_ASN=${constants.zapret2CutoffDir}/asn.txt"
"Z2K_TCP16_SNI=${constants.zapret2CutoffDir}/sni.txt"
"Z2K_SNI_PIN=${constants.zapret2CutoffDir}/pin.txt"
"Z2K_TCP16_NETS=${zapret2Sources.z2k}/files/lists/tcp16_nets.txt"
];
initScript = "${zapretBase}/init.d/sysv/zapret2";
# nfqws2 as the unit's main process, with the command line the init script would
# build. The init script backgrounds it with stdout on /dev/null in a oneshot
# unit: when it dies, the unit stays active and the queue's bypass flag lets
# every packet through untouched, with nothing in the log.
daemonScript = pkgs.writeShellScript "proxy-suite-zapret2" ''
. "$ZAPRET_BASE/init.d/sysv/functions"
opt="--qnum=$QNUM $NFQWS2_OPT"
filter_apply_hostlist_target opt
# --user=root leaves nfqws2 root's own groups (initgroups), and with them only
# CAP_NET_ADMIN and CAP_NET_RAW: the unit's Group= and SupplementaryGroups=, through
# which it appends to the lists the zapret scope's group renames in (theirs, then),
# would be gone. --uid after it keeps uid 0 and the groups the unit started it with.
uid=
if [ "$(id -u)" = 0 ]; then uid="--uid=0:$(id -G | tr ' ' ,)"; fi
set -f
exec "$NFQWS2" $NFQWS2_OPT_BASE $uid $opt
'';
in
{
inherit
package
stateDir
autoHostlistFile
userHostlistFile
excludeHostlistFile
circularStateDir
mkCustomScript
mkRuntime
mkEnv
initScript
daemonScript
;
}