d52c3ee76e
Expand asn address groups to concrete prefixes centrally (one iplocate key, consistent fleet-wide) and refresh them on a per-group TTL (default 24h). A background Refresher scans for due groups, unions each group's ASNs to a deduped prefix set, and writes them to a new resolved/resolved_at column (migration 0002). Fail-safe: a lookup error or empty expansion keeps the last-good set, never emptying it. The compiler folds resolved prefixes into the rendered set members; membership churn bumps the generation but never rewrites rules. The iplocate client is endpoint-configurable and response-tolerant, documented as needing endpoint/key confirmation. Unit tests cover TTL parsing, due-checks, and union/dedup/error propagation with a fake expander.
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
package asnexpand
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// IPLocate expands ASNs via the iplocate.io IP-intelligence API.
|
|
//
|
|
// NOTE: iplocate's documented ASN data type is returned as part of an IP lookup
|
|
// (it carries the single `route` for that IP), so the exact ASN->prefixes
|
|
// endpoint depends on the account/plan. This client is deliberately tolerant of
|
|
// response shape and endpoint-configurable so it can be pointed at the correct
|
|
// endpoint once the API key and plan are confirmed. It expects a JSON body
|
|
// containing a list of prefixes under any of: "prefixes", "routes", "cidrs".
|
|
type IPLocate struct {
|
|
// BaseURL is the API root, default https://iplocate.io/api. The ASN path is
|
|
// BaseURL + "/asn/" + asn.
|
|
BaseURL string
|
|
// APIKey is sent as the ?apikey= query parameter (iplocate's scheme).
|
|
APIKey string
|
|
HTTP *http.Client
|
|
}
|
|
|
|
// NewIPLocate builds a client with sane defaults.
|
|
func NewIPLocate(apiKey string) *IPLocate {
|
|
return &IPLocate{
|
|
BaseURL: "https://iplocate.io/api",
|
|
APIKey: apiKey,
|
|
HTTP: &http.Client{Timeout: 15 * time.Second},
|
|
}
|
|
}
|
|
|
|
// asnResponse tolerates the several field names an ASN-prefix payload might use.
|
|
type asnResponse struct {
|
|
Prefixes []string `json:"prefixes"`
|
|
Routes []string `json:"routes"`
|
|
CIDRs []string `json:"cidrs"`
|
|
}
|
|
|
|
func (r asnResponse) list() []string {
|
|
switch {
|
|
case len(r.Prefixes) > 0:
|
|
return r.Prefixes
|
|
case len(r.Routes) > 0:
|
|
return r.Routes
|
|
default:
|
|
return r.CIDRs
|
|
}
|
|
}
|
|
|
|
// Prefixes implements Expander.
|
|
func (c *IPLocate) Prefixes(ctx context.Context, asn string) ([]string, error) {
|
|
if c.APIKey == "" {
|
|
return nil, fmt.Errorf("iplocate: no API key configured")
|
|
}
|
|
asn = strings.TrimPrefix(strings.ToUpper(strings.TrimSpace(asn)), "AS")
|
|
|
|
u, err := url.Parse(fmt.Sprintf("%s/asn/AS%s", strings.TrimRight(c.BaseURL, "/"), asn))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
q := u.Query()
|
|
q.Set("apikey", c.APIKey)
|
|
u.RawQuery = q.Encode()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
resp, err := c.HTTP.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("iplocate: AS%s: status %d: %s", asn, resp.StatusCode, strings.TrimSpace(string(body)))
|
|
}
|
|
|
|
var parsed asnResponse
|
|
if err := json.Unmarshal(body, &parsed); err != nil {
|
|
return nil, fmt.Errorf("iplocate: AS%s: decoding response: %w", asn, err)
|
|
}
|
|
return parsed.list(), nil
|
|
}
|