Merge branch 'reverseproxy' into 'main'

Fix rules anubis

See merge request nounous/nixos!27
main
lzebulon 2025-08-03 15:49:01 +02:00
commit 6ba4c15870
2 changed files with 229 additions and 205 deletions

View File

@ -1,141 +1,149 @@
{ pkgs, ... }: { pkgs, ... }:
let let
anubisBotsMirror = pkgs.writeText "anubis_bots_mirror.yaml" formatJSON = pkgs.formats.json { };
'' formatYAML = pkgs.formats.yaml { };
- name: whitelist-crans
action: ALLOW
remote_addresses:
- 185.230.79.0/22
- 2a0c:700::/32
- 46.105.102.188/32
- 2001:41d0:2:d5bc::/128
- name: no-user-agent-string anubisBotsMirror = formatYAML.generate "anubis_bots_mirror.yaml" [
action: DENY {
expression: userAgent == "" name = "whitelist-crans";
action = "ALLOW";
remote_addresses = [
"185.230.79.0/22"
"2a0c:700::/32"
"46.105.102.188/32"
"2001:41d0:2:d5bc::/128"
];
}
{
name = "no-user-agent";
action = "DENY";
expression = "userAgent == \"\"";
}
{
name = "ban-gpt";
action = "DENY";
user_agent_regex = ".*gpt.*";
}
{
name = "ban-bot";
action = "DENY";
user_agent_regex = ".*(b|B)ot.*";
}
{
name = "ban-WebKit";
action = "DENY";
expression = {
all = [
"userAgent.startsWith(\"Mozilla\")"
"userAgent.startsWith(\"AppleWebKit\")"
"userAgent.startsWith(\"Safari\")"
"userAgent.startsWith(\"Chrome\")"
];
};
}
{
name = "ban-Barkrowler";
action = "DENY";
user_agent_regex = ".*Barkrowler.*";
}
];
- name: ban-gpt anubisMirror = formatJSON.generate "anubis_mirror.json" {
user_agent_regex: ".*gpt.*" bots = [
action: DENY
- name: ban-bot
user_agent_regex: ".*(b|B)ot.*"
action: DENY
- name: ban-WebKit
action: DENY
expression:
all:
- userAgent.startsWith("Mozilla")
- userAgent.matches("AppleWebKit")
- userAgent.matches("Safari")
- userAgent.matches("Chrome")
- name: ban-Barkrowler
user_agent_regex: ".*Barkrowler.*"
action: DENY
'';
anubisMirror = pkgs.writeText "anubis_mirror.json"
''
{ {
"bots": [ import = "${anubisBotsMirror}";
{
"import": "${anubisBotsMirror}"
},
{
"name": "allow-repo",
"path_regex": "^...*",
"action": "ALLOW"
},
{
"name": "deny-other",
"path_regex": ".*",
"action": "ALLOW"
}
]
} }
'';
antibot = pkgs.writeText "antibot.yaml"
''
- name: whitelist-crans
action: ALLOW
remote_addresses:
- 185.230.79.0/22
- 2a0c:700::/32
- 46.105.102.188/32
- 2001:41d0:2:d5bc::/128
- name: no-user-agent-string
action: DENY
expression: userAgent == ""
- name: ban-gpt
user_agent_regex: ".*gpt.*"
action: DENY
- name: ban-bot
user_agent_regex: ".*(b|B)ot.*"
action: DENY
- name: ban-WebKit
action: CHALLENGE
expression:
all:
- userAgent.startsWith("Mozilla")
- userAgent.matches("AppleWebKit")
- userAgent.matches("Safari")
- userAgent.matches("Chrome")
- name: ban-Barkrowler
user_agent_regex: ".*Barkrowler.*"
action: DENY
'';
anubisChallenge = pkgs.writeText "anubis_challenge.json"
''
{ {
"bots": [ name = "allow-repo";
{ action = "ALLOW";
"import": "${antibot}" path_regex = "^...*";
},
{
"name": "challenge-other",
"path_regex": "^*",
"action": "CHALLENGE"
}
]
} }
'';
anubisMirrors = pkgs.writeText "anubis_mirrors.json"
''
{ {
"bots": [ name = "deny-other";
{ path_regex = ".*";
"import": "${antibot}" action = "ALLOW";
}, }
{ ];
"name": "deny-other", };
"path_regex": ".*cdimage-.*",
"action": "ALLOW" antiBot = formatYAML.generate "antibot.yaml" [
}, {
{ import = "${anubisBotsMirror}";
"name": "allow-repo", }
"path_regex": "^...*", {
"action": "ALLOW" # On refuse les bots qui font souvent de la merde.
}, # https://github.com/TecharoHQ/anubis/blob/main/data/bots/deny-pathological.yaml
{ import = "(data)/bots/_deny-pathological.yaml";
"name": "deny-other", }
"path_regex": ".*", {
"action": "CHALLENGE" # On autorise les indexers des moteurs de recherche.
} # https://github.com/TecharoHQ/anubis/blob/main/data/crawlers/_allow-good.yaml
] import = "(data)/crawlers/_allow-good.yaml";
} }
''; {
in { # On autorise l'accès à favicon, robots.txt, well-known, ...
# https://github.com/TecharoHQ/anubis/blob/main/data/common/keep-internet-working.yaml
import = "(data)/common/keep-internet-working.yaml";
}
{
# On refuse si userAgent = ""
# https://github.com/TecharoHQ/anubis/blob/main/data/common/keep-internet-working.yaml
import = "(data)/common/rfc-violations.yaml";
}
{
# On bloque les AI aggressivement (bots/agent, training et user search par IA)
# https://github.com/TecharoHQ/anubis/blob/main/data/meta/ai-block-aggressive.yaml
import = "(data)/meta/ai-block-aggressive.yaml";
}
];
anubisChallenge = formatJSON.generate "anubis_challenge.json" {
"bots" = [
{
import = "${antiBot}";
}
{
name = "challenge-other";
path_regex = "^*";
action = "CHALLENGE";
}
];
};
anubisMirrors = formatJSON.generate "anubis_mirrors.json" {
"bots" = [
{
import = "${antiBot}";
}
{
name = "deny-other";
path_regex = ".*cdimage-.*";
action = "ALLOW";
}
{
name = "allow-repo";
path_regex = "^...*";
action = "ALLOW";
}
{
name = "deny-other";
path_regex = ".*";
action = "CHALLENGE";
}
];
};
in
{
crans = { crans = {
reverseProxy = { reverseProxy = {
enable = true; enable = true;
virtualHosts = { virtualHosts = {
"collabora" = {
target = "172.16.10.149";
proxyWebsockets = true;
};
"eclat" = { "eclat" = {
anubisConfig = "${anubisMirror}"; anubisConfig = "${anubisMirror}";
httpOnly = true; httpOnly = true;

View File

@ -1,30 +1,36 @@
{ pkgs, lib, config, ... }: {
pkgs,
lib,
config,
...
}:
let let
cfg = config.crans.reverseProxy; cfg = config.crans.reverseProxy;
allowAll = pkgs.writeText "allow_all.json" formatJSON = pkgs.formats.json { };
''
allowAll = formatJSON.generate "allow_all.json" {
bots = [
{ {
"bots": [ name = "allow_all";
{ path_regex = ".*";
"name": "allow_all", action = "ALLOW";
"path_regex": ".*",
"action": "ALLOW"
}
]
} }
''; ];
};
mainTld = "org";
otherTld = [
"fr"
"eu"
];
inherit (lib) inherit (lib)
cartesianProduct
literalExpression literalExpression
mapAttrs
mapAttrs'
mkEnableOption mkEnableOption
mkIf mkIf
mkOption mkOption
nameValuePair
substring
types types
; ;
in in
@ -75,10 +81,21 @@ in
''; '';
example = "true"; example = "true";
}; };
proxyWebsockets = mkOption {
type = types.bool;
default = false;
description = ''
Activer les websockets
'';
example = "true";
};
}; };
} }
); );
default = {};
default = { };
example = literalExpression '' example = literalExpression ''
{ {
"framadate" = { "framadate" = {
@ -95,82 +112,81 @@ in
}; };
config = { config = {
systemd.services = mapAttrs ( systemd.services = lib.mapAttrs (vhostName: vhostConfig: {
vhostName: vhostConfig: { wantedBy = [ "multi-user.target" ];
wantedBy = [ "multi-user.target" ]; }) cfg.virtualHosts;
}
) cfg.virtualHosts;
services = mkIf cfg.enable { services = mkIf cfg.enable {
anubis = { anubis = {
defaultOptions.group = "nginx"; defaultOptions.group = "nginx";
instances = mapAttrs ( instances = lib.mapAttrs (vhostName: vhostConfig: {
vhostName: vhostConfig: { enable = true;
enable = true; settings = {
settings = { BIND = "/run/anubis/anubis-${vhostName}.sock";
BIND = "/run/anubis/anubis-${vhostName}.sock"; BIND_NETWORK = "unix";
BIND_NETWORK = "unix"; TARGET = "unix:///run/nginx/nginx-${vhostName}.sock";
TARGET = "unix:///run/nginx/nginx-${vhostName}.sock"; COOKIE_DOMAIN = "crans.org";
COOKIE_DOMAIN = "crans.org"; REDIRECT_DOMAINS = "${vhostName}.crans.org";
REDIRECT_DOMAINS = "${vhostName}.crans.org"; SOCKET_MODE = "0660";
SOCKET_MODE = "0660"; POLICY_FNAME = if (vhostConfig.anubisConfig == "") then "${allowAll}" else vhostConfig.anubisConfig;
POLICY_FNAME = };
if (vhostConfig.anubisConfig == "") }) cfg.virtualHosts;
then allowAll
else vhostConfig.anubisConfig;
};
}
) cfg.virtualHosts;
}; };
nginx = nginx =
let let
domaines = [ # Configuration du serveur principal.
"crans.org" mainConfig = lib.mapAttrs' (
"crans.fr" vhostName: vhostConfig:
"crans.eu" lib.nameValuePair (vhostName + "-anubis") {
]; enableACME = !vhostConfig.httpOnly;
redirectConfig = mapAttrs ( forceSSL = !vhostConfig.httpOnly;
vhostName: vhostConfig: { rejectSSL = vhostConfig.httpOnly;
locations = mkIf ((substring 0 1 vhostConfig.target) != "/") { locations."/" = {
"/favicon.ico".root = "/var/www/logo/"; proxyPass = "http://unix:/run/anubis/anubis-${vhostName}.sock";
"/".proxyPass = "http://${vhostConfig.target}"; proxyWebsockets = vhostConfig.proxyWebsockets;
}; };
root = mkIf ((substring 0 1 vhostConfig.target) == "/") vhostConfig.target; serverName = "${vhostName}.crans.${mainTld}";
listen = [
{ addr = "unix:/run/nginx/nginx-${vhostName}.sock"; }
];
} }
) cfg.virtualHosts; ) cfg.virtualHosts;
aliasConfig = mapAttrs' (
vhostName: vhostConfig: nameValuePair (vhostName + "-alias") { # Redirections
enableACME = !vhostConfig.httpOnly; redirectConfig = lib.mapAttrs (vhostName: vhostConfig: {
forceSSL = !vhostConfig.httpOnly; # Redirection vers d'autres machines
rejectSSL = vhostConfig.httpOnly; locations = mkIf (!lib.strings.hasPrefix "/" vhostConfig.target) {
serverName = "${vhostName}.crans.fr"; "/favicon.ico".root = "/var/www/logo/";
serverAliases = let "/" = {
aliases = cartesianProduct { proxyPass = "http://${vhostConfig.target}";
name = vhostConfig.serverAliases; proxyWebsockets = vhostConfig.proxyWebsockets;
domaine = domaines; };
}; };
in [ # Redirection vers des fichiers locaux
"${vhostName}.crans.eu" root = mkIf (lib.strings.hasPrefix "/" vhostConfig.target) vhostConfig.target;
] ++ map (value: value.name + "." + value.domaine) aliases; listen = [
globalRedirect = "${vhostName}.crans.org"; { addr = "unix:/run/nginx/nginx-${vhostName}.sock"; }
} ];
) cfg.virtualHosts; }) cfg.virtualHosts;
anubisConfig = mapAttrs' (
vhostName: vhostConfig: nameValuePair (vhostName + "-anubis") { # Configuration des alias .fr et .eu
enableACME = !vhostConfig.httpOnly; aliasConfig = lib.fold (
forceSSL = !vhostConfig.httpOnly; tld: acc:
rejectSSL = vhostConfig.httpOnly; acc
locations."/".proxyPass = "http://unix:/run/anubis/anubis-${vhostName}.sock"; // lib.mapAttrs' (
serverName = "${vhostName}.crans.org"; vhostName: vhostConfig:
} lib.nameValuePair "${vhostName}-alias-${tld}" rec {
) cfg.virtualHosts; rejectSSL = vhostConfig.httpOnly;
in { forceSSL = !rejectSSL;
enableACME = !rejectSSL;
serverName = "${vhostName}.crans.${tld}";
serverAliases = map (name: "${name}.crans.${tld}") vhostConfig.serverAliases;
globalRedirect = "${vhostName}.crans.${mainTld}";
}
) cfg.virtualHosts
) { } otherTld;
in
{
enable = true; enable = true;
virtualHosts = redirectConfig // aliasConfig // anubisConfig; virtualHosts = redirectConfig // aliasConfig // mainConfig;
}; };
}; };
}; };