Changes on informis, for VPN

This commit is contained in:
root@procul 2020-11-19 13:05:24 -06:00
parent e196eeced4
commit 8d88c8682e
2 changed files with 85 additions and 47 deletions

View File

@ -4,29 +4,46 @@ with lib;
let let
cfg = config.fudo.vpn; cfg = config.fudo.vpn;
peerOpts = { peer-name, ... }: { ip-util = import ../../lib/ip.nix { lib = lib; };
options = with types; {
public-key = mkOption {
type = str;
description = "Peer public key.";
};
allowed-ips = mkOption { generate-pubkey-pkg = name: privkey:
type = listOf str; pkgs.runCommand "wireguard-${name}-pubkey" {
description = "List of allowed IP ranges from which this peer can connect."; WIREGUARD_PRIVATE_KEY = privkey;
example = [ "10.100.0.0/16" ]; } ''
default = []; mkdir $out
}; PUBKEY=$(echo $WIREGUARD_PRIVATE_KEY | ${pkgs.wireguard-tools}/bin/wg pubkey)
}; echo $PUBKEY > $out/pubkey.key
'';
generate-client-config = privkey-file: server-pubkey: network: server-ip: listen-port: dns-servers: ''
[Interface]
Address = ${ip-util.networkMinIp network}
PrivateKey = ${fileContents privkey-file}
ListenPort = ${toString listen-port}
DNS = ${concatStringsSep ", " dns-servers}
[Peer]
PublicKey = ${server-pubkey}
Endpoint = ${server-ip}:${toString listen-port}
AllowedIps = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
'';
generate-peer-entry = peer-name: peer-privkey-path: peer-allowed-ips: let
peer-pkg = generate-pubkey-pkg "client-${peer-name}" (fileContents peer-privkey-path);
pubkey-path = "${peer-pkg}/pubkey.key";
in {
publicKey = fileContents pubkey-path;
allowedIPs = peer-allowed-ips;
}; };
in { in {
options.fudo.vpn = with types; { options.fudo.vpn = with types; {
enable = mkEnableOption "Enable Fudo VPN"; enable = mkEnableOption "Enable Fudo VPN";
ips = mkOption { network = mkOption {
type = str; type = str;
description = "IP range to assign this interface."; description = "Network range to assign this interface.";
default = "10.100.0.0/16"; default = "10.100.0.0/16";
}; };
@ -42,36 +59,68 @@ in {
default = 51820; default = 51820;
}; };
peers = mkOption { dns-servers = mkOption {
type = listOf str; type = listOf str;
description = "A list of peers for which to generate config files."; description = "A list of dns servers to pass to clients.";
default = []; default = ["1.1.1.1" "8.8.8.8"];
};
server-ip = mkOption {
type = str;
description = "IP of this WireGuard server.";
}; };
peers = mkOption { peers = mkOption {
type = loaOf (submodule peerOpts); type = loaOf str;
description = "A list of peers allowed to connect."; description = "A map of peer name to a path pointing to that client's private key.";
default = {}; default = {};
example = { example = {
peer0 = { peer0 = "/path/to/priv.key";
public-key = "xyz";
allowed-ips = ["10.100.1.0/24"];
};
}; };
}; };
}; };
config = mkIf cfg.enable { config = mkIf cfg.enable {
environment.etc = let
peer-data = imap1 (i: peer:{
name = peer.name;
privkey-path = peer.privkey-path;
network-range = let
base = ip-util.intToIpv4
((ip-util.ipv4ToInt (ip-util.getNetworkBase cfg.network)) + (i * 256));
in "${base}/24";
}) (mapAttrsToList (name: privkey-path: {
name = name;
privkey-path = privkey-path;
}) cfg.peers);
server-pubkey-pkg = generate-pubkey-pkg "server-pubkey" (fileContents cfg.private-key-file);
server-pubkey = fileContents "${server-pubkey-pkg}/pubkey.key";
in listToAttrs
(map (peer: nameValuePair "wireguard/clients/${peer.name}.conf" {
mode = "0400";
user = "root";
group = "root";
text = generate-client-config
peer.privkey-path
server-pubkey
peer.network-range
cfg.server-ip
cfg.listen-port
cfg.dns-servers;
}) peer-data);
networking.wireguard = { networking.wireguard = {
enable = true; enable = true;
interfaces.wgtun0 = { interfaces.wgtun0 = {
generatePrivateKeyFile = false; generatePrivateKeyFile = false;
ips = [ cfg.ips ]; ips = [ cfg.network ];
listenPort = cfg.listen-port; listenPort = cfg.listen-port;
peers = mapAttrsToList (peer-name: peer-config: { peers = mapAttrsToList
publicKey = peer-config.public-key; (name: private-key: generate-peer-entry name private-key ["0.0.0.0/0" "::/0"])
allowedIPs = peer-config.allowed-ips; cfg.peers;
}) cfg.peers;
privateKeyFile = cfg.private-key-file; privateKeyFile = cfg.private-key-file;
}; };
}; };

View File

@ -31,10 +31,6 @@ in {
../informis/users.nix ../informis/users.nix
]; ];
environment.systemPackages = with pkgs; [
multipath-tools
];
networking = { networking = {
hostName = hostname; hostName = hostname;
@ -273,22 +269,15 @@ in {
}; };
fudo.vpn = { fudo.vpn = {
enable = true; # fer some fuckin reason this sets the default gw to the vpn interface
ips = "10.100.0.0/16"; enable = false;
network = "10.100.0.0/16";
server-ip = host_ipv4;
private-key-file = "/srv/wireguard/secure/secret.key"; private-key-file = "/srv/wireguard/secure/secret.key";
peers = { peers = {
peter = { peter = "/srv/wireguard/clients/peter.key";
allowed-ips = [ "10.100.1.0/24" ]; ken = "/srv/wireguard/clients/ken.key";
public-key = "d1NfRFWRkcKq2gxvqfMy7Oe+JFYf5DjomnsTyisvgB4="; helen = "/srv/wireguard/clients/helen.key";
};
ken = {
allowed-ips = [ "10.100.2.0/24" ];
public-key = "y294rTCK0iSRhA6EIOErPzEuqzJMuYAG4XbHasySMVU=";
};
helen = {
allowed-ips = [ "10.100.3.0/24" ];
public-key = "7Hdko6RibhIYdoPLWXGwmElY5vKvZ+rURmqFTDUfC2w=";
};
}; };
}; };