From fecf26c12678e046275c4e99fad7f9bcda78fd83 Mon Sep 17 00:00:00 2001
From: photostorm <photostorm@users.noreply.github.com>
Date: Fri, 23 Apr 2021 23:20:27 -0400
Subject: [PATCH] netconn.go: Return real remote and local address where
 possible

---
 conn_test.go     |  4 ++--
 netconn.go       | 17 +++++++----------
 netconn_js.go    | 11 +++++++++++
 netconn_notjs.go | 20 ++++++++++++++++++++
 4 files changed, 40 insertions(+), 12 deletions(-)
 create mode 100644 netconn_js.go
 create mode 100644 netconn_notjs.go

diff --git a/conn_test.go b/conn_test.go
index a3f3d78..b9e2063 100644
--- a/conn_test.go
+++ b/conn_test.go
@@ -155,8 +155,8 @@ func TestConn(t *testing.T) {
 		n1.SetDeadline(time.Time{})
 
 		assert.Equal(t, "remote addr", n1.RemoteAddr(), n1.LocalAddr())
-		assert.Equal(t, "remote addr string", "websocket/unknown-addr", n1.RemoteAddr().String())
-		assert.Equal(t, "remote addr network", "websocket", n1.RemoteAddr().Network())
+		assert.Equal(t, "remote addr string", "pipe", n1.RemoteAddr().String())
+		assert.Equal(t, "remote addr network", "pipe", n1.RemoteAddr().Network())
 
 		errs := xsync.Go(func() error {
 			_, err := n2.Write([]byte("hello"))
diff --git a/netconn.go b/netconn.go
index 4af6c20..74000c9 100644
--- a/netconn.go
+++ b/netconn.go
@@ -33,8 +33,13 @@ import (
 // where only the reading/writing goroutines are interrupted but the connection
 // is kept alive.
 //
-// The Addr methods will return a mock net.Addr that returns "websocket" for Network
-// and "websocket/unknown-addr" for String.
+// The Addr methods will return the real addresses for connections obtained
+// from websocket.Accept. But for connections obtained from websocket.Dial, a mock net.Addr
+// will be returned that gives "websocket" for Network() and "websocket/unknown-addr" for
+// String(). This is because websocket.Dial only exposes a io.ReadWriteCloser instead of the
+// full net.Conn to us.
+//
+// When running as WASM, the Addr methods will always return the mock address described above.
 //
 // A received StatusNormalClosure or StatusGoingAway close frame will be translated to
 // io.EOF when reading.
@@ -181,14 +186,6 @@ func (a websocketAddr) String() string {
 	return "websocket/unknown-addr"
 }
 
-func (nc *netConn) RemoteAddr() net.Addr {
-	return websocketAddr{}
-}
-
-func (nc *netConn) LocalAddr() net.Addr {
-	return websocketAddr{}
-}
-
 func (nc *netConn) SetDeadline(t time.Time) error {
 	nc.SetWriteDeadline(t)
 	nc.SetReadDeadline(t)
diff --git a/netconn_js.go b/netconn_js.go
new file mode 100644
index 0000000..ccc8c89
--- /dev/null
+++ b/netconn_js.go
@@ -0,0 +1,11 @@
+package websocket
+
+import "net"
+
+func (nc *netConn) RemoteAddr() net.Addr {
+	return websocketAddr{}
+}
+
+func (nc *netConn) LocalAddr() net.Addr {
+	return websocketAddr{}
+}
diff --git a/netconn_notjs.go b/netconn_notjs.go
new file mode 100644
index 0000000..f3eb0d6
--- /dev/null
+++ b/netconn_notjs.go
@@ -0,0 +1,20 @@
+//go:build !js
+// +build !js
+
+package websocket
+
+import "net"
+
+func (nc *netConn) RemoteAddr() net.Addr {
+	if unc, ok := nc.c.rwc.(net.Conn); ok {
+		return unc.RemoteAddr()
+	}
+	return websocketAddr{}
+}
+
+func (nc *netConn) LocalAddr() net.Addr {
+	if unc, ok := nc.c.rwc.(net.Conn); ok {
+		return unc.LocalAddr()
+	}
+	return websocketAddr{}
+}
-- 
GitLab