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-08 03:54:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
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 {
|
|
|
|
peerId, err := dec.DecodeBytes()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.peer = encoding.NewNodeId(peerId)
|
|
|
|
|
|
|
|
connected, err := dec.DecodeBool()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.connected = connected
|
2024-01-08 03:54:43 +00:00
|
|
|
connectionUriVal, err := utils.DecodeMsgpackArray(dec)
|
2024-01-06 17:51:38 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.connectionUris = make([]*url.URL, 0, len(connectionUriVal))
|
|
|
|
connectionUris := interface{}(connectionUriVal).([]string)
|
|
|
|
|
|
|
|
for _, connectionUri := range connectionUris {
|
|
|
|
uri, err := url.Parse(connectionUri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.connectionUris = append(a.connectionUris, uri)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-07 11:35:41 +00:00
|
|
|
func (a AnnouncePeers) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
|
2024-01-06 17:51:38 +00:00
|
|
|
if len(a.connectionUris) > 0 {
|
|
|
|
firstUrl := a.connectionUris[0]
|
|
|
|
uri := new(url.URL)
|
|
|
|
*uri = *firstUrl
|
|
|
|
|
|
|
|
if firstUrl.User != nil {
|
|
|
|
passwd, empty := firstUrl.User.Password()
|
|
|
|
if empty {
|
|
|
|
passwd = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeId, err := a.peer.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uri.User = url.UserPassword(nodeId, passwd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|