2024-01-06 11:33:46 +00:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2024-01-07 11:35:41 +00:00
|
|
|
var (
|
|
|
|
_ Peer = (*BasePeer)(nil)
|
|
|
|
)
|
|
|
|
|
2024-01-06 11:33:46 +00:00
|
|
|
// EventCallback type for the callback function
|
|
|
|
type EventCallback func(event []byte) error
|
|
|
|
|
2024-01-07 11:33:32 +00:00
|
|
|
// CloseCallback type for the OnClose callback
|
|
|
|
type CloseCallback func()
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
// ErrorCallback type for the onError callback
|
|
|
|
type ErrorCallback func(args ...interface{})
|
|
|
|
|
|
|
|
// ListenerOptions struct for options
|
|
|
|
type ListenerOptions struct {
|
2024-01-07 11:33:32 +00:00
|
|
|
OnClose *CloseCallback
|
2024-01-06 11:33:46 +00:00
|
|
|
OnError *ErrorCallback
|
|
|
|
Logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type Peer interface {
|
|
|
|
SendMessage(message []byte) error
|
|
|
|
RenderLocationURI() string
|
|
|
|
ListenForMessages(callback EventCallback, options ListenerOptions)
|
|
|
|
End() error
|
2024-01-15 18:36:13 +00:00
|
|
|
EndForAbuse() error
|
2024-01-06 11:33:46 +00:00
|
|
|
SetId(id *encoding.NodeId)
|
2024-01-07 11:33:32 +00:00
|
|
|
Id() *encoding.NodeId
|
2024-01-06 11:33:46 +00:00
|
|
|
SetChallenge(challenge []byte)
|
2024-01-07 11:33:32 +00:00
|
|
|
Challenge() []byte
|
|
|
|
SetSocket(socket interface{})
|
|
|
|
Socket() interface{}
|
2024-01-08 13:42:53 +00:00
|
|
|
SetConnected(isConnected bool)
|
|
|
|
IsConnected() bool
|
2024-01-08 14:52:57 +00:00
|
|
|
SetConnectionURIs(uris []*url.URL)
|
|
|
|
ConnectionURIs() []*url.URL
|
2024-01-13 16:22:01 +00:00
|
|
|
IsHandshakeDone() bool
|
|
|
|
SetHandshakeDone(status bool)
|
2024-01-15 15:54:31 +00:00
|
|
|
GetIP() string
|
2024-01-15 18:50:04 +00:00
|
|
|
Abuser() bool
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BasePeer struct {
|
2024-01-07 11:33:32 +00:00
|
|
|
connectionURIs []*url.URL
|
|
|
|
isConnected bool
|
2024-01-06 11:33:46 +00:00
|
|
|
challenge []byte
|
2024-01-07 11:33:32 +00:00
|
|
|
socket interface{}
|
|
|
|
id *encoding.NodeId
|
2024-01-13 16:22:01 +00:00
|
|
|
handshaked bool
|
2024-01-07 11:33:32 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 13:42:53 +00:00
|
|
|
func (b *BasePeer) IsConnected() bool {
|
|
|
|
return b.isConnected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) SetConnected(isConnected bool) {
|
|
|
|
b.isConnected = isConnected
|
|
|
|
}
|
|
|
|
|
2024-01-07 11:35:41 +00:00
|
|
|
func (b *BasePeer) SendMessage(message []byte) error {
|
|
|
|
panic("must implement in child class")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) RenderLocationURI() string {
|
|
|
|
panic("must implement in child class")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) ListenForMessages(callback EventCallback, options ListenerOptions) {
|
|
|
|
panic("must implement in child class")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) End() error {
|
|
|
|
panic("must implement in child class")
|
|
|
|
}
|
2024-01-15 18:36:13 +00:00
|
|
|
func (b *BasePeer) EndForAbuse() error {
|
|
|
|
panic("must implement in child class")
|
|
|
|
}
|
2024-01-15 15:54:31 +00:00
|
|
|
func (b *BasePeer) GetIP() string {
|
|
|
|
panic("must implement in child class")
|
|
|
|
}
|
2024-01-07 11:35:41 +00:00
|
|
|
|
2024-01-07 11:33:32 +00:00
|
|
|
func (b *BasePeer) Challenge() []byte {
|
|
|
|
return b.challenge
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) SetChallenge(challenge []byte) {
|
|
|
|
b.challenge = challenge
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) Socket() interface{} {
|
|
|
|
return b.socket
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) SetSocket(socket interface{}) {
|
|
|
|
b.socket = socket
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) Id() *encoding.NodeId {
|
|
|
|
return b.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) SetId(id *encoding.NodeId) {
|
|
|
|
b.id = id
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
2024-01-08 14:52:57 +00:00
|
|
|
func (b *BasePeer) SetConnectionURIs(uris []*url.URL) {
|
|
|
|
b.connectionURIs = uris
|
|
|
|
}
|
|
|
|
func (b *BasePeer) ConnectionURIs() []*url.URL {
|
|
|
|
return b.connectionURIs
|
|
|
|
}
|
2024-01-13 16:22:01 +00:00
|
|
|
|
|
|
|
func (b *BasePeer) IsHandshakeDone() bool {
|
|
|
|
return b.handshaked
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePeer) SetHandshakeDone(status bool) {
|
|
|
|
b.handshaked = status
|
|
|
|
}
|
2024-01-15 18:36:13 +00:00
|
|
|
|
2024-01-15 18:50:04 +00:00
|
|
|
func (b *BasePeer) Abuser() bool {
|
2024-01-15 18:36:13 +00:00
|
|
|
panic("must implement in child class")
|
|
|
|
}
|