feat: allow setting zapret custom rules
This commit is contained in:
7 files changed
+679
-11
No files matched your search
@@ -270,7 +270,11 @@ zapret = {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
When `syncDirectRouting = true`, the module reuses zapret's upstream `hostlists/list-general.txt`, `list-google.txt`, `list-instagram.txt`, `list-soundcloud.txt`, and `list-twitter.txt`, adds your `listGeneral`, and subtracts both upstream and user domain exclusions before generating sing-box direct rules.
|
When `syncDirectRouting = true`, the module always reuses zapret's upstream `hostlists/list-general.txt` and `list-google.txt`, adds your `listGeneral`, and subtracts both upstream and user domain exclusions before generating sing-box direct rules. If `zapret.includeExtraUpstreamLists = true` (default), it also includes upstream `list-instagram.txt`, `list-soundcloud.txt`, and `list-twitter.txt` in both zapret handling and sing-box direct sync.
|
||||||
|
|
||||||
|
Custom `zapret.hostlistRules` domains also join this direct-domain sync by default. Set `enableDirectSync = false` on a custom hostlist if you want zapret to handle it but do not want sing-box direct-routing rules generated from it.
|
||||||
|
|
||||||
|
The generated zapret config also auto-activates upstream `list-instagram.txt`, `list-soundcloud.txt`, and `list-twitter.txt` when the selected upstream preset does not already reference them. They are attached using the active config's `general` rule family so those hostlists are actually handled by zapret instead of just existing on disk. Set `zapret.includeExtraUpstreamLists = false;` to opt out.
|
||||||
|
|
||||||
When `syncDirectRoutingUpstreamIps = true`, the module also mirrors zapret's upstream `ipset-all.txt`. This is disabled by default because the upstream IP set is intentionally broad.
|
When `syncDirectRoutingUpstreamIps = true`, the module also mirrors zapret's upstream `ipset-all.txt`. This is disabled by default because the upstream IP set is intentionally broad.
|
||||||
|
|
||||||
@@ -285,6 +289,35 @@ zapret.cidrExemption = {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can also define custom zapret hostlists with per-list rules:
|
||||||
|
|
||||||
|
```nix
|
||||||
|
zapret.hostlistRules = [
|
||||||
|
{
|
||||||
|
name = "googlevideo";
|
||||||
|
domains = [ "googlevideo.com" "ggpht.com" ];
|
||||||
|
preset = "google";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "example";
|
||||||
|
domains = [ "example.com" "example.de" ];
|
||||||
|
nfqwsArgs = [
|
||||||
|
"--filter-tcp=443 --dpi-desync=fake,multisplit"
|
||||||
|
];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "social-extra";
|
||||||
|
domains = [ "x.example" ];
|
||||||
|
preset = "twitter";
|
||||||
|
nfqwsArgs = [
|
||||||
|
"--filter-udp=443 --dpi-desync=fake --dpi-desync-repeats=6"
|
||||||
|
];
|
||||||
|
}
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
|
Each entry generates `hostlists/list-<name>.txt`. `preset` clones the active zapret config's matching family rules for that hostlist, while `nfqwsArgs` appends custom NFQWS rule fragments. The module injects the generated `--hostlist=...`, standard zapret exclude files, and trailing `--new` automatically.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Telegram proxy
|
## Telegram proxy
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ in
|
|||||||
))
|
))
|
||||||
|
|
||||||
(lib.mkIf cfg.zapret.enable (
|
(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 (
|
(lib.mkIf cfg.tgWsProxy.enable (
|
||||||
|
|||||||
@@ -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
|
in
|
||||||
{
|
{
|
||||||
options.services.proxy-suite = {
|
options.services.proxy-suite = {
|
||||||
@@ -389,6 +442,43 @@ in
|
|||||||
description = "IPs/CIDRs to exclude from zapret's ipset.";
|
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 = {
|
cidrExemption = {
|
||||||
enable = mkEnableOption "CIDR exemption from zapret NFQUEUE";
|
enable = mkEnableOption "CIDR exemption from zapret NFQUEUE";
|
||||||
|
|
||||||
|
|||||||
@@ -32,13 +32,16 @@ let
|
|||||||
syncZapretDirectUserIps = z.enable && z.syncDirectRoutingUserIps;
|
syncZapretDirectUserIps = z.enable && z.syncDirectRoutingUserIps;
|
||||||
syncZapretDirectAnyIps = syncZapretDirectUpstreamIps || syncZapretDirectUserIps;
|
syncZapretDirectAnyIps = syncZapretDirectUpstreamIps || syncZapretDirectUserIps;
|
||||||
|
|
||||||
zapretDefaultDomainFiles = [
|
zapretDefaultDomainFiles =
|
||||||
"list-general.txt"
|
[
|
||||||
"list-google.txt"
|
"list-general.txt"
|
||||||
"list-instagram.txt"
|
"list-google.txt"
|
||||||
"list-soundcloud.txt"
|
]
|
||||||
"list-twitter.txt"
|
++ lib.optionals z.includeExtraUpstreamLists [
|
||||||
];
|
"list-instagram.txt"
|
||||||
|
"list-soundcloud.txt"
|
||||||
|
"list-twitter.txt"
|
||||||
|
];
|
||||||
|
|
||||||
zapretDefaultDomains =
|
zapretDefaultDomains =
|
||||||
if syncZapretDirectDomains then
|
if syncZapretDirectDomains then
|
||||||
@@ -50,6 +53,11 @@ let
|
|||||||
zapretDefaultIps =
|
zapretDefaultIps =
|
||||||
if syncZapretDirectUpstreamIps then parseListFile "${zapretSrc}/hostlists/ipset-all.txt" else [ ];
|
if syncZapretDirectUpstreamIps then parseListFile "${zapretSrc}/hostlists/ipset-all.txt" else [ ];
|
||||||
zapretUserIps = if syncZapretDirectUserIps then z.ipsetAll 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 =
|
zapretExcludedDomains =
|
||||||
if syncZapretDirectDomains then parseListFile "${zapretSrc}/hostlists/list-exclude.txt" else [ ];
|
if syncZapretDirectDomains then parseListFile "${zapretSrc}/hostlists/list-exclude.txt" else [ ];
|
||||||
zapretExcludedIps = lib.unique (
|
zapretExcludedIps = lib.unique (
|
||||||
@@ -64,7 +72,7 @@ let
|
|||||||
|
|
||||||
zapretDirectDomains =
|
zapretDirectDomains =
|
||||||
if syncZapretDirectDomains then
|
if syncZapretDirectDomains then
|
||||||
subtractItems (lib.unique (zapretDefaultDomains ++ z.listGeneral)) (
|
subtractItems (lib.unique (zapretDefaultDomains ++ z.listGeneral ++ zapretCustomDomains)) (
|
||||||
lib.unique (zapretExcludedDomains ++ z.listExclude)
|
lib.unique (zapretExcludedDomains ++ z.listExclude)
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -1,11 +1,154 @@
|
|||||||
# zapret DPI bypass configuration + optional CIDR exemption from NFQUEUE
|
# zapret DPI bypass configuration + optional CIDR exemption from NFQUEUE
|
||||||
{ lib, pkgs, cfg }:
|
{
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
cfg,
|
||||||
|
zapret,
|
||||||
|
}:
|
||||||
|
|
||||||
let
|
let
|
||||||
z = cfg.zapret;
|
z = cfg.zapret;
|
||||||
|
|
||||||
iptables = "${pkgs.iptables}/bin/iptables";
|
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: ''
|
exemptStart = lib.concatMapStrings (cidr: ''
|
||||||
${iptables} -t mangle -I FORWARD 1 -d ${cidr} -j RETURN
|
${iptables} -t mangle -I FORWARD 1 -d ${cidr} -j RETURN
|
||||||
${iptables} -t mangle -I POSTROUTING 1 -s ${cidr} -j RETURN
|
${iptables} -t mangle -I POSTROUTING 1 -s ${cidr} -j RETURN
|
||||||
@@ -17,6 +160,21 @@ let
|
|||||||
'') z.cidrExemption.cidrs;
|
'') z.cidrExemption.cidrs;
|
||||||
in
|
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 = {
|
services.zapret-discord-youtube = {
|
||||||
enable = true;
|
enable = true;
|
||||||
configName = z.configName;
|
configName = z.configName;
|
||||||
@@ -27,6 +185,32 @@ in
|
|||||||
ipsetExclude = z.ipsetExclude;
|
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 {
|
systemd.services.proxy-suite-zapret-vm-exempt = lib.mkIf z.cidrExemption.enable {
|
||||||
description = "Exempt CIDRs from zapret NFQUEUE";
|
description = "Exempt CIDRs from zapret NFQUEUE";
|
||||||
after = [ "zapret-discord-youtube.service" ];
|
after = [ "zapret-discord-youtube.service" ];
|
||||||
|
|||||||
+156
@@ -54,6 +54,13 @@ let
|
|||||||
dnsHasRuleSet =
|
dnsHasRuleSet =
|
||||||
dnsRules: ruleSet:
|
dnsRules: ruleSet:
|
||||||
builtins.any (rule: (rule ? rule_set) && builtins.elem ruleSet rule.rule_set) dnsRules;
|
builtins.any (rule: (rule ? rule_set) && builtins.elem ruleSet rule.rule_set) dnsRules;
|
||||||
|
mkZapretBase =
|
||||||
|
fixture:
|
||||||
|
let
|
||||||
|
env = fixture.config.systemd.services.zapret-discord-youtube.serviceConfig.Environment;
|
||||||
|
zapretBaseEnv = builtins.head (builtins.filter (value: pkgs.lib.hasPrefix "ZAPRET_BASE=" value) env);
|
||||||
|
in
|
||||||
|
pkgs.lib.removePrefix "ZAPRET_BASE=" zapretBaseEnv;
|
||||||
packagePathMatches =
|
packagePathMatches =
|
||||||
packages: pattern:
|
packages: pattern:
|
||||||
builtins.any (
|
builtins.any (
|
||||||
@@ -222,6 +229,19 @@ let
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
zapretSyncRules = mkRoutingRules zapretSyncFixture;
|
zapretSyncRules = mkRoutingRules zapretSyncFixture;
|
||||||
|
zapretSyncBase = mkZapretBase zapretSyncFixture;
|
||||||
|
|
||||||
|
zapretSyncNoExtraListsFixture = evalProxySuite [
|
||||||
|
baseModule
|
||||||
|
{
|
||||||
|
services.proxy-suite.zapret = {
|
||||||
|
enable = true;
|
||||||
|
includeExtraUpstreamLists = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
zapretSyncNoExtraListsBase = mkZapretBase zapretSyncNoExtraListsFixture;
|
||||||
|
zapretSyncNoExtraListsRules = mkRoutingRules zapretSyncNoExtraListsFixture;
|
||||||
|
|
||||||
zapretSyncIpsFixture = evalProxySuite [
|
zapretSyncIpsFixture = evalProxySuite [
|
||||||
baseModule
|
baseModule
|
||||||
@@ -314,6 +334,93 @@ let
|
|||||||
];
|
];
|
||||||
zapretIpExcludesRules = mkRoutingRules zapretIpExcludesFixture;
|
zapretIpExcludesRules = mkRoutingRules zapretIpExcludesFixture;
|
||||||
|
|
||||||
|
zapretHostlistRulesFixture = evalProxySuite [
|
||||||
|
baseModule
|
||||||
|
{
|
||||||
|
services.proxy-suite.zapret = {
|
||||||
|
enable = true;
|
||||||
|
hostlistRules = [
|
||||||
|
{
|
||||||
|
name = "googlevideo";
|
||||||
|
domains = [ "googlevideo.com" "ggpht.com" ];
|
||||||
|
preset = "google";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "example";
|
||||||
|
domains = [ "example.com" "example.de" ];
|
||||||
|
nfqwsArgs = [ "--filter-tcp=443 --dpi-desync=fake,multisplit" ];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "twitter-no-direct";
|
||||||
|
domains = [ "x.example" ];
|
||||||
|
nfqwsArgs = [ "--filter-udp=443 --dpi-desync=fake --dpi-desync-repeats=6" ];
|
||||||
|
enableDirectSync = false;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
zapretHostlistRules = mkRoutingRules zapretHostlistRulesFixture;
|
||||||
|
zapretHostlistBase = mkZapretBase zapretHostlistRulesFixture;
|
||||||
|
|
||||||
|
duplicateZapretHostlistNames = forceEval (
|
||||||
|
(evalProxySuite [
|
||||||
|
baseModule
|
||||||
|
{
|
||||||
|
services.proxy-suite.zapret = {
|
||||||
|
enable = true;
|
||||||
|
hostlistRules = [
|
||||||
|
{
|
||||||
|
name = "dup";
|
||||||
|
domains = [ "one.example" ];
|
||||||
|
nfqwsArgs = [ "--filter-tcp=443 --dpi-desync=fake" ];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
name = "dup";
|
||||||
|
domains = [ "two.example" ];
|
||||||
|
nfqwsArgs = [ "--filter-tcp=443 --dpi-desync=fake" ];
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
]).config.system.build.toplevel.drvPath
|
||||||
|
);
|
||||||
|
|
||||||
|
emptyZapretHostlistDomains = forceEval (
|
||||||
|
(evalProxySuite [
|
||||||
|
baseModule
|
||||||
|
{
|
||||||
|
services.proxy-suite.zapret = {
|
||||||
|
enable = true;
|
||||||
|
hostlistRules = [
|
||||||
|
{
|
||||||
|
name = "empty";
|
||||||
|
domains = [ ];
|
||||||
|
nfqwsArgs = [ "--filter-tcp=443 --dpi-desync=fake" ];
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
]).config.system.build.toplevel.drvPath
|
||||||
|
);
|
||||||
|
|
||||||
|
missingZapretHostlistAction = forceEval (
|
||||||
|
(evalProxySuite [
|
||||||
|
baseModule
|
||||||
|
{
|
||||||
|
services.proxy-suite.zapret = {
|
||||||
|
enable = true;
|
||||||
|
hostlistRules = [
|
||||||
|
{
|
||||||
|
name = "missing";
|
||||||
|
domains = [ "missing.example" ];
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
]).config.system.build.toplevel.drvPath
|
||||||
|
);
|
||||||
|
|
||||||
proxyDirectFixture = evalProxySuite [
|
proxyDirectFixture = evalProxySuite [
|
||||||
baseModule
|
baseModule
|
||||||
{
|
{
|
||||||
@@ -387,6 +494,18 @@ let
|
|||||||
assert unknownRoutingTarget.success == false;
|
assert unknownRoutingTarget.success == false;
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
(
|
||||||
|
assert duplicateZapretHostlistNames.success == false;
|
||||||
|
true
|
||||||
|
)
|
||||||
|
(
|
||||||
|
assert emptyZapretHostlistDomains.success == false;
|
||||||
|
true
|
||||||
|
)
|
||||||
|
(
|
||||||
|
assert missingZapretHostlistAction.success == false;
|
||||||
|
true
|
||||||
|
)
|
||||||
(
|
(
|
||||||
assert tproxyWithFirewall.config.networking.firewall.enable;
|
assert tproxyWithFirewall.config.networking.firewall.enable;
|
||||||
true
|
true
|
||||||
@@ -447,6 +566,10 @@ let
|
|||||||
assert hasDirectDomain zapretSyncRules "cloudflare-ech.com";
|
assert hasDirectDomain zapretSyncRules "cloudflare-ech.com";
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
(
|
||||||
|
assert hasDirectDomain zapretSyncRules "twitter.com";
|
||||||
|
true
|
||||||
|
)
|
||||||
(
|
(
|
||||||
assert !(hasDirectIP zapretSyncRules "1.1.1.0/24");
|
assert !(hasDirectIP zapretSyncRules "1.1.1.0/24");
|
||||||
true
|
true
|
||||||
@@ -463,6 +586,10 @@ let
|
|||||||
assert hasDirectIP zapretSyncIpsRules "1.1.1.0/24";
|
assert hasDirectIP zapretSyncIpsRules "1.1.1.0/24";
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
(
|
||||||
|
assert !(hasDirectDomain zapretSyncNoExtraListsRules "twitter.com");
|
||||||
|
true
|
||||||
|
)
|
||||||
(
|
(
|
||||||
assert !(hasDirectIP zapretSyncUserIpsDisabledRules "203.0.113.0/24");
|
assert !(hasDirectIP zapretSyncUserIpsDisabledRules "203.0.113.0/24");
|
||||||
true
|
true
|
||||||
@@ -491,6 +618,18 @@ let
|
|||||||
assert !(hasDirectIP zapretIpExcludesRules "1.1.1.0/24");
|
assert !(hasDirectIP zapretIpExcludesRules "1.1.1.0/24");
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
(
|
||||||
|
assert hasDirectDomain zapretHostlistRules "googlevideo.com";
|
||||||
|
true
|
||||||
|
)
|
||||||
|
(
|
||||||
|
assert hasDirectDomain zapretHostlistRules "example.com";
|
||||||
|
true
|
||||||
|
)
|
||||||
|
(
|
||||||
|
assert !(hasDirectDomain zapretHostlistRules "x.example");
|
||||||
|
true
|
||||||
|
)
|
||||||
(
|
(
|
||||||
assert hasRuleSet blockGeoRules "block" "geosite-category-ads-all";
|
assert hasRuleSet blockGeoRules "block" "geosite-category-ads-all";
|
||||||
true
|
true
|
||||||
@@ -539,6 +678,23 @@ in
|
|||||||
{
|
{
|
||||||
proxy-suite-module = builtins.seq validated (pkgs.writeText "proxy-suite-module-check" "ok");
|
proxy-suite-module = builtins.seq validated (pkgs.writeText "proxy-suite-module-check" "ok");
|
||||||
|
|
||||||
|
zapret-hostlist-rules =
|
||||||
|
pkgs.runCommand "proxy-suite-zapret-hostlist-rules-check" { }
|
||||||
|
''
|
||||||
|
grep -F -- '--hostlist="${zapretSyncBase}/hostlists/list-twitter.txt"' "${zapretSyncBase}/config"
|
||||||
|
grep -F -- '--hostlist="${zapretSyncBase}/hostlists/list-instagram.txt"' "${zapretSyncBase}/config"
|
||||||
|
grep -F -- '--hostlist="${zapretSyncBase}/hostlists/list-soundcloud.txt"' "${zapretSyncBase}/config"
|
||||||
|
! grep -F -- '--hostlist="${zapretSyncNoExtraListsBase}/hostlists/list-twitter.txt"' "${zapretSyncNoExtraListsBase}/config"
|
||||||
|
! grep -F -- '--hostlist="${zapretSyncNoExtraListsBase}/hostlists/list-instagram.txt"' "${zapretSyncNoExtraListsBase}/config"
|
||||||
|
! grep -F -- '--hostlist="${zapretSyncNoExtraListsBase}/hostlists/list-soundcloud.txt"' "${zapretSyncNoExtraListsBase}/config"
|
||||||
|
grep -F 'googlevideo.com' "${zapretHostlistBase}/hostlists/list-googlevideo.txt"
|
||||||
|
grep -F 'example.de' "${zapretHostlistBase}/hostlists/list-example.txt"
|
||||||
|
grep -F -- '--hostlist="${zapretHostlistBase}/hostlists/list-googlevideo.txt"' "${zapretHostlistBase}/config"
|
||||||
|
grep -F -- '--hostlist="${zapretHostlistBase}/hostlists/list-example.txt"' "${zapretHostlistBase}/config"
|
||||||
|
grep -F -- '--filter-tcp=443 --dpi-desync=fake,multisplit --hostlist="${zapretHostlistBase}/hostlists/list-example.txt" --hostlist-exclude="${zapretHostlistBase}/hostlists/list-exclude.txt" --hostlist-exclude="${zapretHostlistBase}/hostlists/list-exclude-user.txt" --ipset-exclude="${zapretHostlistBase}/hostlists/ipset-exclude.txt" --ipset-exclude="${zapretHostlistBase}/hostlists/ipset-exclude-user.txt" --new' "${zapretHostlistBase}/config"
|
||||||
|
touch "$out"
|
||||||
|
'';
|
||||||
|
|
||||||
build-outbound-parser =
|
build-outbound-parser =
|
||||||
pkgs.runCommand "build-outbound-parser-check" { nativeBuildInputs = [ pkgs.python3 ]; }
|
pkgs.runCommand "build-outbound-parser-check" { nativeBuildInputs = [ pkgs.python3 ]; }
|
||||||
''
|
''
|
||||||
|
|||||||
@@ -0,0 +1,195 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
BUILTIN_ACTIVATION_FALLBACKS = {
|
||||||
|
"list-instagram.txt": "general",
|
||||||
|
"list-soundcloud.txt": "general",
|
||||||
|
"list-twitter.txt": "general",
|
||||||
|
}
|
||||||
|
|
||||||
|
FAMILY_PATTERNS = {
|
||||||
|
"general": {
|
||||||
|
"match": ["list-general.txt", "list-general-user.txt"],
|
||||||
|
"remove": [
|
||||||
|
r'\s+--hostlist=(?:"[^"]*/hostlists/list-general\.txt"|[^ ]*/hostlists/list-general\.txt)',
|
||||||
|
r'\s+--hostlist=(?:"[^"]*/hostlists/list-general-user\.txt"|[^ ]*/hostlists/list-general-user\.txt)',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"google": {
|
||||||
|
"match": ["list-google.txt"],
|
||||||
|
"remove": [
|
||||||
|
r'\s+--hostlist=(?:"[^"]*/hostlists/list-google\.txt"|[^ ]*/hostlists/list-google\.txt)',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"instagram": {
|
||||||
|
"match": ["list-instagram.txt"],
|
||||||
|
"remove": [
|
||||||
|
r'\s+--hostlist=(?:"[^"]*/hostlists/list-instagram\.txt"|[^ ]*/hostlists/list-instagram\.txt)',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"soundcloud": {
|
||||||
|
"match": ["list-soundcloud.txt"],
|
||||||
|
"remove": [
|
||||||
|
r'\s+--hostlist=(?:"[^"]*/hostlists/list-soundcloud\.txt"|[^ ]*/hostlists/list-soundcloud\.txt)',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"twitter": {
|
||||||
|
"match": ["list-twitter.txt"],
|
||||||
|
"remove": [
|
||||||
|
r'\s+--hostlist=(?:"[^"]*/hostlists/list-twitter\.txt"|[^ ]*/hostlists/list-twitter\.txt)',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def locate_nfqws_block(lines: list[str]) -> tuple[int, int]:
|
||||||
|
start = None
|
||||||
|
for index, line in enumerate(lines):
|
||||||
|
if line.startswith('NFQWS_OPT="'):
|
||||||
|
start = index
|
||||||
|
break
|
||||||
|
if start is None:
|
||||||
|
raise ValueError("Could not find NFQWS_OPT block in zapret config")
|
||||||
|
|
||||||
|
for index in range(start + 1, len(lines)):
|
||||||
|
if lines[index] == '"':
|
||||||
|
return start, index
|
||||||
|
|
||||||
|
raise ValueError("Could not find the end of the NFQWS_OPT block in zapret config")
|
||||||
|
|
||||||
|
|
||||||
|
def strip_trailing_new(line: str) -> str:
|
||||||
|
return re.sub(r"(?:\s+--new)+\s*$", "", line.strip())
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_spaces(line: str) -> str:
|
||||||
|
return re.sub(r"\s+", " ", line).strip()
|
||||||
|
|
||||||
|
|
||||||
|
def add_standard_excludes(line: str, standard_excludes: list[str]) -> str:
|
||||||
|
for arg in standard_excludes:
|
||||||
|
if arg not in line:
|
||||||
|
line = f"{line} {arg}"
|
||||||
|
return line
|
||||||
|
|
||||||
|
|
||||||
|
def render_preset_clone(
|
||||||
|
line: str,
|
||||||
|
family: str,
|
||||||
|
hostlist_path: str,
|
||||||
|
standard_excludes: list[str],
|
||||||
|
) -> str:
|
||||||
|
rendered = strip_trailing_new(line)
|
||||||
|
for pattern in FAMILY_PATTERNS[family]["remove"]:
|
||||||
|
rendered = re.sub(pattern, "", rendered)
|
||||||
|
rendered = normalize_spaces(rendered)
|
||||||
|
rendered = f'{rendered} --hostlist="{hostlist_path}"'
|
||||||
|
rendered = add_standard_excludes(rendered, standard_excludes)
|
||||||
|
return f"{normalize_spaces(rendered)} --new"
|
||||||
|
|
||||||
|
|
||||||
|
def render_custom_args(
|
||||||
|
fragment: str,
|
||||||
|
hostlist_path: str,
|
||||||
|
standard_excludes: list[str],
|
||||||
|
) -> str:
|
||||||
|
rendered = strip_trailing_new(fragment)
|
||||||
|
rendered = re.sub(r'\s+--hostlist=(?:"[^"]+"|\S+)', "", rendered)
|
||||||
|
rendered = normalize_spaces(rendered)
|
||||||
|
rendered = f'{rendered} --hostlist="{hostlist_path}"'
|
||||||
|
rendered = add_standard_excludes(rendered, standard_excludes)
|
||||||
|
return f"{normalize_spaces(rendered)} --new"
|
||||||
|
|
||||||
|
|
||||||
|
def clone_family_lines(
|
||||||
|
existing_lines: list[str],
|
||||||
|
family: str,
|
||||||
|
hostlist_path: str,
|
||||||
|
standard_excludes: list[str],
|
||||||
|
) -> list[str]:
|
||||||
|
matches = [
|
||||||
|
line
|
||||||
|
for line in existing_lines
|
||||||
|
if any(marker in line for marker in FAMILY_PATTERNS[family]["match"])
|
||||||
|
]
|
||||||
|
if not matches:
|
||||||
|
raise ValueError(f"Preset family '{family}' is not present in the selected zapret config")
|
||||||
|
return [render_preset_clone(line, family, hostlist_path, standard_excludes) for line in matches]
|
||||||
|
|
||||||
|
|
||||||
|
def activate_builtin_hostlists(
|
||||||
|
existing_lines: list[str],
|
||||||
|
hostlists_dir: Path,
|
||||||
|
standard_excludes: list[str],
|
||||||
|
) -> list[str]:
|
||||||
|
generated_lines: list[str] = []
|
||||||
|
full_text = "\n".join(existing_lines)
|
||||||
|
|
||||||
|
for filename, family in BUILTIN_ACTIVATION_FALLBACKS.items():
|
||||||
|
marker = str(hostlists_dir / filename)
|
||||||
|
if marker in full_text:
|
||||||
|
continue
|
||||||
|
generated_lines.extend(
|
||||||
|
clone_family_lines(existing_lines, family, marker, standard_excludes)
|
||||||
|
)
|
||||||
|
|
||||||
|
return generated_lines
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Append proxy-suite custom hostlist rules to a zapret config."
|
||||||
|
)
|
||||||
|
parser.add_argument("--config", required=True, help="Path to the selected zapret config file")
|
||||||
|
parser.add_argument("--spec", required=True, help="JSON file describing custom hostlist rules")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
config_path = Path(args.config)
|
||||||
|
spec_path = Path(args.spec)
|
||||||
|
hostlists_dir = config_path.parent / "hostlists"
|
||||||
|
standard_excludes = [
|
||||||
|
f'--hostlist-exclude="{hostlists_dir / "list-exclude.txt"}"',
|
||||||
|
f'--hostlist-exclude="{hostlists_dir / "list-exclude-user.txt"}"',
|
||||||
|
f'--ipset-exclude="{hostlists_dir / "ipset-exclude.txt"}"',
|
||||||
|
f'--ipset-exclude="{hostlists_dir / "ipset-exclude-user.txt"}"',
|
||||||
|
]
|
||||||
|
|
||||||
|
lines = config_path.read_text(encoding="utf-8").splitlines()
|
||||||
|
spec = json.loads(spec_path.read_text(encoding="utf-8"))
|
||||||
|
entries = spec.get("entries", [])
|
||||||
|
include_extra_upstream_lists = spec.get("includeExtraUpstreamLists", True)
|
||||||
|
|
||||||
|
start, end = locate_nfqws_block(lines)
|
||||||
|
nfqws_lines = lines[start + 1 : end]
|
||||||
|
generated_lines: list[str] = (
|
||||||
|
activate_builtin_hostlists(nfqws_lines, hostlists_dir, standard_excludes)
|
||||||
|
if include_extra_upstream_lists
|
||||||
|
else []
|
||||||
|
)
|
||||||
|
|
||||||
|
for entry in entries:
|
||||||
|
hostlist_path = str(hostlists_dir / f'list-{entry["name"]}.txt')
|
||||||
|
preset = entry.get("preset")
|
||||||
|
if preset:
|
||||||
|
generated_lines.extend(
|
||||||
|
clone_family_lines(nfqws_lines, preset, hostlist_path, standard_excludes)
|
||||||
|
)
|
||||||
|
for fragment in entry.get("nfqwsArgs", []):
|
||||||
|
generated_lines.append(render_custom_args(fragment, hostlist_path, standard_excludes))
|
||||||
|
|
||||||
|
updated_lines = lines[:end] + generated_lines + lines[end:]
|
||||||
|
config_path.write_text("\n".join(updated_lines) + "\n", encoding="utf-8")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
raise SystemExit(main())
|
||||||
|
except ValueError as exc:
|
||||||
|
print(f"error: {exc}", file=sys.stderr)
|
||||||
|
raise SystemExit(1)
|
||||||
Reference in new issue
Block a user