feat: cleaner port forwarding

This commit is contained in:
Patrick 2024-12-22 00:10:37 +01:00
parent 65e207d999
commit 268bd66c76
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F
5 changed files with 129 additions and 30 deletions

View file

@ -54,6 +54,7 @@ in
forgejo = { forgejo = {
domain = "forge.${globals.domains.web}"; domain = "forge.${globals.domains.web}";
host = "elisabeth-forgejo"; host = "elisabeth-forgejo";
ip = 13;
}; };
immich = { immich = {
domain = "immich.${globals.domains.web}"; domain = "immich.${globals.domains.web}";
@ -118,6 +119,7 @@ in
netbird = { netbird = {
domain = "netbird.${globals.domains.web}"; domain = "netbird.${globals.domains.web}";
host = "elisabeth-netbird"; host = "elisabeth-netbird";
ip = 16;
}; };
nginx = { nginx = {
domain = globals.domains.web; domain = globals.domains.web;
@ -129,6 +131,15 @@ in
host = "elisabeth-samba"; host = "elisabeth-samba";
ip = 12; ip = 12;
}; };
ddclient = {
domain = "";
host = "elisabeth-ddclient";
};
murmur = {
domain = "ts.${globals.domains.web}";
host = "elisabeth-murmur";
ip = 9;
};
}; };
}; };
} }

View file

@ -1,5 +1,6 @@
{ {
config, config,
globals,
stateVersion, stateVersion,
inputs, inputs,
lib, lib,
@ -17,6 +18,7 @@
enableRenaultFT ? false, enableRenaultFT ? false,
enableBunker ? false, enableBunker ? false,
enableSharedPaperless ? false, enableSharedPaperless ? false,
vlans ? [ "services" ],
... ...
}: }:
{ {
@ -54,6 +56,25 @@
networking.nftables.firewall.zones.untrusted.interfaces = lib.mkIf ( networking.nftables.firewall.zones.untrusted.interfaces = lib.mkIf (
lib.length config.guests.${guestName}.networking.links == 1 lib.length config.guests.${guestName}.networking.links == 1
) config.guests.${guestName}.networking.links; ) config.guests.${guestName}.networking.links;
systemd.network.networks = lib.mkIf (globals.services.${guestName}.ip != null) (
lib.listToAttrs (
lib.flip map vlans (
name:
lib.nameValuePair "09-mv-${name}" {
matchConfig.Name = "mv-${name}";
DHCP = "no";
address = [
(lib.net.cidr.hostCidr globals.services.${guestName}.ip globals.net.vlans.${name}.cidrv4)
(lib.net.cidr.hostCidr globals.services.${guestName}.ip globals.net.vlans.${name}.cidrv6)
];
gateway = [
(lib.net.cidr.hostCidr 1 globals.net.vlans.${name}.cidrv4)
(lib.net.cidr.hostCidr 1 globals.net.vlans.${name}.cidrv6)
];
}
)
)
);
} }
]; ];
}; };
@ -74,17 +95,23 @@
}; };
}; };
mkContainer = guestName: cfg: { mkContainer =
${guestName} = mkGuest guestName cfg // { guestName:
backend = "container"; {
container.macvlans = [ "lan-services" ]; vlans ? [ "services" ],
extraSpecialArgs = { ...
inherit (inputs.self) nodes globals; }@cfg:
inherit (inputs.self.pkgs.x86_64-linux) lib; {
inherit inputs minimal stateVersion; ${guestName} = mkGuest guestName cfg // {
backend = "container";
container.macvlans = lib.flip map vlans (x: "lan-${x}:mv-${x}");
extraSpecialArgs = {
inherit (inputs.self) nodes globals;
inherit (inputs.self.pkgs.x86_64-linux) lib;
inherit inputs minimal stateVersion;
};
}; };
}; };
};
in in
{ } { }
// mkContainer "adguardhome" { } // mkContainer "adguardhome" { }
@ -110,5 +137,6 @@
enableRenaultFT = true; enableRenaultFT = true;
enableBunker = true; enableBunker = true;
enableSharedPaperless = true; enableSharedPaperless = true;
vlans = [ "home" ];
}; };
} }

