feat: allow setting zapret custom rules

This commit is contained in:
FUFSoB committed 2026-04-12 15:52:07 +05:00
1 parent 4810114c83
commit ce0262b3d5
7 files changed
+679 -11

No files matched your search

+3 -1
View File
@@ -30,7 +30,9 @@ in
))
(lib.mkIf cfg.zapret.enable (
import ./zapret.nix { inherit lib pkgs cfg; }
import ./zapret.nix {
inherit lib pkgs cfg zapret;
}
))
(lib.mkIf cfg.tgWsProxy.enable (
+90
View File
@@ -112,6 +112,59 @@ let
};
};
zapretPresetType = types.enum [
"general"
"google"
"instagram"
"soundcloud"
"twitter"
];
zapretHostlistRuleType = types.submodule {
options = {
name = mkOption {
type = types.strMatching "^[a-z0-9][a-z0-9-]*$";
description = "Custom hostlist name. Used to generate list-<name>.txt.";
example = "cloudflare";
};
domains = mkOption {
type = types.listOf types.str;
default = [ ];
description = "Domains written into the generated custom zapret hostlist.";
example = [
"example.com"
"example.de"
];
};
preset = mkOption {
type = types.nullOr zapretPresetType;
default = null;
description = "Clone the active zapret config's built-in rule family for this hostlist.";
example = "google";
};
nfqwsArgs = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
Additional NFQWS argument fragments for this hostlist.
The module injects --hostlist=... and trailing --new automatically.
'';
example = [
"--filter-tcp=443 --dpi-desync=fake,multisplit"
];
};
enableDirectSync = mkOption {
type = types.bool;
default = true;
description = "Whether this custom hostlist should also be mirrored into sing-box direct domain routing.";
};
};
};
in
{
options.services.proxy-suite = {
@@ -389,6 +442,43 @@ in
description = "IPs/CIDRs to exclude from zapret's ipset.";
};
includeExtraUpstreamLists = mkOption {
type = types.bool;
default = true;
description = ''
Automatically activate upstream list-instagram.txt, list-soundcloud.txt,
and list-twitter.txt in the generated zapret config when the selected
upstream preset does not already reference them.
'';
};
hostlistRules = mkOption {
type = types.listOf zapretHostlistRuleType;
default = [ ];
description = ''
Additional named zapret hostlists with per-list DPI mitigation rules.
Each entry generates hostlists/list-<name>.txt and can clone a built-in
zapret family, add custom NFQWS rule fragments, or both.
'';
example = [
{
name = "googlevideo";
domains = [
"googlevideo.com"
"ggpht.com"
];
preset = "google";
}
{
name = "example";
domains = [ "example.com" "example.de" ];
nfqwsArgs = [
"--filter-tcp=443 --dpi-desync=fake,multisplit"
];
}
];
};
cidrExemption = {
enable = mkEnableOption "CIDR exemption from zapret NFQUEUE";
+16 -8
View File
@@ -32,13 +32,16 @@ let
syncZapretDirectUserIps = z.enable && z.syncDirectRoutingUserIps;
syncZapretDirectAnyIps = syncZapretDirectUpstreamIps || syncZapretDirectUserIps;
zapretDefaultDomainFiles = [
"list-general.txt"
"list-google.txt"
"list-instagram.txt"
"list-soundcloud.txt"
"list-twitter.txt"
];
zapretDefaultDomainFiles =
[
"list-general.txt"
"list-google.txt"
]
++ lib.optionals z.includeExtraUpstreamLists [
"list-instagram.txt"
"list-soundcloud.txt"
"list-twitter.txt"
];
zapretDefaultDomains =
if syncZapretDirectDomains then
@@ -50,6 +53,11 @@ let
zapretDefaultIps =
if syncZapretDirectUpstreamIps then parseListFile "${zapretSrc}/hostlists/ipset-all.txt" else [ ];
zapretUserIps = if syncZapretDirectUserIps then z.ipsetAll else [ ];
zapretCustomDomains =
if syncZapretDirectDomains then
lib.unique (lib.concatMap (rule: lib.optionals rule.enableDirectSync rule.domains) z.hostlistRules)
else
[ ];
zapretExcludedDomains =
if syncZapretDirectDomains then parseListFile "${zapretSrc}/hostlists/list-exclude.txt" else [ ];
zapretExcludedIps = lib.unique (
@@ -64,7 +72,7 @@ let
zapretDirectDomains =
if syncZapretDirectDomains then
subtractItems (lib.unique (zapretDefaultDomains ++ z.listGeneral)) (
subtractItems (lib.unique (zapretDefaultDomains ++ z.listGeneral ++ zapretCustomDomains)) (
lib.unique (zapretExcludedDomains ++ z.listExclude)
)
else
+185 -1
View File
@@ -1,11 +1,154 @@
# zapret DPI bypass configuration + optional CIDR exemption from NFQUEUE
{ lib, pkgs, cfg }:
{
lib,
pkgs,
cfg,
zapret,
}:
let
z = cfg.zapret;
iptables = "${pkgs.iptables}/bin/iptables";
runtimeDeps = lib.attrValues {
inherit (pkgs)
iptables
ipset
coreutils
gawk
curl
wget
bash
kmod
findutils
gnused
gnugrep
procps
util-linux
;
};
hostlistRuleNames = map (rule: rule.name) z.hostlistRules;
upstreamZapretPackage =
let
upstreamPackages = zapret.packages.${pkgs.system};
in
if upstreamPackages ? zapret then upstreamPackages.zapret else upstreamPackages.default;
listGeneralFile =
if z.listGeneral != [ ] then pkgs.writeText "proxy-suite-zapret-list-general-user.txt" (
lib.concatStringsSep "\n" z.listGeneral + "\n"
) else null;
listExcludeFile =
if z.listExclude != [ ] then pkgs.writeText "proxy-suite-zapret-list-exclude-user.txt" (
lib.concatStringsSep "\n" z.listExclude + "\n"
) else null;
ipsetAllFile =
if z.ipsetAll != [ ] then pkgs.writeText "proxy-suite-zapret-ipset-all.txt" (
lib.concatStringsSep "\n" z.ipsetAll + "\n"
) else null;
ipsetExcludeFile =
if z.ipsetExclude != [ ] then pkgs.writeText "proxy-suite-zapret-ipset-exclude-user.txt" (
lib.concatStringsSep "\n" z.ipsetExclude + "\n"
) else null;
hostlistRuleSpec = pkgs.writeText "proxy-suite-zapret-hostlist-rules.json" (builtins.toJSON {
includeExtraUpstreamLists = z.includeExtraUpstreamLists;
entries = map (rule: {
inherit (rule)
name
preset
nfqwsArgs
;
}) z.hostlistRules;
});
selectedConfigName = lib.strings.sanitizeDerivationName z.configName;
patchConfigScriptSrc = builtins.path {
path = ../../scripts/patch-zapret-config.py;
name = "patch-zapret-config.py";
};
patchConfigScript = "${pkgs.python3}/bin/python3 ${patchConfigScriptSrc}";
customZapretPackage = pkgs.runCommand "proxy-suite-zapret-${selectedConfigName}" {
nativeBuildInputs = with pkgs; [
coreutils
gnused
python3
];
} ''
set -euo pipefail
mkdir -p "$out"
cp -a ${upstreamZapretPackage}/. "$out/"
chmod -R u+w "$out/opt/zapret" "$out/bin"
find "$out/opt/zapret/configs" -type f -exec ${pkgs.gnused}/bin/sed -i \
-e 's|${upstreamZapretPackage}/opt/zapret|'"$out"'/opt/zapret|g' \
{} \;
if [ -f "$out/opt/zapret/configs/${z.configName}" ]; then
cp "$out/opt/zapret/configs/${z.configName}" "$out/opt/zapret/config"
else
echo "proxy-suite: zapret config '${z.configName}' not found in upstream package" >&2
ls -la "$out/opt/zapret/configs" >&2 || true
exit 1
fi
${pkgs.gnused}/bin/sed -i \
-e 's|${upstreamZapretPackage}/opt/zapret|'"$out"'/opt/zapret|g' \
"$out/opt/zapret/config"
append_list_file() {
local target="$1"
local extra_file="$2"
local tmp="$out/opt/zapret/hostlists/$target.tmp"
cat "$out/opt/zapret/hostlists/$target" > "$tmp"
${pkgs.gnused}/bin/sed -i -e '$a\' "$tmp"
cat "$extra_file" >> "$tmp"
mv "$tmp" "$out/opt/zapret/hostlists/$target"
}
${lib.optionalString (listGeneralFile != null) ''
append_list_file list-general-user.txt ${listGeneralFile}
''}
${lib.optionalString (listExcludeFile != null) ''
append_list_file list-exclude-user.txt ${listExcludeFile}
''}
${lib.optionalString (ipsetAllFile != null) ''
append_list_file ipset-all.txt ${ipsetAllFile}
''}
${lib.optionalString (ipsetExcludeFile != null) ''
append_list_file ipset-exclude-user.txt ${ipsetExcludeFile}
''}
rm -f "$out/opt/zapret/hostlists/.game_filter.enabled"
${lib.optionalString (z.gameFilter != "null") ''
echo "${z.gameFilter}" > "$out/opt/zapret/hostlists/.game_filter.enabled"
''}
${lib.concatMapStrings (rule:
let
domainsFile = pkgs.writeText "proxy-suite-zapret-hostlist-${rule.name}.txt" (
lib.concatStringsSep "\n" (lib.unique rule.domains) + "\n"
);
in
''
cp ${domainsFile} "$out/opt/zapret/hostlists/list-${rule.name}.txt"
''
) z.hostlistRules}
${patchConfigScript} --config "$out/opt/zapret/config" --spec ${hostlistRuleSpec}
'';
exemptStart = lib.concatMapStrings (cidr: ''
${iptables} -t mangle -I FORWARD 1 -d ${cidr} -j RETURN
${iptables} -t mangle -I POSTROUTING 1 -s ${cidr} -j RETURN
@@ -17,6 +160,21 @@ let
'') z.cidrExemption.cidrs;
in
{
assertions = [
{
assertion = builtins.length hostlistRuleNames == builtins.length (lib.unique hostlistRuleNames);
message = "proxy-suite: zapret.hostlistRules names must be unique";
}
{
assertion = builtins.all (rule: rule.domains != [ ]) z.hostlistRules;
message = "proxy-suite: each zapret.hostlistRules entry must define at least one domain";
}
{
assertion = builtins.all (rule: rule.preset != null || rule.nfqwsArgs != [ ]) z.hostlistRules;
message = "proxy-suite: each zapret.hostlistRules entry must set preset, nfqwsArgs, or both";
}
];
services.zapret-discord-youtube = {
enable = true;
configName = z.configName;
@@ -27,6 +185,32 @@ in
ipsetExclude = z.ipsetExclude;
};
environment.systemPackages = lib.mkBefore [ customZapretPackage ];
systemd.services.zapret-discord-youtube = {
preStart = lib.mkForce ''
${customZapretPackage}/opt/zapret/init.d/sysv/zapret stop || true
${lib.getExe' pkgs.kmod "modprobe"} xt_NFQUEUE 2>/dev/null || true
${lib.getExe' pkgs.kmod "modprobe"} xt_connbytes 2>/dev/null || true
${lib.getExe' pkgs.kmod "modprobe"} xt_multiport 2>/dev/null || true
if ! ${pkgs.ipset}/bin/ipset list nozapret >/dev/null 2>&1; then
${pkgs.ipset}/bin/ipset create nozapret hash:net
fi
'';
serviceConfig = {
ExecStart = lib.mkForce "${customZapretPackage}/opt/zapret/init.d/sysv/zapret start";
ExecStop = lib.mkForce "${customZapretPackage}/opt/zapret/init.d/sysv/zapret stop";
ExecReload = lib.mkForce "${customZapretPackage}/opt/zapret/init.d/sysv/zapret restart";
Environment = lib.mkForce [
"ZAPRET_BASE=${customZapretPackage}/opt/zapret"
"PATH=${lib.makeBinPath runtimeDeps}"
];
};
};
systemd.services.proxy-suite-zapret-vm-exempt = lib.mkIf z.cidrExemption.enable {
description = "Exempt CIDRs from zapret NFQUEUE";
after = [ "zapret-discord-youtube.service" ];