meow
This commit is contained in:
parent
c999ed6520
commit
35c8ebb56a
20 changed files with 670 additions and 405 deletions
|
|
@ -7,11 +7,12 @@
|
|||
...
|
||||
}:
|
||||
let
|
||||
wgServer = instanceConfig?wg.server && instanceConfig.wg.server;
|
||||
wgServer = instanceConfig ? wg.server && instanceConfig.wg.server;
|
||||
cfg = config.xyno.services.wireguard;
|
||||
ula = cfg.ula;
|
||||
ulaPrefix = "${ula}:1337"; # /64 for normal vpn
|
||||
monitoringUlaPrefix = "${ula}:2337"; # /64 for monitoring
|
||||
v4Subnet = "10.13.12.0/24";
|
||||
|
||||
# uses a hash digest as the host identifier
|
||||
genUlaForHost =
|
||||
|
|
@ -23,35 +24,49 @@ let
|
|||
in
|
||||
"${prefix}:${localPart}";
|
||||
# peers list for networkd
|
||||
filteredConfigs = builtins.filter (x: x.hostName != config.networking.hostName ) (lib.attrValues instanceConfigs);
|
||||
filteredConfigs = builtins.filter (x: x.hostName != config.networking.hostName) (
|
||||
lib.attrValues instanceConfigs
|
||||
);
|
||||
wgPeersLists = map (
|
||||
c:
|
||||
(
|
||||
(lib.optional (c?publicHostname) {
|
||||
(lib.optional (c ? publicHostname) {
|
||||
# if peer is publicly on the internet
|
||||
AllowedIPs =
|
||||
(lib.optionals ( c?wg.server && c.wg.server) [
|
||||
"${ulaPrefix}::/48" # all traffic in the ula shall be sent to the server
|
||||
(lib.optionals (c ? wg.server && c.wg.server) [
|
||||
# is server
|
||||
"::/0"
|
||||
])
|
||||
++ (lib.optionals (!c?wg.server || !c.wg.server) [
|
||||
++ (lib.optionals (c ? wg.server && c.wg.server && c ? wg.v4 && instanceConfig ? wg.v4) [
|
||||
# both client and server have a v4
|
||||
"0.0.0.0/0"
|
||||
])
|
||||
++ (lib.optionals (!c ? wg.server || !c.wg.server) [
|
||||
# is not server
|
||||
"${genUlaForHost ulaPrefix c.hostName}/128" # if a host is reachable but shouldn't play server, send only to the hosts ip
|
||||
])
|
||||
++ (lib.optionals ((!c ? wg.server || !c.wg.server) && c ? wg.v4 && instanceConfig ? wg.v4) [
|
||||
# no server, no ipv4 yay
|
||||
"${c.wg.v4}/32"
|
||||
]);
|
||||
RouteTable = 1000;
|
||||
Endpoint = "${c.publicHostname}:51820";
|
||||
PersistentKeepalive = 25;
|
||||
PublicKey = c.wg.pubKey;
|
||||
PresharedKeyFile = config.sops.secrets."wg/psk".path;
|
||||
})
|
||||
++ (lib.optional ((!c ? publicHostname) && wgServer && (c ? wg.pubKey)) {
|
||||
# if this is the server and the peer isn't reachable on the internet
|
||||
AllowedIPs = [
|
||||
"${genUlaForHost ulaPrefix c.hostName}/128"
|
||||
"${genUlaForHost monitoringUlaPrefix c.hostName}/128"
|
||||
]
|
||||
++ (lib.optionals (c ? wg.v4 && instanceConfig ? wg.v4) [
|
||||
"${c.wg.v4}/32"
|
||||
]);
|
||||
PublicKey = c.wg.pubKey;
|
||||
PresharedKeyFile = config.sops.secrets."wg/psk".path;
|
||||
})
|
||||
++ (lib.optional
|
||||
((!c?publicHostname) && wgServer && (c?wg.pubKey))
|
||||
{
|
||||
# if this is the server and the peer isn't reachable on the internet
|
||||
AllowedIPs = [
|
||||
"${genUlaForHost ulaPrefix c.hostName}/128"
|
||||
"${genUlaForHost monitoringUlaPrefix c.hostName}/128"
|
||||
];
|
||||
PublicKey = c.wg.pubKey;
|
||||
PresharedKeyFile = config.sops.secrets."wg/psk".path; # TODO
|
||||
}
|
||||
)
|
||||
)
|
||||
) filteredConfigs;
|
||||
wgPeers = lib.flatten wgPeersLists;
|
||||
|
|
@ -79,48 +94,72 @@ in
|
|||
default = genUlaForHost monitoringUlaPrefix config.networking.hostName;
|
||||
};
|
||||
config = lib.mkIf cfg.enable {
|
||||
networking.hosts = (lib.mapAttrs' (
|
||||
n: v: {
|
||||
value = ["${v.hostName}.${cfg.hostsDomain}"];
|
||||
# TODO: add a all traffic through this network
|
||||
networking.hosts =
|
||||
(lib.mapAttrs' (n: v: {
|
||||
value = [ "${v.hostName}.${cfg.hostsDomain}" ];
|
||||
name = (genUlaForHost ulaPrefix v.hostName);
|
||||
}
|
||||
) instanceConfigs) // (lib.mapAttrs' (
|
||||
n: v: {
|
||||
value = ["${v.hostName}.${cfg.monHostsDomain}"];
|
||||
}) instanceConfigs)
|
||||
// (lib.mapAttrs' (n: v: {
|
||||
value = [ "${v.hostName}.${cfg.monHostsDomain}" ];
|
||||
name = (genUlaForHost monitoringUlaPrefix v.hostName);
|
||||
}
|
||||
) instanceConfigs);
|
||||
}) instanceConfigs);
|
||||
networking.firewall.allowedUDPPorts = lib.optional wgServer 51820;
|
||||
networking.firewall.interfaces."wg0".allowedUDPPorts = lib.optional wgServer 53;
|
||||
systemd.network.netdevs."wg0" = {
|
||||
systemd.network.netdevs."99-wg0" = {
|
||||
netdevConfig = {
|
||||
Name = "wg0";
|
||||
Kind = "wireguard";
|
||||
Description = "main wireguard tunnel";
|
||||
|
||||
|
||||
};
|
||||
wireguardConfig = {
|
||||
ListenPort = lib.mkIf wgServer 51820;
|
||||
PrivateKeyFile = config.sops.secrets."wg/privkey".path; # TODO
|
||||
PrivateKeyFile = config.sops.secrets."wg/privkey".path;
|
||||
FirewallMark = "0x8888";
|
||||
};
|
||||
wireguardPeers = wgPeers;
|
||||
};
|
||||
systemd.network.networks."wg0" = {
|
||||
systemd.network.networks."50-wg0" = {
|
||||
matchConfig.Name = "wg0";
|
||||
networkConfig = {
|
||||
Description = "xyno wireguard";
|
||||
IPMasquerade = lib.mkIf (instanceConfig ? wg.server && instanceConfig.wg.server) "both";
|
||||
IPv4Forwarding = (instanceConfig ? wg.server && instanceConfig.wg.server);
|
||||
IPv6Forwarding = (instanceConfig ? wg.server && instanceConfig.wg.server);
|
||||
};
|
||||
address = [
|
||||
"${(genUlaForHost ulaPrefix config.networking.hostName)}/128"
|
||||
"${(genUlaForHost monitoringUlaPrefix config.networking.hostName)}/128"
|
||||
"${(genUlaForHost ulaPrefix config.networking.hostName)}/64"
|
||||
"${(genUlaForHost monitoringUlaPrefix config.networking.hostName)}/64"
|
||||
]
|
||||
++ (lib.optionals (instanceConfig ? wg.v4) [ "${instanceConfig.wg.v4}/24" ]);
|
||||
};
|
||||
systemd.network.networks."51-wg0-all-traffic" = {
|
||||
matchConfig.Name = "wg0";
|
||||
networkConfig = {
|
||||
Description = "xyno wireguard all traffic";
|
||||
DNSDefaultRoute = true;
|
||||
DNS = "2a07:e340::2#dns.mullvad.net";
|
||||
DNSOverTLS = true;
|
||||
};
|
||||
routingPolicyRules = [
|
||||
{
|
||||
FirewallMark = "0x8888";
|
||||
InvertRule = true;
|
||||
Table = 1000;
|
||||
Priority = 10;
|
||||
}
|
||||
];
|
||||
};
|
||||
services.prometheus.exporters.wireguard = lib.mkIf (wgServer && config.xyno.services.monitoring.enable) {
|
||||
enable = true;
|
||||
interfaces = [ "wg0" ];
|
||||
};
|
||||
services.prometheus.exporters.wireguard =
|
||||
lib.mkIf (wgServer && config.xyno.services.monitoring.enable)
|
||||
{
|
||||
enable = true;
|
||||
interfaces = [ "wg0" ];
|
||||
};
|
||||
|
||||
services.coredns = lib.mkIf wgServer { # for non nixos devices to be able to resolve vpn hostnames
|
||||
services.coredns = lib.mkIf wgServer {
|
||||
# for non nixos devices to be able to resolve vpn hostnames
|
||||
enable = true;
|
||||
config = ''
|
||||
. {
|
||||
|
|
@ -132,9 +171,11 @@ in
|
|||
'';
|
||||
};
|
||||
xyno.services.monitoring.exporters.coredns = lib.mkIf wgServer 9153;
|
||||
xyno.services.monitoring.exporters.wireguard = lib.mkIf wgServer config.services.prometheus.exporters.wireguard.port;
|
||||
xyno.services.monitoring.exporters.wireguard =
|
||||
lib.mkIf wgServer config.services.prometheus.exporters.wireguard.port;
|
||||
sops.secrets."wg/privkey" = {
|
||||
reloadUnits = [ "systemd-networkd.service" ];
|
||||
sopsFile = ../../instances/${config.networking.hostName}/secrets/wg.yaml;
|
||||
};
|
||||
sops.secrets."wg/psk" = {
|
||||
reloadUnits = [ "systemd-networkd.service" ];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue