2024-01-06 11:33:46 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2024-01-07 14:08:23 +00:00
|
|
|
"fmt"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
|
|
)
|
|
|
|
|
2024-01-30 03:25:21 +00:00
|
|
|
var _ EncodeableMessage = (*HandshakeOpen)(nil)
|
|
|
|
var _ IncomingMessage = (*HandshakeOpen)(nil)
|
2024-01-07 12:59:14 +00:00
|
|
|
|
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-30 03:25:21 +00:00
|
|
|
HandshakeRequirement
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandshakeOpen(challenge []byte, networkId string) *HandshakeOpen {
|
2024-01-13 16:22:01 +00:00
|
|
|
ho := &HandshakeOpen{
|
2024-01-06 11:33:46 +00:00
|
|
|
challenge: challenge,
|
|
|
|
networkId: networkId,
|
|
|
|
}
|
2024-01-13 16:22:01 +00:00
|
|
|
|
|
|
|
ho.SetRequiresHandshake(false)
|
|
|
|
|
|
|
|
return ho
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
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-30 03:25:21 +00:00
|
|
|
func (h *HandshakeOpen) DecodeMessage(dec *msgpack.Decoder, message IncomingMessageData) error {
|
2024-01-07 14:08:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-30 03:25:21 +00:00
|
|
|
func (h *HandshakeOpen) HandleMessage(message IncomingMessageData) error {
|
2024-01-29 04:39:40 +00:00
|
|
|
peer := message.Peer
|
2024-01-29 06:55:36 +00:00
|
|
|
services := message.Services
|
2024-01-29 04:39:40 +00:00
|
|
|
|
2024-01-29 06:55:36 +00:00
|
|
|
if h.networkId != services.P2P().NetworkId() {
|
2024-01-07 14:08:23 +00:00
|
|
|
return fmt.Errorf("Peer is in different network: %s", h.networkId)
|
|
|
|
}
|
|
|
|
|
2024-01-30 03:35:00 +00:00
|
|
|
handshake := NewHandshakeDoneRequest(h.handshake, types.SupportedFeatures, services.P2P().SelfConnectionUris())
|
2024-01-29 04:39:40 +00:00
|
|
|
hsMessage, err := msgpack.Marshal(handshake)
|
2024-01-07 14:08:23 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-29 06:55:36 +00:00
|
|
|
secureMessage, err := services.P2P().SignMessageSimple(hsMessage)
|
2024-01-07 14:08:23 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = peer.SendMessage(secureMessage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|