services/enthalpy: begin migration to networking.netns

This commit is contained in:
Lu Wang 2024-12-06 17:11:23 +08:00
parent bb65636b38
commit b7566b7975
Signed by: rebmit
SSH key fingerprint: SHA256:3px8QV1zEerIrEWHaqtH5rR9kjetyRST5EipOPrd+bU
9 changed files with 33 additions and 132 deletions

View file

@ -6,6 +6,7 @@
services.enthalpy = { services.enthalpy = {
users.rebmit = { }; users.rebmit = { };
services.nix-daemon = { };
ipsec.interfaces = [ "enp14s0" ]; ipsec.interfaces = [ "enp14s0" ];
clat = { clat = {
enable = true; enable = true;

View file

@ -6,6 +6,7 @@
services.enthalpy = { services.enthalpy = {
users.rebmit = { }; users.rebmit = { };
services.nix-daemon = { };
ipsec = { ipsec = {
interfaces = [ "wlan0" ]; interfaces = [ "wlan0" ];
whitelist = [ "rebmit's edge network" ]; whitelist = [ "rebmit's edge network" ];

View file

@ -422,7 +422,7 @@ in
_name: cfg: _name: cfg:
(imap ( (imap (
index: fp: index: fp:
nameValuePair "netns-${cfg.netns}-port-forward-${fp.netns}-${fp.protocol}-${toString index}" ( nameValuePair "netns-${cfg.netns}-port-forward-${toString index}-${fp.netns}-${fp.protocol}" (
mkPortForwardService cfg fp mkPortForwardService cfg fp
) )
) cfg.forwardPorts) ) cfg.forwardPorts)
@ -433,7 +433,7 @@ in
_name: cfg: _name: cfg:
(imap ( (imap (
index: ev: index: ev:
nameValuePair "netns-${cfg.netns}-extra-veth-${ev.netns}-${toString index}" ( nameValuePair "netns-${cfg.netns}-extra-veth-${toString index}-${ev.netns}" (
mkExtraVethService cfg ev mkExtraVethService cfg ev
) )
) cfg.extraVeths) ) cfg.extraVeths)

View file

@ -44,38 +44,11 @@ in
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
systemd.network.networks."50-enthalpy" = { networking.netns."${cfg.netns}" = {
matchConfig.Name = "enthalpy"; interface = cfg.netns;
linkConfig.RequiredForOnline = false; address = singleton "${cfg.address}/128";
}; enableIPv4Forwarding = false;
enableIPv6Forwarding = true;
systemd.services.enthalpy = {
path = with pkgs; [
iproute2
coreutils
procps
];
script = ''
ip netns add ${cfg.netns}
ip link add enthalpy mtu 1400 address 02:00:00:00:00:01 type veth peer enthalpy mtu 1400 address 02:00:00:00:00:00 netns ${cfg.netns}
ip -n ${cfg.netns} link set lo up
ip -n ${cfg.netns} link set enthalpy up
ip -n ${cfg.netns} addr add ${cfg.address}/128 dev enthalpy
ip netns exec ${cfg.netns} sysctl -w net.ipv6.conf.default.forwarding=1
ip netns exec ${cfg.netns} sysctl -w net.ipv6.conf.all.forwarding=1
ip netns exec ${cfg.netns} sysctl -w net.ipv4.conf.default.forwarding=0
ip netns exec ${cfg.netns} sysctl -w net.ipv4.conf.all.forwarding=0
'';
preStop = ''
ip netns del ${cfg.netns}
'';
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
wants = [ "network.target" ];
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
}; };
}; };
} }

View file

@ -4,11 +4,13 @@
config, config,
lib, lib,
pkgs, pkgs,
mylib,
... ...
}: }:
with lib; with lib;
let let
cfg = config.services.enthalpy; cfg = config.services.enthalpy;
gostPort = config.networking.ports.enthalpy-gost;
in in
{ {
options.services.enthalpy.gost = { options.services.enthalpy.gost = {
@ -16,43 +18,25 @@ in
}; };
config = mkIf (cfg.enable && cfg.gost.enable) { config = mkIf (cfg.enable && cfg.gost.enable) {
systemd.network.networks."50-enthalpy" = {
address = singleton "fc00::";
routes = singleton { Destination = cfg.address; };
};
systemd.services.enthalpy-gost = { systemd.services.enthalpy-gost = {
serviceConfig = { serviceConfig = mylib.misc.serviceHardened // {
Type = "simple"; Type = "simple";
Restart = "on-failure"; Restart = "always";
RestartSec = 5; RestartSec = 5;
DynamicUser = true; DynamicUser = true;
ExecStart = "${pkgs.gost}/bin/gost -L=socks5://[fc00::]:${toString config.networking.ports.enthalpy-gost}"; ExecStart = "${pkgs.gost}/bin/gost -L=socks5://[::1]:${toString gostPort}";
ProtectSystem = "full";
ProtectHome = "yes";
ProtectKernelTunables = true;
ProtectControlGroups = true;
PrivateTmp = true;
PrivateDevices = true;
SystemCallFilter = "~@cpu-emulation @debug @keyring @module @mount @obsolete @raw-io";
MemoryDenyWriteExecute = "yes";
}; };
wants = [ "network-online.target" ]; after = [ "network-online.target" ];
after = [ wantedBy = [ "network-online.target" ];
"enthalpy.service"
"network-online.target"
];
requires = [ "enthalpy.service" ];
wantedBy = [ "multi-user.target" ];
}; };
services.enthalpy.exit.enable = true; networking.netns."${cfg.netns}".forwardPorts = [
services.enthalpy.exit.prefix = singleton { {
type = "static"; protocol = "tcp";
destination = "fc00::/128"; netns = "default";
source = "${cfg.address}/128"; source = "[::1]:${toString gostPort}";
}; target = "[::1]:${toString gostPort}";
}
networking.hosts."fc00::" = singleton "enthalpy-gost"; ];
}; };
} }

View file

@ -190,12 +190,12 @@ in
"strongswan-swanctl.service" "strongswan-swanctl.service"
]; ];
requires = [ requires = [
"enthalpy.service" "netns-enthalpy.service"
]; ];
after = [ after = [
"network-online.target" "network-online.target"
"strongswan-swanctl.service" "strongswan-swanctl.service"
"enthalpy.service" "netns-enthalpy.service"
]; ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
reloadTriggers = [ config.environment.etc."ranet/config.json".source ]; reloadTriggers = [ config.environment.etc."ranet/config.json".source ];

View file

@ -83,10 +83,10 @@ in
}; };
wants = [ "network.target" ]; wants = [ "network.target" ];
after = [ after = [
"enthalpy.service" "netns-enthalpy.service"
"network.target" "network.target"
]; ];
requires = [ "enthalpy.service" ]; requires = [ "netns-enthalpy.service" ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
}; };

