log_writer.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package xray
  2. import (
  3. "regexp"
  4. "runtime"
  5. "strings"
  6. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  7. )
  8. // Compiled once at package load: Write runs on every line Xray emits, so
  9. // recompiling these per write is wasted work.
  10. var (
  11. crashRegex = regexp.MustCompile(`(?i)(panic|exception|stack trace|fatal error)`)
  12. logLineRegex = regexp.MustCompile(`^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}\.\d{6}) \[([^\]]+)\] (.+)$`)
  13. )
  14. // NewLogWriter returns a new LogWriter for processing Xray log output.
  15. func NewLogWriter() *LogWriter {
  16. return &LogWriter{}
  17. }
  18. // LogWriter processes and filters log output from the Xray process, handling crash detection and message filtering.
  19. type LogWriter struct {
  20. lastLine string
  21. }
  22. // Write processes and filters log output from the Xray process, handling crash detection and message filtering.
  23. func (lw *LogWriter) Write(m []byte) (n int, err error) {
  24. // Convert the data to a string
  25. message := strings.TrimSpace(string(m))
  26. msgLowerAll := strings.ToLower(message)
  27. // Suppress noisy Windows process-kill signal that surfaces as exit status 1
  28. if runtime.GOOS == "windows" && strings.Contains(msgLowerAll, "exit status 1") {
  29. return len(m), nil
  30. }
  31. // Check if the message contains a crash
  32. if crashRegex.MatchString(message) {
  33. logger.Debug("Core crash detected:\n", message)
  34. lw.lastLine = message
  35. err1 := writeCrashReport(m)
  36. if err1 != nil {
  37. logger.Error("Unable to write crash report:", err1)
  38. }
  39. return len(m), nil
  40. }
  41. messages := strings.SplitSeq(message, "\n")
  42. for msg := range messages {
  43. matches := logLineRegex.FindStringSubmatch(msg)
  44. if len(matches) > 3 {
  45. level := matches[2]
  46. msgBody := matches[3]
  47. msgBodyLower := strings.ToLower(msgBody)
  48. if strings.Contains(msgBodyLower, "tls handshake error") ||
  49. strings.Contains(msgBodyLower, "connection ends") {
  50. logger.Debug("XRAY: " + msgBody)
  51. lw.lastLine = ""
  52. continue
  53. }
  54. if strings.Contains(msgBodyLower, "failed") {
  55. logger.Error("XRAY: " + msgBody)
  56. } else {
  57. switch level {
  58. case "Debug":
  59. logger.Debug("XRAY: " + msgBody)
  60. case "Info":
  61. logger.Info("XRAY: " + msgBody)
  62. case "Warning":
  63. logger.Warning("XRAY: " + msgBody)
  64. case "Error":
  65. logger.Error("XRAY: " + msgBody)
  66. default:
  67. logger.Debug("XRAY: " + msg)
  68. }
  69. }
  70. lw.lastLine = ""
  71. } else if msg != "" {
  72. msgLower := strings.ToLower(msg)
  73. if strings.Contains(msgLower, "tls handshake error") ||
  74. strings.Contains(msgLower, "connection ends") {
  75. logger.Debug("XRAY: " + msg)
  76. lw.lastLine = msg
  77. continue
  78. }
  79. if strings.Contains(msgLower, "failed") {
  80. logger.Error("XRAY: " + msg)
  81. } else {
  82. logger.Debug("XRAY: " + msg)
  83. }
  84. lw.lastLine = msg
  85. }
  86. }
  87. return len(m), nil
  88. }