libs5-go/protocol/signed/handshake_done.go

142 lines
3.1 KiB
Go
Raw Normal View History

2024-01-06 11:33:46 +00:00
package signed
import (
"bytes"
"errors"
2024-01-07 08:58:22 +00:00
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
2024-01-06 11:33:46 +00:00
"git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
"git.lumeweb.com/LumeWeb/libs5-go/types"
2024-01-06 11:33:46 +00:00
"github.com/vmihailenco/msgpack/v5"
"net/url"
2024-01-06 11:33:46 +00:00
)
var _ base.IncomingMessageTyped = (*HandshakeDone)(nil)
var _ base.EncodeableMessage = (*HandshakeDone)(nil)
2024-01-06 11:33:46 +00:00
type HandshakeDone struct {
challenge []byte
networkId string
base.IncomingMessageTypedImpl
base.IncomingMessageHandler
2024-01-06 11:33:46 +00:00
supportedFeatures int
connectionUris []*url.URL
handshake []byte
}
func NewHandshakeDoneRequest(handshake []byte, supportedFeatures int, connectionUris []*url.URL) *HandshakeDone {
return &HandshakeDone{
handshake: handshake,
supportedFeatures: supportedFeatures,
connectionUris: connectionUris,
}
}
func (m HandshakeDone) EncodeMsgpack(enc *msgpack.Encoder) error {
err := enc.EncodeUint(uint64(types.ProtocolMethodHandshakeDone))
if err != nil {
return err
}
err = enc.EncodeBytes(m.Original())
if err != nil {
return err
}
err = enc.EncodeString(m.networkId)
if err != nil {
return err
}
err = enc.EncodeInt(int64(m.supportedFeatures))
if err != nil {
return err
}
return nil
2024-01-06 11:33:46 +00:00
}
func (m *HandshakeDone) SetChallenge(challenge []byte) {
m.challenge = challenge
}
func (m *HandshakeDone) SetNetworkId(networkId string) {
m.networkId = networkId
}
2024-01-06 11:33:46 +00:00
func NewHandshakeDone() *HandshakeDone {
return &HandshakeDone{challenge: nil, networkId: "", supportedFeatures: -1}
2024-01-06 11:33:46 +00:00
}
func (h HandshakeDone) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
2024-01-07 08:58:22 +00:00
if !node.IsStarted() {
err := peer.End()
2024-01-06 11:33:46 +00:00
if err != nil {
return nil
}
return nil
}
if !bytes.Equal(peer.Challenge(), h.challenge) {
2024-01-06 11:33:46 +00:00
return errors.New("Invalid challenge")
}
/*
if !verifyId {
peer.SetId(h)
2024-01-06 11:33:46 +00:00
} else {
if !peer.ID.Equals(pId) {
return errInvalidChallenge
}
}
peer.isConnected = true
2024-01-06 11:33:46 +00:00
supportedFeatures := data.UnpackInt()
if supportedFeatures != 3 {
return errors.New("Remote node does not support required features")
}
node.Services.P2P.Peers[peer.ID.String()] = peer
node.Services.P2P.ReconnectDelay[peer.ID.String()] = 1
connectionUrisCount := data.UnpackInt()
peer.ConnectionUris = make([]*url.URL, 0)
for i := 0; i < connectionUrisCount; i++ {
uriStr := data.UnpackString()
uri, err := url.Parse(uriStr)
if err != nil {
return err
}
peer.ConnectionUris = append(peer.ConnectionUris, uri)
}
// Log information - Assuming a logging method exists
node.Logger.Info(fmt.Sprintf("[+] %s (%s)", peer.ID.String(), peer.RenderLocationUri().String()))
// Send peer lists and emit 'peerConnected' event
// Assuming appropriate methods exist in node.Services.P2P
node.Services.P2P.SendPublicPeersToPeer(peer)
*/
return nil
}
func (h HandshakeDone) DecodeMessage(dec *msgpack.Decoder) error {
challenge, err := dec.DecodeBytes()
if err != nil {
return err
}
h.challenge = challenge
2024-01-06 11:33:46 +00:00
supportedFeatures, err := dec.DecodeInt()
if err != nil {
return err
}
h.supportedFeatures = supportedFeatures
return nil
}