From c9df7554263001a01f649f03f574b479f96dcc77 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 23 Apr 2022 19:13:06 -0400 Subject: [PATCH] Add quic qlog to debug logs --- go.mod | 1 + go.sum | 1 + transport/internet/quic/dialer.go | 6 ++++++ transport/internet/quic/hub.go | 6 ++++++ transport/internet/quic/qlogWriter.go | 26 ++++++++++++++++++++++++++ 5 files changed, 40 insertions(+) create mode 100644 transport/internet/quic/qlogWriter.go diff --git a/go.mod b/go.mod index 0cd6b574..f109d302 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 0b9f826a..009d2815 100644 --- a/go.sum +++ b/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= diff --git a/transport/internet/quic/dialer.go b/transport/internet/quic/dialer.go index 1b9a1baa..386952f7 100644 --- a/transport/internet/quic/dialer.go +++ b/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) diff --git a/transport/internet/quic/hub.go b/transport/internet/quic/hub.go index 492070df..8858bfbf 100644 --- a/transport/internet/quic/hub.go +++ b/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) diff --git a/transport/internet/quic/qlogWriter.go b/transport/internet/quic/qlogWriter.go new file mode 100644 index 00000000..1b7913e6 --- /dev/null +++ b/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 +}