diff --git a/go.mod b/go.mod index 87a6948..486220f 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,14 @@ go 1.18 require ( github.com/fatih/color v1.13.0 - github.com/grpc/grpc/tools/http2_interop v0.0.0-20220615001509-d0751191a921 github.com/olekukonko/tablewriter v0.0.5 + golang.org/x/net v0.0.0-20220728211354-c7608f3a8462 ) require ( github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect - golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect + golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect + golang.org/x/text v0.3.7 // indirect ) diff --git a/go.sum b/go.sum index 4c36bc5..6d2ad7e 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= -github.com/grpc/grpc/tools/http2_interop v0.0.0-20220615001509-d0751191a921 h1:zTONUZ32JlNtzbzxOEcAhV3vMwXX0bGkS803DYf4RBM= -github.com/grpc/grpc/tools/http2_interop v0.0.0-20220615001509-d0751191a921/go.mod h1:BeiyA4wp8QEh/jcnxJlpfalCT/XiQ5bbFhikEDrXQcw= github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= @@ -11,7 +9,12 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= +golang.org/x/net v0.0.0-20220728211354-c7608f3a8462 h1:UreQrH7DbFXSi9ZFox6FNT3WBooWmdANpU+IfkT1T4I= +golang.org/x/net v0.0.0-20220728211354-c7608f3a8462/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= diff --git a/protocol/grpc.go b/protocol/grpc.go index 46b59d9..a39c0d4 100644 --- a/protocol/grpc.go +++ b/protocol/grpc.go @@ -1,15 +1,15 @@ package protocol import ( - "encoding/binary" + "bytes" "encoding/hex" "fmt" "io" "strings" "github.com/fatih/color" - "github.com/grpc/grpc/tools/http2_interop" "github.com/kevwan/tproxy/display" + "golang.org/x/net/http2" ) const ( @@ -59,22 +59,35 @@ func (i *GrpcInterop) explain(b []byte) (string, int) { return "", len(b) } - var frame http2interop.FrameHeader - // ignore errors - if err := frame.UnmarshalBinary(b[:http2HeaderLen]); err != nil { + frame, err := http2.ReadFrameHeader(bytes.NewReader(b[:http2HeaderLen])) + if err != nil { return "", len(b) } - if frame.Type == http2interop.FrameType(priFrameType) { - return "http2:pri", len(b) + frameLen := http2HeaderLen + int(frame.Length) + switch frame.Type { + case priFrameType: + return "http2:preface", frameLen + case http2.FrameSettings: + switch frame.Flags { + case http2.FlagSettingsAck: + return "http2:settings:ack", frameLen + default: + return "http2:settings", frameLen + } + case http2.FramePing: + switch frame.Flags { + case http2.FlagPingAck: + return "http2:ping:ack", frameLen + default: + return "http2:ping", frameLen + } } - payloadLen := binary.BigEndian.Uint32(append([]byte{0}, b[:3]...)) - id := binary.BigEndian.Uint32(b[5:http2HeaderLen]) & 0x7fffffff - if id > 0 { - return fmt.Sprintf("http2:%s stream:%d", strings.ToLower(frame.Type.String()), id), - http2HeaderLen + int(payloadLen) + if frame.StreamID > 0 { + desc := fmt.Sprintf("http2:%s stream:%d", strings.ToLower(frame.Type.String()), frame.StreamID) + return desc, frameLen } - return "http2:" + strings.ToLower(frame.Type.String()), http2HeaderLen + int(payloadLen) + return "http2:" + strings.ToLower(frame.Type.String()), frameLen } diff --git a/protocol/dump.go b/protocol/interop.go similarity index 81% rename from protocol/dump.go rename to protocol/interop.go index 0907398..e4defb0 100644 --- a/protocol/dump.go +++ b/protocol/interop.go @@ -13,7 +13,7 @@ const ( grpcProtocol = "grpc" ) -var defaultDumper Dumper +var interop defaultInterop type Interop interface { Dump(r io.Reader, source string, id int, quiet bool) @@ -24,13 +24,13 @@ func CreateInterop(protocol string) Interop { case grpcProtocol: return new(GrpcInterop) default: - return defaultDumper + return interop } } -type Dumper struct{} +type defaultInterop struct{} -func (d Dumper) Dump(r io.Reader, source string, id int, quiet bool) { +func (d defaultInterop) Dump(r io.Reader, source string, id int, quiet bool) { data := make([]byte, bufferSize) for { n, err := r.Read(data)