2021-09-20 14:45:07 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
let
|
2022-02-02 16:51:32 +07:00
|
|
|
port = 51820;
|
|
|
|
|
serverName = "sugarcane";
|
|
|
|
|
serverInterface = "ens3";
|
2022-02-02 20:50:59 +07:00
|
|
|
serverIp = "51.79.240.130";
|
2021-09-20 14:45:07 +00:00
|
|
|
|
2022-02-13 17:45:31 +07:00
|
|
|
forwarding = {
|
|
|
|
|
"80" = [ "10.100.0.2" "80" ];
|
|
|
|
|
"443" = [ "10.100.0.2" "443" ];
|
2022-02-27 01:27:19 +07:00
|
|
|
"22727" = [ "10.100.0.3" "7777" ];
|
2022-02-13 17:45:31 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mapForwards = type:
|
|
|
|
|
builtins.concatStringsSep "\n" (
|
|
|
|
|
lib.mapAttrsToList (sport: tuple:
|
|
|
|
|
let
|
|
|
|
|
dest = builtins.head tuple;
|
|
|
|
|
dport = lib.last tuple;
|
|
|
|
|
in ''
|
|
|
|
|
${pkgs.iptables}/bin/iptables -${type} PREROUTING -t nat -i ${serverInterface} -p tcp --dport ${sport} -j DNAT --to ${dest}:${dport}
|
|
|
|
|
${pkgs.iptables}/bin/iptables -${type} FORWARD -p tcp -d ${dest} --dport ${dport} -j ACCEPT
|
|
|
|
|
'') forwarding
|
|
|
|
|
);
|
|
|
|
|
|
2022-02-13 15:22:08 +07:00
|
|
|
routeBypass = {
|
|
|
|
|
caramel = {
|
|
|
|
|
gateway = "192.168.100.1";
|
|
|
|
|
interface = "wlan0";
|
|
|
|
|
routes = [
|
|
|
|
|
serverIp
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
blossom = {
|
|
|
|
|
gateway = "192.168.100.1";
|
|
|
|
|
interface = "wlp3s0";
|
|
|
|
|
routes = [
|
|
|
|
|
serverIp
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-20 14:45:07 +00:00
|
|
|
clients = {
|
2022-02-13 15:22:08 +07:00
|
|
|
caramel = {
|
|
|
|
|
publicKey = "VDqcpS0lJzFgwikj61MJ1xc9P8Cuq0NXa+Hc+etn2iA=";
|
|
|
|
|
allowedIPs = [ "10.100.0.2/32" ];
|
|
|
|
|
};
|
2021-11-02 00:53:56 +07:00
|
|
|
blossom = {
|
2021-09-20 14:45:07 +00:00
|
|
|
publicKey = "6nVhazYdmC15A/nke9VrqIg3sOBVOmqj4GEsyBq7MVo=";
|
|
|
|
|
allowedIPs = [ "10.100.0.3/32" ];
|
|
|
|
|
};
|
2021-10-07 11:45:54 +07:00
|
|
|
strawberry = {
|
|
|
|
|
publicKey = "Fkcp/VSN4Dkhly8V4hskF4lnDviA7VZHCnWf7OliFCg=";
|
|
|
|
|
allowedIPs = [ "10.100.0.4/32" ];
|
|
|
|
|
};
|
2021-09-20 14:45:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
clientPeers = builtins.attrValues clients;
|
|
|
|
|
serverPeer = {
|
2022-02-02 16:51:32 +07:00
|
|
|
publicKey = "3ugIk2tQZXjAH9/95s63ld2WNUHQrd4Mz5jzbln6oj0=";
|
|
|
|
|
allowedIPs = [ "0.0.0.0/0" ];
|
2022-02-02 20:50:59 +07:00
|
|
|
endpoint = "${serverIp}:${toString port}";
|
2021-09-20 14:45:07 +00:00
|
|
|
persistentKeepalive = 25;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
serverConfig = {
|
|
|
|
|
nat = {
|
|
|
|
|
enable = true;
|
|
|
|
|
externalInterface = serverInterface;
|
|
|
|
|
internalInterfaces = [ "wg0" ];
|
|
|
|
|
};
|
2022-02-02 16:51:32 +07:00
|
|
|
firewall = {
|
2022-02-13 17:45:31 +07:00
|
|
|
allowedTCPPorts = (builtins.map (s: lib.strings.toInt s) (builtins.attrNames forwarding));
|
2022-02-02 16:51:32 +07:00
|
|
|
allowedUDPPorts = [ port ];
|
|
|
|
|
};
|
2021-09-20 14:45:07 +00:00
|
|
|
|
|
|
|
|
wireguard.interfaces.wg0 = {
|
|
|
|
|
ips = [ "10.100.0.1/24" ];
|
2022-02-02 16:51:32 +07:00
|
|
|
listenPort = port;
|
2021-09-20 14:45:07 +00:00
|
|
|
|
|
|
|
|
postSetup = ''
|
|
|
|
|
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
|
2022-02-13 17:45:31 +07:00
|
|
|
${mapForwards "A"}
|
2021-09-20 14:45:07 +00:00
|
|
|
'';
|
|
|
|
|
postShutdown = ''
|
|
|
|
|
${pkgs.iptables}/bin/iptables -t nat -D POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
|
2022-02-13 17:45:31 +07:00
|
|
|
${mapForwards "D"}
|
2021-09-20 14:45:07 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
privateKeyFile = config.age.secrets."wg_${serverName}".path;
|
|
|
|
|
peers = clientPeers;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
clientConfig = {
|
2022-02-02 20:50:59 +07:00
|
|
|
wireguard.interfaces.wg0 =
|
|
|
|
|
let
|
2022-02-13 15:33:16 +07:00
|
|
|
client = clients."${config.networking.hostName}";
|
|
|
|
|
routes = routeBypass."${config.networking.hostName}";
|
2022-02-13 17:45:31 +07:00
|
|
|
mapRoutes = type: lib.concatMapStringsSep "\n" (r: "${pkgs.iproute2}/bin/ip route ${type} ${r} via ${routes.gateway} dev ${routes.interface}") routes.routes;
|
2022-02-02 20:50:59 +07:00
|
|
|
in {
|
|
|
|
|
ips = client.allowedIPs;
|
2022-02-02 16:51:32 +07:00
|
|
|
listenPort = port;
|
2021-09-20 14:45:07 +00:00
|
|
|
|
2022-02-13 15:40:11 +07:00
|
|
|
postSetup = ''
|
2022-02-13 17:45:31 +07:00
|
|
|
${mapRoutes "add"}
|
2022-02-02 20:50:59 +07:00
|
|
|
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
|
|
|
|
|
'';
|
|
|
|
|
|
2022-02-13 15:40:11 +07:00
|
|
|
postShutdown = ''
|
2022-02-13 17:45:31 +07:00
|
|
|
${mapRoutes "del"}
|
2022-02-02 20:50:59 +07:00
|
|
|
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -s 10.100.0.0/24 -o ${serverInterface} -j MASQUERADE
|
|
|
|
|
'';
|
|
|
|
|
|
2021-09-20 14:45:07 +00:00
|
|
|
privateKeyFile = config.age.secrets."wg_${config.networking.hostName}".path;
|
|
|
|
|
peers = [ serverPeer ];
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
in {
|
|
|
|
|
networking =
|
|
|
|
|
lib.mkMerge [
|
|
|
|
|
(lib.mkIf (config.networking.hostName == serverName) serverConfig)
|
|
|
|
|
(lib.mkIf (builtins.hasAttr config.networking.hostName clients) clientConfig)
|
|
|
|
|
];
|
|
|
|
|
}
|