mirror of
https://github.com/gensokyo-zone/infrastructure.git
synced 2026-02-09 12:29:19 -08:00
cloudflared refactor
This commit is contained in:
parent
b74668cfba
commit
fcd61d39ea
2 changed files with 56 additions and 9 deletions
|
|
@ -1,20 +1,30 @@
|
|||
{ config, lib, ... }: let
|
||||
inherit (lib) mapAttrs' nameValuePair splitString last;
|
||||
{ pkgs, config, utils, lib, ... }: let
|
||||
inherit (lib) mapAttrsToList mapAttrs' nameValuePair splitString last singleton
|
||||
mkIf mkMerge mkForce;
|
||||
inherit (config) services;
|
||||
inherit (services.kanidm.serverSettings) domain;
|
||||
cfg = services.cloudflared;
|
||||
apartment = "131222b0-9db0-4168-96f5-7d45ec51c3be";
|
||||
shadowTunnel = {
|
||||
${apartment}.ingress.deluge = {
|
||||
hostname._secret = config.sops.secrets.cloudflared-tunnel-apartment-deluge.path;
|
||||
service = "http://localhost:${toString services.deluge.web.port}";
|
||||
};
|
||||
};
|
||||
in {
|
||||
sops.secrets.cloudflared-tunnel-apartment.owner = services.cloudflared.user;
|
||||
sops.secrets.cloudflared-tunnel-apartment-deluge.owner = services.cloudflared.user;
|
||||
services.cloudflared = {
|
||||
enable = true;
|
||||
tunnels = {
|
||||
"131222b0-9db0-4168-96f5-7d45ec51c3be" = {
|
||||
${apartment} = {
|
||||
credentialsFile = config.sops.secrets.cloudflared-tunnel-apartment.path;
|
||||
default = "http_status:404";
|
||||
ingress = mapAttrs' (prefix: nameValuePair "${prefix}${domain}") {
|
||||
"" = "http://localhost:80";
|
||||
"home." = "http://localhost:${toString services.home-assistant.config.http.server_port}";
|
||||
"z2m." = "http://localhost:80";
|
||||
"login." = "http://localhost:${toString services.vouch-proxy.settings.vouch.port}";
|
||||
"".service = "http://localhost:80";
|
||||
"home.".service = "http://localhost:${toString services.home-assistant.config.http.server_port}";
|
||||
"z2m.".service = "http://localhost:80";
|
||||
"login.".service = "http://localhost:${toString services.vouch-proxy.settings.vouch.port}";
|
||||
"id." = let
|
||||
inherit (services.kanidm.serverSettings) bindaddress;
|
||||
port = last (splitString ":" bindaddress);
|
||||
|
|
@ -26,4 +36,40 @@ in {
|
|||
};
|
||||
};
|
||||
};
|
||||
systemd.services = let
|
||||
filterConfig = lib.filterAttrsRecursive (_: v: ! builtins.elem v [ null [ ] { } ]);
|
||||
mapIngress = hostname: ingress: {
|
||||
inherit hostname;
|
||||
} // filterConfig (filterConfig ingress);
|
||||
in mkIf cfg.enable (mapAttrs' (uuid: tunnel: let
|
||||
RuntimeDirectory = "cloudflared-tunnel-${uuid}";
|
||||
configPath = "/run/${RuntimeDirectory}/config.yml";
|
||||
settings = {
|
||||
tunnel = uuid;
|
||||
credentials-file = tunnel.credentialsFile;
|
||||
ingress = mapAttrsToList mapIngress tunnel.ingress
|
||||
++ mapAttrsToList mapIngress shadowTunnel.${uuid}.ingress or { }
|
||||
++ singleton { service = tunnel.default; };
|
||||
};
|
||||
in nameValuePair "cloudflared-tunnel-${uuid}" (mkMerge [
|
||||
{
|
||||
after = [ "tailscale-autoconnect.service" ];
|
||||
serviceConfig = {
|
||||
RestartSec = 10;
|
||||
};
|
||||
}
|
||||
(mkIf (shadowTunnel.${uuid} or { } != { }) {
|
||||
serviceConfig = {
|
||||
inherit RuntimeDirectory;
|
||||
ExecStart = mkForce [
|
||||
"${cfg.package}/bin/cloudflared tunnel --config=${configPath} --no-autoupdate run"
|
||||
];
|
||||
ExecStartPre = [
|
||||
(pkgs.writeShellScript "cloudflared-tunnel-${uuid}-prepare" ''
|
||||
${utils.genJqSecretsReplacementSnippet settings configPath}
|
||||
'')
|
||||
];
|
||||
};
|
||||
})
|
||||
])) cfg.tunnels);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue