I am building a TCP proxy with go
, but I am facing a small problem. Before actually handling the connection c1
and forwarding it to c2
, I want to do some checks. To do this, I need the string representation of the byte slice from c1
. Unfortunately, _, err := io.CopyBuffer(w, r, buf)
is copying the []byte
directly between the writer and reader, and if I do c1.Read()
before the cp
function, the []byte
have already been read.
Here is the function containing the connection handling:
func (p *proxy) handle(c1 net.Conn) {
p.log.Printf("accepted %v", c1.RemoteAddr())
defer p.log.Printf("disconnected %v", c1.RemoteAddr())
defer c1.Close()
c2, err := dialer.Dial("tcp", p.dial)
log.Println("DIAL:", p.dial)
if err != nil {
p.log.Print("C2", err)
return
}
defer c2.Close()
errc := make(chan error, 2)
cp := func(w io.Writer, r io.Reader) {
buf := bufferPool.Get().([]byte)
_, err := io.CopyBuffer(w, r, buf)
errc <- err
bufferPool.Put(buf)
}
go cp(struct{ io.Writer }{c1}, c2)
go cp(c2, struct{ io.Reader }{c1})
err = <-errc
if err != nil {
p.log.Print("F-ERROR ->", err)
}
}
Is there a way to "duplicate" the []byte
so that I can use the duplicate to display as a string?