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