From 4c2a0ffab706ed24a471a6844dc70c389c6bab0b Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 21:52:13 +0000 Subject: [PATCH] fix(log): keep access log columns stable Co-Authored-By: Virgil --- accesslog_impl.go | 16 ++++++---------- accesslog_test.go | 42 ++++++++++++++++++++++++++++++++++++++++-- log/impl.go | 16 ++++++---------- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/accesslog_impl.go b/accesslog_impl.go index b8fa07c..d2c5894 100644 --- a/accesslog_impl.go +++ b/accesslog_impl.go @@ -81,16 +81,12 @@ func (l *accessLogSink) writeLine(kind, ip, user, agent string, rx, tx uint64) { builder.WriteString(ip) builder.WriteString(" ") builder.WriteString(user) - if agent != "" { - builder.WriteString(" ") - builder.WriteString(agent) - } - if rx > 0 || tx > 0 { - builder.WriteString(" rx=") - builder.WriteString(formatUint(rx)) - builder.WriteString(" tx=") - builder.WriteString(formatUint(tx)) - } + builder.WriteString(" ") + builder.WriteString(agent) + builder.WriteString(" rx=") + builder.WriteString(formatUint(rx)) + builder.WriteString(" tx=") + builder.WriteString(formatUint(tx)) builder.WriteByte('\n') _, _ = l.file.WriteString(builder.String()) } diff --git a/accesslog_test.go b/accesslog_test.go index 85affed..f01d08b 100644 --- a/accesslog_test.go +++ b/accesslog_test.go @@ -44,14 +44,52 @@ func TestProxy_AccessLog_WritesLifecycleLines(t *testing.T) { t.Fatalf("read access log: %v", err) } text := string(data) - if !strings.Contains(text, "CONNECT 10.0.0.1 WALLET XMRig/6.21.0") { + if !strings.Contains(text, "CONNECT 10.0.0.1 WALLET XMRig/6.21.0 rx=0 tx=0") { t.Fatalf("expected CONNECT line, got %q", text) } - if !strings.Contains(text, "CLOSE 10.0.0.1 WALLET rx=512 tx=4096") { + if !strings.Contains(text, "CLOSE 10.0.0.1 WALLET rx=512 tx=4096") { t.Fatalf("expected CLOSE line, got %q", text) } } +func TestProxy_AccessLog_WritesFixedColumns(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "access.log") + + cfg := &Config{ + Mode: "nicehash", + Workers: WorkersByRigID, + AccessLogFile: path, + Bind: []BindAddr{{Host: "127.0.0.1", Port: 3333}}, + Pools: []PoolConfig{{URL: "pool.example:3333", Enabled: true}}, + } + p, result := New(cfg) + if !result.OK { + t.Fatalf("expected valid proxy, got error: %v", result.Error) + } + + miner := &Miner{ + ip: "10.0.0.1", + user: "WALLET", + conn: noopConn{}, + } + p.events.Dispatch(Event{Type: EventLogin, Miner: miner}) + p.events.Dispatch(Event{Type: EventClose, Miner: miner}) + p.Stop() + + data, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read access log: %v", err) + } + text := string(data) + if !strings.Contains(text, "CONNECT 10.0.0.1 WALLET rx=0 tx=0") { + t.Fatalf("expected CONNECT line with zero counters, got %q", text) + } + if !strings.Contains(text, "CLOSE 10.0.0.1 WALLET rx=0 tx=0") { + t.Fatalf("expected CLOSE line with zero counters, got %q", text) + } +} + type noopConn struct{} func (noopConn) Read([]byte) (int, error) { return 0, os.ErrClosed } diff --git a/log/impl.go b/log/impl.go index f71854a..dea337b 100644 --- a/log/impl.go +++ b/log/impl.go @@ -65,16 +65,12 @@ func (l *AccessLog) writeLine(kind, ip, user, agent string, rx, tx uint64) { builder.WriteString(ip) builder.WriteString(" ") builder.WriteString(user) - if agent != "" { - builder.WriteString(" ") - builder.WriteString(agent) - } - if rx > 0 || tx > 0 { - builder.WriteString(" rx=") - builder.WriteString(strconv.FormatUint(rx, 10)) - builder.WriteString(" tx=") - builder.WriteString(strconv.FormatUint(tx, 10)) - } + builder.WriteString(" ") + builder.WriteString(agent) + builder.WriteString(" rx=") + builder.WriteString(strconv.FormatUint(rx, 10)) + builder.WriteString(" tx=") + builder.WriteString(strconv.FormatUint(tx, 10)) builder.WriteByte('\n') _, _ = l.f.WriteString(builder.String()) }