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 } } }