Repository: pires/go-proxyproto (go)

Issue:
Export underlying connection
Hi,

It would be good to export the underlying `proxyproto.Conn.conn` connection. 
Since proxyproto.Conn is not an `net.TCPConn`, and I need to cast it. 



Pull request description:
exporting underlying Connection
this blocks implementing the listener in traefik

https://github.com/traefik/traefik/pull/7320

If you accept it, please create a new version tag, thank you!

closes #50 

Patch:
diff --git a/protocol.go b/protocol.go
index a686e8e..dea810d 100644
--- a/protocol.go
+++ b/protocol.go
@@ -147,6 +147,24 @@ func (p *Conn) RemoteAddr() net.Addr {
 	return p.header.SourceAddr
 }
 
+// TCPConn returns is the underlying connection as TCPConn
+func (p *Conn) TCPConn() (conn *net.TCPConn, ok bool) {
+	conn, ok = p.conn.(*net.TCPConn)
+	return
+}
+
+// UnixConn returns is the underlying connection as UnixConn
+func (p *Conn) UnixConn() (conn *net.UnixConn, ok bool) {
+	conn, ok = p.conn.(*net.UnixConn)
+	return
+}
+
+// UDPConn returns is the underlying connection as UDPConn
+func (p *Conn) UDPConn() (conn *net.UDPConn, ok bool) {
+	conn, ok = p.conn.(*net.UDPConn)
+	return
+}
+
 // SetDeadline wraps original conn.SetDeadline
 func (p *Conn) SetDeadline(t time.Time) error {
 	return p.conn.SetDeadline(t)


Test patch:
diff --git a/protocol_test.go b/protocol_test.go
index 025b485..429cee7 100644
--- a/protocol_test.go
+++ b/protocol_test.go
@@ -470,6 +470,46 @@ func TestReadingIsRefusedOnErrorWhenLocalAddrRequestedFirst(t *testing.T) {
 	}
 }
 
+func Test_ConnectionCasts(t *testing.T) {
+	l, err := net.Listen("tcp", "127.0.0.1:0")
+	if err != nil {
+		t.Fatalf("err: %v", err)
+	}
+
+	policyFunc := func(upstream net.Addr) (Policy, error) { return REQUIRE, nil }
+
+	pl := &Listener{Listener: l, Policy: policyFunc}
+
+	go func() {
+		conn, err := net.Dial("tcp", pl.Addr().String())
+		if err != nil {
+			t.Fatalf("err: %v", err)
+		}
+		defer conn.Close()
+		conn.Write([]byte("ping"))
+	}()
+
+	conn, err := pl.Accept()
+	if err != nil {
+		t.Fatalf("err: %v", err)
+	}
+	defer conn.Close()
+
+	proxyprotoConn := conn.(*Conn)
+	_, ok := proxyprotoConn.TCPConn()
+	if !ok {
+		t.Fatal("err: should be a tcp connection")
+	}
+	_, ok = proxyprotoConn.UDPConn()
+	if ok {
+		t.Fatal("err: should be a tcp connection not udp")
+	}
+	_, ok = proxyprotoConn.UnixConn()
+	if ok {
+		t.Fatal("err: should be a tcp connection not unix")
+	}
+}
+
 func Test_ConnectionErrorsWhenHeaderValidationFails(t *testing.T) {
 	l, err := net.Listen("tcp", "127.0.0.1:0")
 	if err != nil {