It works a bit better, but stil slow first time load
This commit is contained in:
parent
f59c52e9e1
commit
9c36a97f46
6 changed files with 100 additions and 61 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
caddy
|
20
Caddyfile
Normal file
20
Caddyfile
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
|
||||||
|
{
|
||||||
|
order anubis after encode
|
||||||
|
order redir after anubis
|
||||||
|
}
|
||||||
|
|
||||||
|
:18888 {
|
||||||
|
request_header +X-Real-IP {remote_host}
|
||||||
|
request_header +X-Forwarded-For {remote_host}
|
||||||
|
|
||||||
|
anubis
|
||||||
|
|
||||||
|
handle / {
|
||||||
|
try_files {path} /index.html
|
||||||
|
file_server {
|
||||||
|
root web/
|
||||||
|
browse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
115
caddy_anubis.go
115
caddy_anubis.go
|
@ -1,120 +1,119 @@
|
||||||
package caddyanubis
|
package caddyanubis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log/slog"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis"
|
"github.com/TecharoHQ/anubis"
|
||||||
libanubis "github.com/TecharoHQ/anubis/lib"
|
libanubis "github.com/TecharoHQ/anubis/lib"
|
||||||
|
"github.com/TecharoHQ/anubis/lib/policy"
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterModule(Middleware{})
|
caddy.RegisterModule(AnubisMiddleware{})
|
||||||
httpcaddyfile.RegisterHandlerDirective("anubis", parseCaddyfile)
|
httpcaddyfile.RegisterHandlerDirective("anubis", parseCaddyfile)
|
||||||
|
httpcaddyfile.RegisterDirectiveOrder("anubis", httpcaddyfile.After, "templates")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Middleware implements an HTTP handler that writes the
|
type AnubisMiddleware struct {
|
||||||
// visitor's IP address to a file or stream.
|
Target *string `json:"target,omitempty"`
|
||||||
type Middleware struct {
|
AnubisPolicy *policy.ParsedConfig
|
||||||
// The file or stream to write to. Can be "stdout"
|
AnubisServer *libanubis.Server
|
||||||
// or "stderr".
|
Next caddyhttp.Handler
|
||||||
Output string `json:"output,omitempty"`
|
|
||||||
|
|
||||||
w io.Writer
|
logger *zap.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaddyModule returns the Caddy module information.
|
// CaddyModule returns the Caddy module information.
|
||||||
func (Middleware) CaddyModule() caddy.ModuleInfo {
|
func (AnubisMiddleware) CaddyModule() caddy.ModuleInfo {
|
||||||
return caddy.ModuleInfo{
|
return caddy.ModuleInfo{
|
||||||
ID: "http.handlers.anubis",
|
ID: "http.handlers.anubis",
|
||||||
New: func() caddy.Module { return new(Middleware) },
|
New: func() caddy.Module { return new(AnubisMiddleware) },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provision implements caddy.Provisioner.
|
// Provision implements caddy.Provisioner.
|
||||||
func (m *Middleware) Provision(ctx caddy.Context) error {
|
func (m *AnubisMiddleware) Provision(ctx caddy.Context) error {
|
||||||
switch m.Output {
|
m.logger = ctx.Logger().Named("anubis")
|
||||||
case "stdout":
|
m.logger.Info("Anubis middleware provisioning")
|
||||||
m.w = os.Stdout
|
|
||||||
case "stderr":
|
|
||||||
m.w = os.Stderr
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("an output stream is required")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate implements caddy.Validator.
|
|
||||||
func (m *Middleware) Validate() error {
|
|
||||||
if m.w == nil {
|
|
||||||
return fmt.Errorf("no writer")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeHTTP implements caddyhttp.MiddlewareHandler.
|
|
||||||
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
|
||||||
m.w.Write([]byte(r.RemoteAddr))
|
|
||||||
|
|
||||||
policy, err := libanubis.LoadPoliciesOrDefault("", anubis.DefaultDifficulty)
|
policy, err := libanubis.LoadPoliciesOrDefault("", anubis.DefaultDifficulty)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
errChan := make(chan error)
|
m.AnubisPolicy = policy
|
||||||
anubisServer, err := libanubis.New(libanubis.Options{
|
|
||||||
|
m.AnubisServer, err = libanubis.New(libanubis.Options{
|
||||||
Next: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
Next: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
errChan <- next.ServeHTTP(w, r)
|
m.logger.Info("Anubis middleware calling next")
|
||||||
|
|
||||||
|
if m.Target != nil {
|
||||||
|
http.Redirect(w, r, *m.Target, http.StatusTemporaryRedirect)
|
||||||
|
} else {
|
||||||
|
m.Next.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
Policy: policy,
|
Policy: m.AnubisPolicy,
|
||||||
ServeRobotsTXT: true,
|
ServeRobotsTXT: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
anubisServer.ServeHTTP(w, r)
|
m.logger.Info("Anubis middleware provisioned")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
for err := range errChan {
|
// Validate implements caddy.Validator.
|
||||||
if err != nil {
|
func (m *AnubisMiddleware) Validate() error {
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// ServeHTTP implements caddyhttp.MiddlewareHandler.
|
||||||
|
func (m *AnubisMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
||||||
|
m.logger.Info("Anubis middleware processing request")
|
||||||
|
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||||
|
m.logger.Info("Anubis middleware sending request")
|
||||||
|
m.Next = next
|
||||||
|
m.AnubisServer.ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
|
||||||
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
func (m *AnubisMiddleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
d.Next() // consume directive name
|
d.Next() // consume directive name
|
||||||
|
|
||||||
// require an argument
|
// require an argument
|
||||||
if !d.NextArg() {
|
for nesting := d.Nesting(); d.NextBlock(nesting); {
|
||||||
return d.ArgErr()
|
switch d.Val() {
|
||||||
|
case "target":
|
||||||
|
if d.NextArg() {
|
||||||
|
val := d.Val()
|
||||||
|
m.Target = &val
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// store the argument
|
|
||||||
m.Output = d.Val()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseCaddyfile unmarshals tokens from h into a new Middleware.
|
// parseCaddyfile unmarshals tokens from h into a new Middleware.
|
||||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
|
||||||
var m Middleware
|
var m AnubisMiddleware
|
||||||
err := m.UnmarshalCaddyfile(h.Dispenser)
|
err := m.UnmarshalCaddyfile(h.Dispenser)
|
||||||
return m, err
|
return &m, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interface guards
|
// Interface guards
|
||||||
var (
|
var (
|
||||||
_ caddy.Provisioner = (*Middleware)(nil)
|
_ caddy.Provisioner = (*AnubisMiddleware)(nil)
|
||||||
_ caddy.Validator = (*Middleware)(nil)
|
_ caddy.Validator = (*AnubisMiddleware)(nil)
|
||||||
_ caddyhttp.MiddlewareHandler = (*Middleware)(nil)
|
_ caddyhttp.MiddlewareHandler = (*AnubisMiddleware)(nil)
|
||||||
_ caddyfile.Unmarshaler = (*Middleware)(nil)
|
_ caddyfile.Unmarshaler = (*AnubisMiddleware)(nil)
|
||||||
)
|
)
|
||||||
|
|
14
go.mod
14
go.mod
|
@ -2,9 +2,18 @@ module github.com/daegalus/caddy-anubis
|
||||||
|
|
||||||
go 1.24.1
|
go 1.24.1
|
||||||
|
|
||||||
replace github.com/TecharoHQ/anubis => ../anubis
|
require (
|
||||||
|
github.com/caddyserver/caddy/v2 v2.9.1
|
||||||
|
github.com/TecharoHQ/anubis v1.14.3-0.20250322224449-6156d3d7293a
|
||||||
|
)
|
||||||
|
|
||||||
require github.com/caddyserver/caddy/v2 v2.9.1
|
require (
|
||||||
|
github.com/a-h/templ v0.3.833 // indirect
|
||||||
|
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
|
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a // indirect
|
||||||
|
github.com/yl2chen/cidranger v1.0.2 // indirect
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
dario.cat/mergo v1.0.1 // indirect
|
dario.cat/mergo v1.0.1 // indirect
|
||||||
|
@ -14,7 +23,6 @@ require (
|
||||||
github.com/Masterminds/semver/v3 v3.3.0 // indirect
|
github.com/Masterminds/semver/v3 v3.3.0 // indirect
|
||||||
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
|
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
|
||||||
github.com/Microsoft/go-winio v0.6.0 // indirect
|
github.com/Microsoft/go-winio v0.6.0 // indirect
|
||||||
github.com/TecharoHQ/anubis v1.14.2
|
|
||||||
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
|
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
|
||||||
github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b // indirect
|
github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b // indirect
|
||||||
github.com/beorn7/perks v1.0.1 // indirect
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -25,6 +25,8 @@ github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSC
|
||||||
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
|
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
|
||||||
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
|
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
|
||||||
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
||||||
|
github.com/TecharoHQ/anubis v1.14.3-0.20250322224449-6156d3d7293a h1:juchXqktszLfbOpuM1Oht32OOj5T+03qjeEfYBs+HLM=
|
||||||
|
github.com/TecharoHQ/anubis v1.14.3-0.20250322224449-6156d3d7293a/go.mod h1:xWCWbEh+wZ5W/Vv0BFpWIQH0MWsiwZgmV5IlKwXxmMs=
|
||||||
github.com/a-h/templ v0.3.833 h1:L/KOk/0VvVTBegtE0fp2RJQiBm7/52Zxv5fqlEHiQUU=
|
github.com/a-h/templ v0.3.833 h1:L/KOk/0VvVTBegtE0fp2RJQiBm7/52Zxv5fqlEHiQUU=
|
||||||
github.com/a-h/templ v0.3.833/go.mod h1:cAu4AiZhtJfBjMY0HASlyzvkrtjnHWPeEsyGK2YYmfk=
|
github.com/a-h/templ v0.3.833/go.mod h1:cAu4AiZhtJfBjMY0HASlyzvkrtjnHWPeEsyGK2YYmfk=
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
|
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
|
||||||
|
|
9
web/index.html
Normal file
9
web/index.html
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Home</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Welcome to the Home Page</h1>
|
||||||
|
<p>Click <a href="about.html">here</a> to go to the About Page</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Add table
Add a link
Reference in a new issue