nixos-config/machines/sing-box.nix

151 lines
4.5 KiB
Nix
Raw Normal View History

2023-09-26 15:32:52 +00:00
{ config, lib, pkgs, ... }:
let
password = {
_secret = config.sops.secrets.singbox_password.path;
};
uuid = {
_secret = config.sops.secrets.singbox_uuid.path;
2023-09-26 15:32:52 +00:00
};
2023-11-28 13:38:50 +00:00
sg_server = {
_secret = config.sops.secrets.singbox_sg_server.path;
};
2023-12-06 14:54:22 +00:00
jp_server = {
_secret = config.sops.secrets.singbox_jp_server.path;
};
2023-09-26 15:32:52 +00:00
in
{
services.sing-box = {
enable = true;
settings = {
log = { level = "warning"; };
experimental = {
clash_api = {
external_controller = "127.0.0.1:9090";
store_selected = true;
external_ui = "${config.nur.repos.linyinfeng.yacd}";
};
};
dns = {
rules = [
{
disable_cache = true;
geosite = "category-ads-all";
server = "_dns_block";
}
{
geosite = "cn";
server = "_dns_doh_mainland";
}
{
disable_cache = false;
2023-11-28 13:38:50 +00:00
domain_suffix = sg_server;
server = "_dns_doh_mainland";
}
2023-12-06 14:54:22 +00:00
{
disable_cache = false;
2023-12-06 14:54:22 +00:00
domain_suffix = jp_server;
server = "_dns_doh_mainland";
}
2023-09-26 15:32:52 +00:00
];
servers = [
{
2023-11-28 13:38:50 +00:00
address = "tls://dns.google:853/";
address_resolver = "_dns_udp_global";
2023-09-26 15:32:52 +00:00
detour = "_proxy_select";
tag = "_dns_global";
}
2023-11-28 13:38:50 +00:00
{
address = "1.1.1.1";
detour = "_proxy_select";
tag = "_dns_udp_global";
}
2023-09-26 15:32:52 +00:00
{
address = "119.29.29.29";
detour = "direct";
tag = "_dns_udp_mainland";
}
{
address = "tls://1.12.12.12:853/";
2023-09-26 15:32:52 +00:00
address_resolver = "_dns_udp_mainland";
detour = "direct";
tag = "_dns_doh_mainland";
}
{
address = "rcode://success";
tag = "_dns_block";
}
];
final = "_dns_global";
strategy = "prefer_ipv4";
2023-11-28 13:38:50 +00:00
disable_cache = true;
2023-09-26 15:32:52 +00:00
};
inbounds = [
{
type = "mixed";
tag = "mixed-in";
listen = "127.0.0.1";
listen_port = 7891;
}
{
type = "tun";
tag = "tun-in";
auto_route = true;
strict_route = false;
inet4_address = "172.19.0.1/30";
2023-11-28 13:38:50 +00:00
inet6_address = "fdfe:dcba:9876::1/126";
2023-09-26 15:32:52 +00:00
sniff = true;
}
];
route = {
auto_detect_interface = true;
final = "_proxy_select";
rules = [
{ outbound = "dns-out"; protocol = "dns"; }
{
geoip = "cn";
geosite = "cn";
outbound = "direct";
}
{ geoip = "private"; outbound = "direct"; }
2023-11-30 04:07:23 +00:00
{
domain = sg_server;
outbound = "direct";
}
2023-09-26 15:32:52 +00:00
{
geosite = "cn";
geoip = "cn";
invert = true;
outbound = "_proxy_select";
}
];
};
outbounds = [
{ tag = "selfhost"; type = "urltest"; outbounds = lib.forEach (lib.range 0 4) (id: "jp" + toString id) ++ lib.forEach (lib.range 0 4) (id: "sg" + toString id); tolerance = 50; url = "http://cp.cloudflare.com/"; }
{ tag = "sg0"; type = "trojan"; server = sg_server; server_port = 8080; password = password; tls = { enabled = true; server_name = sg_server; utls = { enabled = true; fingerprint = "firefox"; }; }; }
{ tag = "jp0"; type = "trojan"; server = jp_server; server_port = 8080; password = password; tls = { enabled = true; server_name = jp_server; utls = { enabled = true; fingerprint = "firefox"; }; }; }
{ default = "auto"; outbounds = [ "selfhost" "direct" "block"]; tag = "_proxy_select"; type = "selector"; }
2023-09-26 15:32:52 +00:00
{ tag = "direct"; type = "direct"; }
{ tag = "block"; type = "block"; }
{ tag = "dns-out"; type = "dns"; }
2023-11-30 04:07:23 +00:00
] ++ lib.forEach (lib.range 6311 6314) (port: {
inherit uuid password;
2023-11-30 04:07:23 +00:00
tag = "sg" + toString (port - 6310);
type = "tuic";
congestion_control = "bbr";
server = sg_server;
server_port = port;
tls = { enabled = true; server_name = sg_server; };
2023-12-06 14:54:22 +00:00
}) ++ lib.forEach (lib.range 6311 6314) (port: {
inherit uuid password;
2023-12-06 14:54:22 +00:00
tag = "jp" + toString (port - 6310);
type = "tuic";
congestion_control = "bbr";
server = jp_server;
server_port = port;
tls = { enabled = true; server_name = jp_server; };
2023-11-30 04:07:23 +00:00
});
2023-09-26 15:32:52 +00:00
};
};
}
2023-11-30 04:07:23 +00:00