chore: refactoring (split)
This commit is contained in:
84 files changed
+9149
-7244
No files matched your search
+23
-464
@@ -9,478 +9,37 @@
|
||||
|
||||
let
|
||||
derived = import ./derived.nix { inherit lib cfg; };
|
||||
constants = derived.constants;
|
||||
inherit (derived)
|
||||
proxyCfg
|
||||
singBoxCfg
|
||||
xrayEnabled
|
||||
hybridEnabled
|
||||
pureXrayEnabled
|
||||
globalTun
|
||||
globalTproxy
|
||||
clashApiEnabled
|
||||
;
|
||||
inherit (constants)
|
||||
xrayGlobalTunIPv6Address
|
||||
xrayPerAppTunIPv6Address
|
||||
xrayDnsBridgePorts
|
||||
;
|
||||
perAppTun = derived.perAppRoutingTun;
|
||||
direct = rules.direct;
|
||||
defaultTunAutoRouteTableIndex = constants.tunAutoRouteTableIndex;
|
||||
defaultTunAutoRouteRulePriority = constants.tunAutoRouteRulePriority;
|
||||
xrayFakeDnsPools = [
|
||||
{
|
||||
ipPool = "198.18.0.0/15";
|
||||
poolSize = 32768;
|
||||
}
|
||||
{
|
||||
ipPool = "fc00::/18";
|
||||
poolSize = 32768;
|
||||
}
|
||||
];
|
||||
stripCidr = cidr: builtins.head (lib.splitString "/" cidr);
|
||||
|
||||
mkSingBoxDnsServer =
|
||||
tag: upstream: detour:
|
||||
{
|
||||
inherit tag;
|
||||
type = upstream.type;
|
||||
server = upstream.address;
|
||||
server_port = upstream.port;
|
||||
}
|
||||
// lib.optionalAttrs (detour != null) { inherit detour; };
|
||||
|
||||
mkSingBoxDnsConfig =
|
||||
{
|
||||
localDetour ? null,
|
||||
}:
|
||||
{
|
||||
servers = [
|
||||
(mkSingBoxDnsServer "remote" proxyCfg.dns.remote "proxy")
|
||||
(mkSingBoxDnsServer "local" proxyCfg.dns.local localDetour)
|
||||
];
|
||||
rules =
|
||||
lib.optional (builtins.elem "google" proxyCfg.routing.proxy.geosites) {
|
||||
rule_set = [ "geosite-google" ];
|
||||
server = "remote";
|
||||
}
|
||||
++ lib.optional (direct.geosites != [ ]) {
|
||||
rule_set = map (s: "geosite-${s}") direct.geosites;
|
||||
server = "local";
|
||||
};
|
||||
final = if proxyCfg.proxyByDefault then "remote" else "local";
|
||||
};
|
||||
|
||||
xrayDnsBridgeHijackRule = {
|
||||
inbound = [ "xray-dns-in" ];
|
||||
network = [
|
||||
"tcp"
|
||||
"udp"
|
||||
];
|
||||
action = "hijack-dns";
|
||||
singBoxTemplates = import ./config-templates/sing-box.nix {
|
||||
inherit
|
||||
lib
|
||||
derived
|
||||
rules
|
||||
;
|
||||
};
|
||||
xrayTemplates = import ./config-templates/xray.nix {
|
||||
inherit
|
||||
lib
|
||||
derived
|
||||
rules
|
||||
;
|
||||
};
|
||||
|
||||
clashApiBlock = lib.optionalAttrs clashApiEnabled {
|
||||
experimental = {
|
||||
clash_api = {
|
||||
external_controller = "127.0.0.1:${toString singBoxCfg.clashApiPort}";
|
||||
};
|
||||
};
|
||||
};
|
||||
selectedTemplates = if derived.pureXrayEnabled then xrayTemplates else singBoxTemplates;
|
||||
routeModeRules =
|
||||
if derived.pureXrayEnabled then rules.xrayRouteModeRules else rules.singBoxRouteModeRules;
|
||||
|
||||
mkSingBoxConfig =
|
||||
{
|
||||
enableMixed ? false,
|
||||
enableTProxy ? false,
|
||||
enableTun ? false,
|
||||
tunInterface ? globalTun.interface,
|
||||
tunAddress ? globalTun.address,
|
||||
tunMtu ? globalTun.mtu,
|
||||
tunAutoRoute ? true,
|
||||
tunAutoRouteTableIndex ? defaultTunAutoRouteTableIndex,
|
||||
tunAutoRouteRuleIndex ? defaultTunAutoRouteRulePriority,
|
||||
tunAutoRedirect ? true,
|
||||
tunStrictRoute ? true,
|
||||
forceLocalDnsViaProxy ? false,
|
||||
useOutboundRoutingMark ? false,
|
||||
enableClashApi ? clashApiEnabled,
|
||||
enableXrayDnsBridge ? hybridEnabled,
|
||||
xrayDnsBridgePort ? xrayDnsBridgePorts.socks,
|
||||
}:
|
||||
{
|
||||
log.level = "warn";
|
||||
|
||||
dns = mkSingBoxDnsConfig {
|
||||
localDetour = if forceLocalDnsViaProxy then "proxy" else null;
|
||||
};
|
||||
|
||||
inbounds =
|
||||
lib.optional enableXrayDnsBridge {
|
||||
type = "direct";
|
||||
tag = "xray-dns-in";
|
||||
listen = "127.0.0.1";
|
||||
listen_port = xrayDnsBridgePort;
|
||||
}
|
||||
++ lib.optional enableMixed {
|
||||
type = "mixed";
|
||||
tag = "mixed-in";
|
||||
listen = proxyCfg.listenAddress;
|
||||
listen_port = proxyCfg.port;
|
||||
}
|
||||
++ lib.optional enableTProxy {
|
||||
type = "tproxy";
|
||||
tag = "tproxy-in";
|
||||
listen = "127.0.0.1";
|
||||
listen_port = globalTproxy.port;
|
||||
}
|
||||
++ lib.optional enableTun (
|
||||
{
|
||||
type = "tun";
|
||||
tag = "tun-in";
|
||||
interface_name = tunInterface;
|
||||
address = [ tunAddress ];
|
||||
mtu = tunMtu;
|
||||
auto_route = tunAutoRoute;
|
||||
auto_redirect = tunAutoRedirect;
|
||||
strict_route = tunStrictRoute;
|
||||
stack = "mixed";
|
||||
}
|
||||
// lib.optionalAttrs tunAutoRoute {
|
||||
iproute2_table_index = tunAutoRouteTableIndex;
|
||||
iproute2_rule_index = tunAutoRouteRuleIndex;
|
||||
}
|
||||
);
|
||||
|
||||
outbounds = [
|
||||
(
|
||||
{
|
||||
type = "direct";
|
||||
tag = "direct";
|
||||
}
|
||||
// lib.optionalAttrs useOutboundRoutingMark { routing_mark = globalTproxy.proxyMark; }
|
||||
)
|
||||
{
|
||||
type = "block";
|
||||
tag = "block";
|
||||
}
|
||||
];
|
||||
|
||||
route = {
|
||||
default_domain_resolver = "local";
|
||||
rule_set = rules.geositeRuleSets ++ rules.geoIPRuleSets;
|
||||
rules = lib.optionals enableXrayDnsBridge [ xrayDnsBridgeHijackRule ] ++ rules.singBoxRoutingRules;
|
||||
final = if proxyCfg.proxyByDefault then "proxy" else "direct";
|
||||
}
|
||||
// lib.optionalAttrs (enableTun && tunAutoRoute) {
|
||||
auto_detect_interface = true;
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs enableClashApi clashApiBlock;
|
||||
|
||||
xrayDnsAddress =
|
||||
upstream: if upstream.type == "tcp" then "tcp://${upstream.address}" else upstream.address;
|
||||
|
||||
mkXrayDnsServer = tag: upstream: {
|
||||
address = xrayDnsAddress upstream;
|
||||
inherit tag;
|
||||
port = upstream.port;
|
||||
queryStrategy = "UseIP";
|
||||
};
|
||||
|
||||
mkXraySniffing =
|
||||
{
|
||||
fakeDnsOnly ? false,
|
||||
}:
|
||||
if fakeDnsOnly then
|
||||
{
|
||||
enabled = true;
|
||||
destOverride = [ "fakedns" ];
|
||||
metadataOnly = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
enabled = true;
|
||||
destOverride = [
|
||||
"http"
|
||||
"tls"
|
||||
"quic"
|
||||
];
|
||||
};
|
||||
|
||||
xraySniffing = mkXraySniffing { };
|
||||
|
||||
mkXrayTunDnsConfig =
|
||||
{
|
||||
preferRemote ? proxyCfg.proxyByDefault,
|
||||
}:
|
||||
let
|
||||
primaryTag = if preferRemote then "remote" else "local";
|
||||
secondaryTag = if preferRemote then "local" else "remote";
|
||||
primaryUpstream = if preferRemote then proxyCfg.dns.remote else proxyCfg.dns.local;
|
||||
secondaryUpstream = if preferRemote then proxyCfg.dns.local else proxyCfg.dns.remote;
|
||||
in
|
||||
{
|
||||
queryStrategy = "UseIP";
|
||||
tag = "dns-in";
|
||||
servers = [
|
||||
{
|
||||
address = "fakedns";
|
||||
tag = "fakedns";
|
||||
}
|
||||
(mkXrayDnsServer primaryTag primaryUpstream)
|
||||
(mkXrayDnsServer secondaryTag secondaryUpstream)
|
||||
];
|
||||
};
|
||||
|
||||
xrayTunDnsHijackRule = {
|
||||
type = "field";
|
||||
inboundTag = [ "tun-in" ];
|
||||
network = "tcp,udp";
|
||||
port = 53;
|
||||
outboundTag = "dns-out";
|
||||
ruleTag = "dns-hijack";
|
||||
};
|
||||
|
||||
xrayTunDnsUpstreamRule = {
|
||||
type = "field";
|
||||
inboundTag = [ "dns-in" ];
|
||||
network = "tcp,udp";
|
||||
outboundTag = "direct";
|
||||
ruleTag = "dns-upstream-direct";
|
||||
};
|
||||
|
||||
xrayFinalRule =
|
||||
tag:
|
||||
{
|
||||
type = "field";
|
||||
network = "tcp,udp";
|
||||
ruleTag = "final-default";
|
||||
}
|
||||
// (
|
||||
if tag == "proxy" && proxyCfg.selection == "urltest" then
|
||||
{ balancerTag = "proxy"; }
|
||||
else
|
||||
{ outboundTag = tag; }
|
||||
);
|
||||
|
||||
xrayRouteRules = rules.xrayRoutingRules ++ [
|
||||
(xrayFinalRule (if proxyCfg.proxyByDefault then "proxy" else "direct"))
|
||||
];
|
||||
|
||||
xrayDirectOutbound =
|
||||
useOutboundRoutingMark:
|
||||
{
|
||||
protocol = "freedom";
|
||||
tag = "direct";
|
||||
settings = { };
|
||||
}
|
||||
// lib.optionalAttrs useOutboundRoutingMark {
|
||||
streamSettings.sockopt.mark = globalTproxy.proxyMark;
|
||||
};
|
||||
|
||||
mkXrayConfig =
|
||||
{
|
||||
enableMixed ? false,
|
||||
enableTProxy ? false,
|
||||
enableTun ? false,
|
||||
tunInterface ? globalTun.interface,
|
||||
tunAddress ? globalTun.address,
|
||||
tunIPv6Address ? null,
|
||||
tunMtu ? globalTun.mtu,
|
||||
useOutboundRoutingMark ? false,
|
||||
enableUrlTest ? proxyCfg.selection == "urltest",
|
||||
domainStrategy ? "IPIfNonMatch",
|
||||
enableTunFakeDns ? false,
|
||||
}:
|
||||
let
|
||||
tunDnsServers = [
|
||||
(if proxyCfg.proxyByDefault then proxyCfg.dns.remote.address else proxyCfg.dns.local.address)
|
||||
(if proxyCfg.proxyByDefault then proxyCfg.dns.local.address else proxyCfg.dns.remote.address)
|
||||
];
|
||||
xrayTunSniffing = mkXraySniffing { fakeDnsOnly = enableTunFakeDns; };
|
||||
xrayDnsConfig =
|
||||
if enableTunFakeDns then
|
||||
mkXrayTunDnsConfig { }
|
||||
else
|
||||
{
|
||||
servers = [
|
||||
(mkXrayDnsServer "remote" proxyCfg.dns.remote)
|
||||
(mkXrayDnsServer "local" proxyCfg.dns.local)
|
||||
];
|
||||
};
|
||||
xrayTunOutbounds = lib.optionals enableTunFakeDns [
|
||||
{
|
||||
protocol = "dns";
|
||||
tag = "dns-out";
|
||||
settings = {
|
||||
userLevel = 0;
|
||||
rules = [
|
||||
{
|
||||
action = "direct";
|
||||
qType = "2-27,29-65535";
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
];
|
||||
xrayRoutingRules =
|
||||
lib.optionals enableTunFakeDns [
|
||||
xrayTunDnsUpstreamRule
|
||||
xrayTunDnsHijackRule
|
||||
]
|
||||
++ rules.xrayRoutingRules
|
||||
++ [ (xrayFinalRule (if proxyCfg.proxyByDefault then "proxy" else "direct")) ];
|
||||
in
|
||||
{
|
||||
log.loglevel = "warning";
|
||||
dns = xrayDnsConfig;
|
||||
inbounds =
|
||||
lib.optional enableMixed {
|
||||
tag = "mixed-in";
|
||||
protocol = "socks";
|
||||
listen = proxyCfg.listenAddress;
|
||||
port = proxyCfg.port;
|
||||
settings = {
|
||||
auth = "noauth";
|
||||
udp = true;
|
||||
ip = proxyCfg.listenAddress;
|
||||
};
|
||||
sniffing = xraySniffing;
|
||||
}
|
||||
++ lib.optional enableTProxy {
|
||||
tag = "tproxy-in";
|
||||
protocol = "tunnel";
|
||||
listen = "127.0.0.1";
|
||||
port = globalTproxy.port;
|
||||
settings = {
|
||||
allowedNetwork = "tcp,udp";
|
||||
followRedirect = true;
|
||||
};
|
||||
streamSettings.sockopt.tproxy = "tproxy";
|
||||
sniffing = xraySniffing;
|
||||
}
|
||||
++ lib.optional enableTun {
|
||||
tag = "tun-in";
|
||||
protocol = "tun";
|
||||
settings = {
|
||||
name = tunInterface;
|
||||
mtu = tunMtu;
|
||||
gateway = [ tunAddress ] ++ lib.optionals (tunIPv6Address != null) [ tunIPv6Address ];
|
||||
dns = tunDnsServers;
|
||||
userLevel = 0;
|
||||
autoOutboundsInterface = "auto";
|
||||
};
|
||||
sniffing = xrayTunSniffing;
|
||||
};
|
||||
outbounds = xrayTunOutbounds ++ [
|
||||
(xrayDirectOutbound useOutboundRoutingMark)
|
||||
{
|
||||
protocol = "blackhole";
|
||||
tag = "block";
|
||||
settings = { };
|
||||
}
|
||||
];
|
||||
routing = {
|
||||
domainStrategy = domainStrategy;
|
||||
domainMatcher = "hybrid";
|
||||
rules = xrayRoutingRules;
|
||||
}
|
||||
// lib.optionalAttrs enableUrlTest {
|
||||
balancers = [
|
||||
{
|
||||
tag = "proxy";
|
||||
selector = [ "proxy-suite-ob-" ];
|
||||
strategy = {
|
||||
type = "leastPing";
|
||||
};
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs enableUrlTest {
|
||||
observatory = {
|
||||
subjectSelector = [ "proxy-suite-ob-" ];
|
||||
probeUrl = proxyCfg.urlTest.url;
|
||||
probeInterval = proxyCfg.urlTest.interval;
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs enableTunFakeDns { fakedns = xrayFakeDnsPools; };
|
||||
|
||||
singBoxTproxyTemplate = mkSingBoxConfig {
|
||||
enableMixed = true;
|
||||
enableTProxy = true;
|
||||
useOutboundRoutingMark = true;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.socks;
|
||||
};
|
||||
|
||||
singBoxTunTemplate = mkSingBoxConfig {
|
||||
enableTun = true;
|
||||
tunInterface = globalTun.interface;
|
||||
tunAddress = globalTun.address;
|
||||
tunMtu = globalTun.mtu;
|
||||
tunAutoRoute = true;
|
||||
tunAutoRedirect = true;
|
||||
tunStrictRoute = true;
|
||||
forceLocalDnsViaProxy = false;
|
||||
enableClashApi = false;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.tun;
|
||||
};
|
||||
|
||||
singBoxPerAppTunTemplate = mkSingBoxConfig {
|
||||
enableTun = true;
|
||||
tunInterface = perAppTun.interface;
|
||||
tunAddress = perAppTun.address;
|
||||
tunMtu = perAppTun.mtu;
|
||||
tunAutoRoute = false;
|
||||
tunAutoRedirect = false;
|
||||
tunStrictRoute = false;
|
||||
forceLocalDnsViaProxy = false;
|
||||
useOutboundRoutingMark = globalTproxy.enable;
|
||||
enableClashApi = false;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.perAppTun;
|
||||
};
|
||||
|
||||
xrayTproxyTemplate = mkXrayConfig {
|
||||
enableMixed = true;
|
||||
enableTProxy = true;
|
||||
useOutboundRoutingMark = true;
|
||||
};
|
||||
|
||||
xrayTunTemplate = mkXrayConfig {
|
||||
enableTun = true;
|
||||
tunInterface = globalTun.interface;
|
||||
tunAddress = globalTun.address;
|
||||
tunIPv6Address = xrayGlobalTunIPv6Address;
|
||||
tunMtu = globalTun.mtu;
|
||||
useOutboundRoutingMark = true;
|
||||
enableTunFakeDns = true;
|
||||
};
|
||||
|
||||
xrayPerAppTunTemplate = mkXrayConfig {
|
||||
enableTun = true;
|
||||
tunInterface = perAppTun.interface;
|
||||
tunAddress = perAppTun.address;
|
||||
tunIPv6Address = xrayPerAppTunIPv6Address;
|
||||
tunMtu = perAppTun.mtu;
|
||||
useOutboundRoutingMark = true;
|
||||
enableTunFakeDns = true;
|
||||
};
|
||||
|
||||
tproxyTemplate = if pureXrayEnabled then xrayTproxyTemplate else singBoxTproxyTemplate;
|
||||
tunTemplate = if pureXrayEnabled then xrayTunTemplate else singBoxTunTemplate;
|
||||
perAppTunTemplate = if pureXrayEnabled then xrayPerAppTunTemplate else singBoxPerAppTunTemplate;
|
||||
routeModeRules = if pureXrayEnabled then rules.xrayRouteModeRules else rules.singBoxRouteModeRules;
|
||||
|
||||
tproxyFile = pkgs.writeText "proxy-suite-tproxy-template.json" (builtins.toJSON tproxyTemplate);
|
||||
tunFile = pkgs.writeText "proxy-suite-tun-template.json" (builtins.toJSON tunTemplate);
|
||||
tproxyFile = pkgs.writeText "proxy-suite-tproxy-template.json" (
|
||||
builtins.toJSON selectedTemplates.tproxy
|
||||
);
|
||||
tunFile = pkgs.writeText "proxy-suite-tun-template.json" (
|
||||
builtins.toJSON selectedTemplates.tun
|
||||
);
|
||||
perAppTunFile = pkgs.writeText "proxy-suite-per-app-tun-template.json" (
|
||||
builtins.toJSON perAppTunTemplate
|
||||
builtins.toJSON selectedTemplates.perAppTun
|
||||
);
|
||||
routeModeRulesFile = pkgs.writeText "proxy-suite-route-mode-rules.json" (
|
||||
builtins.toJSON routeModeRules
|
||||
);
|
||||
|
||||
in
|
||||
{
|
||||
inherit
|
||||
|
||||
Reference in new issue
Block a user