script/fmt now runs golines -m 77 after gofmt, repeating golines until it changes nothing, since one pass can leave a line the next splits. script/fmt-check fails when either would change a file. script/bootstrap installs golines v0.13.0 with go install, pinned to its commit, and checks the installed version with go version -m. The tree is reformatted. The longer lines this makes are fixed by hand: nolint directives moved onto their own line above the statement, one string split, and 20 functions over funlen's 80 lines shortened or split, RunWAF and limitPercentages among them. Model: opus-5-5
This commit is contained in:
1 parent
63e58e326b
commit
7f8a59de04
118 files changed
+9447
-2670
No files matched your search
@@ -43,7 +43,13 @@ const (
|
||||
|
||||
// Scopes returns every scope.
|
||||
func Scopes() []string {
|
||||
return []string{ScopeClient, ScopeNet, ScopeASN, ScopeTotal, ScopeWatchedNet}
|
||||
return []string{
|
||||
ScopeClient,
|
||||
ScopeNet,
|
||||
ScopeASN,
|
||||
ScopeTotal,
|
||||
ScopeWatchedNet,
|
||||
}
|
||||
}
|
||||
|
||||
// The windows a counter counts in, as the alerts name them.
|
||||
@@ -191,8 +197,12 @@ func (c *Counters) Snapshot() []Counter {
|
||||
c.mu.Unlock()
|
||||
|
||||
slices.SortFunc(counters, func(a, b Counter) int {
|
||||
return cmp.Or(cmp.Compare(a.Scope, b.Scope), a.Netblock.Compare(b.Netblock),
|
||||
cmp.Compare(a.ASN, b.ASN), cmp.Compare(a.Name, b.Name))
|
||||
return cmp.Or(
|
||||
cmp.Compare(a.Scope, b.Scope),
|
||||
a.Netblock.Compare(b.Netblock),
|
||||
cmp.Compare(a.ASN, b.ASN),
|
||||
cmp.Compare(a.Name, b.Name),
|
||||
)
|
||||
})
|
||||
|
||||
return counters
|
||||
@@ -257,7 +267,11 @@ func (c *Counters) scopesOf(r Request) []scope {
|
||||
for _, named := range p.NamedNetblocks {
|
||||
if named.Netblock.Contains(client) {
|
||||
all = append(all, scope{
|
||||
key{scope: ScopeWatchedNet, netblock: named.Netblock, name: named.Name}, p.Watch,
|
||||
key{
|
||||
scope: ScopeWatchedNet,
|
||||
netblock: named.Netblock,
|
||||
name: named.Name,
|
||||
}, p.Watch,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -316,7 +330,12 @@ func (c *Counter) add(
|
||||
|
||||
counted := count.buckets.Add(now, count.length, n)
|
||||
if !passed && counted > float64(threshold) {
|
||||
first = overThreshold{count.kind, count.window, counted, threshold}
|
||||
first = overThreshold{
|
||||
count.kind,
|
||||
count.window,
|
||||
counted,
|
||||
threshold,
|
||||
}
|
||||
passed = true
|
||||
}
|
||||
}
|
||||
@@ -359,12 +378,22 @@ func (c *Counter) lastStart() time.Time {
|
||||
|
||||
// key returns what tells c from the other counters.
|
||||
func (c *Counter) key() key {
|
||||
return key{scope: c.Scope, netblock: c.Netblock, asn: c.ASN, name: c.Name}
|
||||
return key{
|
||||
scope: c.Scope,
|
||||
netblock: c.Netblock,
|
||||
asn: c.ASN,
|
||||
name: c.Name,
|
||||
}
|
||||
}
|
||||
|
||||
// counter returns a counter for k, with nothing counted yet.
|
||||
func (k key) counter() *Counter {
|
||||
return &Counter{Scope: k.scope, Netblock: k.netblock, ASN: k.asn, Name: k.name}
|
||||
return &Counter{
|
||||
Scope: k.scope,
|
||||
Netblock: k.netblock,
|
||||
ASN: k.asn,
|
||||
Name: k.name,
|
||||
}
|
||||
}
|
||||
|
||||
// alertFor returns the anomaly alert for o, a count over its threshold in
|
||||
@@ -404,8 +433,13 @@ func alertFor(r Request, k key, o overThreshold) alerts.Alert {
|
||||
ASN: r.ASN,
|
||||
ASName: r.ASName,
|
||||
Country: r.Country,
|
||||
Reason: fmt.Sprintf("%s per %s of %s over the threshold of %d", o.kind, o.window,
|
||||
counted, o.threshold),
|
||||
Reason: fmt.Sprintf(
|
||||
"%s per %s of %s over the threshold of %d",
|
||||
o.kind,
|
||||
o.window,
|
||||
counted,
|
||||
o.threshold,
|
||||
),
|
||||
Detail: detail,
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,10 @@ func TestEachScopeHasACooldownOfItsOwn(t *testing.T) {
|
||||
// name one netblock.
|
||||
NetV4Prefix: 32,
|
||||
NamedNetblocks: []anomaly.NamedNetblock{
|
||||
{Name: "office", Netblock: office}, {Name: "hq", Netblock: office},
|
||||
{
|
||||
Name: "office",
|
||||
Netblock: office,
|
||||
}, {Name: "hq", Netblock: office},
|
||||
},
|
||||
Alerts: queue,
|
||||
})
|
||||
@@ -80,11 +83,16 @@ func TestEachScopeHasACooldownOfItsOwn(t *testing.T) {
|
||||
}
|
||||
|
||||
if after.Suppressed() != 9 {
|
||||
t.Errorf("after loading, %d alerts are held back, want 9", after.Suppressed())
|
||||
t.Errorf(
|
||||
"after loading, %d alerts are held back, want 9",
|
||||
after.Suppressed(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeepsAtMost20000CountersDroppingTheLeastRecentlyCounted(t *testing.T) {
|
||||
func TestKeepsAtMost20000CountersDroppingTheLeastRecentlyCounted(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
counters := newCounters(anomaly.Params{
|
||||
@@ -103,17 +111,28 @@ func TestKeepsAtMost20000CountersDroppingTheLeastRecentlyCounted(t *testing.T) {
|
||||
got := counters.Snapshot()
|
||||
if len(got) != maxCounters || !holds(got, 0) || holds(got, 1) ||
|
||||
!holds(got, maxCounters) {
|
||||
t.Errorf("%d counters, holding the first client %v, the second %v and the "+
|
||||
"new one %v, want %d, the first and the new one", len(got), holds(got, 0),
|
||||
holds(got, 1), holds(got, maxCounters), maxCounters)
|
||||
t.Errorf(
|
||||
"%d counters, holding the first client %v, the second %v and the "+
|
||||
"new one %v, want %d, the first and the new one",
|
||||
len(got),
|
||||
holds(got, 0),
|
||||
holds(got, 1),
|
||||
holds(got, maxCounters),
|
||||
maxCounters,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadEmptiesBucketsWhoseTimeHasPassedAndDropsEmptyCounters(t *testing.T) {
|
||||
func TestLoadEmptiesBucketsWhoseTimeHasPassedAndDropsEmptyCounters(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
counters := newCounters(anomaly.Params{
|
||||
Net: anomaly.Thresholds{RequestsPerMinute: 1000, RequestsPerHour: 1000},
|
||||
Net: anomaly.Thresholds{
|
||||
RequestsPerMinute: 1000,
|
||||
RequestsPerHour: 1000,
|
||||
},
|
||||
Total: anomaly.Thresholds{RequestsPerMinute: 1000},
|
||||
NetV4Prefix: 24,
|
||||
})
|
||||
@@ -142,7 +161,9 @@ func TestLoadEmptiesBucketsWhoseTimeHasPassedAndDropsEmptyCounters(t *testing.T)
|
||||
Hour: ratelimit.Buckets{Start: midnight(), Current: 7},
|
||||
}
|
||||
if got, want := counters.Snapshot(), []anomaly.Counter{netblock}; !reflect.DeepEqual(
|
||||
got, want) {
|
||||
got,
|
||||
want,
|
||||
) {
|
||||
t.Errorf("counters read\n%+v\nwant\n%+v", got, want)
|
||||
}
|
||||
|
||||
@@ -192,9 +213,12 @@ func TestLoadDropsTheLeastRecentlyCountedFirst(t *testing.T) {
|
||||
|
||||
got := counters.Snapshot()
|
||||
if !holds(got, 0) || holds(got, maxCounters/2) {
|
||||
t.Errorf("holding the first client of the file %v, and the first counted in "+
|
||||
"the minute before %v, want only the first", holds(got, 0),
|
||||
holds(got, maxCounters/2))
|
||||
t.Errorf(
|
||||
"holding the first client of the file %v, and the first counted in "+
|
||||
"the minute before %v, want only the first",
|
||||
holds(got, 0),
|
||||
holds(got, maxCounters/2),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,9 +249,14 @@ func newQueue() *alerts.Queue {
|
||||
// request returns a request from client number i, an address in
|
||||
// 10.0.0.0/8.
|
||||
func request(i int) anomaly.Request {
|
||||
client := netip.MustParseAddr(fmt.Sprintf("10.%d.%d.%d", i>>16, i>>8&255, i&255))
|
||||
client := netip.MustParseAddr(
|
||||
fmt.Sprintf("10.%d.%d.%d", i>>16, i>>8&255, i&255),
|
||||
)
|
||||
|
||||
return anomaly.Request{Client: client, ClientGroup: netip.PrefixFrom(client, 32)}
|
||||
return anomaly.Request{
|
||||
Client: client,
|
||||
ClientGroup: netip.PrefixFrom(client, 32),
|
||||
}
|
||||
}
|
||||
|
||||
// holds reports whether counters hold the counter of client number i.
|
||||
|
||||
Reference in new issue
Block a user