mirror of
https://github.com/kevwan/tproxy.git
synced 2026-05-23 07:40:35 +00:00
9ec58b02f9
- Add protocol field to http2Interop struct to track actual protocol - Use stored protocol name instead of hardcoded 'grpc' in output - Now http2 mode shows 'http2:' prefix and grpc mode shows 'grpc:' prefix - Move grpcHeaderLen constant to grpc.go to avoid duplication
76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
package protocol
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/kevwan/tproxy/display"
|
|
)
|
|
|
|
const (
|
|
ServerSide = "SERVER"
|
|
ClientSide = "CLIENT"
|
|
|
|
bufferSize = 1 << 20
|
|
grpcProtocol = "grpc"
|
|
http2Protocol = "http2"
|
|
redisProtocol = "redis"
|
|
mongoProtocol = "mongo"
|
|
mqttProtocol = "mqtt"
|
|
mysqlProtocol = "mysql"
|
|
textProtocol = "text"
|
|
)
|
|
|
|
var interop defaultInterop
|
|
|
|
type Interop interface {
|
|
Dump(r io.Reader, source string, id int, quiet bool)
|
|
}
|
|
|
|
func CreateInterop(protocol string) Interop {
|
|
switch protocol {
|
|
case textProtocol:
|
|
return new(textInterop)
|
|
case grpcProtocol:
|
|
return &http2Interop{
|
|
explainer: new(grpcExplainer),
|
|
protocol: grpcProtocol,
|
|
}
|
|
case http2Protocol:
|
|
return &http2Interop{
|
|
protocol: http2Protocol,
|
|
}
|
|
case redisProtocol:
|
|
return new(redisInterop)
|
|
case mongoProtocol:
|
|
return new(mongoInterop)
|
|
case mqttProtocol:
|
|
return new(mqttInterop)
|
|
case mysqlProtocol:
|
|
return new(mysqlInterop)
|
|
default:
|
|
return interop
|
|
}
|
|
}
|
|
|
|
type defaultInterop struct{}
|
|
|
|
func (d defaultInterop) Dump(r io.Reader, source string, id int, quiet bool) {
|
|
data := make([]byte, bufferSize)
|
|
for {
|
|
n, err := r.Read(data)
|
|
if n > 0 && !quiet {
|
|
display.PrintfWithTime("from %s [%d]:\n", source, id)
|
|
fmt.Println(hex.Dump(data[:n]))
|
|
}
|
|
if err != nil && err != io.EOF {
|
|
fmt.Printf("unable to read data %v", err)
|
|
break
|
|
}
|
|
if n == 0 {
|
|
break
|
|
}
|
|
}
|
|
}
|