Files
masyunis/lib/config/geoip_databases.go
sol_bot Bot caf4d72880 Update to Anubis v1.28.2 (#1)
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:

![Challenge page (en)](https://git.unvr.site/attachments/36112ff0-5155-495c-8bd2-59606ee3bd64)
![Reject page (en)](https://git.unvr.site/attachments/2cad0256-1b0e-4f71-8d06-d1a447fd5263)

- 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>
2026-11-28 12:10:27 +00:00

219 lines
6.5 KiB
Go

package config
import (
"errors"
"fmt"
"net/url"
"os"
"time"
)
var (
ErrGeoIPNoDatabases = errors.New("config.GeoIPDatabases: must define at least one of asn or country")
ErrGeoIPNoPath = errors.New("config.GeoIPDatabase: must define path")
ErrGeoIPCountryFieldOnASN = errors.New("config.GeoIPDatabase: country_field can only be set on the country database")
ErrGeoIPInvalidCountryField = errors.New("config.GeoIPDatabase: invalid country_field")
ErrGeoIPEmptyEditionID = errors.New("config.GeoIPDatabase: edition_id must not be empty when set")
ErrGeoIPNoAccountID = errors.New("config.GeoIPAutoUpdate: must define account_id")
ErrGeoIPNoLicenseKeyEnv = errors.New("config.GeoIPAutoUpdate: must define license_key_env")
ErrGeoIPLicenseKeyEnvNotSet = errors.New("config.GeoIPAutoUpdate: environment variable named by license_key_env is not set")
ErrGeoIPInvalidInterval = errors.New("config.GeoIPAutoUpdate: invalid interval")
ErrGeoIPIntervalTooShort = errors.New("config.GeoIPAutoUpdate: interval is too short")
ErrGeoIPInvalidEndpoint = errors.New("config.GeoIPAutoUpdate: invalid endpoint")
)
const (
DefaultGeoIPASNEditionID = "GeoLite2-ASN"
DefaultGeoIPCountryEditionID = "GeoLite2-Country"
DefaultGeoIPUpdateInterval = 24 * time.Hour
MinimumGeoIPUpdateInterval = time.Hour
DefaultGeoIPCountryField = CountryFieldCountry
)
// CountryField selects which country record in a MaxMind country database is
// used for geoip rules.
type CountryField string
const (
// CountryFieldCountry is the country where MaxMind believes the IP address is located.
CountryFieldCountry CountryField = "country"
// CountryFieldRegisteredCountry is the country the IP address is registered to.
CountryFieldRegisteredCountry CountryField = "registered_country"
)
func (cf CountryField) Valid() error {
switch cf {
case CountryFieldCountry, CountryFieldRegisteredCountry:
return nil
default:
return fmt.Errorf("%w: %q, must be one of %q or %q", ErrGeoIPInvalidCountryField, string(cf), CountryFieldCountry, CountryFieldRegisteredCountry)
}
}
// GeoIPDatabases configures the MaxMind databases used for asns and geoip
// rules and ASN logging.
type GeoIPDatabases struct {
ASN *GeoIPDatabase `json:"asn,omitempty" yaml:"asn,omitempty"`
Country *GeoIPDatabase `json:"country,omitempty" yaml:"country,omitempty"`
AutoUpdate *GeoIPAutoUpdate `json:"auto_update,omitempty" yaml:"auto_update,omitempty"`
}
func (g *GeoIPDatabases) Valid() error {
var errs []error
if g.ASN == nil && g.Country == nil {
errs = append(errs, ErrGeoIPNoDatabases)
}
if g.ASN != nil {
if err := g.ASN.Valid(); err != nil {
errs = append(errs, fmt.Errorf("asn: %w", err))
}
if g.ASN.CountryField != nil {
errs = append(errs, fmt.Errorf("asn: %w", ErrGeoIPCountryFieldOnASN))
}
}
if g.Country != nil {
if err := g.Country.Valid(); err != nil {
errs = append(errs, fmt.Errorf("country: %w", err))
}
}
if g.AutoUpdate != nil {
if err := g.AutoUpdate.Valid(); err != nil {
errs = append(errs, err)
}
}
if len(errs) != 0 {
return fmt.Errorf("config.GeoIPDatabases: invalid geoip settings:\n%w", errors.Join(errs...))
}
return nil
}
// ASNEditionID returns the MaxMind edition ID for the ASN database.
func (g *GeoIPDatabases) ASNEditionID() string {
return g.ASN.editionID(DefaultGeoIPASNEditionID)
}
// CountryEditionID returns the MaxMind edition ID for the country database.
func (g *GeoIPDatabases) CountryEditionID() string {
return g.Country.editionID(DefaultGeoIPCountryEditionID)
}
// GeoIPDatabase is a single MaxMind database on disk.
type GeoIPDatabase struct {
Path string `json:"path" yaml:"path"`
EditionID *string `json:"edition_id,omitempty" yaml:"edition_id,omitempty"`
CountryField *CountryField `json:"country_field,omitempty" yaml:"country_field,omitempty"`
}
func (g *GeoIPDatabase) Valid() error {
var errs []error
if g.Path == "" {
errs = append(errs, ErrGeoIPNoPath)
}
if g.EditionID != nil && *g.EditionID == "" {
errs = append(errs, ErrGeoIPEmptyEditionID)
}
if g.CountryField != nil {
if err := g.CountryField.Valid(); err != nil {
errs = append(errs, err)
}
}
if len(errs) != 0 {
return errors.Join(errs...)
}
return nil
}
// GetCountryField returns the configured country field or the default.
func (g *GeoIPDatabase) GetCountryField() CountryField {
if g == nil || g.CountryField == nil {
return DefaultGeoIPCountryField
}
return *g.CountryField
}
func (g *GeoIPDatabase) editionID(def string) string {
if g == nil || g.EditionID == nil {
return def
}
return *g.EditionID
}
// GeoIPAutoUpdate configures automatic database updates from MaxMind.
type GeoIPAutoUpdate struct {
AccountID int `json:"account_id" yaml:"account_id"`
LicenseKeyEnv string `json:"license_key_env" yaml:"license_key_env"`
Interval *string `json:"interval,omitempty" yaml:"interval,omitempty"`
Endpoint *string `json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
}
func (g *GeoIPAutoUpdate) Valid() error {
var errs []error
if g.AccountID <= 0 {
errs = append(errs, ErrGeoIPNoAccountID)
}
switch {
case g.LicenseKeyEnv == "":
errs = append(errs, ErrGeoIPNoLicenseKeyEnv)
case os.Getenv(g.LicenseKeyEnv) == "":
errs = append(errs, fmt.Errorf("%w: %s", ErrGeoIPLicenseKeyEnvNotSet, g.LicenseKeyEnv))
}
if g.Interval != nil {
d, err := time.ParseDuration(*g.Interval)
switch {
case err != nil:
errs = append(errs, fmt.Errorf("%w: %w", ErrGeoIPInvalidInterval, err))
case d < MinimumGeoIPUpdateInterval:
errs = append(errs, fmt.Errorf("%w: %s is less than %s", ErrGeoIPIntervalTooShort, d, MinimumGeoIPUpdateInterval))
}
}
if g.Endpoint != nil {
u, err := url.Parse(*g.Endpoint)
switch {
case err != nil:
errs = append(errs, fmt.Errorf("%w: %w", ErrGeoIPInvalidEndpoint, err))
case u.Scheme != "http" && u.Scheme != "https", u.Host == "":
errs = append(errs, fmt.Errorf("%w: %q must be an absolute http or https URL", ErrGeoIPInvalidEndpoint, *g.Endpoint))
}
}
if len(errs) != 0 {
return fmt.Errorf("auto_update: %w", errors.Join(errs...))
}
return nil
}
// GetInterval returns the parsed update interval or the default. Call Valid first.
func (g *GeoIPAutoUpdate) GetInterval() time.Duration {
if g.Interval == nil {
return DefaultGeoIPUpdateInterval
}
// XXX(Xe): already validated in Valid()
d, _ := time.ParseDuration(*g.Interval)
return d
}
// LicenseKey reads the license key from the configured environment variable.
func (g *GeoIPAutoUpdate) LicenseKey() string {
return os.Getenv(g.LicenseKeyEnv)
}