summary refs log tree commit diff
path: root/modules/vpn/ingress.nix
blob: 6c6a78e4d772a8e64c23c2e7bd4ff336afefdc4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{
  config,
  lib,
  ...
}:

let
  inherit (lib)
    imap0
    attrValues
    mergeAttrsList
    replaceString
    concatImapStringsSep
    ;

  definition = import ./definition.nix;

  inherit (definition) paths users mask;

  ownAddress = "10.123.X.1"; # ip of host running the ingress vpn (per-interface)

  addressFromTemplate =
    index: template: prefix:
    "${replaceString "X" (toString (index + 1)) template}/${toString prefix}";

  ingressName = index: "vpn-ingress${toString index}";
  egressName = "vpn-egress0";
  egressAddress = "10.123.255.1/16"; # /16 encompasses all possible subnet addresses
  egressMTU = 1400;

  egressHost = name: "${name}.rnrd.eu";
in
{
  boot.kernel.sysctl = {
    "net.ipv4.ip_forward" = 1; # allow ipv4 forwarding
  };

  networking.firewall = {
    allowedUDPPorts = map (x: x.port) paths;
    allowedTCPPorts = map (x: x.port) paths;
    checkReversePath = "loose";
  };

  age.secrets.ingress-key = {
    file = ../../secrets/vpn/ingress-key.age;
    owner = "systemd-network";
  };

  systemd.network =
    let
      mkNetdev = index: path: {
        "10-${ingressName index}" = {
          netdevConfig = {
            Kind = "wireguard";
            Name = ingressName index;
          };
          wireguardConfig = {
            PrivateKeyFile = config.age.secrets.ingress-key.path;
            ListenPort = path.port;
          };
          wireguardPeers = map (user: {
            PublicKey = user.key;
            AllowedIPs = [ (addressFromTemplate index user.ip 32) ];
          }) (attrValues users);
        };
      };

      mkNetwork = index: path: {
        "10-${ingressName index}" = {
          name = ingressName index;
          address = [ (addressFromTemplate index ownAddress 24) ];
          routingPolicyRules = [
            {
              IncomingInterface = ingressName index;
              Table = 100;
            }
          ];
        };
      };

      ingressNetdevs = imap0 mkNetdev paths;

      ingressNetworks = imap0 mkNetwork paths;
      egressNetworks = [
        {
          "20-${egressName}" = {
            name = egressName;
            address = [ egressAddress ];
            networkConfig = {
              IPv4ReversePathFilter = "loose";
            };
            linkConfig = {
              ActivationPolicy = "up";
              RequiredForOnline = "no"; # does not count as online
              MTUBytes = toString egressMTU;
            };
            routes = [
              {
                Destination = "0.0.0.0/0";
                Table = 100;
                Scope = "link";
              }
            ];
          };
        }
      ];
    in
    {
      netdevs = mergeAttrsList ingressNetdevs;
      networks = mergeAttrsList (ingressNetworks ++ egressNetworks);
    };

  # allow forwarding packets between egress and ingress, but avoid any snat,
  # ip should always keep it's origin form, for correct egress routing.
  # also adapt mss to outgoing mss value, so that we don't shatter packets.
  networking.nftables.ruleset =
    let
      ingressInterfaces = concatImapStringsSep "\", \"" (i: _: ingressName (i - 1)) paths;
    in
    ''
      table inet filter {
        chain forward {
          type filter hook forward priority 0; policy drop;

          tcp flags syn tcp option maxseg size set rt mtu

          iifname { "${ingressInterfaces}" } oifname "${egressName}" accept
          iifname "${egressName}" oifname { "${ingressInterfaces}" } accept
        }
      }
    '';

  # sing-box is a vpn client supporting various protocols which will allow us
  # to configure it in whichever way we want to avoid russian dpi.
  # in this case, our communications crossing the borders are relying on vless.
  services.sing-box =
    let
      inboundName = "vpn-in";
      outboundName = egress: "vpn-out-${egress}";
    in
    {
      enable = true;
      settings = {
        inbounds = [
          {
            type = "tun";
            tag = inboundName;
            interface_name = egressName;
            address = [ egressAddress ];
            mtu = egressMTU;
            stack = "gvisor";
            auto_route = false; # we route manually
            strict_route = false;
            endpoint_independent_nat = true;
          }
        ];

        outbounds = map (path: {
          type = "vless";
          flow = "xtls-rprx-vision";

          server = egressHost path.egress;
          server_port = 443;

          tag = outboundName path.egress;
          uuid = path.info.uuid;

          tls = {
            enabled = true;
            server_name = "www.${mask}";

            utls = {
              enabled = true;
              fingerprint = "chrome";
            };

            reality = {
              enabled = true;
              public_key = path.info.public;
              short_id = path.info.short;
            };
          };
        }) paths;

        route = {
          rules = imap0 (index: path: {
            inbound = inboundName;
            source_ip_cidr = [ (addressFromTemplate index "10.123.X.0" 24) ];
            outbound = outboundName path.egress;
          }) paths;

          auto_detect_interface = true;
        };

        log = {
          level = "debug";
          timestamp = true;
        };
      };
    };
}