Add RemoteAddr to ircConn interface

This commit is contained in:
Simon Ser 2020-07-01 17:02:37 +02:00
parent dc59263681
commit 3397965dea
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 24 additions and 7 deletions

20
conn.go
View File

@ -20,6 +20,7 @@ type ircConn interface {
Close() error
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
RemoteAddr() net.Addr
}
func newNetIRCConn(c net.Conn) ircConn {
@ -33,10 +34,11 @@ func newNetIRCConn(c net.Conn) ircConn {
type websocketIRCConn struct {
conn *websocket.Conn
readDeadline, writeDeadline time.Time
remoteAddr string
}
func newWebsocketIRCConn(c *websocket.Conn) ircConn {
return websocketIRCConn{conn: c}
func newWebsocketIRCConn(c *websocket.Conn, remoteAddr string) ircConn {
return websocketIRCConn{conn: c, remoteAddr: remoteAddr}
}
func (wic websocketIRCConn) ReadMessage() (*irc.Message, error) {
@ -83,6 +85,20 @@ func (wic websocketIRCConn) SetWriteDeadline(t time.Time) error {
return nil
}
func (wic websocketIRCConn) RemoteAddr() net.Addr {
return websocketAddr(wic.remoteAddr)
}
type websocketAddr string
func (websocketAddr) Network() string {
return "ws"
}
func (wa websocketAddr) String() string {
return string(wa)
}
type conn struct {
conn ircConn
srv *Server

View File

@ -99,7 +99,8 @@ type downstreamConn struct {
saslServer sasl.Server
}
func newDownstreamConn(srv *Server, ic ircConn, remoteAddr string, id uint64) *downstreamConn {
func newDownstreamConn(srv *Server, ic ircConn, id uint64) *downstreamConn {
remoteAddr := ic.RemoteAddr().String()
logger := &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", remoteAddr)}
dc := &downstreamConn{
conn: *newConn(srv, ic, logger),

View File

@ -117,9 +117,9 @@ func (s *Server) getUser(name string) *user {
var lastDownstreamID uint64 = 0
func (s *Server) handle(ic ircConn, remoteAddr string) {
func (s *Server) handle(ic ircConn) {
id := atomic.AddUint64(&lastDownstreamID, 1)
dc := newDownstreamConn(s, ic, remoteAddr, id)
dc := newDownstreamConn(s, ic, id)
if err := dc.runUntilRegistered(); err != nil {
dc.logger.Print(err)
} else {
@ -139,7 +139,7 @@ func (s *Server) Serve(ln net.Listener) error {
return fmt.Errorf("failed to accept connection: %v", err)
}
go s.handle(newNetIRCConn(conn), conn.RemoteAddr().String())
go s.handle(newNetIRCConn(conn))
}
}
@ -168,5 +168,5 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
remoteAddr = net.JoinHostPort(forwardedHost, forwardedPort)
}
s.handle(newWebsocketIRCConn(conn), remoteAddr)
s.handle(newWebsocketIRCConn(conn, remoteAddr))
}