zaphyra's git: nixfiles

zaphyra's nixfiles

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 
{
  machineConfig,
  config,
  lib,
  pkgs,
  ...
}:

{

  options.zpha.websites."ip.fc9f.de".enable = lib.mkEnableOption "";

  config = lib.mkIf config.zpha.websites."ip.fc9f.de".enable {
    dns.zones."fc9f.de".subdomains = {
      "ip" =
        pkgs.dnsNix.combinators.host machineConfig.networking.ip4Address machineConfig.networking.ip6Address;
      "ip4".A = [ machineConfig.networking.ip4Address ];
      "ip6".AAAA = [ machineConfig.networking.ip6Address ];
    };

    services.nginx.virtualHosts = {
      "ip.fc9f.de" = {
        useACMEHost = config.networking.fqdn;
        forceSSL = true;
        kTLS = true;
        locations."/" = {
          extraConfig = "types { } default_type 'text/html; charset=utf-8';";
          return = ''
            200 '
              <!DOCTYPE html>
              <html>
                <head>
                  <title>ip.fc9f.de</title>
                </head>
                <body>
                  <h1>ip.fc9f.de</h1>
                  <ul>
                    <li><span style="user-select: none;"><b>IPv6:</b> </span><span id="ip6">Loading...</span></li>
                    <li><span style="user-select: none;"><b>IPv4:</b> </span><span id="ip4">Loading...</span></li>
                  </ul>
                  <p>Use bash and curl: <code>curl ip{4,6}.zaphyra.eu</code></p>
                  <p><small>Because any other "Whats my IP?"-tool sucks. <a href="https://git.clerie.de/clerie/ip.clerie.de">Host yourself :3</a></small></p>

                  <script>
                    window.addEventListener("DOMContentLoaded", (event) => {
                      [ "ip6", "ip4" ].forEach(async (ipVersion) => {
                        try {
                          const url      = "https://" + ipVersion + ".fc9f.de/";
                          const response = await fetch(url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime());
                          if(response.status != 200) {
                            document.getElementById(ipVersion).innerText = "Error!";
                          } else {
                            document.getElementById(ipVersion).innerText = await response.text();
                          }
                        } catch {
                          document.getElementById(ipVersion).innerText = "Error!";
                        }
                      });
                    });
                  </script>
                </body>
              </html>'
          '';
        };
      };

      "ip4.fc9f.de" = {
        useACMEHost = config.networking.fqdn;
        forceSSL = true;
        kTLS = true;
        locations."/" = {
          return = "200 '$remote_addr\n'";
          extraConfig = ''
            types { } default_type "text/plain; charset=utf-8";
            add_header Access-Control-Allow-Origin *;
          '';
        };
      };

      "ip6.fc9f.de" = {
        useACMEHost = config.networking.fqdn;
        forceSSL = true;
        kTLS = true;
        locations."/" = {
          return = "200 '$remote_addr\n'";
          extraConfig = ''
            types { } default_type "text/plain; charset=utf-8";
            add_header Access-Control-Allow-Origin *;
          '';
        };
      };
    };
  };

}