From bf059ed261e19d71c3750eb270d8cbc64aaedaf2 Mon Sep 17 00:00:00 2001 From: Pyjacpp Date: Wed, 28 Oct 2026 22:53:19 +0100 Subject: [PATCH] Anubis subrequest --- modules/crans/anubis.nix | 18 +++--- modules/services/reverseproxy.nix | 92 ++++++++++++++----------------- 2 files changed, 47 insertions(+), 63 deletions(-) diff --git a/modules/crans/anubis.nix b/modules/crans/anubis.nix index 9911691..e14355e 100644 --- a/modules/crans/anubis.nix +++ b/modules/crans/anubis.nix @@ -1,12 +1,9 @@ { config, lib, - pkgs, ... }: let - formatJSON = pkgs.formats.json { }; - inherit (lib) types mkEnableOption @@ -37,13 +34,6 @@ in Where Anubis should listen ''; }; - target = mkOption { - type = types.str; - example = "unix:///run/nginx/socket.sock"; - description = '' - Target that will handle the request. - ''; - }; bots = mkOption { type = types.listOf types.attrs; description = '' @@ -67,6 +57,12 @@ in instances = lib.mapAttrs (name: conf: { enable = true; policy = { + settings = { + statusCodes = { + CHALLENGE = 200; + DENY = 403; + }; + }; extraBots = [ { name = "whitelist-crans"; @@ -96,7 +92,7 @@ in BIND = conf.bind; BIND_NETWORK = "unix"; METRIC_BIND = "/run/anubis/anubis-${name}/metrics.sock"; - TARGET = conf.target; + TARGET = " "; # Subrequest authentification. COOKIE_DOMAIN = conf.hostname; # TODO: db pour mettre en commun les token. # Transitoirement on les sépare diff --git a/modules/services/reverseproxy.nix b/modules/services/reverseproxy.nix index 6d8e28f..fb64fd0 100644 --- a/modules/services/reverseproxy.nix +++ b/modules/services/reverseproxy.nix @@ -157,7 +157,6 @@ in instances = lib.mapAttrs (vhostName: vhostConfig: { hostname = mkHostName vhostName mainTld; bind = "/run/anubis/anubis-${vhostName}/socket.sock"; - target = "unix:/run/nginx/nginx-${vhostName}.sock"; bots = vhostConfig.anubisConfig; }) (lib.filterAttrs (_: c: c.anubisConfig != null) cfg.virtualHosts); }; @@ -203,32 +202,6 @@ in acmeRoot = null; }; - # Conf pour l’hôte en entrée - entryExtraConf = { - extraConfig = '' - set_real_ip_from 172.16.0.0/16; - set_real_ip_from fd00::/56; - real_ip_header X-Real-Ip; - ''; - } - // sslConf; - # Conf supplémentaire pour le proxy principal - vhostExtraConf = - if vhostConfig.anubisConfig != null then - # Il reçoit les requêtes d’Anubis - { - listen = [ - { addr = "unix:/run/nginx/nginx-${vhostName}.sock"; } - ]; - extraConfig = '' - set_real_ip_from unix:; - real_ip_header X-Real-IP; - ''; - } - else - # Il est en entrée - entryExtraConf; - logHostConf = '' access_log /var/log/nginx/host/${vhostName}/access.log; error_log /var/log/nginx/host/${vhostName}/error.log; @@ -249,37 +222,52 @@ in in { # Configuration du service à proxy. - "${vhostName}" = vhostExtraConf // { + "${vhostName}" = sslConf // { serverName = hostname; - locations."/" = mkIf (vhostConfig.proxyPass != null) { - proxyPass = "http://${vhostConfig.proxyPass}"; - proxyWebsockets = vhostConfig.proxyWebsockets; - }; + + # Site statiques et alias root = vhostConfig.serveLocalFiles; globalRedirect = vhostConfig.globalRedirect; - extraConfig = vhostExtraConf.extraConfig + logHostConf; - }; - - # Entrée d’Anubis - "${vhostName}-anubis" = mkIf (vhostConfig.anubisConfig != null) ( - entryExtraConf - // rec { - serverName = hostname; - locations."/" = { - proxyPass = "http://unix:/run/anubis/anubis-${vhostName}/socket.sock"; + locations = { + # Entrée site, via proxy + "/" = mkIf (vhostConfig.proxyPass != null) { + proxyPass = "http://${vhostConfig.proxyPass}"; proxyWebsockets = vhostConfig.proxyWebsockets; - }; - locations."/.within.website/x/cmd/anubis/api/honeypot/" = locations."/" // { - extraConfig = '' - add_header 'X-Robots-Tag' 'noindex,nofollow' always; + extraConfig = lib.optionalString (vhostConfig.anubisConfig != null) '' + auth_request /.within.website/x/cmd/anubis/api/check; + error_page 401 = @redirectToAnubis; ''; }; - extraConfig = entryExtraConf.extraConfig + '' - access_log /var/log/nginx/anubis/access.log; - error_log /var/log/nginx/anubis/error.log; - ''; - } - ); + # Anubis + "/.within.website/" = mkIf (vhostConfig.anubisConfig != null) { + # https://anubis.techaro.lol/docs/admin/configuration/subrequest-auth/ + proxyPass = "http://unix:/run/anubis/anubis-${vhostName}/socket.sock"; + extraConfig = '' + proxy_pass_request_body off; + proxy_set_header content-length ""; + add_header 'X-Robots-Tag' 'noindex,nofollow' always; + auth_request off; + access_log /var/log/nginx/anubis/access.log; + error_log /var/log/nginx/anubis/error.log; + ''; + }; + "@redirectToAnubis" = mkIf (vhostConfig.anubisConfig != null) { + extraConfig = '' + return 307 /.within.website/?redir=$scheme://$host$request_uri; + auth_request off; + access_log /var/log/nginx/anubis/access.log; + error_log /var/log/nginx/anubis/error.log; + ''; + }; + }; + + extraConfig = '' + set_real_ip_from 172.16.0.0/16; + set_real_ip_from fd00::/56; + real_ip_header X-Real-Ip; + '' + + logHostConf; + }; } # Les alias // lib.mergeAttrsList (