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
f9bec5146b
commit
a8b24a843b
117 files changed
+9441
-2670
No files matched your search
+118
-32
@@ -89,7 +89,11 @@ func fileNames() []string {
|
||||
// isCacheFile reports whether name is the name of a cache file.
|
||||
func isCacheFile(name string) bool {
|
||||
switch name {
|
||||
case lookupsJSON, listsJSON, dnsblJSON, abuseIPDBScoresJSON, routeWatchJSON:
|
||||
case lookupsJSON,
|
||||
listsJSON,
|
||||
dnsblJSON,
|
||||
abuseIPDBScoresJSON,
|
||||
routeWatchJSON:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
@@ -103,10 +107,14 @@ var (
|
||||
errCause = errors.New("is not limit, attack, admin or crowdsec")
|
||||
errExpires = errors.New("is not after its start")
|
||||
errDestination = errors.New("is not webhook, slack or ntfy")
|
||||
errScope = errors.New("is not client, net, asn, total or watched_net")
|
||||
errWaitingList = errors.New(`waiting is a list, but now lists the alerts by ` +
|
||||
`destination: put the list under "webhook", as "waiting": {"webhook": [...]}, ` +
|
||||
`or remove the file`)
|
||||
errScope = errors.New(
|
||||
"is not client, net, asn, total or watched_net",
|
||||
)
|
||||
errWaitingList = errors.New(
|
||||
`waiting is a list, but now lists the alerts by ` +
|
||||
`destination: put the list under "webhook", as "waiting": {"webhook": [...]}, ` +
|
||||
`or remove the file`,
|
||||
)
|
||||
)
|
||||
|
||||
// Params are what Load needs.
|
||||
@@ -283,9 +291,23 @@ func Load(params Params) (*Files, error) {
|
||||
f.readCache(abuseIPDBScoresJSON)
|
||||
f.readCache(routeWatchJSON)
|
||||
|
||||
params.ProcessLog.Info("read the state and cache files", "directory", params.Dir,
|
||||
"cache_directory", params.CacheDir, "bans", bansRead, "clients", clientsRead,
|
||||
"lookups", lookupsRead, "lists", listsRead, "alerts_waiting", alertsRead)
|
||||
params.ProcessLog.Info(
|
||||
"read the state and cache files",
|
||||
"directory",
|
||||
params.Dir,
|
||||
"cache_directory",
|
||||
params.CacheDir,
|
||||
"bans",
|
||||
bansRead,
|
||||
"clients",
|
||||
clientsRead,
|
||||
"lookups",
|
||||
lookupsRead,
|
||||
"lists",
|
||||
listsRead,
|
||||
"alerts_waiting",
|
||||
alertsRead,
|
||||
)
|
||||
|
||||
return f, nil
|
||||
}
|
||||
@@ -344,8 +366,11 @@ func (f *Files) WriteAll() error {
|
||||
func (f *Files) Watch(ctx context.Context) {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
f.params.ProcessLog.Error("cannot watch the state and cache files for edits",
|
||||
"error", err.Error())
|
||||
f.params.ProcessLog.Error(
|
||||
"cannot watch the state and cache files for edits",
|
||||
"error",
|
||||
err.Error(),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -395,8 +420,11 @@ func (f *Files) Watch(ctx context.Context) {
|
||||
f.fileChanged(name)
|
||||
}
|
||||
case err = <-watcher.Errors:
|
||||
f.params.ProcessLog.Warn("watching the state and cache files failed",
|
||||
"error", err.Error())
|
||||
f.params.ProcessLog.Warn(
|
||||
"watching the state and cache files failed",
|
||||
"error",
|
||||
err.Error(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -416,7 +444,10 @@ func (f *Files) logFailure(name string, err error) {
|
||||
Event: alerts.EventFileError,
|
||||
Reason: failed,
|
||||
Detail: map[string]any{
|
||||
"file": filepath.Join(f.dir(name), name), "error": err.Error(),
|
||||
"file": filepath.Join(
|
||||
f.dir(name),
|
||||
name,
|
||||
), "error": err.Error(),
|
||||
},
|
||||
})
|
||||
f.params.ProcessLog.Error(failed, "error", err.Error())
|
||||
@@ -457,7 +488,11 @@ func (f *Files) takeInEdit(name string, data []byte) error {
|
||||
// Counted before it is logged, so that the count is there once the
|
||||
// log line is.
|
||||
f.params.Metrics.StateFileEditTakenIn(name)
|
||||
f.params.ProcessLog.Info(tookIn, "file", filepath.Join(f.dir(name), name))
|
||||
f.params.ProcessLog.Info(
|
||||
tookIn,
|
||||
"file",
|
||||
filepath.Join(f.dir(name), name),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -508,8 +543,13 @@ func (f *Files) readCache(name string) int {
|
||||
// start, unless it is nil.
|
||||
func (f *Files) logUnread(name string, err error) {
|
||||
if err != nil {
|
||||
f.params.ProcessLog.Warn("cannot read a cache file, so its cache starts empty",
|
||||
"file", filepath.Join(f.dir(name), name), "error", err.Error())
|
||||
f.params.ProcessLog.Warn(
|
||||
"cannot read a cache file, so its cache starts empty",
|
||||
"file",
|
||||
filepath.Join(f.dir(name), name),
|
||||
"error",
|
||||
err.Error(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -520,7 +560,8 @@ func (f *Files) logUnread(name string, err error) {
|
||||
func (f *Files) readChanged(name string) ([]byte, bool, error) {
|
||||
path := filepath.Join(f.dir(name), name)
|
||||
|
||||
data, err := os.ReadFile(path) //nolint:gosec // a file in one of the two directories
|
||||
//nolint:gosec // a file in one of the two directories
|
||||
data, err := os.ReadFile(path)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, false, nil
|
||||
}
|
||||
@@ -564,7 +605,11 @@ func (f *Files) takeIn(name string, data []byte, edit bool) (int, error) {
|
||||
|
||||
// takeInState parses data, what the state file name, at path, holds, and
|
||||
// puts it in, as takeIn describes.
|
||||
func (f *Files) takeInState(name, path string, data []byte, edit bool) (int, error) {
|
||||
func (f *Files) takeInState(
|
||||
name, path string,
|
||||
data []byte,
|
||||
edit bool,
|
||||
) (int, error) {
|
||||
var entries int
|
||||
|
||||
switch name {
|
||||
@@ -793,9 +838,18 @@ func (f *Files) setAside(name string, parseErr error) error {
|
||||
f.params.Alerts.Raise(alerts.Alert{
|
||||
Event: alerts.EventFileError,
|
||||
Reason: setAside,
|
||||
Detail: map[string]any{"file": path + ".bad", "error": parseErr.Error()},
|
||||
Detail: map[string]any{
|
||||
"file": path + ".bad",
|
||||
"error": parseErr.Error(),
|
||||
},
|
||||
})
|
||||
f.params.ProcessLog.Error(setAside, "file", path+".bad", "error", parseErr.Error())
|
||||
f.params.ProcessLog.Error(
|
||||
setAside,
|
||||
"file",
|
||||
path+".bad",
|
||||
"error",
|
||||
parseErr.Error(),
|
||||
)
|
||||
f.params.Metrics.StateFileEditSetAside(name)
|
||||
|
||||
return nil
|
||||
@@ -814,7 +868,9 @@ func (f *Files) encode(name string) ([]byte, error) {
|
||||
case lookupsJSON:
|
||||
return encodeOnePerLine("lookups", f.params.GeoJS.Snapshot())
|
||||
case listsJSON:
|
||||
return encodeIndented(listsFile{Version: version, Lists: f.params.Lists.Snapshot()})
|
||||
return encodeIndented(
|
||||
listsFile{Version: version, Lists: f.params.Lists.Snapshot()},
|
||||
)
|
||||
case dnsblJSON:
|
||||
return encodeOnePerLine("verdicts", f.params.DNSBL.Snapshot())
|
||||
case abuseIPDBScoresJSON:
|
||||
@@ -931,7 +987,12 @@ func (f *bansFile) check(data []byte) error {
|
||||
case entry.Cause != "" && entry.Cause != bans.CauseLimit &&
|
||||
entry.Cause != bans.CauseAttack && entry.Cause != bans.CauseAdmin &&
|
||||
entry.Cause != bans.CauseCrowdSec:
|
||||
return fmt.Errorf("entry %d's cause %q %w", i+1, entry.Cause, errCause)
|
||||
return fmt.Errorf(
|
||||
"entry %d's cause %q %w",
|
||||
i+1,
|
||||
entry.Cause,
|
||||
errCause,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1140,9 +1201,17 @@ func (f *alertsFile) check([]byte) error {
|
||||
for i, alert := range f.Waiting[destination] {
|
||||
switch {
|
||||
case alert.Event == "":
|
||||
return fmt.Errorf("waiting %s %w", destination, missing(i, "event"))
|
||||
return fmt.Errorf(
|
||||
"waiting %s %w",
|
||||
destination,
|
||||
missing(i, "event"),
|
||||
)
|
||||
case alert.Time.IsZero():
|
||||
return fmt.Errorf("waiting %s %w", destination, missing(i, "time"))
|
||||
return fmt.Errorf(
|
||||
"waiting %s %w",
|
||||
destination,
|
||||
missing(i, "time"),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1215,7 +1284,12 @@ func missing(i int, field string) error {
|
||||
func (f *bansFile) toUTC() {
|
||||
for i := range f.Bans {
|
||||
ban := &f.Bans[i]
|
||||
convertToUTC(&ban.Start, ban.Expires, ban.Lifted, &ban.Notes.Request.Time)
|
||||
convertToUTC(
|
||||
&ban.Start,
|
||||
ban.Expires,
|
||||
ban.Lifted,
|
||||
&ban.Notes.Request.Time,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1223,11 +1297,18 @@ func (f *bansFile) toUTC() {
|
||||
func (f *clientsFile) toUTC() {
|
||||
for i := range f.Clients {
|
||||
client := &f.Clients[i]
|
||||
convertToUTC(&client.Minute.Start, &client.Hour.Start, &client.Day.Start,
|
||||
&client.MinuteBytes.Start, &client.HourBytes.Start,
|
||||
&client.DayBytes.Start, &client.MinuteRefusals.Start,
|
||||
&client.History.FirstSeen, &client.History.LastSeen,
|
||||
&client.History.LookedUp)
|
||||
convertToUTC(
|
||||
&client.Minute.Start,
|
||||
&client.Hour.Start,
|
||||
&client.Day.Start,
|
||||
&client.MinuteBytes.Start,
|
||||
&client.HourBytes.Start,
|
||||
&client.DayBytes.Start,
|
||||
&client.MinuteRefusals.Start,
|
||||
&client.History.FirstSeen,
|
||||
&client.History.LastSeen,
|
||||
&client.History.LookedUp,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1420,7 +1501,8 @@ func write(dir, name string, data []byte) error {
|
||||
|
||||
// syncDirectory syncs dir to the disk, so that a rename in it lasts.
|
||||
func syncDirectory(dir string) error {
|
||||
directory, err := os.Open(dir) //nolint:gosec // SWW_STATE_DIR or SWW_CACHE_DIR itself
|
||||
//nolint:gosec // SWW_STATE_DIR or SWW_CACHE_DIR itself
|
||||
directory, err := os.Open(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1431,7 +1513,11 @@ func syncDirectory(dir string) error {
|
||||
// writeSynced writes data to the file at path, and syncs it to the disk.
|
||||
func writeSynced(path string, data []byte) error {
|
||||
//nolint:gosec // a file's temporary file, in SWW_STATE_DIR or SWW_CACHE_DIR
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileMode)
|
||||
file, err := os.OpenFile(
|
||||
path,
|
||||
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
|
||||
fileMode,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+513
-161
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user