Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:(conn): add RemoteAddr and LocalAddr method #490

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/textproto"
"net/url"
"path"
"strconv"
"strings"

"github.com/coder/websocket/internal/errd"
Expand Down Expand Up @@ -150,12 +152,34 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Con
b, _ := brw.Reader.Peek(brw.Reader.Buffered())
brw.Reader.Reset(io.MultiReader(bytes.NewReader(b), netConn))

ipStr, portStr, err := net.SplitHostPort(r.RemoteAddr)
kehiy marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
err = errors.New("*http.Request contains invalid RemoteAddr")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
kehiy marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}

ip := net.ParseIP(ipStr)
if ip == nil {
err = errors.New("*http.Request contains invalid IP address")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return nil, err
}

port, err := strconv.Atoi(portStr)
if err != nil {
err = errors.New("*http.Request contains invalid port number")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return nil, err
}

return newConn(connConfig{
subprotocol: w.Header().Get("Sec-WebSocket-Protocol"),
rwc: netConn,
client: false,
copts: copts,
flateThreshold: opts.CompressionThreshold,
remoteAddr: &net.TCPAddr{IP: ip, Port: port},

br: brw.Reader,
bw: brw.Writer,
Expand Down
8 changes: 8 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Conn struct {
client bool
copts *compressionOptions
flateThreshold int
remoteAddr net.Addr
br *bufio.Reader
bw *bufio.Writer

Expand Down Expand Up @@ -88,6 +89,7 @@ type connConfig struct {
client bool
copts *compressionOptions
flateThreshold int
remoteAddr net.Addr

br *bufio.Reader
bw *bufio.Writer
Expand All @@ -100,6 +102,7 @@ func newConn(cfg connConfig) *Conn {
client: cfg.client,
copts: cfg.copts,
flateThreshold: cfg.flateThreshold,
remoteAddr: cfg.remoteAddr,

br: cfg.br,
bw: cfg.bw,
Expand Down Expand Up @@ -192,6 +195,11 @@ func (c *Conn) flate() bool {
return c.copts != nil
}

// RemoteAddr returns the remote address of websocket connection.
func (c *Conn) RemoteAddr() net.Addr {
return c.remoteAddr
kehiy marked this conversation as resolved.
Show resolved Hide resolved
}

// Ping sends a ping to the peer and waits for a pong.
// Use this to measure latency or ensure the peer is responsive.
// Ping must be called concurrently with Reader as it does
Expand Down
2 changes: 2 additions & 0 deletions internal/examples/echo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer c.CloseNow()

s.logf("new incoming connection: %s", c.RemoteAddr().String())

if c.Subprotocol() != "echo" {
c.Close(websocket.StatusPolicyViolation, "client must speak the echo subprotocol")
return
Expand Down
Loading