2024-01-30 03:35:00 +00:00
|
|
|
package protocol
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2024-01-08 17:11:34 +00:00
|
|
|
"fmt"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
2024-01-07 14:09:17 +00:00
|
|
|
"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"
|
2024-01-07 14:09:17 +00:00
|
|
|
"net/url"
|
2024-01-06 11:33:46 +00:00
|
|
|
)
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
var _ IncomingMessageSigned = (*HandshakeDone)(nil)
|
2024-01-30 03:35:00 +00:00
|
|
|
var _ EncodeableMessage = (*HandshakeDone)(nil)
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
type HandshakeDone struct {
|
2024-01-29 04:39:40 +00:00
|
|
|
challenge []byte
|
|
|
|
networkId string
|
2024-01-06 11:33:46 +00:00
|
|
|
supportedFeatures int
|
2024-01-07 14:09:17 +00:00
|
|
|
connectionUris []*url.URL
|
|
|
|
handshake []byte
|
2024-01-30 03:35:00 +00:00
|
|
|
HandshakeRequirement
|
2024-01-07 14:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandshakeDoneRequest(handshake []byte, supportedFeatures int, connectionUris []*url.URL) *HandshakeDone {
|
2024-01-13 16:22:01 +00:00
|
|
|
ho := &HandshakeDone{
|
2024-01-07 14:09:17 +00:00
|
|
|
handshake: handshake,
|
|
|
|
supportedFeatures: supportedFeatures,
|
|
|
|
connectionUris: connectionUris,
|
|
|
|
}
|
2024-01-13 16:22:01 +00:00
|
|
|
|
|
|
|
ho.SetRequiresHandshake(false)
|
|
|
|
|
|
|
|
return ho
|
2024-01-07 14:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m HandshakeDone) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
err := enc.EncodeUint(uint64(types.ProtocolMethodHandshakeDone))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-08 02:38:26 +00:00
|
|
|
err = enc.EncodeBytes(m.handshake)
|
2024-01-07 14:09:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-07 14:09:17 +00:00
|
|
|
return nil
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 10:13:09 +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 {
|
2024-01-13 16:22:01 +00:00
|
|
|
hn := &HandshakeDone{challenge: nil, networkId: "", supportedFeatures: -1}
|
|
|
|
|
|
|
|
hn.SetRequiresHandshake(false)
|
|
|
|
|
|
|
|
return hn
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
func (h HandshakeDone) HandleMessage(message IncomingMessageDataSigned) error {
|
2024-01-29 06:55:36 +00:00
|
|
|
services := message.Services
|
2024-01-29 04:39:40 +00:00
|
|
|
peer := message.Peer
|
|
|
|
verifyId := message.VerifyId
|
|
|
|
nodeId := message.NodeId
|
2024-01-29 06:55:36 +00:00
|
|
|
logger := message.Logger
|
2024-01-29 04:39:40 +00:00
|
|
|
|
2024-01-29 06:55:36 +00:00
|
|
|
if !services.IsStarted() {
|
2024-01-07 11:35:41 +00:00
|
|
|
err := peer.End()
|
2024-01-06 11:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-07 11:35:41 +00:00
|
|
|
if !bytes.Equal(peer.Challenge(), h.challenge) {
|
2024-01-06 11:33:46 +00:00
|
|
|
return errors.New("Invalid challenge")
|
|
|
|
}
|
2024-01-08 17:11:34 +00:00
|
|
|
|
|
|
|
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-08 17:11:34 +00:00
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-08 17:11:34 +00:00
|
|
|
peer.SetConnected(true)
|
2024-01-13 16:22:01 +00:00
|
|
|
peer.SetHandshakeDone(true)
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-08 17:11:34 +00:00
|
|
|
if h.supportedFeatures != types.SupportedFeatures {
|
|
|
|
return fmt.Errorf("Remote node does not support required features")
|
|
|
|
}
|
2024-01-29 06:55:36 +00:00
|
|
|
err := services.P2P().AddPeer(peer)
|
2024-01-08 17:11:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-08 17:11:34 +00:00
|
|
|
peer.SetConnectionURIs(h.connectionUris)
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-08 17:11:34 +00:00
|
|
|
peerId, err := peer.Id().ToString()
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-08 17:11:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-29 06:55:36 +00:00
|
|
|
logger.Info(fmt.Sprintf("[+] %s (%s)", peerId, peer.RenderLocationURI()))
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-29 06:55:36 +00:00
|
|
|
err = services.P2P().SendPublicPeersToPeer(peer, []net.Peer{peer})
|
2024-01-08 17:11:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
func (h *HandshakeDone) DecodeMessage(dec *msgpack.Decoder, message IncomingMessageDataSigned) error {
|
2024-01-07 10:13:09 +00:00
|
|
|
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
|
|
|
|
}
|