check / check (push) Waiting to run
go.uber.org/fx builds the parts of waf and rproxy in internal/sww, one provider per part. Each background loop is a lifecycle hook added where its part is built. waf.Params takes every part built, and waf.New builds none. GeoJS calls back into waf, so waf exports AddLookup and GeoJSFailed, and clientGroup and banNetblock take the settings. newServer, loadStateFiles, the inBackground list and serveRProxy are gone. serve is the one hook that serves both daemons, and reports a failure to run on a channel of its own; fx's signal handling is not used. Once fx has stopped, run takes each daemon's last step: waf writes the state files, and rproxy waits for its reports. Model: opus-5-5
68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package waf
|
|
|
|
// The heuristic abuseipdb. It reads SWW_WAF_ABUSEIPDB_KEY,
|
|
// SWW_WAF_ABUSEIPDB_MIN_SCORE and SWW_WAF_DNSBL_ABUSEIPDB_ACTION.
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
|
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
)
|
|
|
|
// abuseIPDB adds the tag abuseipdb and notes AbuseIPDB, as noteHit does, with
|
|
// the score, when its score of the client is a hit, and refuses the request
|
|
// when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, as dnsbl does for a zone. While
|
|
// SWW_WAF_ABUSEIPDB_KEY is unset it does nothing. A client without a score is
|
|
// checked in the background, by the request's address, if its history counts
|
|
// an offence, and the request does not wait for the answer. The score is then
|
|
// used for each address of the client. Its figures are the score and
|
|
// SWW_WAF_ABUSEIPDB_MIN_SCORE.
|
|
type abuseIPDB struct{}
|
|
|
|
func (abuseIPDB) name() string { return "abuseipdb" }
|
|
|
|
func (abuseIPDB) check(
|
|
ctx context.Context,
|
|
rq *request,
|
|
_ time.Time,
|
|
) *verdict {
|
|
if rq.h.config.AbuseIPDBKey == "" {
|
|
return nil
|
|
}
|
|
|
|
client := clientGroup(rq.h.config, rq.client)
|
|
held, _ := rq.h.limiter.Client(client)
|
|
offender := held.History.Offences != ratelimit.Offences{}
|
|
|
|
score, hit := rq.h.abuseIPDB.Hit(ctx, client, rq.client, offender)
|
|
if !hit {
|
|
return nil
|
|
}
|
|
|
|
rq.abuseIPDBHit = true
|
|
rq.addTag(config.AbuseIPDBTag)
|
|
rq.noteHit(
|
|
bans.ReputationHit{
|
|
Source: reputation.AbuseIPDBSource,
|
|
Score: &score,
|
|
},
|
|
"scored by AbuseIPDB at or over SWW_WAF_ABUSEIPDB_MIN_SCORE",
|
|
)
|
|
|
|
if rq.h.config.DNSBLAbuseIPDBAction != deny {
|
|
return nil
|
|
}
|
|
|
|
return &verdict{
|
|
action: requestlog.ActionClientDenied,
|
|
figures: map[string]any{
|
|
figureScore: score, "min_score": rq.h.config.AbuseIPDBMinScore,
|
|
},
|
|
}
|
|
}
|