netmaker/mq/util.go

93 lines
2.3 KiB
Go
Raw Normal View History

2022-01-29 04:33:30 +08:00
package mq
import (
2022-05-16 21:38:47 +08:00
"errors"
"fmt"
"strings"
2022-05-16 21:38:47 +08:00
"time"
2022-01-29 04:33:30 +08:00
"github.com/gravitl/netmaker/logic"
2022-01-29 22:14:18 +08:00
"github.com/gravitl/netmaker/models"
2022-01-29 04:33:30 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
)
2022-01-30 04:02:37 +08:00
func decryptMsg(node *models.Node, msg []byte) ([]byte, error) {
if len(msg) <= 24 { // make sure message is of appropriate length
2022-02-15 03:21:56 +08:00
return nil, fmt.Errorf("recieved invalid message from broker %v", msg)
}
2022-01-30 04:02:37 +08:00
trafficKey, trafficErr := logic.RetrievePrivateTrafficKey() // get server private key
2022-01-29 04:33:30 +08:00
if trafficErr != nil {
return nil, trafficErr
}
2022-01-30 04:02:37 +08:00
serverPrivTKey, err := ncutils.ConvertBytesToKey(trafficKey)
if err != nil {
return nil, err
}
nodePubTKey, err := ncutils.ConvertBytesToKey(node.TrafficKeys.Mine)
if err != nil {
return nil, err
}
if strings.Contains(node.Version, "0.10.0") {
return ncutils.BoxDecrypt(msg, nodePubTKey, serverPrivTKey)
}
2022-02-15 03:21:56 +08:00
return ncutils.DeChunk(msg, nodePubTKey, serverPrivTKey)
2022-01-29 04:33:30 +08:00
}
2022-01-30 04:02:37 +08:00
func encryptMsg(node *models.Node, msg []byte) ([]byte, error) {
// fetch server public key to be certain hasn't changed in transit
trafficKey, trafficErr := logic.RetrievePrivateTrafficKey()
if trafficErr != nil {
return nil, trafficErr
}
serverPrivKey, err := ncutils.ConvertBytesToKey(trafficKey)
if err != nil {
return nil, err
2022-01-29 04:33:30 +08:00
}
2022-01-30 04:02:37 +08:00
nodePubKey, err := ncutils.ConvertBytesToKey(node.TrafficKeys.Mine)
if err != nil {
return nil, err
}
if strings.Contains(node.Version, "0.10.0") {
return ncutils.BoxEncrypt(msg, nodePubKey, serverPrivKey)
}
2022-02-15 03:21:56 +08:00
return ncutils.Chunk(msg, nodePubKey, serverPrivKey)
2022-01-29 04:33:30 +08:00
}
2022-01-29 22:14:18 +08:00
func publish(node *models.Node, dest string, msg []byte) error {
2022-01-30 04:02:37 +08:00
encrypted, encryptErr := encryptMsg(node, msg)
2022-01-29 04:33:30 +08:00
if encryptErr != nil {
return encryptErr
}
2022-08-30 02:08:01 +08:00
if mqclient == nil {
return errors.New("cannot publish ... mqclient not connected")
}
if token := mqclient.Publish(dest, 0, true, encrypted); !token.WaitTimeout(MQ_TIMEOUT*time.Second) || token.Error() != nil {
2022-05-16 21:38:47 +08:00
var err error
if token.Error() == nil {
err = errors.New("connection timeout")
} else {
err = token.Error()
}
return err
2022-01-29 04:33:30 +08:00
}
return nil
}
2022-08-30 02:08:01 +08:00
// decodes a message queue topic and returns the embedded node.ID
func getID(topic string) (string, error) {
parts := strings.Split(topic, "/")
count := len(parts)
if count == 1 {
return "", fmt.Errorf("invalid topic")
}
//the last part of the topic will be the node.ID
return parts[count-1], nil
}