diff --git a/prefix.go b/prefix.go new file mode 100644 index 0000000..3bf471a --- /dev/null +++ b/prefix.go @@ -0,0 +1,29 @@ +package tcpproxy + +import ( + "bufio" + "bytes" +) + +func (p *Proxy) AddPrefixRoute(ipPort string, prefix []byte, dest Target) { + p.addRoute(ipPort, prefixMatch{prefix: prefix, target: dest}) +} + +type prefixMatch struct { + prefix []byte + target Target +} + +func (p prefixMatch) match(br *bufio.Reader) (Target, string) { + if len(p.prefix) == 0 { + return nil, "" + } + b, err := br.Peek(len(p.prefix)) + if err != nil { + return nil, "" + } + if bytes.Equal(b, p.prefix) { + return p.target, "" + } + return nil, "" +}