2024-01-30 03:35:00 +00:00
|
|
|
package protocol
|
2024-01-06 17:51:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
2024-01-08 17:10:41 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-06 17:51:38 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-29 04:39:40 +00:00
|
|
|
_ IncomingMessageSigned = (*AnnouncePeers)(nil)
|
2024-01-06 17:51:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AnnouncePeers struct {
|
2024-01-08 17:10:41 +00:00
|
|
|
peer net.Peer
|
2024-01-06 17:51:38 +00:00
|
|
|
connectionUris []*url.URL
|
2024-01-08 17:10:41 +00:00
|
|
|
peersToSend []net.Peer
|
2024-01-30 03:35:00 +00:00
|
|
|
HandshakeRequirement
|
2024-01-06 17:51:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 17:10:41 +00:00
|
|
|
func (a *AnnouncePeers) PeersToSend() []net.Peer {
|
|
|
|
return a.peersToSend
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AnnouncePeers) SetPeersToSend(peersToSend []net.Peer) {
|
|
|
|
a.peersToSend = peersToSend
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAnnounceRequest(peer net.Peer, peersToSend []net.Peer) *AnnouncePeers {
|
|
|
|
return &AnnouncePeers{peer: peer, connectionUris: nil, peersToSend: peersToSend}
|
|
|
|
}
|
|
|
|
|
2024-01-06 17:51:38 +00:00
|
|
|
func NewAnnouncePeers() *AnnouncePeers {
|
2024-01-13 16:22:01 +00:00
|
|
|
ap := &AnnouncePeers{peer: nil, connectionUris: nil}
|
|
|
|
|
|
|
|
ap.SetRequiresHandshake(false)
|
|
|
|
|
|
|
|
return ap
|
2024-01-06 17:51:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder, message IncomingMessageDataSigned) error {
|
2024-01-09 13:23:35 +00:00
|
|
|
// CIDFromString the number of peers.
|
2024-01-08 04:40:13 +00:00
|
|
|
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++ {
|
2024-01-09 13:23:35 +00:00
|
|
|
// CIDFromString peer ID.
|
2024-01-08 04:40:13 +00:00
|
|
|
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-09 13:23:35 +00:00
|
|
|
// CIDFromString the number of connection URIs for this peer.
|
2024-01-08 04:40:13 +00:00
|
|
|
numUris, err := dec.DecodeInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 17:51:38 +00:00
|
|
|
|
2024-01-09 13:23:35 +00:00
|
|
|
// CIDFromString each connection URI for this peer.
|
2024-01-08 04:40:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
func (a AnnouncePeers) HandleMessage(message IncomingMessageDataSigned) error {
|
2024-01-29 06:55:36 +00:00
|
|
|
services := message.Services
|
2024-01-29 04:39:40 +00:00
|
|
|
peer := message.Peer
|
2024-01-08 04:40:13 +00:00
|
|
|
if len(a.connectionUris) > 0 {
|
2024-01-29 06:55:36 +00:00
|
|
|
err := services.P2P().ConnectToNode([]*url.URL{a.connectionUris[0]}, false, peer)
|
2024-01-08 04:40:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-06 17:51:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-08 17:10:56 +00:00
|
|
|
|
|
|
|
func (a AnnouncePeers) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
err := enc.EncodeUint(uint64(types.ProtocolMethodAnnouncePeers))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the number of peers.
|
|
|
|
err = enc.EncodeInt(int64(len(a.peersToSend)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through each peer.
|
|
|
|
for _, peer := range a.peersToSend {
|
|
|
|
err = enc.EncodeBytes(peer.Id().Raw())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode connection status.
|
|
|
|
err = enc.EncodeBool(peer.IsConnected())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the number of connection URIs for this peer.
|
|
|
|
err = enc.EncodeInt(int64(len(peer.ConnectionURIs())))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode each connection URI for this peer.
|
|
|
|
for _, uri := range peer.ConnectionURIs() {
|
|
|
|
err = enc.EncodeString(uri.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|