2024-01-06 11:33:46 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-01-07 14:08:23 +00:00
|
|
|
"fmt"
|
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"
|
2024-01-07 10:12:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
2024-01-07 14:08:23 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
)
|
|
|
|
|
2024-01-07 12:59:14 +00:00
|
|
|
var _ base.IncomingMessageTyped = (*HandshakeOpen)(nil)
|
|
|
|
|
2024-01-06 11:33:46 +00:00
|
|
|
type HandshakeOpen struct {
|
|
|
|
challenge []byte
|
|
|
|
networkId string
|
2024-01-07 14:08:23 +00:00
|
|
|
handshake []byte
|
2024-01-07 10:12:43 +00:00
|
|
|
base.IncomingMessageTypedImpl
|
|
|
|
base.IncomingMessageHandler
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 12:14:25 +00:00
|
|
|
func (h *HandshakeOpen) SetHandshake(handshake []byte) {
|
|
|
|
h.handshake = handshake
|
|
|
|
}
|
|
|
|
|
2024-01-07 14:08:23 +00:00
|
|
|
func (h HandshakeOpen) Challenge() []byte {
|
|
|
|
return h.challenge
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 14:08:23 +00:00
|
|
|
func (h HandshakeOpen) NetworkId() string {
|
|
|
|
return h.networkId
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 10:12:43 +00:00
|
|
|
var _ base.EncodeableMessage = (*HandshakeOpen)(nil)
|
2024-01-06 11:33:46 +00:00
|
|
|
var (
|
|
|
|
errInvalidChallenge = errors.New("Invalid challenge")
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewHandshakeOpen(challenge []byte, networkId string) *HandshakeOpen {
|
|
|
|
return &HandshakeOpen{
|
|
|
|
challenge: challenge,
|
|
|
|
networkId: networkId,
|
|
|
|
}
|
|
|
|
}
|
2024-01-07 14:08:23 +00:00
|
|
|
func (h HandshakeOpen) EncodeMsgpack(enc *msgpack.Encoder) error {
|
2024-01-06 11:33:46 +00:00
|
|
|
err := enc.EncodeUint(uint64(types.ProtocolMethodHandshakeOpen))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-07 14:08:23 +00:00
|
|
|
err = enc.EncodeBytes(h.challenge)
|
2024-01-06 11:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-09 15:39:58 +00:00
|
|
|
if h.networkId != "" {
|
|
|
|
err = enc.EncodeString(h.networkId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-07 14:08:23 +00:00
|
|
|
func (h *HandshakeOpen) DecodeMessage(dec *msgpack.Decoder) error {
|
|
|
|
handshake, err := dec.DecodeBytes()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.handshake = handshake
|
|
|
|
|
2024-01-07 15:54:26 +00:00
|
|
|
_, err = dec.PeekCode()
|
2024-01-07 14:08:23 +00:00
|
|
|
|
2024-01-07 15:54:26 +00:00
|
|
|
networkId := ""
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "EOF" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.networkId = networkId
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
networkId, err = dec.DecodeString()
|
2024-01-07 14:08:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.networkId = networkId
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HandshakeOpen) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
|
|
|
|
if h.networkId != node.NetworkId() {
|
|
|
|
return fmt.Errorf("Peer is in different network: %s", h.networkId)
|
|
|
|
}
|
|
|
|
|
2024-01-10 14:37:27 +00:00
|
|
|
handshake := signed.NewHandshakeDoneRequest(h.handshake, types.SupportedFeatures, node.Services().P2P().SelfConnectionUris())
|
2024-01-07 14:08:23 +00:00
|
|
|
message, err := msgpack.Marshal(handshake)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
secureMessage, err := node.Services().P2P().SignMessageSimple(message)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = peer.SendMessage(secureMessage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|