mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 16:57:51 +08:00
first draft of chunks
This commit is contained in:
@@ -26,7 +26,7 @@ func decryptMsg(node *models.Node, msg []byte) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ncutils.BoxDecrypt(msg, nodePubTKey, serverPrivTKey)
|
return ncutils.DestructMessage(string(msg), nodePubTKey, serverPrivTKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func encryptMsg(node *models.Node, msg []byte) ([]byte, error) {
|
func encryptMsg(node *models.Node, msg []byte) ([]byte, error) {
|
||||||
@@ -46,7 +46,8 @@ func encryptMsg(node *models.Node, msg []byte) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ncutils.BoxEncrypt(msg, nodePubKey, serverPrivKey)
|
var encrypted, encErr = ncutils.BuildMessage(msg, nodePubKey, serverPrivKey)
|
||||||
|
return []byte(encrypted), encErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func publish(node *models.Node, dest string, msg []byte) error {
|
func publish(node *models.Node, dest string, msg []byte) error {
|
||||||
|
@@ -553,7 +553,7 @@ func publish(cfg *config.ClientConfig, dest string, msg []byte) error {
|
|||||||
|
|
||||||
client := SetupMQTT(cfg, true)
|
client := SetupMQTT(cfg, true)
|
||||||
defer client.Disconnect(250)
|
defer client.Disconnect(250)
|
||||||
encrypted, err := ncutils.BoxEncrypt(msg, serverPubKey, trafficPrivKey)
|
encrypted, err := ncutils.BuildMessage(msg, serverPubKey, trafficPrivKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -584,7 +584,7 @@ func decryptMsg(cfg *config.ClientConfig, msg []byte) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ncutils.BoxDecrypt(msg, serverPubKey, diskKey)
|
return ncutils.DestructMessage(string(msg), serverPubKey, diskKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pingServer(cfg *config.ClientConfig) error {
|
func pingServer(cfg *config.ClientConfig) error {
|
||||||
|
@@ -2,6 +2,7 @@ package ncutils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,3 +23,51 @@ func BackOff(isExponential bool, maxTime int, f interface{}) (interface{}, error
|
|||||||
}
|
}
|
||||||
return nil, fmt.Errorf("could not find result")
|
return nil, fmt.Errorf("could not find result")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DestructMessage - reconstruct original message through chunks
|
||||||
|
func DestructMessage(builtMsg string, senderPublicKey *[32]byte, recipientPrivateKey *[32]byte) ([]byte, error) {
|
||||||
|
var chunks = strings.Split(builtMsg, splitKey)
|
||||||
|
var totalMessage = make([]byte, len(builtMsg))
|
||||||
|
for _, chunk := range chunks {
|
||||||
|
var bytes, decErr = BoxDecrypt([]byte(chunk), senderPublicKey, recipientPrivateKey)
|
||||||
|
if decErr != nil || bytes == nil {
|
||||||
|
return nil, decErr
|
||||||
|
}
|
||||||
|
totalMessage = append(totalMessage, bytes...)
|
||||||
|
}
|
||||||
|
return totalMessage, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildMessage Build a message for publishing
|
||||||
|
func BuildMessage(originalMessage []byte, recipientPubKey *[32]byte, senderPrivateKey *[32]byte) (string, error) {
|
||||||
|
chunks := getSliceChunks(originalMessage, 16128)
|
||||||
|
var sb strings.Builder
|
||||||
|
for i := 0; i < len(chunks); i++ {
|
||||||
|
var encryptedText, encryptErr = BoxEncrypt(chunks[i], recipientPubKey, senderPrivateKey)
|
||||||
|
if encryptErr != nil {
|
||||||
|
return "", encryptErr
|
||||||
|
}
|
||||||
|
sb.Write(encryptedText)
|
||||||
|
if i < len(chunks)-1 {
|
||||||
|
sb.WriteString(splitKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var splitKey = "<|#|>"
|
||||||
|
|
||||||
|
func getSliceChunks(slice []byte, chunkSize int) [][]byte {
|
||||||
|
var chunks [][]byte
|
||||||
|
for i := 0; i < len(slice); i += chunkSize {
|
||||||
|
lastByte := i + chunkSize
|
||||||
|
|
||||||
|
if lastByte > len(slice) {
|
||||||
|
lastByte = len(slice)
|
||||||
|
}
|
||||||
|
|
||||||
|
chunks = append(chunks, slice[i:lastByte])
|
||||||
|
}
|
||||||
|
|
||||||
|
return chunks
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user