fix: 1.4.4 read Traefik text logs, degraded source alert, mobile menu
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m46s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 24s
CI / Tests (Linux, race detector) (push) Successful in 1m22s
CI / Dependency audit (push) Successful in 27s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m46s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 24s
CI / Tests (Linux, race detector) (push) Successful in 1m22s
CI / Dependency audit (push) Successful in 27s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
50cd2bd3ce
commit
0dc3268b29
13 files changed
+297
-9
No files matched your search
@@ -266,3 +266,25 @@ func TestZonesAreRejectedEverywhere(t *testing.T) {
|
||||
}
|
||||
_ = strings.ToLower
|
||||
}
|
||||
|
||||
func TestParseTraefikText(t *testing.T) {
|
||||
ev, err := ParseTraefik([]byte(`10.0.15.1 - - [15/Nov/2026:04:10:51 +0000] "GET /wp-content/uploads/2026/08/IMG.jpg HTTP/2.0" 200 252131 "-" "-" 22219 "https-0-fwl1r20xzp8uf2g48log9q49-wordpress-1870@docker" "http://10.0.5.2:80" 3ms`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ev.IP != "10.0.15.1" || ev.Method != "GET" || ev.Path != "/wp-content/uploads/2026/08/IMG.jpg" || ev.Proto != "HTTP/2.0" ||
|
||||
ev.Status != 200 || ev.Bytes != 252131 || ev.UA != "" || ev.Referer != "" || ev.OriginStatus != 200 ||
|
||||
ev.DurationNS != 3e6 || ev.Domain != "" || ev.Router != "https-0-fwl1r20xzp8uf2g48log9q49-wordpress-1870@docker" ||
|
||||
ev.Time.Format("2006-01-02T15:04:05Z") != "2026-11-15T04:10:51Z" {
|
||||
t.Fatalf("%+v", ev)
|
||||
}
|
||||
ev, err = ParseTraefik([]byte(`203.0.113.9 - - [15/Nov/2026:04:10:52 +0000] "POST /wp-login.php HTTP/1.1" 404 19 "https://x/\"y" "Mozilla \"q\" 5.0" 1 "web@docker" "-" 12ms`))
|
||||
if err != nil || ev.Method != "POST" || ev.Status != 404 || ev.UA != `Mozilla "q" 5.0` || ev.Referer != `https://x/"y` || ev.OriginStatus != 0 {
|
||||
t.Fatalf("%+v %v", ev, err)
|
||||
}
|
||||
for _, bad := range []string{"garbage", `1.2.3.4 - - [bad] "GET / HTTP/1.1" 200 1 "-" "-" 1`, `1.2.3.4 - - [15/Nov/2026:04:10:52 +0000] "GET / HTTP/1.1" 200 1 "-" "-`, `nope - - [15/Nov/2026:04:10:52 +0000] "GET / HTTP/1.1" 200 1 "-" "-" 1`} {
|
||||
if _, err := ParseTraefik([]byte(bad)); err == nil {
|
||||
t.Errorf("accepted %q", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/netip"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -72,6 +74,9 @@ type traefikLine struct {
|
||||
|
||||
// ParseTraefik parses one Traefik JSON access log line.
|
||||
func ParseTraefik(line []byte) (Event, error) {
|
||||
if t := bytes.TrimLeft(line, " \t"); len(t) > 0 && t[0] != '{' {
|
||||
return parseTraefikText(string(t))
|
||||
}
|
||||
var l traefikLine
|
||||
if err := json.Unmarshal(line, &l); err != nil {
|
||||
return Event{}, err
|
||||
@@ -213,3 +218,119 @@ func ParseFail2ban(line string) (Event, error) {
|
||||
Unban: m[3] == "Unban", Restore: m[3] == "Restore Ban",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// textFields splits a Traefik text (extended CLF) access log line into its
|
||||
// fields. Quoted fields may carry backslash escapes, the [time] field is read
|
||||
// up to the closing bracket.
|
||||
func textFields(s string) ([]string, bool) {
|
||||
var out []string
|
||||
i := 0
|
||||
for i < len(s) {
|
||||
switch c := s[i]; {
|
||||
case c == ' ' || c == '\t':
|
||||
i++
|
||||
case c == '"':
|
||||
var b strings.Builder
|
||||
i++
|
||||
closed := false
|
||||
for i < len(s) {
|
||||
if s[i] == '\\' && i+1 < len(s) {
|
||||
b.WriteByte(s[i+1])
|
||||
i += 2
|
||||
continue
|
||||
}
|
||||
if s[i] == '"' {
|
||||
closed = true
|
||||
i++
|
||||
break
|
||||
}
|
||||
b.WriteByte(s[i])
|
||||
i++
|
||||
}
|
||||
if !closed {
|
||||
return nil, false
|
||||
}
|
||||
out = append(out, b.String())
|
||||
case c == '[':
|
||||
j := strings.IndexByte(s[i:], ']')
|
||||
if j < 0 {
|
||||
return nil, false
|
||||
}
|
||||
out = append(out, s[i+1:i+j])
|
||||
i += j + 1
|
||||
default:
|
||||
j := i
|
||||
for j < len(s) && s[j] != ' ' && s[j] != '\t' {
|
||||
j++
|
||||
}
|
||||
out = append(out, s[i:j])
|
||||
i = j
|
||||
}
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
|
||||
func dash(s string) string {
|
||||
if s == "-" {
|
||||
return ""
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// parseTraefikText parses the default Traefik access log format:
|
||||
// host ident user [time] "METHOD path PROTO" status size "referer" "ua" count "router" "backend" Nms
|
||||
// The format carries no Host header, so Domain stays empty.
|
||||
func parseTraefikText(line string) (Event, error) {
|
||||
f, ok := textFields(line)
|
||||
if !ok || len(f) < 9 {
|
||||
return Event{}, errors.New("not a traefik text line")
|
||||
}
|
||||
ip, ok := cleanIP(f[0])
|
||||
if !ok {
|
||||
if _, err := netip.ParseAddr(strings.TrimSpace(f[0])); err != nil {
|
||||
return Event{}, errors.New("bad client address")
|
||||
}
|
||||
return Event{}, ErrIgnored
|
||||
}
|
||||
t, err := time.Parse("02/Jan/2006:15:04:05 -0700", f[3])
|
||||
if err != nil {
|
||||
return Event{}, err
|
||||
}
|
||||
rl := strings.Fields(f[4])
|
||||
if len(rl) < 2 {
|
||||
return Event{}, errors.New("bad request line")
|
||||
}
|
||||
method, path, proto := rl[0], rl[1], ""
|
||||
if len(rl) >= 3 {
|
||||
proto = rl[len(rl)-1]
|
||||
}
|
||||
status, err := strconv.Atoi(f[5])
|
||||
if err != nil || status < 100 || status > 999 {
|
||||
return Event{}, errors.New("bad status")
|
||||
}
|
||||
var size int64
|
||||
if f[6] != "-" {
|
||||
if size, err = strconv.ParseInt(f[6], 10, 64); err != nil {
|
||||
return Event{}, err
|
||||
}
|
||||
}
|
||||
ev := Event{
|
||||
Time: t.UTC(), IP: ip, Source: "traefik", Status: status,
|
||||
Method: clip(method, 12), Path: clip(path, maxPath), Proto: clip(proto, 12),
|
||||
Bytes: size, Referer: clip(dash(f[7]), 300), UA: clip(dash(f[8]), maxUA),
|
||||
}
|
||||
if len(f) > 10 {
|
||||
ev.Router = clip(dash(f[10]), 64)
|
||||
}
|
||||
if len(f) > 11 && dash(f[11]) != "" {
|
||||
ev.OriginStatus = status
|
||||
}
|
||||
if len(f) > 12 {
|
||||
if d, err := time.ParseDuration(strings.TrimSpace(f[12])); err == nil {
|
||||
ev.DurationNS = d.Nanoseconds()
|
||||
}
|
||||
}
|
||||
c := Classify(method, path, status)
|
||||
ev.Cat, ev.Soft, ev.Strong, ev.Weak = c.Cat, c.Soft, c.Strong, c.Weak
|
||||
return ev, nil
|
||||
}
|
||||
Reference in new issue
Block a user