View file

@ -0,0 +1,79 @@
{ globals, lib, ... }:
let
inherit (lib)
concatStringsSep
net
toUpper
mkMerge
;
forward =
{
service,
ports,
protocol,
...
}:
{
networking.nftables = {
chains = {
prerouting.port-forward = {
after = [ "hook" ];
rules = [
"iifname lan-fritz ${protocol} dport { ${concatStringsSep ", " (map toString ports)} } dnat ip to ${
net.cidr.host globals.services.${service}.ip globals.net.vlans.services.cidrv4
}"
"iifname lan-fritz ${protocol} dport { ${concatStringsSep ", " (map toString ports)} } dnat ip6 to ${
net.cidr.host globals.services.${service}.ip globals.net.vlans.services.cidrv6
}"
];
};
};
firewall = {
zones = {
${service}.ipv4Addresses = [
(lib.net.cidr.host globals.services.${service}.ip globals.net.vlans.services.cidrv4)
];
};
rules = {
"forward-${service}" = {
from = [ "fritz" ];
to = [ service ];
"allowed${toUpper protocol}Ports" = ports;
};
};
};
};
};
in
mkMerge [
(forward {
service = "nginx";
ports = [
80
443
];
protocol = "tcp";
})
(forward {
service = "forgejo";
ports = [
9922
];
protocol = "tcp";
})
(forward {
service = "murmur";
ports = [
9987
];
protocol = "udp";
})
(forward {
service = "netbird";
ports = [
3478
5349
];
protocol = "udp";
})
]

View file

@ -17,6 +17,7 @@ in
imports = [ imports = [
./hostapd.nix ./hostapd.nix
./kea.nix ./kea.nix
./forwarding.nix
]; ];
boot.kernel.sysctl."net.ipv4.ip_forward" = 1; boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
networking.nftables.firewall.zones = mkMerge [ networking.nftables.firewall.zones = mkMerge [
@ -25,9 +26,6 @@ in
adguard.ipv4Addresses = [ adguard.ipv4Addresses = [
(lib.net.cidr.host globals.services.adguardhome.ip globals.net.vlans.services.cidrv4) (lib.net.cidr.host globals.services.adguardhome.ip globals.net.vlans.services.cidrv4)
]; ];
nginx.ipv4Addresses = [
(lib.net.cidr.host globals.services.nginx.ip globals.net.vlans.services.cidrv4)
];
} }
(genAttrs (attrNames globals.net.vlans) (name: { (genAttrs (attrNames globals.net.vlans) (name: {
interfaces = [ "lan-${name}" ]; interfaces = [ "lan-${name}" ];
@ -134,26 +132,9 @@ in
} }
)) ))
); );
networking.nftables.chains = {
prerouting.port-forward = {
after = [ "hook" ];
rules = [
"iifname lan-fritz tcp dport { 80, 443 } dnat ip to ${lib.net.cidr.host globals.services.nginx.ip globals.net.vlans.services.cidrv4}"
"iifname lan-fritz tcp dport { 80, 443 } dnat ip6 to ${lib.net.cidr.host globals.services.nginx.ip globals.net.vlans.services.cidrv6}"
];
};
};
networking.nftables.firewall = { networking.nftables.firewall = {
snippets.nnf-ssh.enable = lib.mkForce false; snippets.nnf-ssh.enable = lib.mkForce false;
rules = { rules = {
forward-nginx = {
from = [ "fritz" ];
to = [ "nginx" ];
allowedTCPPorts = [
80
443
];
};
ssh = { ssh = {
from = [ from = [
"fritz" "fritz"

View file

@ -95,7 +95,7 @@
image = config.lib.stylix.pixel "base00"; image = config.lib.stylix.pixel "base00";
base16Scheme = { base16Scheme = {
yaml = "${pkgs.base16-schemes}/share/themes/vice.yaml"; yaml = "${pkgs.base16-schemes}/share/themes/vice.yaml";
use-ifd = "auto"; use-ifd = "always";
}; };
# Has to be green # Has to be green
override.base0B = "#00CC99"; override.base0B = "#00CC99";