From 944067522a535b0db744f8c8d4f43e49e09a9121 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 15 Jan 2024 13:36:13 -0500 Subject: [PATCH] feat: add abused to peer so we can know when a peer has abused us and not log errors for them --- net/peer.go | 9 +++++++++ net/ws.go | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/net/peer.go b/net/peer.go index eb19240..3173508 100644 --- a/net/peer.go +++ b/net/peer.go @@ -33,6 +33,7 @@ type Peer interface { RenderLocationURI() string ListenForMessages(callback EventCallback, options ListenerOptions) End() error + EndForAbuse() error SetId(id *encoding.NodeId) Id() *encoding.NodeId SetChallenge(challenge []byte) @@ -46,6 +47,7 @@ type Peer interface { IsHandshakeDone() bool SetHandshakeDone(status bool) GetIP() string + Abused() bool } type BasePeer struct { @@ -80,6 +82,9 @@ func (b *BasePeer) ListenForMessages(callback EventCallback, options ListenerOpt func (b *BasePeer) End() error { panic("must implement in child class") } +func (b *BasePeer) EndForAbuse() error { + panic("must implement in child class") +} func (b *BasePeer) GetIP() string { panic("must implement in child class") } @@ -121,3 +126,7 @@ func (b *BasePeer) IsHandshakeDone() bool { func (b *BasePeer) SetHandshakeDone(status bool) { b.handshaked = status } + +func (b *BasePeer) Abused() bool { + panic("must implement in child class") +} diff --git a/net/ws.go b/net/ws.go index 5ee4943..ace8c6f 100644 --- a/net/ws.go +++ b/net/ws.go @@ -16,6 +16,7 @@ var ( type WebSocketPeer struct { BasePeer socket *websocket.Conn + abused bool } func (p *WebSocketPeer) Connect(uri *url.URL) (interface{}, error) { @@ -103,7 +104,16 @@ func (p *WebSocketPeer) End() error { 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) { p.id = id } @@ -126,3 +136,6 @@ func (b *WebSocketPeer) GetIP() string { return ipAddr } +func (p *WebSocketPeer) Abused() bool { + return p.abused +}