Anubis module for Caddy

Add difficulty and policy_fname

Changed files
+48 -9
example
+17 -6
caddy_anubis.go
···
import (
"fmt"
-
"log/slog"
"net"
"net/http"
+
"strconv"
"github.com/TecharoHQ/anubis"
libanubis "github.com/TecharoHQ/anubis/lib"
···
func (m *AnubisMiddleware) Provision(ctx caddy.Context) error {
m.log = ctx.Logger(m)
-
slog.SetLogLoggerLevel(slog.LevelDebug) // TODO: customizable log level
+
// slog.SetLogLoggerLevel(slog.LevelDebug) // TODO: customizable log level
m.log.Debug("loading anubis policies", zap.String("policy_file", m.PolicyFname), zap.Int("default_difficulty", m.DefaultDifficulty))
policy, err := libanubis.LoadPoliciesOrDefault(ctx, m.PolicyFname, m.DefaultDifficulty)
···
m.Options.CookieSecure = true // TODO: temporary
for nesting := d.Nesting(); d.NextBlock(nesting); {
+
var err error
+
switch d.Val() {
-
case "target":
-
case "opengraph":
-
m.Options.OpenGraph.Enabled = true
-
+
case "difficulty":
+
if !d.Next() {
+
return d.ArgErr()
+
}
+
m.DefaultDifficulty, err = strconv.Atoi(d.Val())
+
if err != nil {
+
return d.WrapErr(err)
+
}
+
case "policy_fname":
+
if !d.Next() {
+
return d.ArgErr()
+
}
+
m.PolicyFname = d.Val()
}
} // anubis options
+17 -3
example/Caddyfile
···
localhost {
+
log {
+
level debug
+
}
+
@anubis {
-
path / # don't let AI scrapers list files!
-
path /.within.website/* # required
+
# This matcher allows you to select specific paths for Anubis to handle.
+
# If you want to handle all paths, remove this block and use `anubis {...}` instead!
+
path / # don't let AI scrapers browse the file index
+
path /.within.website/* # required for anubis to work
+
+
not path /api/* # exclude api routes from anubis
}
anubis @anubis {
+
# This setting gets overridden a lot by the default bot policy.
+
difficulty 4
+
+
# Custom bot policy
+
policy_fname example/botPolicy.yaml
+
# FIXME: required for OpenGraph passthrough!
-
## TODO: access backend directly somehow?
+
## TODO: access upstream directly somehow?
## otherwise anubis may trigger itself
## additionally, https://github.com/TecharoHQ/anubis/issues/329
# target http://localhost:8080
+14
example/botPolicy.yaml
···
+
{
+
"bots": [
+
{
+
"name": "Denied route",
+
"path_regex": "/deny",
+
"action": "DENY"
+
},
+
{
+
"name": "Generic browser",
+
"user_agent_regex": "Mozilla",
+
"action": "CHALLENGE"
+
}
+
]
+
}