fix(server): keep the access log view when one line is over 64 KiB

GetXrayLogs read access.log with a default bufio.Scanner, whose token
limit is 64 KiB, and returned nothing at all when it stopped with
ErrTooLong. Xray logs an HTTP request's URL whole, so one proxied
request with a long URL blanked the Access Logs view for every client.

Lines are now read with a bounded reader: a line that fits is kept
whole, and a longer one keeps its first 64 KiB and its last 1 KiB,
where the route and email are, so it still shows as a correctly
attributed row.
This commit is contained in:
MHSanaei
2026-11-01 13:42:12 +01:00
parent e02f51320a
commit 54339661a6
2 changed files with 101 additions and 7 deletions
+41 -7
View File
@@ -1477,6 +1477,37 @@ func (s *ServerService) GetAmneziaWGLogs(count string, filter string) *AmneziaWG
return logs
}
// Xray's HTTP inbound logs the requested URL whole, so an access-log line has
// no length bound; GetXrayLogs keeps this much of a line's start and of its end.
const (
maxAccessLogLineBytes = 64 << 10
accessLogLineEndBytes = 1 << 10
)
// readAccessLogLine returns the next line of r. A line longer than r's buffer
// loses its middle and keeps its end (route, email) instead of failing the read.
func readAccessLogLine(r *bufio.Reader) (string, error) {
chunk, more, err := r.ReadLine()
if err != nil {
return "", err
}
line := string(chunk)
var end []byte
for more {
chunk, more, err = r.ReadLine()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return "", err
}
end = append(end, chunk...)
end = end[max(0, len(end)-accessLogLineEndBytes):]
}
return line + string(end), nil
}
func (s *ServerService) GetXrayLogs(
count string,
filter string,
@@ -1509,10 +1540,17 @@ func (s *ServerService) GetXrayLogs(
}
defer file.Close()
scanner := bufio.NewScanner(file)
reader := bufio.NewReaderSize(file, maxAccessLogLineBytes)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
for {
line, err := readAccessLogLine(reader)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil
}
line = strings.TrimSpace(line)
if line == "" || strings.Contains(line, "api -> api") {
// skipping empty lines and api calls
@@ -1551,10 +1589,6 @@ func (s *ServerService) GetXrayLogs(
}
}
if err := scanner.Err(); err != nil {
return nil
}
return entries
}