Anubis subrequest

merge-requests/100/head
Pyjacpp 2026-10-28 22:53:19 +01:00
parent 19f40c1c4c
commit bf059ed261
No known key found for this signature in database
GPG Key ID: ED479A5A26930939
2 changed files with 47 additions and 63 deletions

View File

@ -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

View File

@ -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 + ''
# 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 (