libs5-go/protocol/signed/accounce_peers.go

87 lines
1.7 KiB
Go
Raw Normal View History

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"
"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 (
_ base.IncomingMessageTyped = (*AnnouncePeers)(nil)
2024-01-06 17:51:38 +00:00
)
type AnnouncePeers struct {
connected bool
peer *encoding.NodeId
connectionUris []*url.URL
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
}
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
}