Skip to content

Commit fecf26c

Browse files
photostormnhooyr
authored andcommittedOct 13, 2023
netconn.go: Return real remote and local address where possible
1 parent 9873274 commit fecf26c

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed
 

‎conn_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ func TestConn(t *testing.T) {
155155
n1.SetDeadline(time.Time{})
156156

157157
assert.Equal(t, "remote addr", n1.RemoteAddr(), n1.LocalAddr())
158-
assert.Equal(t, "remote addr string", "websocket/unknown-addr", n1.RemoteAddr().String())
159-
assert.Equal(t, "remote addr network", "websocket", n1.RemoteAddr().Network())
158+
assert.Equal(t, "remote addr string", "pipe", n1.RemoteAddr().String())
159+
assert.Equal(t, "remote addr network", "pipe", n1.RemoteAddr().Network())
160160

161161
errs := xsync.Go(func() error {
162162
_, err := n2.Write([]byte("hello"))

‎netconn.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ import (
3333
// where only the reading/writing goroutines are interrupted but the connection
3434
// is kept alive.
3535
//
36-
// The Addr methods will return a mock net.Addr that returns "websocket" for Network
37-
// and "websocket/unknown-addr" for String.
36+
// The Addr methods will return the real addresses for connections obtained
37+
// from websocket.Accept. But for connections obtained from websocket.Dial, a mock net.Addr
38+
// will be returned that gives "websocket" for Network() and "websocket/unknown-addr" for
39+
// String(). This is because websocket.Dial only exposes a io.ReadWriteCloser instead of the
40+
// full net.Conn to us.
41+
//
42+
// When running as WASM, the Addr methods will always return the mock address described above.
3843
//
3944
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
4045
// io.EOF when reading.
@@ -181,14 +186,6 @@ func (a websocketAddr) String() string {
181186
return "websocket/unknown-addr"
182187
}
183188

184-
func (nc *netConn) RemoteAddr() net.Addr {
185-
return websocketAddr{}
186-
}
187-
188-
func (nc *netConn) LocalAddr() net.Addr {
189-
return websocketAddr{}
190-
}
191-
192189
func (nc *netConn) SetDeadline(t time.Time) error {
193190
nc.SetWriteDeadline(t)
194191
nc.SetReadDeadline(t)

‎netconn_js.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package websocket
2+
3+
import "net"
4+
5+
func (nc *netConn) RemoteAddr() net.Addr {
6+
return websocketAddr{}
7+
}
8+
9+
func (nc *netConn) LocalAddr() net.Addr {
10+
return websocketAddr{}
11+
}

‎netconn_notjs.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build !js
2+
// +build !js
3+
4+
package websocket
5+
6+
import "net"
7+
8+
func (nc *netConn) RemoteAddr() net.Addr {
9+
if unc, ok := nc.c.rwc.(net.Conn); ok {
10+
return unc.RemoteAddr()
11+
}
12+
return websocketAddr{}
13+
}
14+
15+
func (nc *netConn) LocalAddr() net.Addr {
16+
if unc, ok := nc.c.rwc.(net.Conn); ok {
17+
return unc.LocalAddr()
18+
}
19+
return websocketAddr{}
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.