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
+57
-18
@@ -291,7 +291,15 @@ func cooldownKeyOf(alert *Alert) cooldownKey {
|
||||
asn, _ := alert.Detail["asn"].(string)
|
||||
name, _ := alert.Detail["name"].(string)
|
||||
|
||||
return cooldownKey{alert.Event, alert.Netblock, file, source, scope, asn, name}
|
||||
return cooldownKey{
|
||||
alert.Event,
|
||||
alert.Netblock,
|
||||
file,
|
||||
source,
|
||||
scope,
|
||||
asn,
|
||||
name,
|
||||
}
|
||||
}
|
||||
|
||||
// New returns a Queue with no alert yet.
|
||||
@@ -333,7 +341,8 @@ func New(params Params) *Queue {
|
||||
// queue of each destination, from which Run sends it, and with queueSize
|
||||
// alerts waiting for a destination, the oldest is dropped.
|
||||
func (q *Queue) Raise(alert Alert) {
|
||||
if len(q.destinations) == 0 || !slices.Contains(q.params.Events, alert.Event) {
|
||||
if len(q.destinations) == 0 ||
|
||||
!slices.Contains(q.params.Events, alert.Event) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -380,7 +389,8 @@ func (q *Queue) WouldSend(event string, netblock netip.Prefix) bool {
|
||||
now := q.params.Now()
|
||||
|
||||
last, found := q.cooldowns[cooldownKey{event: event, netblock: netblock}]
|
||||
if q.params.Cooldown > 0 && found && now.Sub(last.Sent) < q.params.Cooldown {
|
||||
if q.params.Cooldown > 0 && found &&
|
||||
now.Sub(last.Sent) < q.params.Cooldown {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -474,10 +484,15 @@ func (q *Queue) Snapshot() State {
|
||||
}
|
||||
|
||||
slices.SortFunc(state.Cooldowns, func(a, b Cooldown) int {
|
||||
return cmp.Or(a.Netblock.Compare(b.Netblock), cmp.Compare(a.Event, b.Event),
|
||||
cmp.Compare(a.File, b.File), cmp.Compare(a.Source, b.Source),
|
||||
cmp.Compare(a.Scope, b.Scope), cmp.Compare(a.ASN, b.ASN),
|
||||
cmp.Compare(a.Name, b.Name))
|
||||
return cmp.Or(
|
||||
a.Netblock.Compare(b.Netblock),
|
||||
cmp.Compare(a.Event, b.Event),
|
||||
cmp.Compare(a.File, b.File),
|
||||
cmp.Compare(a.Source, b.Source),
|
||||
cmp.Compare(a.Scope, b.Scope),
|
||||
cmp.Compare(a.ASN, b.ASN),
|
||||
cmp.Compare(a.Name, b.Name),
|
||||
)
|
||||
})
|
||||
|
||||
for _, d := range q.destinations {
|
||||
@@ -609,9 +624,12 @@ func (q *Queue) endHour(now time.Time) {
|
||||
var reasons []string
|
||||
|
||||
if heldBack > 0 {
|
||||
reasons = append(reasons, fmt.Sprintf("%d alerts held back in the hour from %s, "+
|
||||
"past the %d an hour SWW_ALERT_MAX_PER_HOUR allows", heldBack,
|
||||
q.hour.Start.Format(time.RFC3339), q.params.MaxPerHour))
|
||||
reasons = append(
|
||||
reasons,
|
||||
fmt.Sprintf("%d alerts held back in the hour from %s, "+
|
||||
"past the %d an hour SWW_ALERT_MAX_PER_HOUR allows", heldBack,
|
||||
q.hour.Start.Format(time.RFC3339), q.params.MaxPerHour),
|
||||
)
|
||||
}
|
||||
|
||||
if repeats > 0 {
|
||||
@@ -690,12 +708,19 @@ func (d *destination) run(ctx context.Context) {
|
||||
case ctx.Err() == nil: // not cut off as sww stops
|
||||
d.sendFailures.Add(1)
|
||||
|
||||
retryDelay = min(max(retryDelayFactor*retryDelay, firstRetryDelay),
|
||||
maxRetryDelay)
|
||||
retryDelay = min(
|
||||
max(retryDelayFactor*retryDelay, firstRetryDelay),
|
||||
maxRetryDelay,
|
||||
)
|
||||
retryAt = time.Now().Add(retryDelay)
|
||||
|
||||
d.processLog.Warn("sending an alert to "+d.setting+" failed",
|
||||
"error", err.Error(), "sending_again_in", retryDelay.String())
|
||||
d.processLog.Warn(
|
||||
"sending an alert to "+d.setting+" failed",
|
||||
"error",
|
||||
err.Error(),
|
||||
"sending_again_in",
|
||||
retryDelay.String(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -783,8 +808,12 @@ func (d *destination) send(ctx context.Context, alert *Alert) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, sendTimeout)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, d.url.String(),
|
||||
bytes.NewReader(body))
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
d.url.String(),
|
||||
bytes.NewReader(body),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("make the request: %w", err)
|
||||
}
|
||||
@@ -838,7 +867,14 @@ func (q *Queue) webhookMessage(alert *Alert) ([]byte, http.Header, error) {
|
||||
// in JSON: its title in bold, then its text, with &, < and > escaped, as
|
||||
// Slack asks, so that nothing in them is read as a link or a mention.
|
||||
func slackMessage(alert *Alert) ([]byte, http.Header, error) {
|
||||
escape := strings.NewReplacer("&", "&", "<", "<", ">", ">").Replace
|
||||
escape := strings.NewReplacer(
|
||||
"&",
|
||||
"&",
|
||||
"<",
|
||||
"<",
|
||||
">",
|
||||
">",
|
||||
).Replace
|
||||
|
||||
body, err := json.Marshal(map[string]string{
|
||||
"text": "*" + escape(title(alert)) + "*\n" + escape(text(alert)),
|
||||
@@ -936,7 +972,10 @@ func text(alert *Alert) string {
|
||||
}
|
||||
|
||||
if alert.SuppressedRepeats > 0 {
|
||||
lines = append(lines, fmt.Sprintf("suppressed repeats: %d", alert.SuppressedRepeats))
|
||||
lines = append(
|
||||
lines,
|
||||
fmt.Sprintf("suppressed repeats: %d", alert.SuppressedRepeats),
|
||||
)
|
||||
}
|
||||
|
||||
return strings.Join(lines, "\n")
|
||||
|
||||
Reference in new issue
Block a user