nix/host/x86_64-linux/home/Network.nix

123 lines
3.7 KiB
Nix
Raw Normal View History

2024-08-14 02:19:47 +03:00
{ util, config, lib, ... }: let
internal = "10.0.0.1"; # Lan host IP address.
external = "188.242.247.132"; # Wan host IP address.
wifi = "10.0.0.2"; # Wifi router IP address.
2024-08-14 02:19:47 +03:00
lan = "br0"; # Lan interface.
wan = "enp8s0"; # Wan interface.
2024-08-14 02:19:47 +03:00
in {
# Allow packet routing (we are a router).
2024-08-14 02:19:47 +03:00
boot.kernel.sysctl = {
"net.ipv4.conf.all.src_valid_mark" = 1;
"net.ipv4.ip_forward" = 1;
};
# Disable SSH access from everywhere, configure access bellow.
services.openssh.openFirewall = false;
2024-08-02 23:45:19 +03:00
networking = {
# Use only external DNS.
2024-08-02 23:45:19 +03:00
networkmanager.insertNameservers = [
"1.1.1.1"
"8.8.8.8"
];
2024-08-14 02:19:47 +03:00
# Some extra hosts for local access.
2024-08-24 19:15:30 +03:00
extraHosts = with config.container.module; util.trimTabs ''
${git.address} git.voronind.com
${proxy.address} iot.voronind.com
${proxy.address} pass.voronind.com
2024-08-02 23:45:19 +03:00
'';
2024-08-14 02:19:47 +03:00
firewall = {
enable = true;
2024-08-14 02:19:47 +03:00
extraCommands = let
# Container configs.
2024-08-14 02:19:47 +03:00
cfg = config.container.module;
2024-08-17 14:52:15 +03:00
# Const.
tcp = "tcp";
udp = "udp";
# Create port forwarding rule.
2024-08-14 02:19:47 +03:00
mkForward = src: sport: dst: dport: proto: "iptables -t nat -I PREROUTING -d ${src} -p ${proto} --dport ${toString sport} -j DNAT --to-destination ${dst}:${toString dport}\n";
in ''
# Wan access for 10.0.0.0/24 subnet.
2024-08-14 02:19:47 +03:00
iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -d 0/0 -o ${wan} -j MASQUERADE
# Full access from VPN clients.
2024-08-14 20:59:42 +03:00
iptables -I INPUT -j ACCEPT -s ${cfg.vpn.address} -d ${internal}
2024-08-19 04:16:23 +03:00
iptables -I INPUT -j ACCEPT -s ${cfg.zapret.address} -d ${internal}
# Full access from Lan.
iptables -I INPUT -j ACCEPT -i ${lan} -d ${internal}
2024-08-14 02:19:47 +03:00
''
# Expose DNS server for internal network.
2024-08-17 14:52:15 +03:00
+ (mkForward internal cfg.dns.port cfg.dns.address cfg.dns.port tcp)
+ (mkForward internal cfg.dns.port cfg.dns.address cfg.dns.port udp)
2024-08-14 02:19:47 +03:00
# Email server.
2024-08-17 14:52:15 +03:00
+ (mkForward external 25 cfg.mail.address 25 tcp)
+ (mkForward internal 25 cfg.mail.address 25 tcp)
+ (mkForward internal 465 cfg.mail.address 465 tcp)
+ (mkForward internal 993 cfg.mail.address 993 tcp)
2024-08-14 02:19:47 +03:00
# FRKN internal proxy server.
2024-08-17 14:52:15 +03:00
+ (mkForward internal cfg.zapret.port cfg.zapret.address cfg.zapret.port tcp)
+ (mkForward internal cfg.zapret.torport cfg.zapret.address cfg.zapret.torport tcp)
+ (mkForward internal cfg.zapret.port cfg.zapret.address cfg.zapret.port udp)
+ (mkForward internal cfg.zapret.torport cfg.zapret.address cfg.zapret.torport udp)
2024-08-14 02:19:47 +03:00
# Allow VPN connections from Wan.
2024-08-17 14:52:15 +03:00
+ (mkForward external cfg.vpn.port cfg.vpn.address cfg.vpn.port udp)
2024-08-14 02:19:47 +03:00
# Nginx HTTP access from Wan.
2024-08-17 14:52:15 +03:00
+ (mkForward external cfg.proxy.port cfg.proxy.address cfg.proxy.port tcp)
+ (mkForward internal cfg.proxy.port cfg.proxy.address cfg.proxy.port tcp)
2024-08-14 02:19:47 +03:00
# Download ports for torrents.
2024-08-17 14:52:15 +03:00
+ (mkForward external 54630 cfg.download.address 54630 tcp)
+ (mkForward external 54631 cfg.download.address 54631 tcp)
+ (mkForward external 54630 cfg.download.address 54630 udp)
+ (mkForward external 54631 cfg.download.address 54631 udp)
2024-08-24 18:01:30 +03:00
# Git ssh connections.
+ (mkForward external cfg.git.portSsh cfg.git.address cfg.git.portSsh tcp)
+ (mkForward internal cfg.git.portSsh cfg.git.address cfg.git.portSsh tcp)
2024-08-14 02:19:47 +03:00
;
};
# Create Lan bridge.
bridges.${lan}.interfaces = [
2024-08-14 02:19:47 +03:00
"enp6s0f0"
"enp6s0f1"
];
interfaces = {
2024-08-14 20:59:42 +03:00
${lan}.ipv4 = {
# Assign Lan address and subnet.
2024-08-14 02:19:47 +03:00
addresses = [{
address = internal;
prefixLength = 24;
}];
# Assign traffic routes.
2024-08-14 02:19:47 +03:00
routes = [
# Wifi 5G clients.
2024-08-14 02:19:47 +03:00
{
address = "192.168.1.0";
prefixLength = 24;
via = wifi;
}
# Wifi 2.4G clients.
2024-08-14 02:19:47 +03:00
{
address = "192.168.2.0";
prefixLength = 24;
via = wifi;
}
];
};
};
2024-08-02 23:45:19 +03:00
};
2024-03-29 09:05:08 +03:00
}