Includes, but not limited to: - zapret2 support - declarative inbounds - proxy-ctl revamp - automatic proxy learning
233 lines
6.5 KiB
Nix
233 lines
6.5 KiB
Nix
# Per-instance zapret2 runtime: a generated config and custom.d hook, no package copy.
|
|
{
|
|
lib,
|
|
pkgs,
|
|
cfg,
|
|
packages,
|
|
}:
|
|
|
|
let
|
|
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";
|
|
|
|
runtimeDeps = lib.attrValues {
|
|
inherit (pkgs)
|
|
coreutils
|
|
findutils
|
|
gawk
|
|
gnugrep
|
|
gnused
|
|
iproute2
|
|
iptables
|
|
ipset
|
|
kmod
|
|
nftables
|
|
procps
|
|
util-linux
|
|
;
|
|
};
|
|
|
|
toHex = value: "0x" + lib.toHexString value;
|
|
|
|
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>"
|
|
"<HOSTLIST_NOAUTO>"
|
|
];
|
|
|
|
# Static domain filters join every profile that uses a hostlist marker.
|
|
staticListArgs =
|
|
lib.optional (
|
|
zapret2Cfg.domains != [ ]
|
|
) "--hostlist-domains=${lib.concatStringsSep "," zapret2Cfg.domains}"
|
|
++ 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
|
|
);
|
|
|
|
nfqwsOpt = lib.concatStringsSep " " (
|
|
blobArgs ++ [ (lib.concatStringsSep " --new " (map renderProfile zapret2Cfg.profiles)) ]
|
|
);
|
|
|
|
# The NFQUEUE window must exceed the retransmission threshold it counts.
|
|
packetWindow = {
|
|
tcpOut = 6 + autoCfg.retransThreshold;
|
|
tcpIn = 15;
|
|
udpOut = 6 + autoCfg.retransThreshold;
|
|
udpIn = 3;
|
|
};
|
|
|
|
mkConfig =
|
|
{
|
|
qnum,
|
|
desyncMark,
|
|
desyncMarkPostnat,
|
|
nftTable,
|
|
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}"
|
|
""
|
|
]
|
|
)
|
|
);
|
|
|
|
# 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 ? [ ],
|
|
}:
|
|
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"
|
|
)
|
|
++ 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-custom.sh" (
|
|
lib.concatStringsSep "\n" (
|
|
[ "zapret_custom_firewall_nft() {" ]
|
|
++ rules
|
|
++ [
|
|
" return 0"
|
|
"}"
|
|
""
|
|
]
|
|
)
|
|
);
|
|
|
|
mkRuntime =
|
|
{
|
|
name,
|
|
customScript ? null,
|
|
...
|
|
}@args:
|
|
pkgs.runCommand name { } ''
|
|
mkdir -p "$out/init.d/sysv/custom.d"
|
|
cp ${
|
|
mkConfig (
|
|
builtins.removeAttrs args [
|
|
"name"
|
|
"customScript"
|
|
]
|
|
)
|
|
} "$out/config"
|
|
${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}"
|
|
];
|
|
in
|
|
{
|
|
inherit
|
|
package
|
|
stateDir
|
|
autoHostlistFile
|
|
userHostlistFile
|
|
excludeHostlistFile
|
|
mkCustomScript
|
|
mkRuntime
|
|
mkEnv
|
|
;
|
|
|
|
initScript = "${zapretBase}/init.d/sysv/zapret2";
|
|
}
|