nixos-configuration/hosts/magnesium/services/ip.nix
2025-12-09 15:31:25 +01:00

64 lines
2.1 KiB
Nix

{ pkgs, lib, ... }:
let
baseDomain = "jalr.de";
webDomain = "ip.${baseDomain}";
ip4Domain = "ip4.${baseDomain}";
ip6Domain = "ip6.${baseDomain}";
in
{
services.nginx.virtualHosts = lib.attrsets.genAttrs [ ip4Domain ip6Domain ]
(_: {
enableACME = true;
addSSL = true;
locations."/" = {
return = ''200 "$remote_addr\n"'';
extraConfig = ''
types { } default_type "text/plain; charset=utf-8";
add_header Access-Control-Allow-Origin *;
'';
};
}) // {
"${webDomain}" = {
enableACME = true;
forceSSL = true;
root = pkgs.writeTextDir "index.html" ''
<!DOCTYPE html>
<html>
<head>
<title>${webDomain}</title>
</head>
<body>
<h1>${webDomain}</h1>
<ul>
<li><b>IPv6:</b> <span id="ip6" data-url="https://${ip6Domain}/">Loading...</span></li>
<li><b>IPv4:</b> <span id="ip4" data-url="https://${ip4Domain}/">Loading...</span></li>
</ul>
<script>
function httpGet(url, loaded, error) {
var r = new XMLHttpRequest();
r.open("GET", url + "?" + (new Date()).getTime(), true);
r.onload = () => {
if(r.status == 200) {
loaded(r.responseText);
}
else {
error();
}
};
r.onerror = () => {error();};
r.send(null);
}
document.querySelectorAll("span").forEach(el => {
httpGet(el.dataset.url,
(response) => {el.innerText = response;},
() => {el.innerText = "Error";}
);
});
</script>
</body>
</html>
'';
};
};
}