2023-09-23 10:39:48 -07:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let cfg = config.modules.services.wireguard-server;
|
|
|
|
in {
|
|
|
|
options.modules.services.wireguard-server.enable = lib.mkEnableOption "wireguard-server";
|
|
|
|
config = lib.mkIf cfg.enable {
|
2023-10-07 02:00:29 -07:00
|
|
|
# Enable NAT
|
|
|
|
networking.nat = {
|
|
|
|
enable = true;
|
|
|
|
enableIPv6 = true;
|
|
|
|
externalInterface = "enp4s0";
|
|
|
|
internalInterfaces = [ "wg0" ];
|
2023-09-23 10:39:48 -07:00
|
|
|
};
|
|
|
|
|
2023-10-07 02:00:29 -07:00
|
|
|
# Open ports in the firewall
|
|
|
|
networking.firewall = {
|
|
|
|
allowedTCPPorts = [ 53 ];
|
|
|
|
allowedUDPPorts = [ 53 51820 ];
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.wg-quick.interfaces = {
|
2023-09-23 10:39:48 -07:00
|
|
|
# "wg0" is the network interface name. You can name the interface arbitrarily.
|
|
|
|
wg0 = {
|
2023-10-07 02:00:29 -07:00
|
|
|
# Determines the IP/IPv6 address and subnet of the client's end of the tunnel interface
|
|
|
|
address = [ "192.168.2.1/24" ];
|
|
|
|
# The port that WireGuard listens to - recommended that this be changed from default
|
2023-09-23 10:39:48 -07:00
|
|
|
listenPort = 51820;
|
2023-10-07 02:00:29 -07:00
|
|
|
# Path to the server's private key
|
|
|
|
privateKeyFile = "/engi/apps/wireguard/private";
|
2023-09-23 10:39:48 -07:00
|
|
|
|
|
|
|
# This allows the wireguard server to route your traffic to the internet and hence be like a VPN
|
2023-10-07 02:00:29 -07:00
|
|
|
postUp = ''
|
|
|
|
${pkgs.iptables}/bin/iptables -A FORWARD -i %i -j ACCEPT
|
|
|
|
${pkgs.iptables}/bin/iptables -A FORWARD -o %i -j ACCEPT
|
|
|
|
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -o enp4s0 -j MASQUERADE
|
2023-09-23 10:39:48 -07:00
|
|
|
|
|
|
|
'';
|
|
|
|
|
2023-10-07 02:00:29 -07:00
|
|
|
# Undo the above
|
|
|
|
preDown = ''
|
|
|
|
${pkgs.iptables}/bin/iptables -D FORWARD -i %i -j ACCEPT
|
|
|
|
${pkgs.iptables}/bin/iptables -D FORWARD -o %i -j ACCEPT
|
|
|
|
${pkgs.iptables}/bin/iptables -t nat -D POSTROUTING -o enp4s0 -j MASQUERADE
|
|
|
|
'';
|
2023-09-23 10:39:48 -07:00
|
|
|
|
|
|
|
peers = [
|
2023-10-07 02:00:29 -07:00
|
|
|
{
|
|
|
|
# Adjudicator
|
|
|
|
publicKey = "r2/IeYCO1T+l248387wUBoNnc2DK9O8pHcIr/NQqezM=";
|
|
|
|
allowedIPs = [ "192.168.2.2/32" ];
|
2023-09-23 10:39:48 -07:00
|
|
|
}
|
2023-10-07 02:00:29 -07:00
|
|
|
# More peers can be added here.
|
2023-09-23 10:39:48 -07:00
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|