refactor(access): pbx

This commit is contained in:
arcnmx 2024-06-13 14:51:58 -07:00
parent dc9ee1d789
commit 691ffc1797

View file

@ -1,74 +1,96 @@
{ {
config, config,
access, access,
gensokyo-zone,
lib, lib,
... ...
}: let }: let
inherit (lib.modules) mkIf mkMerge mkDefault; inherit (gensokyo-zone.lib) mapDefaults;
inherit (lib.modules) mkIf mkDefault;
inherit (lib.lists) optional; inherit (lib.lists) optional;
inherit (config.services) nginx; inherit (config.services) nginx;
system = access.systemForService "freepbx"; system = access.systemForService "freepbx";
inherit (system.exports.services) freepbx; inherit (system.exports.services) freepbx;
upstreamName = "freepbx";
upstreamName'ucp = "freepbx'ucp";
in { in {
config.services.nginx = { config.services.nginx = {
upstreams' = {
${upstreamName} = {
servers.access = {
accessService = mapDefaults {
inherit (freepbx) name id;
system = system.name;
port = "https";
};
};
};
${upstreamName'ucp} = {
servers.access = {
accessService = mapDefaults {
inherit (freepbx) name id;
system = system.name;
port = "ucp-ssl";
getAddressFor = "getAddress4For";
};
};
};
};
virtualHosts = let virtualHosts = let
proxyScheme = "https"; ucpPath = "/socket.io/";
url = access.proxyUrlFor {
serviceName = "freepbx";
portName = proxyScheme;
};
ucpUrl = access.proxyUrlFor {
serviceName = "freepbx";
portName = "ucp-ssl";
getAddressFor = "getAddress4For";
};
ucpPath = "/socket.io";
# TODO: ports.asterisk/asterisk-ssl? # TODO: ports.asterisk/asterisk-ssl?
extraConfig = '' hostCommon = {xvars, ...}: {
proxy_buffer_size 128k; extraConfig = ''
proxy_buffers 4 256k; proxy_buffer_size 128k;
proxy_busy_buffers_size 256k; proxy_buffers 4 256k;
''; proxy_busy_buffers_size 256k;
locations = { proxy_cookie_flags ~ nosamesite;
"/" = {xvars, ...}: { proxy_cookie_domain ~ ${xvars.get.host};
xvars.enable = true; '';
proxy = { locations = {
enable = true; ${ucpPath} = {
redirect = { xvars,
virtualHost,
...
}: {
proxy = {
enable = true; enable = true;
fromScheme = xvars.get.proxy_scheme; websocket.enable = true;
headers.hide.Access-Control-Allow-Origin = true;
};
headers.set.Access-Control-Allow-Origin = "${xvars.get.scheme}://${virtualHost.serverName}";
};
};
};
hostWeb = {...}: {
imports = [hostCommon];
locations = {
"/" = {xvars, ...}: {
xvars.enable = true;
proxy = {
enable = true;
redirect = {
enable = true;
fromScheme = xvars.get.proxy_scheme;
};
}; };
}; };
}; ${ucpPath}.proxy = {
${ucpPath} = { upstream = mkDefault nginx.virtualHosts.freepbx'ucp.proxy.upstream;
xvars,
virtualHost,
...
}: {
proxy = {
enable = true;
websocket.enable = true;
headers.hide.Access-Control-Allow-Origin = true;
}; };
headers.set.Access-Control-Allow-Origin = "${xvars.get.scheme}://${virtualHost.serverName}";
}; };
}; };
allLocations = mkMerge [
locations
{
${ucpPath}.proxy.url = mkDefault nginx.virtualHosts.freepbx'ucp.proxy.url;
}
];
name.shortServer = mkDefault "pbx"; name.shortServer = mkDefault "pbx";
in { in {
freepbx = { freepbx = {...}: {
imports = [hostWeb];
vouch.enable = mkDefault true; vouch.enable = mkDefault true;
ssl.force = true; ssl.force = true;
proxy.url = mkDefault url; proxy.upstream = upstreamName;
locations = allLocations; inherit name;
inherit name extraConfig;
}; };
freepbx'ucp = { freepbx'ucp = {...}: {
imports = [hostCommon];
serverName = mkDefault nginx.virtualHosts.freepbx.serverName; serverName = mkDefault nginx.virtualHosts.freepbx.serverName;
ssl.cert.copyFromVhost = "freepbx"; ssl.cert.copyFromVhost = "freepbx";
listen' = { listen' = {
@ -83,17 +105,14 @@ in {
}; };
}; };
proxy = { proxy = {
url = mkDefault ucpUrl; upstream = mkDefault upstreamName'ucp;
websocket.enable = true; websocket.enable = true;
}; };
vouch.enable = mkDefault true; vouch.enable = mkDefault true;
local.denyGlobal = mkDefault nginx.virtualHosts.freepbx.local.denyGlobal; local.denyGlobal = mkDefault nginx.virtualHosts.freepbx.local.denyGlobal;
locations = {
inherit (locations) "/socket.io";
};
inherit extraConfig;
}; };
freepbx'local = { freepbx'local = {...}: {
imports = [hostWeb];
listen' = { listen' = {
http = {}; http = {};
https.ssl = true; https.ssl = true;
@ -106,10 +125,9 @@ in {
}; };
}; };
ssl.cert.copyFromVhost = "freepbx"; ssl.cert.copyFromVhost = "freepbx";
proxy.url = mkDefault nginx.virtualHosts.freepbx.proxy.url; proxy.copyFromVhost = "freepbx";
local.enable = true; local.enable = true;
locations = allLocations; inherit name;
inherit name extraConfig;
}; };
}; };
}; };