This upgrades the Masyunis fork from Anubis v1.25.0 to the latest stable release, **v1.28.2** (“Wuk Lamat Echo 2”), while keeping the fork’s custom styling and removals. ## Approach The Gitea repo is a tree snapshot of v1.25.0, not a git-level fork of [TecharoHQ/anubis](https://github.com/TecharoHQ/anubis). The histories share no commits, so a merge or rebase onto the upstream tag is not usable. The first commit on this branch replaces the tree with a snapshot of the **v1.28.2** tag (`git rm -r` plus `git checkout v1.28.2 -- .`; the import tree is identical to the tag). A short patch series on top restores the Masyunis customizations. ## Customizations kept - Masyunya mascots (`web/static/img/{happy,pensive,reject}.webp`), hashes match current `master`. - `web/static/img/ATTRIBUTIONS.txt` stays deleted. - Challenge footer: drop the `made_with` (Canada / maple-leaf) line and the CELPHASE mascot credit; keep “Protected by Anubis from Techaro”. The generated `web/index_templ.go` matches `go generate ./web`. - Short README (the old `anubisc` typo is fixed). - Version stamped `v1.28.2/masyunis` via Makefile ldflags. `anubis.go` `init()` does not overwrite a link-time version; `go run` / `go test` still report `devel` on purpose. - Upstream `.github`, `.devcontainer`, `.husky`, and `.vscode` stay deleted. - `"uk"` is removed from `lib/localization/locales/manifest.json` **on purpose**, because of Alpine locale problems. `uk.json` and its test entry stay; the served manifest lists 27 languages. - `package.json` `prepare` no longer runs `husky` (the hooks directory is gone). The rest of the script is unchanged. `husky` remains in devDependencies, as upstream left it. ## Deploy notes - **Cookie rename (v1.27):** with the default `DYNAMIC_COOKIE_SUFFIX=true`, cookie names become e.g. `techaro.lol-anubis-auth-<hash>`. Existing clients get a **one-time re-challenge**. That is expected, not a loop. - **Docker / ko / yeet builds** skip the Makefile ldflags, so the displayed version is `<git-version>/masyunis`. It is `v1.28.2/masyunis` only when built from a `v1.28.2` tag (or via `make build` / `make prebaked-build`). - Go 1.27.3 is required (upstream bump in v1.28.2). Thoth (`THOTH_URL` / `THOTH_TOKEN`) was removed upstream; those settings now warn and do nothing. New WASM challenge methods stay disabled unless opted in. ## Verification - `make build` succeeds. `./var/anubis --version` prints `Anubis v1.28.2/masyunis`. - `SKIP_INTEGRATION=1 go test ./...` passes (unit tests; integration package is skipped). - Local challenge/reject pages (en and ru) show Masyunya, version `v1.28.2/masyunis`, and no Canada / CELPHASE / mascot-credit text. Screenshots:   - Upstream Playwright tests (`make test` without `SKIP_INTEGRATION`) fail on this box because the browser download fails. A clean v1.28.2 tree fails the same three Playwright tests here, so this is not a fork regression. - `npm audit` reports **4** findings in upstream’s lockfile (2 moderate, 2 high: `brace-expansion`, `fast-uri`, `lodash.trim`, `source-map-js`). Left alone. Please review and merge when ready. I will not merge this myself. Reviewed-on: #1 Co-authored-by: Lead <3+lead_bot@noreply.git.unvr.site>
254 lines
6.7 KiB
Go
254 lines
6.7 KiB
Go
package policy
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/netip"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/TecharoHQ/anubis"
|
|
"github.com/TecharoHQ/anubis/internal"
|
|
"github.com/TecharoHQ/anubis/internal/iplist"
|
|
"github.com/TecharoHQ/anubis/lib/config"
|
|
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
|
"github.com/gaissmai/bart"
|
|
)
|
|
|
|
const (
|
|
remoteAddressesURLRefreshInterval = 24 * time.Hour
|
|
remoteAddressesURLRetryInterval = 15 * time.Minute
|
|
remoteAddressesURLMaxRetries = 5
|
|
remoteAddressesURLHTTPTimeout = 30 * time.Second
|
|
remoteAddressesURLMaxBodyBytes = 8 << 20
|
|
)
|
|
|
|
// RemoteAddressesURLChecker matches client IPs against a prefix list fetched
|
|
// from a remote JSON document. The list starts empty and is replaced on each
|
|
// successful refresh. Failed fetches never clobber a previously loaded list.
|
|
type RemoteAddressesURLChecker struct {
|
|
url string
|
|
logger *slog.Logger
|
|
client *http.Client
|
|
|
|
mu sync.RWMutex
|
|
prefixTable *bart.Lite
|
|
hash string
|
|
creationTime string
|
|
rebuildCount int
|
|
|
|
cycleStart time.Time
|
|
consecutiveFail int
|
|
|
|
refreshInterval time.Duration
|
|
retryInterval time.Duration
|
|
maxRetries int
|
|
|
|
stopped chan struct{}
|
|
}
|
|
|
|
func NewRemoteAddressesURLChecker(ctx context.Context, rawURL string, logger *slog.Logger) (checker.Impl, error) {
|
|
if logger == nil {
|
|
logger = slog.Default()
|
|
}
|
|
|
|
if err := parseRemoteAddressesURL(rawURL); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c := &RemoteAddressesURLChecker{
|
|
url: rawURL,
|
|
logger: logger,
|
|
client: &http.Client{Timeout: remoteAddressesURLHTTPTimeout, CheckRedirect: remoteAddressesRedirect},
|
|
prefixTable: new(bart.Lite),
|
|
hash: internal.FastHash(""),
|
|
refreshInterval: remoteAddressesURLRefreshInterval,
|
|
retryInterval: remoteAddressesURLRetryInterval,
|
|
maxRetries: remoteAddressesURLMaxRetries,
|
|
stopped: make(chan struct{}),
|
|
}
|
|
|
|
go c.run(ctx)
|
|
return c, nil
|
|
}
|
|
|
|
func parseRemoteAddressesURL(rawURL string) error {
|
|
rawURL = strings.TrimSpace(rawURL)
|
|
u, err := url.Parse(rawURL)
|
|
if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") {
|
|
return fmt.Errorf("%w: %q", config.ErrInvalidRemoteAddressesURL, rawURL)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *RemoteAddressesURLChecker) Check(r *http.Request) (bool, error) {
|
|
host := r.Header.Get("X-Real-IP")
|
|
if host == "" {
|
|
return false, fmt.Errorf("%w: header X-Real-IP is not set", ErrMisconfiguration)
|
|
}
|
|
|
|
addr, err := netip.ParseAddr(host)
|
|
if err != nil {
|
|
return false, fmt.Errorf("%w: %s is not an IP address: %w", ErrMisconfiguration, host, err)
|
|
}
|
|
|
|
if addr.Is6() && addr.Is4In6() {
|
|
addr = addr.Unmap()
|
|
}
|
|
|
|
c.mu.RLock()
|
|
table := c.prefixTable
|
|
c.mu.RUnlock()
|
|
|
|
if table == nil {
|
|
return false, nil
|
|
}
|
|
|
|
return table.Contains(addr), nil
|
|
}
|
|
|
|
func (c *RemoteAddressesURLChecker) Hash() string {
|
|
c.mu.RLock()
|
|
defer c.mu.RUnlock()
|
|
return c.hash
|
|
}
|
|
|
|
func (c *RemoteAddressesURLChecker) run(ctx context.Context) {
|
|
defer close(c.stopped)
|
|
|
|
for {
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
|
|
delay := c.refreshOnce(ctx)
|
|
timer := time.NewTimer(delay)
|
|
select {
|
|
case <-timer.C:
|
|
case <-ctx.Done():
|
|
if !timer.Stop() {
|
|
select {
|
|
case <-timer.C:
|
|
default:
|
|
}
|
|
}
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *RemoteAddressesURLChecker) refreshOnce(ctx context.Context) time.Duration {
|
|
if c.cycleStart.IsZero() {
|
|
c.cycleStart = time.Now()
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.url, nil)
|
|
if err != nil {
|
|
return c.handleRetryable(ctx, "can't build remote_addresses_url request", err)
|
|
}
|
|
req.Header.Set("User-Agent", "TecharoHQ-Anubis/"+anubis.Version)
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
resp, err := c.client.Do(req)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return 0
|
|
}
|
|
return c.handleRetryable(ctx, "can't fetch remote_addresses_url", err)
|
|
}
|
|
defer resp.Body.Close() //nolint:errcheck
|
|
|
|
switch {
|
|
case resp.StatusCode >= 500:
|
|
return c.handleRetryable(ctx, "remote_addresses_url fetch returned server error", fmt.Errorf("http status %d", resp.StatusCode))
|
|
case resp.StatusCode >= 400:
|
|
c.logger.WarnContext(ctx, "remote_addresses_url fetch returned client error", "url", c.url, "status", resp.StatusCode)
|
|
return c.delayUntilNextCycle()
|
|
case resp.StatusCode < 200 || resp.StatusCode > 299:
|
|
return c.handleRetryable(ctx, "remote_addresses_url fetch returned unexpected status", fmt.Errorf("http status %d", resp.StatusCode))
|
|
}
|
|
|
|
pl, err := iplist.Parse(io.LimitReader(resp.Body, remoteAddressesURLMaxBodyBytes))
|
|
if err != nil {
|
|
c.logger.WarnContext(ctx, "failed to parse remote_addresses_url", "url", c.url, "err", err)
|
|
return c.delayUntilNextCycle()
|
|
}
|
|
|
|
if err := c.applyList(pl); err != nil {
|
|
c.logger.WarnContext(ctx, "failed to apply remote_addresses_url", "url", c.url, "err", err)
|
|
return c.delayUntilNextCycle()
|
|
}
|
|
|
|
c.cycleStart = time.Now()
|
|
c.consecutiveFail = 0
|
|
return c.refreshInterval
|
|
}
|
|
|
|
func (c *RemoteAddressesURLChecker) applyList(pl *iplist.PrefixList) error {
|
|
if pl.CreationTime != "" && pl.CreationTime == c.creationTime {
|
|
c.logger.Debug("remote_addresses_url unchanged, skip rebuild", "url", c.url, "creationTime", pl.CreationTime)
|
|
return nil
|
|
}
|
|
|
|
cidrs := pl.CIDRs()
|
|
if len(cidrs) == 0 {
|
|
return config.ErrInvalidRemoteAddressesURL
|
|
}
|
|
table := new(bart.Lite)
|
|
for _, cidr := range cidrs {
|
|
prefix, err := netip.ParsePrefix(cidr)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid CIDR %q: %w", cidr, err)
|
|
}
|
|
table.Insert(prefix)
|
|
}
|
|
|
|
hash := internal.FastHash(strings.Join(cidrs, ","))
|
|
|
|
c.mu.Lock()
|
|
c.prefixTable = table
|
|
c.hash = hash
|
|
c.creationTime = pl.CreationTime
|
|
c.rebuildCount++
|
|
c.mu.Unlock()
|
|
|
|
c.logger.Info("updated remote_addresses_url", "url", c.url, "prefixes", len(cidrs), "creationTime", pl.CreationTime)
|
|
return nil
|
|
}
|
|
|
|
func (c *RemoteAddressesURLChecker) handleRetryable(ctx context.Context, msg string, err error) time.Duration {
|
|
c.consecutiveFail++
|
|
if c.consecutiveFail < c.maxRetries {
|
|
c.logger.WarnContext(ctx, msg, "url", c.url, "attempt", c.consecutiveFail, "err", err)
|
|
return c.retryInterval
|
|
}
|
|
|
|
c.logger.WarnContext(ctx, "remote_addresses_url fetch failed, giving up until next refresh window", "url", c.url, "attempts", c.consecutiveFail, "err", err)
|
|
return c.delayUntilNextCycle()
|
|
}
|
|
|
|
func (c *RemoteAddressesURLChecker) delayUntilNextCycle() time.Duration {
|
|
c.consecutiveFail = 0
|
|
remaining := time.Until(c.cycleStart.Add(c.refreshInterval))
|
|
if remaining < 0 {
|
|
remaining = 0
|
|
}
|
|
c.cycleStart = time.Time{}
|
|
return remaining
|
|
}
|
|
|
|
func remoteAddressesRedirect(req *http.Request, via []*http.Request) error {
|
|
if len(via) == 0 || len(via) >= 10 {
|
|
return config.ErrInvalidRemoteAddressesURL
|
|
}
|
|
if req.URL.Scheme != via[0].URL.Scheme || req.URL.Host != via[0].URL.Host {
|
|
return config.ErrInvalidRemoteAddressesURL
|
|
}
|
|
return nil
|
|
}
|