Pause a registry's WHOIS server after it refuses a query (closes #218)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The fetcher kept one interval for every registry, so a registry that refused a query was asked again within seconds. RIPE blocked the test host's address that way. The WHOIS client now pauses an RIR's or NIC.br's server for an hour after the server refuses a query. A refused or reset connection is a refusal, and so is an %ERROR:201: or %ERROR:202: line. Lookups at the other servers go on. A lookup whose RIR is paused leaves the AS stale, and counts as neither a success nor a failure. IANA is never paused, because a pause of IANA would stop every lookup. NIC.br's hour-long skip after its over-limit answer is now the same pause. Model: opus-5-5
This commit was merged in pull request #221.
This commit is contained in:
@@ -718,9 +718,10 @@ func TestLookupASNKeepsLACNICsAnswerWhenNICbrFails(t *testing.T) {
|
||||
|
||||
// TestLookupASNSkipsNICbrAfterItsLimit looks up an AS that NIC.br
|
||||
// registers three times with one client, while NIC.br answers that the
|
||||
// client is over its query limit. The first lookup asks NIC.br. The
|
||||
// second, a second before NICbrSkipTime has passed, does not. The third,
|
||||
// once NICbrSkipTime has passed, asks again. Each keeps LACNIC's fields.
|
||||
// client is over its query limit. The test sets the client's clock. The
|
||||
// first lookup asks NIC.br. The next comes a second before PauseTime has
|
||||
// passed, and does not ask NIC.br. The last comes at PauseTime and asks
|
||||
// NIC.br again. Each keeps LACNIC's fields.
|
||||
func TestLookupASNSkipsNICbrAfterItsLimit(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -740,8 +741,8 @@ func TestLookupASNSkipsNICbrAfterItsLimit(t *testing.T) {
|
||||
wantAsks int
|
||||
}{
|
||||
{after: 0, wantAsks: 1},
|
||||
{after: whois.NICbrSkipTime - time.Second, wantAsks: 1},
|
||||
{after: whois.NICbrSkipTime, wantAsks: 2},
|
||||
{after: whois.PauseTime - time.Second, wantAsks: 1},
|
||||
{after: whois.PauseTime, wantAsks: 2},
|
||||
} {
|
||||
now = start.Add(step.after)
|
||||
|
||||
@@ -785,6 +786,142 @@ func TestLookupASNReportsARefusal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// resetServer starts a TCP server on the loopback address and returns the
|
||||
// server's address. The server reads one query on each connection, then
|
||||
// resets the connection.
|
||||
func resetServer(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
var listenConfig net.ListenConfig
|
||||
|
||||
listener, err := listenConfig.Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() { _ = listener.Close() })
|
||||
|
||||
go func() {
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = bufio.NewReader(conn).ReadString('\n')
|
||||
|
||||
// Closing a connection whose linger time is zero resets it.
|
||||
if tcpConn, ok := conn.(*net.TCPConn); ok {
|
||||
_ = tcpConn.SetLinger(0)
|
||||
}
|
||||
|
||||
_ = conn.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
return listener.Addr().String()
|
||||
}
|
||||
|
||||
// newRIPEClient returns a client whose fake servers send answers. The
|
||||
// client adds one to *ripeAsks each time it connects to RIPE's server. When
|
||||
// reset is true, RIPE's server resets the connection instead of answering.
|
||||
func newRIPEClient(
|
||||
t *testing.T, answers map[string]string, reset bool, ripeAsks *int,
|
||||
) *whois.Client {
|
||||
t.Helper()
|
||||
|
||||
dial := fakeServers(t, answers)
|
||||
resetAddress := resetServer(t)
|
||||
|
||||
client := whois.NewClient(slog.New(slog.DiscardHandler))
|
||||
client.DialContext = func(
|
||||
ctx context.Context, network, address string,
|
||||
) (net.Conn, error) {
|
||||
if address == ripeServer {
|
||||
*ripeAsks++
|
||||
}
|
||||
|
||||
if address == ripeServer && reset {
|
||||
var dialer net.Dialer
|
||||
|
||||
return dialer.DialContext(ctx, network, resetAddress)
|
||||
}
|
||||
|
||||
return dial(ctx, network, address)
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// TestLookupASNPausesAServerThatRefuses looks up an AS at RIPE while RIPE
|
||||
// refuses the query, in each of the ways a server refuses. The test sets
|
||||
// the client's clock. A lookup a second before PauseTime has passed fails
|
||||
// with ErrPaused and asks RIPE nothing. A lookup of an AS at APNIC succeeds
|
||||
// during the pause. A lookup at PauseTime asks RIPE again.
|
||||
func TestLookupASNPausesAServerThatRefuses(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for name, test := range map[string]struct {
|
||||
// ripeAnswer is RIPE's answer. With none, RIPE's server refuses
|
||||
// the connection, or resets it when reset is true.
|
||||
ripeAnswer string
|
||||
reset bool
|
||||
}{
|
||||
"access denied": {ripeAnswer: ripeRefusal},
|
||||
"limit reached": {ripeAnswer: ripeLimitReached + "\n"},
|
||||
"refused connection": {},
|
||||
"reset connection": {reset: true},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
answers := map[string]string{
|
||||
ianaQuery: ianaReferral,
|
||||
apnicIANAQuery: "refer: whois.apnic.net\n",
|
||||
apnicQuery: apnicRecord,
|
||||
}
|
||||
if test.ripeAnswer != "" {
|
||||
answers[ripeQuery] = test.ripeAnswer
|
||||
}
|
||||
|
||||
ripeAsks := 0
|
||||
client := newRIPEClient(t, answers, test.reset, &ripeAsks)
|
||||
|
||||
start := time.Date(2026, 11, 2, 0, 0, 0, 0, time.UTC)
|
||||
now := start
|
||||
client.Now = func() time.Time { return now }
|
||||
|
||||
_, err := client.LookupASN(t.Context(), testASN)
|
||||
if err == nil || errors.Is(err, whois.ErrPaused) {
|
||||
t.Fatalf("LookupASN while RIPE refuses: error %v, want a "+
|
||||
"refusal", err)
|
||||
}
|
||||
|
||||
now = start.Add(whois.PauseTime - time.Second)
|
||||
|
||||
_, err = client.LookupASN(t.Context(), testASN)
|
||||
if !errors.Is(err, whois.ErrPaused) || ripeAsks != 1 {
|
||||
t.Errorf("LookupASN during the pause: error %v after %d "+
|
||||
"queries to RIPE, want ErrPaused after 1", err, ripeAsks)
|
||||
}
|
||||
|
||||
_, err = client.LookupASN(t.Context(), apnicASN)
|
||||
if err != nil {
|
||||
t.Errorf("LookupASN of an AS at APNIC during RIPE's "+
|
||||
"pause: %v", err)
|
||||
}
|
||||
|
||||
now = start.Add(whois.PauseTime)
|
||||
|
||||
_, _ = client.LookupASN(t.Context(), testASN)
|
||||
if ripeAsks != 2 {
|
||||
t.Errorf("RIPE was asked %d times after the pause, want 2",
|
||||
ripeAsks)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLookupASNFailsWhenTheRIRGivesNoAnswer looks up an AS IANA refers to
|
||||
// RIPE when RIPE's server refuses the connection, and when it answers only
|
||||
// with an error. Neither is a lookup that found the AS.
|
||||
|
||||
Reference in New Issue
Block a user