check / check (push) Failing after 3m22s
The GeoJS lookup and its cache move from internal/lookup to internal/geojs, which imports no other package of this repository. waf hands it functions to count requests, report failures and count lookups that went without an answer; waf keeps the log line, the source_failure alert and the metrics. The caller now passes its wait to each lookup. waf passes SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS. A request to GeoJS has its own fixed limit of 10 seconds, and only passing it or an error from GeoJS is a failure. An answer that comes after the caller stopped waiting is kept. internal/lookup keeps the lookup database and returns geojs.Answer. Model: opus-5-5
131 lines
4.2 KiB
Go
131 lines
4.2 KiB
Go
package waf
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/netip"
|
|
"strings"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
|
)
|
|
|
|
// The headers in which an answer of allow gives rproxy the client's AS number
|
|
// and country, for the app, while SWW_WAF_ADD_LOOKUP_HEADERS is true. Go writes
|
|
// every header name in this form, as it sends it and as it receives it, so
|
|
// X-Client-ASN arrives as X-Client-Asn; header names are not case-sensitive.
|
|
const (
|
|
asnHeader = "X-Client-Asn"
|
|
countryHeader = "X-Client-Country"
|
|
)
|
|
|
|
// lookUp looks up the client's AS number and country, in the lookup
|
|
// database or through GeoJS, and notes them for the log line and as the
|
|
// request's asn= and country= tags, unless
|
|
// SWW_WAF_LOOKUP_SOURCE is off or the client is on a private, loopback or
|
|
// link-local address, which no lookup can place. The lookup database
|
|
// answers at once. With GeoJS, while a setting needs the answer, such as a
|
|
// country list or a biased threshold, a new client's request waits for it up
|
|
// to SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, unless refusedWhateverTheAnswer
|
|
// reports true. ctx is the request's own context.
|
|
func (rq *request) lookUp(ctx context.Context, now time.Time) {
|
|
if rq.h.config.LookupSource == "off" || !canBePlaced(rq.client) {
|
|
return
|
|
}
|
|
|
|
client := rq.h.clientGroup(rq.client)
|
|
|
|
switch {
|
|
case rq.h.config.LookupSource == "file":
|
|
rq.lookupAnswer = rq.h.lookupFile.LookUp(client)
|
|
case rq.refusedWhateverTheAnswer(now):
|
|
rq.lookupAnswer = rq.h.geojs.LookUpWithoutWaiting(ctx, client)
|
|
rq.skippedLookupWait = rq.lookupAnswer.Answered.IsZero()
|
|
case !rq.h.waitForGeoJS:
|
|
rq.lookupAnswer = rq.h.geojs.LookUpWithoutWaiting(ctx, client)
|
|
default:
|
|
rq.lookupAnswer = rq.h.geojs.LookUp(ctx, client,
|
|
rq.h.config.ASNAndCountryLookupTimeout)
|
|
}
|
|
|
|
rq.lookedUp = true
|
|
rq.line.ASN = rq.lookupAnswer.ASN
|
|
rq.line.ASName = rq.lookupAnswer.ASName
|
|
rq.line.Country = rq.lookupAnswer.Country
|
|
|
|
if tag := asnTag(rq.line.ASN); tag != "" {
|
|
rq.addTag(tag)
|
|
}
|
|
|
|
if tag := countryTag(rq.line.Country); tag != "" {
|
|
rq.addTag(tag)
|
|
}
|
|
}
|
|
|
|
// asnTag returns the tag of asn, an AS number as the lookup gives it, such as
|
|
// asn=64496 for AS64496, or "" for no AS number.
|
|
func asnTag(asn string) string {
|
|
if asn == "" {
|
|
return ""
|
|
}
|
|
|
|
return config.ASNTagPrefix + strings.TrimPrefix(asn, "AS")
|
|
}
|
|
|
|
// countryTag returns the tag of country, a code as the lookup gives it, such
|
|
// as country=DE for DE, or "" for no country.
|
|
func countryTag(country string) string {
|
|
if country == "" {
|
|
return ""
|
|
}
|
|
|
|
return config.CountryTagPrefix + country
|
|
}
|
|
|
|
// addLookup adds answer, an answer about a client from the lookup
|
|
// database or GeoJS, to the client's history, and to the notes of the bans
|
|
// on its netblock that have no AS number, AS name or country yet.
|
|
func (h *handler) addLookup(answer geojs.Answer) {
|
|
h.limiter.AddLookup(answer.Client, answer.Answered,
|
|
answer.ASN, answer.ASName, answer.Country)
|
|
h.ledger.AddLookup(h.netblock(answer.Client.Addr()),
|
|
answer.ASN, answer.ASName, answer.Country)
|
|
}
|
|
|
|
// geojsFailed logs a failure of GeoJS, raises a source_failure alert for it
|
|
// and counts it. askingAgainIn is how long GeoJS is left alone after it.
|
|
func (h *handler) geojsFailed(err error, askingAgainIn time.Duration) {
|
|
h.processLog.Warn("asking GeoJS failed",
|
|
"error", err.Error(), "asking_again_in", askingAgainIn.String())
|
|
h.alerts.Raise(alerts.Alert{
|
|
Event: alerts.EventSourceFailure,
|
|
Reason: "asking GeoJS failed",
|
|
Detail: map[string]any{
|
|
"source": "geojs", "error": err.Error(),
|
|
"asking_again_in": askingAgainIn.String(),
|
|
},
|
|
})
|
|
h.metrics.GeoJSFailures.Inc()
|
|
}
|
|
|
|
// setLookupHeaders sets the headers that give the client's AS number and
|
|
// country, leaving out one that is unknown.
|
|
func setLookupHeaders(header http.Header, asn, country string) {
|
|
if asn != "" {
|
|
header.Set(asnHeader, asn)
|
|
}
|
|
|
|
if country != "" {
|
|
header.Set(countryHeader, country)
|
|
}
|
|
}
|
|
|
|
// canBePlaced reports whether a lookup can place addr: private, loopback
|
|
// and link-local addresses have no AS number or country.
|
|
func canBePlaced(addr netip.Addr) bool {
|
|
return !addr.IsPrivate() && !addr.IsLoopback() &&
|
|
!addr.IsLinkLocalUnicast()
|
|
}
|