feat: implement AnnouncePeers

This commit is contained in:
Derrick Hammer 2024-01-06 12:51:38 -05:00
parent 16ce7338bd
commit 8742a4139b
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,85 @@
package signed
import (
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
"github.com/vmihailenco/msgpack/v5"
"net/url"
)
var (
_ protocol.IncomingMessageTyped = (*AnnouncePeers)(nil)
)
type AnnouncePeers struct {
connected bool
peer *encoding.NodeId
connectionUris []*url.URL
protocol.IncomingMessageTypedImpl
}
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
connectionUriVal, err := dec.DecodeSlice()
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 *libs5_go.Node, peer *net.Peer, verifyId bool) error {
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
}

View File

@ -29,6 +29,9 @@ func init() {
RegisterMessageType(types.ProtocolMethodHandshakeDone, func() IncomingMessage { RegisterMessageType(types.ProtocolMethodHandshakeDone, func() IncomingMessage {
return NewHandshakeDone() return NewHandshakeDone()
}) })
RegisterMessageType(types.ProtocolMethodAnnouncePeers, func() IncomingMessage {
return NewAnnouncePeers()
})
} }
func RegisterMessageType(messageType types.ProtocolMethod, factoryFunc func() IncomingMessage) { func RegisterMessageType(messageType types.ProtocolMethod, factoryFunc func() IncomingMessage) {