feat: add abused to peer so we can know when a peer has abused us and not log errors for them

This commit is contained in:
Derrick Hammer 2024-01-15 13:36:13 -05:00
parent 5e80831335
commit 944067522a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 22 additions and 0 deletions

View File

@ -33,6 +33,7 @@ type Peer interface {
RenderLocationURI() string RenderLocationURI() string
ListenForMessages(callback EventCallback, options ListenerOptions) ListenForMessages(callback EventCallback, options ListenerOptions)
End() error End() error
EndForAbuse() error
SetId(id *encoding.NodeId) SetId(id *encoding.NodeId)
Id() *encoding.NodeId Id() *encoding.NodeId
SetChallenge(challenge []byte) SetChallenge(challenge []byte)
@ -46,6 +47,7 @@ type Peer interface {
IsHandshakeDone() bool IsHandshakeDone() bool
SetHandshakeDone(status bool) SetHandshakeDone(status bool)
GetIP() string GetIP() string
Abused() bool
} }
type BasePeer struct { type BasePeer struct {
@ -80,6 +82,9 @@ func (b *BasePeer) ListenForMessages(callback EventCallback, options ListenerOpt
func (b *BasePeer) End() error { func (b *BasePeer) End() error {
panic("must implement in child class") panic("must implement in child class")
} }
func (b *BasePeer) EndForAbuse() error {
panic("must implement in child class")
}
func (b *BasePeer) GetIP() string { func (b *BasePeer) GetIP() string {
panic("must implement in child class") panic("must implement in child class")
} }
@ -121,3 +126,7 @@ func (b *BasePeer) IsHandshakeDone() bool {
func (b *BasePeer) SetHandshakeDone(status bool) { func (b *BasePeer) SetHandshakeDone(status bool) {
b.handshaked = status b.handshaked = status
} }
func (b *BasePeer) Abused() bool {
panic("must implement in child class")
}

View File

@ -16,6 +16,7 @@ var (
type WebSocketPeer struct { type WebSocketPeer struct {
BasePeer BasePeer
socket *websocket.Conn socket *websocket.Conn
abused bool
} }
func (p *WebSocketPeer) Connect(uri *url.URL) (interface{}, error) { func (p *WebSocketPeer) Connect(uri *url.URL) (interface{}, error) {
@ -103,7 +104,16 @@ func (p *WebSocketPeer) End() error {
return nil return nil
} }
func (p *WebSocketPeer) EndForAbuse() error {
err := p.socket.Close(websocket.StatusPolicyViolation, "")
if err != nil {
return err
}
p.abused = true
return nil
}
func (p *WebSocketPeer) SetId(id *encoding.NodeId) { func (p *WebSocketPeer) SetId(id *encoding.NodeId) {
p.id = id p.id = id
} }
@ -126,3 +136,6 @@ func (b *WebSocketPeer) GetIP() string {
return ipAddr return ipAddr
} }
func (p *WebSocketPeer) Abused() bool {
return p.abused
}