浏览代码

Add quic qlog to debug logs

yuhan6665 3 年之前
父节点
当前提交
c9df755426
共有 5 个文件被更改,包括 40 次插入0 次删除
  1. 1 0
      go.mod
  2. 1 0
      go.sum
  3. 6 0
      transport/internet/quic/dialer.go
  4. 6 0
      transport/internet/quic/hub.go
  5. 26 0
      transport/internet/quic/qlogWriter.go

+ 1 - 0
go.mod

@@ -31,6 +31,7 @@ require (
 	github.com/cheekybits/genny v1.0.0 // indirect
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140 // indirect
+	github.com/francoispqt/gojay v1.2.13 // indirect
 	github.com/fsnotify/fsnotify v1.5.1 // indirect
 	github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
 	github.com/marten-seemann/qtls-go1-16 v0.1.5 // indirect

+ 1 - 0
go.sum

@@ -42,6 +42,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
 github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
 github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
 github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
+github.com/francoispqt/gojay v1.2.13 h1:d2m3sFjloqoIUQU3TsHBgj6qg/BVGlTBeHDUmyJnXKk=
 github.com/francoispqt/gojay v1.2.13/go.mod h1:ehT5mTG4ua4581f1++1WLG0vPdaA9HaiDsoyrBGkyDY=
 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
 github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=

+ 6 - 0
transport/internet/quic/dialer.go

@@ -2,10 +2,13 @@ package quic
 
 import (
 	"context"
+	"io"
 	"sync"
 	"time"
 
 	"github.com/lucas-clemente/quic-go"
+	"github.com/lucas-clemente/quic-go/logging"
+	"github.com/lucas-clemente/quic-go/qlog"
 
 	"github.com/xtls/xray-core/common"
 	"github.com/xtls/xray-core/common/net"
@@ -140,6 +143,9 @@ func (s *clientConnections) openConnection(ctx context.Context, destAddr net.Add
 	quicConfig := &quic.Config{
 		ConnectionIDLength: 12,
 		KeepAlive:          false,
+		Tracer: qlog.NewTracer(func(_ logging.Perspective, connID []byte) io.WriteCloser {
+			return &QlogWriter{connID: connID}
+		}),
 	}
 
 	udpConn, _ := rawConn.(*net.UDPConn)

+ 6 - 0
transport/internet/quic/hub.go

@@ -2,9 +2,12 @@ package quic
 
 import (
 	"context"
+	"io"
 	"time"
 
 	"github.com/lucas-clemente/quic-go"
+	"github.com/lucas-clemente/quic-go/logging"
+	"github.com/lucas-clemente/quic-go/qlog"
 
 	"github.com/xtls/xray-core/common"
 	"github.com/xtls/xray-core/common/net"
@@ -106,6 +109,9 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
 		KeepAlive:             false,
 		MaxIncomingStreams:    32,
 		MaxIncomingUniStreams: -1,
+		Tracer: qlog.NewTracer(func(_ logging.Perspective, connID []byte) io.WriteCloser {
+			return &QlogWriter{connID: connID}
+		}),
 	}
 
 	conn, err := wrapSysConn(rawConn.(*net.UDPConn), config)

+ 26 - 0
transport/internet/quic/qlogWriter.go

@@ -0,0 +1,26 @@
+package quic
+
+import (
+	"fmt"
+
+	"github.com/xtls/xray-core/common/log"
+)
+
+type QlogWriter struct {
+	connID []byte
+}
+
+func (w *QlogWriter) Write(b []byte) (int, error) {
+	if len(b) > 1 { // skip line separator "0a" in qlog
+		log.Record(&log.GeneralMessage{
+			Severity: log.Severity_Debug,
+			Content:  fmt.Sprintf("[%x] %s", w.connID, b),
+		})
+	}
+	return len(b), nil
+}
+
+func (w *QlogWriter) Close() error {
+	// Noop
+	return nil
+}