window for tray icon, new toggles, formatting

This commit is contained in:
FUFSoB committed 2026-04-12 16:35:01 +05:00
1 parent ce0262b3d5
commit c071f4d3a8
15 files changed
+644 -321

No files matched your search

+17 -7
View File
@@ -1,6 +1,11 @@
# Build-time sing-box configuration templates.
# The proxy outbound(s) are injected at service start time, not here.
{ lib, pkgs, cfg, rules }:
{
lib,
pkgs,
cfg,
rules,
}:
let
sb = cfg.singBox;
@@ -44,9 +49,7 @@ let
};
dnsConfigTun = dnsConfig // {
servers = map (
s: if s.tag == "local" then s // { detour = "proxy"; } else s
) dnsConfig.servers;
servers = map (s: if s.tag == "local" then s // { detour = "proxy"; } else s) dnsConfig.servers;
};
mkConfig =
@@ -89,10 +92,16 @@ let
# proxy outbound(s) are prepended at runtime by the start script
outbounds = [
(
{ type = "direct"; tag = "direct"; }
{
type = "direct";
tag = "direct";
}
// lib.optionalAttrs useOutboundRoutingMark { routing_mark = sb.proxyMark; }
)
{ type = "block"; tag = "block"; }
{
type = "block";
tag = "block";
}
];
route = {
@@ -100,7 +109,8 @@ let
rule_set = rules.geositeRuleSets ++ rules.geoIPRuleSets;
rules = rules.routingRules;
final = if sb.proxyByDefault then "proxy" else "direct";
} // lib.optionalAttrs enableTun { auto_detect_interface = true; };
}
// lib.optionalAttrs enableTun { auto_detect_interface = true; };
}
// clashApiBlock;
+63 -27
View File
@@ -1,8 +1,13 @@
# Factory function — receives the zapret flake, returns a NixOS module.
# Factory function – receives the zapret flake, returns a NixOS module.
# This lets consumers add a single flake input and get everything transitively.
{ zapret }:
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.proxy-suite;
@@ -15,32 +20,63 @@ in
zapret.nixosModules.default
];
config = lib.mkIf cfg.enable (lib.mkMerge [
(lib.mkIf cfg.singBox.enable (
let
rules = import ./rules.nix { inherit lib pkgs cfg zapret; };
configs = import ./config.nix { inherit lib pkgs cfg rules; };
nftr = import ./nftables.nix { inherit lib pkgs cfg; };
in
import ./service.nix {
inherit config lib pkgs cfg;
inherit (configs) tproxyFile tunFile;
inherit (nftr) nftablesRulesFile ip nft;
}
))
config = lib.mkIf cfg.enable (
lib.mkMerge [
(lib.mkIf cfg.singBox.enable (
let
rules = import ./rules.nix {
inherit
lib
pkgs
cfg
zapret
;
};
configs = import ./config.nix {
inherit
lib
pkgs
cfg
rules
;
};
nftr = import ./nftables.nix { inherit lib pkgs cfg; };
in
import ./service.nix {
inherit
config
lib
pkgs
cfg
;
inherit (configs) tproxyFile tunFile;
inherit (nftr) nftablesRulesFile ip nft;
}
))
(lib.mkIf cfg.zapret.enable (
import ./zapret.nix {
inherit lib pkgs cfg zapret;
}
))
(lib.mkIf cfg.zapret.enable (
import ./zapret.nix {
inherit
lib
pkgs
cfg
zapret
;
}
))
(lib.mkIf cfg.tgWsProxy.enable (
import ./tg-ws-proxy.nix { inherit lib pkgs cfg; }
))
(lib.mkIf cfg.tgWsProxy.enable (import ./tg-ws-proxy.nix { inherit lib pkgs cfg; }))
(lib.mkIf cfg.tray.enable (
import ./tray.nix { inherit config lib pkgs cfg; }
))
]);
(lib.mkIf cfg.tray.enable (
import ./tray.nix {
inherit
config
lib
pkgs
cfg
;
}
))
]
);
}
+38 -34
View File
@@ -1,47 +1,51 @@
# TProxy nftables rules
{ lib, pkgs, cfg }:
{
lib,
pkgs,
cfg,
}:
let
sb = cfg.singBox;
localSubnetLines = lib.concatMapStrings (cidr: ''
ip daddr ${cidr} tcp dport != 53 return
ip daddr ${cidr} udp dport != 53 return
ip daddr ${cidr} tcp dport != 53 return
ip daddr ${cidr} udp dport != 53 return
'') sb.tproxy.localSubnets;
nftablesRulesFile = pkgs.writeText "proxy-suite-tproxy.nft" ''
define RESERVED_IP = {
10.0.0.0/8,
100.64.0.0/10,
127.0.0.0/8,
169.254.0.0/16,
172.16.0.0/12,
192.0.0.0/24,
224.0.0.0/4,
240.0.0.0/4,
255.255.255.255/32
}
define RESERVED_IP = {
10.0.0.0/8,
100.64.0.0/10,
127.0.0.0/8,
169.254.0.0/16,
172.16.0.0/12,
192.0.0.0/24,
224.0.0.0/4,
240.0.0.0/4,
255.255.255.255/32
}
table ip singbox {
chain prerouting {
type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return
# Packets re-entering via loopback after output marking should not
# be skipped just because the host has an RFC1918 source address.
iifname != "lo" ip saddr $RESERVED_IP return
${localSubnetLines}
ip protocol tcp tproxy to 127.0.0.1:${toString sb.tproxyPort} meta mark set ${toString sb.fwmark}
ip protocol udp tproxy to 127.0.0.1:${toString sb.tproxyPort} meta mark set ${toString sb.fwmark}
}
chain output {
type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return
${localSubnetLines}
meta mark ${toString sb.proxyMark} return
ip protocol tcp meta mark set ${toString sb.fwmark}
ip protocol udp meta mark set ${toString sb.fwmark}
}
}
table ip singbox {
chain prerouting {
type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return
# Packets re-entering via loopback after output marking should not
# be skipped just because the host has an RFC1918 source address.
iifname != "lo" ip saddr $RESERVED_IP return
${localSubnetLines}
ip protocol tcp tproxy to 127.0.0.1:${toString sb.tproxyPort} meta mark set ${toString sb.fwmark}
ip protocol udp tproxy to 127.0.0.1:${toString sb.tproxyPort} meta mark set ${toString sb.fwmark}
}
chain output {
type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return
${localSubnetLines}
meta mark ${toString sb.proxyMark} return
ip protocol tcp meta mark set ${toString sb.fwmark}
ip protocol udp meta mark set ${toString sb.fwmark}
}
}
'';
ip = "${pkgs.iproute2}/bin/ip";
+5 -2
View File
@@ -41,7 +41,7 @@ let
};
};
# An explicit routing rule — associates traffic patterns with a named outbound.
# An explicit routing rule – associates traffic patterns with a named outbound.
routingRuleType = types.submodule {
options = {
outbound = mkOption {
@@ -471,7 +471,10 @@ in
}
{
name = "example";
domains = [ "example.com" "example.de" ];
domains = [
"example.com"
"example.de"
];
nfqwsArgs = [
"--filter-tcp=443 --dpi-desync=fake,multisplit"
];
+10 -11
View File
@@ -32,16 +32,15 @@ let
syncZapretDirectUserIps = z.enable && z.syncDirectRoutingUserIps;
syncZapretDirectAnyIps = syncZapretDirectUpstreamIps || syncZapretDirectUserIps;
zapretDefaultDomainFiles =
[
"list-general.txt"
"list-google.txt"
]
++ lib.optionals z.includeExtraUpstreamLists [
"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
@@ -192,7 +191,7 @@ let
}
{ action = "sniff"; }
# Per-outbound and explicit rules come first — they take priority.
# Per-outbound and explicit rules come first – they take priority.
(lib.concatMap mkCustomRuleEntries customRules)
# Global proxy lists
+48 -43
View File
@@ -41,9 +41,7 @@ let
if ob.json != null then
let
outboundJson = builtins.toJSON (
ob.json
// { tag = tag; }
// lib.optionalAttrs (routingMark != null) { routing_mark = routingMark; }
ob.json // { tag = tag; } // lib.optionalAttrs (routingMark != null) { routing_mark = routingMark; }
);
jsonFile = pkgs.writeText "proxy-suite-ob-${tag}.json" outboundJson;
in
@@ -58,7 +56,7 @@ let
if ob.urlFile != null then
ob.urlFile
else
# Literal URL — write to nix store so the script can cat it.
# Literal URL – write to nix store so the script can cat it.
# Less secret than urlFile, but convenient for non-sensitive configs.
"${pkgs.writeText "proxy-suite-url-${ob.tag}" ob.url}";
in
@@ -146,7 +144,10 @@ let
proxyCtl = pkgs.writeShellApplication {
name = "proxy-ctl";
runtimeInputs = with pkgs; [ curl jq ];
runtimeInputs = with pkgs; [
curl
jq
];
text = ''
# Embedded at build time from module config
CLASH_API="${clashApi}"
@@ -253,7 +254,7 @@ let
-d "$payload" >/dev/null; then
echo "Switched to: $tag"
else
echo "Failed — is proxy-suite-socks running with selector mode?"
echo "Failed – is proxy-suite-socks running with selector mode?"
exit 1
fi
;;
@@ -272,45 +273,49 @@ in
# nftables must be on for TProxy to work.
networking.nftables.enable = lib.mkIf sb.tproxy.enable (lib.mkDefault true);
assertions =
[
{
assertion = !sb.enable || sb.outbounds != [ ];
message = "proxy-suite: singBox.outbounds must not be empty when singBox.enable = true";
}
{
assertion = builtins.length outboundTags == builtins.length (lib.unique outboundTags);
message = "proxy-suite: outbound tags must be unique";
}
{
assertion = builtins.all (tag: !builtins.elem tag builtinTags) outboundTags;
message = "proxy-suite: outbound tags must not use reserved names: proxy, direct, block";
}
{
assertion = invalidRoutingTargets == [ ];
message = "proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}";
}
{
assertion =
!t.enable
|| builtins.length (builtins.filter (x: x != null) [
t.secret
t.secretFile
]) == 1;
message = "proxy-suite: tgWsProxy requires exactly one of secret or secretFile";
}
]
++ lib.concatMap (ob: [
{
assertion =
builtins.length (builtins.filter (x: x != null) [
assertions = [
{
assertion = !sb.enable || sb.outbounds != [ ];
message = "proxy-suite: singBox.outbounds must not be empty when singBox.enable = true";
}
{
assertion = builtins.length outboundTags == builtins.length (lib.unique outboundTags);
message = "proxy-suite: outbound tags must be unique";
}
{
assertion = builtins.all (tag: !builtins.elem tag builtinTags) outboundTags;
message = "proxy-suite: outbound tags must not use reserved names: proxy, direct, block";
}
{
assertion = invalidRoutingTargets == [ ];
message = "proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}";
}
{
assertion =
!t.enable
||
builtins.length (
builtins.filter (x: x != null) [
t.secret
t.secretFile
]
) == 1;
message = "proxy-suite: tgWsProxy requires exactly one of secret or secretFile";
}
]
++ lib.concatMap (ob: [
{
assertion =
builtins.length (
builtins.filter (x: x != null) [
ob.urlFile
ob.url
ob.json
]) == 1;
message = "proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, or json";
}
]) sb.outbounds;
]
) == 1;
message = "proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, or json";
}
]) sb.outbounds;
systemd.services = {
# Always-on: SOCKS5/HTTP mixed inbound, also ready for TProxy interception.
@@ -331,7 +336,7 @@ in
# systemctl start proxy-suite-tproxy
# systemctl stop proxy-suite-tproxy
proxy-suite-tproxy = {
description = "sing-box TProxy — nftables rules and policy routing";
description = "sing-box TProxy – nftables rules and policy routing";
after = [
"network.target"
"proxy-suite-socks.service"
+12 -7
View File
@@ -1,20 +1,25 @@
# Telegram MTProto WebSocket proxy service
{ lib, pkgs, cfg }:
{
lib,
pkgs,
cfg,
}:
let
t = cfg.tgWsProxy;
tgPkg = import ../../pkgs/tg-ws-proxy.nix { inherit pkgs; };
dcArgs = lib.concatMapStrings (id: " --dc-ip=${id}:${t.dcIps.${id}}") (
builtins.attrNames t.dcIps
);
dcArgs = lib.concatMapStrings (id: " --dc-ip=${id}:${t.dcIps.${id}}") (builtins.attrNames t.dcIps);
startScript = pkgs.writeShellScript "proxy-suite-tg-ws-proxy-start" ''
exec ${tgPkg}/bin/tg-ws-proxy \
--port=${toString t.port} \
--host=${t.host} \
${if t.secretFile != null
then "--secret-file=$CREDENTIALS_DIRECTORY/tg_ws_proxy_secret"
else "--secret=${t.secret}"}${dcArgs}
${
if t.secretFile != null then
"--secret-file=$CREDENTIALS_DIRECTORY/tg_ws_proxy_secret"
else
"--secret=${t.secret}"
}${dcArgs}
'';
in
{
+110 -94
View File
@@ -33,40 +33,52 @@ let
upstreamZapretPackage =
let
upstreamPackages = zapret.packages.${pkgs.system};
upstreamPackages = zapret.packages.${pkgs.stdenv.hostPlatform.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;
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;
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;
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;
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;
});
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 {
@@ -75,79 +87,83 @@ let
};
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
customZapretPackage =
pkgs.runCommand "proxy-suite-zapret-${selectedConfigName}"
{
nativeBuildInputs = with pkgs; [
coreutils
gnused
python3
];
}
''
cp ${domainsFile} "$out/opt/zapret/hostlists/list-${rule.name}.txt"
''
) z.hostlistRules}
set -euo pipefail
${patchConfigScript} --config "$out/opt/zapret/config" --spec ${hostlistRuleSpec}
'';
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