2024-01-06 17:51:38 +00:00
|
|
|
package signed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-07 08:58:22 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
2024-01-06 17:51:38 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
2024-01-07 10:12:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
2024-01-06 17:51:38 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-07 10:12:43 +00:00
|
|
|
_ base.IncomingMessageTyped = (*AnnouncePeers)(nil)
|
2024-01-06 17:51:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AnnouncePeers struct {
|
|
|
|
connected bool
|
|
|
|
peer *encoding.NodeId
|
|
|
|
connectionUris []*url.URL
|
2024-01-07 10:12:43 +00:00
|
|
|
base.IncomingMessageTypedImpl
|
2024-01-06 17:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAnnouncePeers() *AnnouncePeers {
|
|
|
|
return &AnnouncePeers{connected: false, peer: nil, connectionUris: nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder) error {
|
2024-01-08 04:40:13 +00:00
|
|
|
// Decode the number of peers.
|
|
|
|
numPeers, err := dec.DecodeInt()
|
2024-01-06 17:51:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-08 04:40:13 +00:00
|
|
|
// Initialize the slice for storing connection URIs.
|
|
|
|
var connectionURIs []*url.URL
|
2024-01-06 17:51:38 +00:00
|
|
|
|
2024-01-08 04:40:13 +00:00
|
|
|
// Loop through each peer.
|
|
|
|
for i := 0; i < numPeers; i++ {
|
|
|
|
// Decode peer ID.
|
|
|
|
peerIdBytes, err := dec.DecodeBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
peerId := encoding.NewNodeId(peerIdBytes)
|
2024-01-06 17:51:38 +00:00
|
|
|
|
2024-01-08 04:40:13 +00:00
|
|
|
// Skip decoding connection status as it is not used.
|
|
|
|
_, err = dec.DecodeBool() // Connection status, not used.
|
2024-01-06 17:51:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-08 04:40:13 +00:00
|
|
|
// Decode the number of connection URIs for this peer.
|
|
|
|
numUris, err := dec.DecodeInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 17:51:38 +00:00
|
|
|
|
2024-01-08 04:40:13 +00:00
|
|
|
// Decode each connection URI for this peer.
|
|
|
|
for j := 0; j < numUris; j++ {
|
|
|
|
uriStr, err := dec.DecodeString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 17:51:38 +00:00
|
|
|
|
2024-01-08 04:40:13 +00:00
|
|
|
uri, err := url.Parse(uriStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-06 17:51:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 04:40:13 +00:00
|
|
|
pid, err := peerId.ToString()
|
2024-01-06 17:51:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-08 04:40:13 +00:00
|
|
|
passwd, empty := uri.User.Password()
|
|
|
|
if empty {
|
|
|
|
passwd = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Incorporate the peer ID into the URI.
|
|
|
|
uri.User = url.UserPassword(pid, passwd)
|
|
|
|
|
|
|
|
connectionURIs = append(connectionURIs, uri)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a.connectionUris = connectionURIs
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a AnnouncePeers) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
|
|
|
|
if len(a.connectionUris) > 0 {
|
|
|
|
err := node.Services().P2P().ConnectToNode([]*url.URL{a.connectionUris[0]}, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-06 17:51:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|