Files
trihuy-russian/xray/log_writer.go
T

101 lines
2.5 KiB
Go
Raw Normal View History

2023-12-10 13:07:50 +01:00
package xray
import (
2024-02-17 21:37:58 +03:30
"regexp"
2025-09-14 20:04:12 +02:00
"runtime"
2023-12-10 13:07:50 +01:00
"strings"
2024-03-11 01:01:24 +03:30
2026-05-10 02:13:42 +02:00
"github.com/mhsanaei/3x-ui/v3/logger"
2023-12-10 13:07:50 +01:00
)
2025-09-20 09:35:50 +02:00
// NewLogWriter returns a new LogWriter for processing Xray log output.
2023-12-10 13:07:50 +01:00
func NewLogWriter() *LogWriter {
return &LogWriter{}
}
2025-09-20 09:35:50 +02:00
// LogWriter processes and filters log output from the Xray process, handling crash detection and message filtering.
2023-12-10 13:07:50 +01:00
type LogWriter struct {
lastLine string
}
2025-09-20 09:35:50 +02:00
// Write processes and filters log output from the Xray process, handling crash detection and message filtering.
2023-12-10 13:07:50 +01:00
func (lw *LogWriter) Write(m []byte) (n int, err error) {
2024-12-03 23:07:24 +01:00
crashRegex := regexp.MustCompile(`(?i)(panic|exception|stack trace|fatal error)`)
2023-12-10 13:07:50 +01:00
// Convert the data to a string
message := strings.TrimSpace(string(m))
2025-09-14 20:04:12 +02:00
msgLowerAll := strings.ToLower(message)
// Suppress noisy Windows process-kill signal that surfaces as exit status 1
if runtime.GOOS == "windows" && strings.Contains(msgLowerAll, "exit status 1") {
return len(m), nil
}
2024-12-03 23:07:24 +01:00
// Check if the message contains a crash
if crashRegex.MatchString(message) {
logger.Debug("Core crash detected:\n", message)
lw.lastLine = message
2025-07-10 02:36:02 +02:00
err1 := writeCrashReport(m)
2024-12-03 23:07:24 +01:00
if err1 != nil {
logger.Error("Unable to write crash report:", err1)
}
return len(m), nil
}
2025-03-10 13:46:46 +01:00
regex := regexp.MustCompile(`^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}\.\d{6}) \[([^\]]+)\] (.+)$`)
2025-03-10 14:12:30 +01:00
messages := strings.SplitSeq(message, "\n")
2023-12-10 13:07:50 +01:00
2025-03-10 14:12:30 +01:00
for msg := range messages {
2024-02-17 21:37:58 +03:30
matches := regex.FindStringSubmatch(msg)
2024-01-10 16:16:18 +03:30
2024-02-17 21:37:58 +03:30
if len(matches) > 3 {
level := matches[2]
msgBody := matches[3]
2025-03-13 11:48:00 +01:00
msgBodyLower := strings.ToLower(msgBody)
2023-12-10 13:07:50 +01:00
2025-03-13 11:48:00 +01:00
if strings.Contains(msgBodyLower, "tls handshake error") ||
strings.Contains(msgBodyLower, "connection ends") {
logger.Debug("XRAY: " + msgBody)
lw.lastLine = ""
continue
}
if strings.Contains(msgBodyLower, "failed") {
2024-02-19 00:45:00 +03:30
logger.Error("XRAY: " + msgBody)
2025-03-10 14:12:30 +01:00
} else {
switch level {
case "Debug":
logger.Debug("XRAY: " + msgBody)
case "Info":
logger.Info("XRAY: " + msgBody)
case "Warning":
logger.Warning("XRAY: " + msgBody)
case "Error":
logger.Error("XRAY: " + msgBody)
default:
logger.Debug("XRAY: " + msg)
}
2023-12-10 13:07:50 +01:00
}
2024-12-03 23:07:24 +01:00
lw.lastLine = ""
2023-12-10 13:07:50 +01:00
} else if msg != "" {
2025-03-13 11:48:00 +01:00
msgLower := strings.ToLower(msg)
if strings.Contains(msgLower, "tls handshake error") ||
strings.Contains(msgLower, "connection ends") {
logger.Debug("XRAY: " + msg)
lw.lastLine = msg
continue
}
if strings.Contains(msgLower, "failed") {
2025-03-10 14:12:30 +01:00
logger.Error("XRAY: " + msg)
} else {
logger.Debug("XRAY: " + msg)
}
2024-12-03 23:07:24 +01:00
lw.lastLine = msg
2023-12-10 13:07:50 +01:00
}
}
return len(m), nil
}