feat: implement GetIP as a net.Addr

This commit is contained in:
Derrick Hammer 2024-03-10 07:19:49 -04:00
parent 5f5b522e68
commit 4db7430abe
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 15 additions and 3 deletions

View File

@ -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
}

View File

@ -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
}