check / check (push) Failing after 2m4s
Each of the four keeps the cancel function of every ask it started, and a wait group of their goroutines. Its new method (GeoJS.StopAsking, DNSBL.StopAsking, AbuseIPDB.StopChecking, routewatch Client.StopAsking) refuses further asks, ends those under way, and returns once their goroutines have. An ask ended that way is no failure: no alert, no failure count, no wait before the next. Each provider in internal/sww adds the method as a stop hook, so the last write of the state files follows it. Model: opus-5-5
347 lines
9.5 KiB
Go
347 lines
9.5 KiB
Go
// Package routewatch asks a routewatch instance about client addresses, in the
|
|
// background, and keeps its answers. A routewatch instance knows, for an
|
|
// address, the netblock it is announced in and the AS that originates it. The
|
|
// answers are kept in memory, for at most 100,000 clients and for an hour each,
|
|
// and are written to routewatch.json and read from it by the state package.
|
|
package routewatch
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/netip"
|
|
"net/url"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/hashicorp/golang-lru/v2/simplelru"
|
|
)
|
|
|
|
const (
|
|
// answerKept is how long an answer is used after it came.
|
|
answerKept = time.Hour
|
|
// failureKept is how long routewatch is not asked again after a query
|
|
// fails, so that a dead routewatch is not asked on every request.
|
|
failureKept = time.Minute
|
|
// queryTimeout is how long one query may take. routewatch answers only
|
|
// after a reverse DNS lookup it allows 2 seconds, so this is longer.
|
|
queryTimeout = 5 * time.Second
|
|
// maxAnswers is how many answers are kept. Past it, the one that came
|
|
// longest ago is dropped.
|
|
maxAnswers = 100000
|
|
// maxQueries is how many queries may be under way at once. Past it, a
|
|
// client is not asked about until its next request, so that a swarm of
|
|
// new addresses cannot fill the memory.
|
|
maxQueries = 100
|
|
// maxAnswerBytes is the most of an answer that is read.
|
|
maxAnswerBytes = 64 << 10
|
|
)
|
|
|
|
// Missing is why Route gives no route. The request log's field
|
|
// routewatch_tags_missing gives these values.
|
|
type Missing string
|
|
|
|
const (
|
|
// NoAnswerYet is a client routewatch has not answered about yet.
|
|
NoAnswerYet Missing = "no_answer_yet"
|
|
// RequestFailed is no answer because a recent query failed.
|
|
RequestFailed Missing = "request_failed"
|
|
// NoRoute is routewatch knowing no live route for the address.
|
|
NoRoute Missing = "no_route"
|
|
)
|
|
|
|
var (
|
|
errStatus = errors.New("routewatch answered")
|
|
errNoResult = errors.New("the answer holds no result")
|
|
errNotNetblock = errors.New("the answer's netblock is not a netblock")
|
|
)
|
|
|
|
// Route is what routewatch knows of an address: the netblock it is announced
|
|
// in, and the AS that originates it.
|
|
type Route struct {
|
|
Netblock netip.Prefix
|
|
ASN uint32
|
|
}
|
|
|
|
// Answer is what routewatch answered about a client, as routewatch.json holds
|
|
// it: the netblock the client's address is announced in, none for an address
|
|
// without a live route, the AS that originates it, and when routewatch
|
|
// answered.
|
|
type Answer struct {
|
|
Client netip.Prefix `json:"client"`
|
|
Netblock netip.Prefix `json:"netblock"`
|
|
ASN uint32 `json:"asn"`
|
|
Answered time.Time `json:"answered"`
|
|
}
|
|
|
|
// Params are what New needs.
|
|
type Params struct {
|
|
// URL is where routewatch is (SWW_WAF_ROUTEWATCH_URL).
|
|
URL string
|
|
// Now tells the time, normally time.Now in UTC.
|
|
Now func() time.Time
|
|
// ProcessLog receives the query that fails, and why.
|
|
ProcessLog *slog.Logger
|
|
}
|
|
|
|
// Client asks a routewatch instance about clients, in the background, and keeps
|
|
// its answers. It is safe for concurrent use.
|
|
type Client struct {
|
|
params Params
|
|
httpClient *http.Client
|
|
|
|
mu sync.Mutex
|
|
// answers are by client. Each is added as it comes and never moved up,
|
|
// so that the one that came longest ago is the first dropped.
|
|
answers *simplelru.LRU[netip.Prefix, Answer]
|
|
// asking are the clients whose query is under way, each with the
|
|
// function that ends it. stopped is true once StopAsking has been
|
|
// called, and queryGoroutines waits for the goroutine of each query.
|
|
asking map[netip.Prefix]context.CancelFunc
|
|
stopped bool
|
|
queryGoroutines sync.WaitGroup
|
|
// retryAt is when routewatch may be asked again after a query failed.
|
|
retryAt time.Time
|
|
}
|
|
|
|
// New returns a Client with no answer yet.
|
|
func New(params Params) *Client {
|
|
answers, err := simplelru.NewLRU[netip.Prefix, Answer](maxAnswers, nil)
|
|
if err != nil {
|
|
panic(err) // NewLRU fails only for a size below one
|
|
}
|
|
|
|
return &Client{
|
|
params: params,
|
|
httpClient: &http.Client{},
|
|
answers: answers,
|
|
asking: map[netip.Prefix]context.CancelFunc{},
|
|
}
|
|
}
|
|
|
|
// Route returns what routewatch answered about client, an IPv4 address or an
|
|
// IPv6 group, or why it has no route. It never waits for a query. A client
|
|
// without an answer is asked about in the background, by addr, the address its
|
|
// request came from, unless its query is under way, a query failed less than
|
|
// failureKept ago, maxQueries are under way, or StopAsking has been called. ctx
|
|
// is the context of the client's request, and a query goes on after the request
|
|
// ends, until StopAsking.
|
|
func (c *Client) Route(
|
|
ctx context.Context, client netip.Prefix, addr netip.Addr,
|
|
) (Route, Missing) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
|
|
now := c.params.Now()
|
|
|
|
kept, found := c.answers.Peek(client)
|
|
if found && now.Sub(kept.Answered) < answerKept {
|
|
if !kept.Netblock.IsValid() {
|
|
return Route{}, NoRoute
|
|
}
|
|
|
|
return Route{Netblock: kept.Netblock, ASN: kept.ASN}, ""
|
|
}
|
|
|
|
if now.Before(c.retryAt) {
|
|
return Route{}, RequestFailed
|
|
}
|
|
|
|
_, asking := c.asking[client]
|
|
if !asking && !c.stopped && len(c.asking) < maxQueries {
|
|
queryCtx, end := context.WithCancel(context.WithoutCancel(ctx))
|
|
c.asking[client] = end
|
|
|
|
c.queryGoroutines.Go(func() {
|
|
defer end()
|
|
|
|
c.ask(queryCtx, client, addr)
|
|
})
|
|
}
|
|
|
|
return Route{}, NoAnswerYet
|
|
}
|
|
|
|
// StopAsking ends the queries under way, and returns once their goroutines
|
|
// have. A query it ends gives no answer and is no failure: it is not logged,
|
|
// and routewatch is not left alone after it. After StopAsking, Route asks
|
|
// routewatch nothing.
|
|
func (c *Client) StopAsking() {
|
|
c.mu.Lock()
|
|
|
|
c.stopped = true
|
|
for _, end := range c.asking {
|
|
end()
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
c.queryGoroutines.Wait()
|
|
}
|
|
|
|
// Snapshot returns every answer still in use, sorted by client, as
|
|
// routewatch.json lists them.
|
|
func (c *Client) Snapshot() []Answer {
|
|
c.mu.Lock()
|
|
|
|
now := c.params.Now()
|
|
answers := make([]Answer, 0, c.answers.Len())
|
|
|
|
for _, kept := range c.answers.Values() {
|
|
if now.Sub(kept.Answered) < answerKept {
|
|
answers = append(answers, kept)
|
|
}
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
slices.SortFunc(answers, func(a, b Answer) int {
|
|
return a.Client.Compare(b.Client)
|
|
})
|
|
|
|
return answers
|
|
}
|
|
|
|
// Load keeps answers, read from routewatch.json, in place of those it keeps,
|
|
// but for those that came answerKept ago or more and, past maxAnswers, those
|
|
// that came longest ago.
|
|
func (c *Client) Load(answers []Answer) {
|
|
answers = slices.Clone(answers)
|
|
slices.SortStableFunc(answers, func(a, b Answer) int {
|
|
return a.Answered.Compare(b.Answered)
|
|
})
|
|
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
|
|
c.answers.Purge()
|
|
|
|
now := c.params.Now()
|
|
|
|
for _, kept := range answers {
|
|
if now.Sub(kept.Answered) < answerKept {
|
|
c.answers.Add(kept.Client, kept)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ask asks routewatch about addr, keeps the answer as client's, and notes the
|
|
// query as no longer under way. A query that fails gives no answer, and
|
|
// routewatch is not asked again for failureKept. Only the first failure of
|
|
// those under way is logged. A query ended as ctx ends, by StopAsking, is no
|
|
// failure.
|
|
func (c *Client) ask(
|
|
ctx context.Context,
|
|
client netip.Prefix,
|
|
addr netip.Addr,
|
|
) {
|
|
route, err := c.query(ctx, addr)
|
|
now := c.params.Now()
|
|
isFailure := err != nil && ctx.Err() == nil
|
|
|
|
c.mu.Lock()
|
|
|
|
delete(c.asking, client)
|
|
|
|
firstFailure := isFailure && !now.Before(c.retryAt)
|
|
|
|
switch {
|
|
case err == nil:
|
|
c.answers.Add(client, Answer{
|
|
Client: client, Netblock: route.Netblock, ASN: route.ASN, Answered: now,
|
|
})
|
|
case isFailure:
|
|
c.retryAt = now.Add(failureKept)
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
if firstFailure {
|
|
c.params.ProcessLog.Warn(
|
|
"asking routewatch failed",
|
|
"error",
|
|
err.Error(),
|
|
)
|
|
}
|
|
}
|
|
|
|
// query asks routewatch's IP info API about addr. An answer other than 200, one
|
|
// that cannot be read, and none within queryTimeout, fail. A result with no
|
|
// netblock is an address without a live route, and gives a Route without one.
|
|
func (c *Client) query(ctx context.Context, addr netip.Addr) (Route, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, queryTimeout)
|
|
defer cancel()
|
|
|
|
endpoint, err := url.JoinPath(c.params.URL, "api/v1/ip", addr.String())
|
|
if err != nil {
|
|
return Route{}, fmt.Errorf("make the URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(
|
|
ctx,
|
|
http.MethodGet,
|
|
endpoint,
|
|
http.NoBody,
|
|
)
|
|
if err != nil {
|
|
return Route{}, fmt.Errorf("make the request: %w", err)
|
|
}
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
res, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
// Do's error names the URL, which holds the client's address, which
|
|
// is not to be logged: only what went wrong is kept.
|
|
return Route{}, fmt.Errorf("ask routewatch: %w", errors.Unwrap(err))
|
|
}
|
|
|
|
defer func() {
|
|
_ = res.Body.Close()
|
|
}()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
return Route{}, fmt.Errorf("%w %s", errStatus, res.Status)
|
|
}
|
|
|
|
return readAnswer(io.LimitReader(res.Body, maxAnswerBytes))
|
|
}
|
|
|
|
// readAnswer reads routewatch's answer about one address.
|
|
func readAnswer(body io.Reader) (Route, error) {
|
|
var decoded struct {
|
|
Data struct {
|
|
Results []struct {
|
|
Netblock string `json:"netblock"`
|
|
ASN uint32 `json:"asn"`
|
|
} `json:"results"`
|
|
} `json:"data"`
|
|
}
|
|
|
|
err := json.NewDecoder(body).Decode(&decoded)
|
|
if err != nil {
|
|
return Route{}, fmt.Errorf("read the answer: %w", err)
|
|
}
|
|
|
|
if len(decoded.Data.Results) == 0 {
|
|
return Route{}, errNoResult
|
|
}
|
|
|
|
result := decoded.Data.Results[0]
|
|
if result.Netblock == "" {
|
|
return Route{}, nil
|
|
}
|
|
|
|
netblock, err := netip.ParsePrefix(result.Netblock)
|
|
if err != nil {
|
|
return Route{}, fmt.Errorf("%w: %q", errNotNetblock, result.Netblock)
|
|
}
|
|
|
|
return Route{Netblock: netblock.Masked(), ASN: result.ASN}, nil
|
|
}
|