libs5-go/protocol/signed/handshake_done.go

160 lines
3.1 KiB
Go
Raw Normal View History

2024-01-06 11:33:46 +00:00
package signed
import (
"bytes"
"errors"
"fmt"
2024-01-06 11:33:46 +00:00
"git.lumeweb.com/LumeWeb/libs5-go/net"
2024-01-30 03:25:21 +00:00
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
"git.lumeweb.com/LumeWeb/libs5-go/types"
2024-01-08 03:35:41 +00:00
"git.lumeweb.com/LumeWeb/libs5-go/utils"
2024-01-06 11:33:46 +00:00
"github.com/vmihailenco/msgpack/v5"
"net/url"
2024-01-06 11:33:46 +00:00
)
var _ IncomingMessageSigned = (*HandshakeDone)(nil)
2024-01-30 03:28:39 +00:00
var _ protocol.EncodeableMessage = (*HandshakeDone)(nil)
2024-01-06 11:33:46 +00:00
type HandshakeDone struct {
challenge []byte
networkId string
2024-01-06 11:33:46 +00:00
supportedFeatures int
connectionUris []*url.URL
handshake []byte
2024-01-30 03:25:21 +00:00
protocol.HandshakeRequirement
}
func NewHandshakeDoneRequest(handshake []byte, supportedFeatures int, connectionUris []*url.URL) *HandshakeDone {
ho := &HandshakeDone{
handshake: handshake,
supportedFeatures: supportedFeatures,
connectionUris: connectionUris,
}
ho.SetRequiresHandshake(false)
return ho
}
func (m HandshakeDone) EncodeMsgpack(enc *msgpack.Encoder) error {
err := enc.EncodeUint(uint64(types.ProtocolMethodHandshakeDone))
if err != nil {
return err
}
err = enc.EncodeBytes(m.handshake)
if err != nil {
return err
}
err = enc.EncodeInt(int64(m.supportedFeatures))
if err != nil {
return err
}
2024-01-08 03:35:41 +00:00
err = utils.EncodeMsgpackArray(enc, m.connectionUris)
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 {
hn := &HandshakeDone{challenge: nil, networkId: "", supportedFeatures: -1}
hn.SetRequiresHandshake(false)
return hn
2024-01-06 11:33:46 +00:00
}
func (h HandshakeDone) HandleMessage(message IncomingMessageDataSigned) error {
services := message.Services
peer := message.Peer
verifyId := message.VerifyId
nodeId := message.NodeId
logger := message.Logger
if !services.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(nodeId)
} else {
if !peer.Id().Equals(nodeId) {
return fmt.Errorf("Invalid transports id on initial list")
2024-01-06 11:33:46 +00:00
}
}
2024-01-06 11:33:46 +00:00
peer.SetConnected(true)
peer.SetHandshakeDone(true)
2024-01-06 11:33:46 +00:00
if h.supportedFeatures != types.SupportedFeatures {
return fmt.Errorf("Remote node does not support required features")
}
err := services.P2P().AddPeer(peer)
if err != nil {
return err
}
2024-01-06 11:33:46 +00:00
peer.SetConnectionURIs(h.connectionUris)
2024-01-06 11:33:46 +00:00
peerId, err := peer.Id().ToString()
2024-01-06 11:33:46 +00:00
if err != nil {
return err
}
2024-01-06 11:33:46 +00:00
logger.Info(fmt.Sprintf("[+] %s (%s)", peerId, peer.RenderLocationURI()))
2024-01-06 11:33:46 +00:00
err = services.P2P().SendPublicPeersToPeer(peer, []net.Peer{peer})
if err != nil {
return err
}
2024-01-06 11:33:46 +00:00
return nil
}
func (h *HandshakeDone) DecodeMessage(dec *msgpack.Decoder, message IncomingMessageDataSigned) 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
2024-01-08 17:11:54 +00:00
connectionUris, err := utils.DecodeMsgpackURLArray(dec)
if err != nil {
return err
}
h.connectionUris = connectionUris
2024-01-06 11:33:46 +00:00
return nil
}