2023-03-02 16:00:19 +03:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
2023-07-28 18:07:17 +03:00
|
|
|
# Source: https://habr.com/ru/companies/xakep/articles/699000/
|
|
|
|
|
2023-03-02 16:00:19 +03:00
|
|
|
let
|
2023-07-28 17:08:13 +03:00
|
|
|
istalData = import ../../istal/data.secret.nix;
|
|
|
|
|
2023-03-20 23:32:29 +03:00
|
|
|
canigouData = import ../data.secret.nix;
|
2023-07-28 17:08:13 +03:00
|
|
|
port = canigouData.wireguard.port;
|
|
|
|
|
|
|
|
update_ru_routes = pkgs.callPackage ./update_ru_routes.nix { };
|
2023-03-02 16:00:19 +03:00
|
|
|
in
|
|
|
|
{
|
2023-07-28 17:08:13 +03:00
|
|
|
boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
|
|
|
|
boot.kernel.sysctl."net.ipv4.conf.all.forwarding" = 1;
|
|
|
|
|
2023-03-02 16:00:19 +03:00
|
|
|
# enable NAT
|
|
|
|
networking.nat = {
|
|
|
|
enable = true;
|
|
|
|
externalInterface = "eth0";
|
|
|
|
internalInterfaces = [ "wg0" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall = {
|
|
|
|
allowedUDPPorts = [ port ];
|
|
|
|
};
|
|
|
|
|
2023-07-28 17:08:13 +03:00
|
|
|
environment.systemPackages = [ update_ru_routes ];
|
|
|
|
|
|
|
|
networking.wg-quick.interfaces = {
|
2023-03-02 16:00:19 +03:00
|
|
|
# "wg0" is the network interface name. You can name the interface arbitrarily.
|
|
|
|
wg0 = {
|
|
|
|
# Determines the IP address and subnet of the server's end of the tunnel interface.
|
2023-07-28 17:08:13 +03:00
|
|
|
address = [ "10.20.30.1/32" ];
|
2023-03-02 16:00:19 +03:00
|
|
|
|
|
|
|
# The port that WireGuard listens to. Must be accessible by the client.
|
|
|
|
listenPort = port;
|
|
|
|
|
|
|
|
# This allows the wireguard server to route your traffic to the internet and hence be like a VPN
|
|
|
|
# For this to work you have to set the dnsserver IP of your router (or dnsserver of choice) in your clients
|
2023-07-28 17:08:13 +03:00
|
|
|
postUp = ''
|
|
|
|
gateway=`${pkgs.iproute}/bin/ip route | ${pkgs.gawk}/bin/awk '/default/ {print $3; exit}'`
|
|
|
|
interface=`${pkgs.iproute}/bin/ip route | ${pkgs.gawk}/bin/awk '/default/ {print $5; exit}'`
|
|
|
|
${pkgs.iptables}/bin/iptables -t nat -A POSTROUTING -o $interface -j MASQUERADE
|
|
|
|
${pkgs.iproute}/bin/ip rule add from ${canigouData.addr} table main
|
|
|
|
${pkgs.iproute}/bin/ip route add 193.0.6.150 via $gateway dev $interface
|
2023-03-02 16:00:19 +03:00
|
|
|
'';
|
2023-07-28 17:08:13 +03:00
|
|
|
preDown = ''
|
|
|
|
gateway=`${pkgs.iproute}/bin/ip route | ${pkgs.gawk}/bin/awk '/default/ {print $3; exit}'`
|
|
|
|
interface=`${pkgs.iproute}/bin/ip route | ${pkgs.gawk}/bin/awk '/default/ {print $5; exit}'`
|
|
|
|
${pkgs.iptables}/bin/iptables -t nat -D POSTROUTING -o $interface -j MASQUERADE
|
|
|
|
${pkgs.iproute}/bin/ip rule del from ${canigouData.addr} table main
|
|
|
|
${pkgs.iproute}/bin/ip route del 193.0.6.150 via $gateway dev $interface
|
2023-03-02 16:00:19 +03:00
|
|
|
'';
|
|
|
|
|
|
|
|
# Path to the private key file.
|
|
|
|
privateKeyFile = config.age.secrets.wireguard-canigou-private.path;
|
|
|
|
|
|
|
|
peers = [
|
2023-07-28 17:08:13 +03:00
|
|
|
# Istal
|
|
|
|
{
|
|
|
|
publicKey = istalData.wireguard.publicKey;
|
|
|
|
allowedIPs = [ "10.20.30.2/32" "0.0.0.0/0" ];
|
|
|
|
}
|
|
|
|
# Home
|
2023-03-02 16:00:19 +03:00
|
|
|
{
|
|
|
|
publicKey = "Gg+p7tysAhu2X841weBiQrqoKXh6kvcmDiCY62rLwQg=";
|
2023-07-28 17:08:13 +03:00
|
|
|
allowedIPs = [ "10.20.30.3/32" ];
|
2023-03-02 16:00:19 +03:00
|
|
|
}
|
2023-07-28 17:08:13 +03:00
|
|
|
# Asus
|
2023-03-02 16:00:19 +03:00
|
|
|
{
|
|
|
|
publicKey = "mzVH0N3q7UE/XjMwgRks+D8KFuIj91VkOK2ytgjsnkw=";
|
2023-07-28 17:08:13 +03:00
|
|
|
allowedIPs = [ "10.20.30.4/32" ];
|
2023-03-02 16:00:19 +03:00
|
|
|
}
|
2023-07-28 18:07:17 +03:00
|
|
|
# Phone 1
|
|
|
|
{
|
|
|
|
publicKey = "KRgGzoFk8UjZ1wUWXkt53wI7sJfdP/QaoVY3Xnh13Ws=";
|
|
|
|
allowedIPs = [ "10.20.30.5/32" ];
|
|
|
|
}
|
|
|
|
# Phone 2
|
|
|
|
{
|
|
|
|
publicKey = "EtnHCU478Ne7dtIctq4yFUNjHGXB7SIQEs2g82VzmBc=";
|
|
|
|
allowedIPs = [ "10.20.30.6/32" ];
|
|
|
|
}
|
2023-03-02 16:00:19 +03:00
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
age.secrets.wireguard-canigou-private = {
|
2023-03-18 16:47:02 +03:00
|
|
|
file = ../../../../secrets/wireguard-canigou-private.age;
|
2023-03-02 16:00:19 +03:00
|
|
|
mode = "0400";
|
|
|
|
};
|
|
|
|
}
|