View file

@ -46,11 +46,11 @@ in
BindReadOnlyPaths = [ BindReadOnlyPaths = [
"/etc/netns/${cfg.netns}/resolv.conf:/etc/resolv.conf:norbind" "/etc/netns/${cfg.netns}/resolv.conf:/etc/resolv.conf:norbind"
"/etc/netns/${cfg.netns}/nsswitch.conf:/etc/nsswitch.conf:norbind" "/etc/netns/${cfg.netns}/nsswitch.conf:/etc/nsswitch.conf:norbind"
"/run/enthalpy/nscd:/run/nscd:norbind" "/run/netns-${cfg.netns}/nscd:/run/nscd:norbind"
]; ];
}; };
after = [ "enthalpy.service" ]; after = [ "netns-enthalpy.service" ];
requires = [ "enthalpy.service" ]; requires = [ "netns-enthalpy.service" ];
}) cfg.services; }) cfg.services;
services.enthalpy.services = mapAttrs' ( services.enthalpy.services = mapAttrs' (
@ -60,63 +60,5 @@ in
} }
) cfg.users; ) cfg.users;
} }
# https://philipdeljanov.com/posts/2019/05/31/dns-leaks-with-network-namespaces
# https://flokli.de/posts/2022-11-18-nsncd
(mkIf (cfg.services != { }) {
environment.etc."netns/enthalpy/resolv.conf".text = mkDefault ''
nameserver 2606:4700:4700::1111
'';
environment.etc."netns/enthalpy/nsswitch.conf".text = ''
passwd: ${concatStringsSep " " config.system.nssDatabases.passwd}
group: ${concatStringsSep " " config.system.nssDatabases.group}
shadow: ${concatStringsSep " " config.system.nssDatabases.shadow}
sudoers: ${concatStringsSep " " config.system.nssDatabases.sudoers}
hosts: ${concatStringsSep " " (remove "resolve [!UNAVAIL=return]" config.system.nssDatabases.hosts)}
networks: files
ethers: files
services: ${concatStringsSep " " config.system.nssDatabases.services}
protocols: files
rpc: files
'';
systemd.services.enthalpy-nsncd = {
serviceConfig = {
NetworkNamespacePath = "/run/netns/${cfg.netns}";
BindReadOnlyPaths = [
"/etc/netns/${cfg.netns}/resolv.conf:/etc/resolv.conf:norbind"
"/etc/netns/${cfg.netns}/nsswitch.conf:/etc/nsswitch.conf:norbind"
];
BindPaths = [
"/run/enthalpy/nscd:/run/nscd:norbind"
];
ExecStart = "${pkgs.nsncd}/bin/nsncd";
Type = "notify";
DynamicUser = true;
RemoveIPC = true;
NoNewPrivileges = true;
RestrictSUIDSGID = true;
ProtectSystem = "strict";
ProtectHome = "read-only";
ProtectKernelTunables = true;
ProtectControlGroups = true;
PrivateTmp = true;
RuntimeDirectory = "enthalpy/nscd";
Restart = "always";
SystemCallFilter = "~@cpu-emulation @debug @keyring @module @mount @obsolete @raw-io";
MemoryDenyWriteExecute = "yes";
};
environment.LD_LIBRARY_PATH = config.system.nssModules.path;
after = [
"enthalpy.service"
"network.target"
];
requires = [ "enthalpy.service" ];
wantedBy = [ "multi-user.target" ];
};
})
]); ]);
} }

View file

@ -72,10 +72,10 @@ in
]; ];
}; };
after = [ after = [
"enthalpy.service" "netns-enthalpy.service"
"network-online.target" "network-online.target"
]; ];
requires = [ "enthalpy.service" ]; requires = [ "netns-enthalpy.service" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
}; };