feat: implement GetIP as a net.Addr
This commit is contained in:
parent
5f5b522e68
commit
4db7430abe
|
@ -3,6 +3,7 @@ package net
|
|||
import (
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||
"go.uber.org/zap"
|
||||
"net"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
|
@ -45,6 +46,7 @@ type Peer interface {
|
|||
IsHandshakeDone() bool
|
||||
SetHandshakeDone(status bool)
|
||||
GetIPString() string
|
||||
GetIP() net.Addr
|
||||
Abuser() bool
|
||||
}
|
||||
|
||||
|
@ -87,6 +89,10 @@ func (b *BasePeer) GetIPString() string {
|
|||
panic("must implement in child class")
|
||||
}
|
||||
|
||||
func (b *BasePeer) GetIP() net.Addr {
|
||||
panic("must implement in child class")
|
||||
}
|
||||
|
||||
func (b *BasePeer) Challenge() []byte {
|
||||
return b.challenge
|
||||
}
|
||||
|
|
12
net/ws.go
12
net/ws.go
|
@ -3,6 +3,7 @@ package net
|
|||
import (
|
||||
"context"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||
"net"
|
||||
"net/url"
|
||||
"nhooyr.io/websocket"
|
||||
"sync"
|
||||
|
@ -137,16 +138,21 @@ func (p *WebSocketPeer) GetChallenge() []byte {
|
|||
return p.challenge
|
||||
}
|
||||
|
||||
func (b *WebSocketPeer) GetIPString() string {
|
||||
func (p *WebSocketPeer) GetIP() net.Addr {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
netConn := websocket.NetConn(ctx, b.socket, websocket.MessageBinary)
|
||||
netConn := websocket.NetConn(ctx, p.socket, websocket.MessageBinary)
|
||||
|
||||
ipAddr := netConn.RemoteAddr().String()
|
||||
ipAddr := netConn.RemoteAddr()
|
||||
|
||||
cancel()
|
||||
|
||||
return ipAddr
|
||||
}
|
||||
|
||||
func (b *WebSocketPeer) GetIPString() string {
|
||||
return b.GetIP().String()
|
||||
}
|
||||
|
||||
func (p *WebSocketPeer) Abuser() bool {
|
||||
return p.abuser
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue