nix/module/Zapret.nix

81 lines
2.1 KiB
Nix
Raw Normal View History

2024-08-28 02:34:26 +03:00
# SOURCE: https://github.com/bol-van/zapret
2024-08-28 01:31:17 +03:00
{ lib, config, pkgs, util, ... }: with lib; let
cfg = config.module.zapret;
whitelist = if cfg.whitelist != null then
"--hostlist ${pkgs.writeText "ZapretWhitelist" (util.trimTabs cfg.whitelist)}"
else "";
blacklist = if cfg.blacklist != null then
"--hostlist-exclude ${pkgs.writeText "ZapretBlacklist" (util.trimTabs cfg.blacklist)}"
else "";
2024-09-02 13:03:19 +03:00
# ISSUE: Seems broken. Adds nothing automatically.
autolist = if cfg.autolist != null then
"--hostlist-auto ${cfg.autolist}"
else "";
2024-08-28 01:31:17 +03:00
in {
options = {
module.zapret = mkOption {
default = {};
type = types.submodule {
options = {
enable = mkEnableOption "Enable Zapret service.";
params = mkOption {
2024-08-28 02:38:36 +03:00
default = null;
2024-08-28 01:31:17 +03:00
type = types.str;
};
whitelist = mkOption {
default = null;
type = types.nullOr types.str;
};
blacklist = mkOption {
default = null;
type = types.nullOr types.str;
};
2024-09-02 13:03:19 +03:00
autolist = mkOption {
default = null;
type = types.nullOr types.str;
};
2024-08-28 02:38:36 +03:00
qnum = mkOption {
default = 200;
type = types.int;
};
2024-08-28 01:31:17 +03:00
};
};
};
};
config = mkIf cfg.enable {
networking.firewall.extraCommands = ''
2024-08-28 02:38:36 +03:00
iptables -t mangle -I POSTROUTING -p tcp -m multiport --dports 80,443 -m connbytes --connbytes-dir=original --connbytes-mode=packets --connbytes 1:6 -m mark ! --mark 0x40000000/0x40000000 -j NFQUEUE --queue-num ${toString cfg.qnum} --queue-bypass
2024-08-28 01:31:17 +03:00
'';
systemd = {
services.zapret = {
description = "FRKN";
wantedBy = [ ];
requires = [ "network.target" ];
path = with pkgs; [ zapret ];
serviceConfig = {
2024-09-02 13:03:19 +03:00
ExecStart = "${pkgs.zapret}/bin/nfqws --pidfile=/run/nfqws.pid ${cfg.params} ${whitelist} ${blacklist} ${autolist} --qnum=${toString cfg.qnum}";
2024-08-28 01:31:17 +03:00
Type = "simple";
PIDFile = "/run/nfqws.pid";
ExecReload = "/bin/kill -HUP $MAINPID";
2024-09-03 18:16:27 +03:00
Restart = "always";
RestartSec = "5s";
RuntimeMaxSec = "1h";
2024-08-28 01:31:17 +03:00
};
};
timers.zapret = {
timerConfig = {
OnBootSec = 5;
Unit = "zapret.service";
};
wantedBy = [ "timers.target" ];
};
};
};
}