Browse Source

refactored struct

0xdcarns 3 years ago
parent
commit
ac632a75b7
7 changed files with 29 additions and 17 deletions
  1. 2 2
      controllers/node_grpc.go
  2. 2 1
      database/database.go
  3. 4 3
      logic/telemetry.go
  4. 15 4
      logic/traffic.go
  5. 4 3
      models/structs.go
  6. 1 3
      mq/util.go
  7. 1 1
      netclient/ncutils/netclientutils.go

+ 2 - 2
controllers/node_grpc.go

@@ -77,7 +77,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object)
 	}
 	// TODO consolidate functionality around files
 	node.NetworkSettings.DefaultServerAddrs = serverAddrs
-	key, keyErr := logic.RetrieveTrafficKey()
+	key, keyErr := logic.RetrievePublicTrafficKey()
 	if keyErr != nil {
 		logger.Log(0, "error retrieving key: ", keyErr.Error())
 		return nil, keyErr
@@ -85,7 +85,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object)
 
 	node.TrafficKeys = models.TrafficKeys{
 		Mine:   node.TrafficKeys.Mine,
-		Server: key.PublicKey,
+		Server: key,
 	}
 
 	err = logic.CreateNode(&node)

+ 2 - 1
database/database.go

@@ -210,8 +210,9 @@ func initializeUUID() error {
 	if keyErr != nil {
 		return keyErr
 	}
+	var rsaPublicKey = &rsaPrivKey.PublicKey
 
-	telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKey: *rsaPrivKey}
+	telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKeyPriv: *rsaPrivKey, TrafficKeyPub: *rsaPublicKey}
 	telJSON, err := json.Marshal(&telemetry)
 	if err != nil {
 		return err

+ 4 - 3
logic/telemetry.go

@@ -77,9 +77,10 @@ func fetchTelemetryData() (telemetryData, error) {
 func setTelemetryTimestamp(telRecord *models.Telemetry) error {
 	lastsend := time.Now().Unix()
 	var serverTelData = models.Telemetry{
-		UUID:       telRecord.UUID,
-		LastSend:   lastsend,
-		TrafficKey: telRecord.TrafficKey,
+		UUID:           telRecord.UUID,
+		LastSend:       lastsend,
+		TrafficKeyPriv: telRecord.TrafficKeyPriv,
+		TrafficKeyPub:  telRecord.TrafficKeyPub,
 	}
 	jsonObj, err := json.Marshal(&serverTelData)
 	if err != nil {

+ 15 - 4
logic/traffic.go

@@ -5,13 +5,24 @@ import (
 	"fmt"
 )
 
-// RetrieveTrafficKey - retrieves public key based on node
-func RetrieveTrafficKey() (rsa.PrivateKey, error) {
+// RetrievePrivateTrafficKey - retrieves private key of server
+func RetrievePrivateTrafficKey() (rsa.PrivateKey, error) {
 	var telRecord, err = fetchTelemetryRecord()
 	if err != nil {
 		return rsa.PrivateKey{}, err
 	}
-	fmt.Printf("fetched key %v \n", telRecord.TrafficKey)
+	fmt.Printf("fetched priv key %v \n", telRecord.TrafficKeyPriv)
 
-	return telRecord.TrafficKey, nil
+	return telRecord.TrafficKeyPriv, nil
+}
+
+// RetrievePublicTrafficKey - retrieves public key of server
+func RetrievePublicTrafficKey() (rsa.PublicKey, error) {
+	var telRecord, err = fetchTelemetryRecord()
+	if err != nil {
+		return rsa.PublicKey{}, err
+	}
+	fmt.Printf("fetched pub key %v \n", telRecord.TrafficKeyPub)
+
+	return telRecord.TrafficKeyPub, nil
 }

+ 4 - 3
models/structs.go

@@ -170,9 +170,10 @@ type ServerUpdateData struct {
 
 // Telemetry - contains UUID of the server and timestamp of last send to posthog
 type Telemetry struct {
-	UUID       string         `json:"uuid" bson:"uuid"`
-	LastSend   int64          `json:"lastsend" bson:"lastsend"`
-	TrafficKey rsa.PrivateKey `json:"traffickey" bson:"traffickey"`
+	UUID           string         `json:"uuid" bson:"uuid"`
+	LastSend       int64          `json:"lastsend" bson:"lastsend"`
+	TrafficKeyPriv rsa.PrivateKey `json:"traffickeypriv" bson:"traffickeypriv"`
+	TrafficKeyPub  rsa.PublicKey  `json:"traffickeypub" bson:"traffickeypub"`
 }
 
 // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not

+ 1 - 3
mq/util.go

@@ -3,15 +3,13 @@ package mq
 import (
 	"fmt"
 
-	"github.com/gravitl/netmaker/logger"
 	"github.com/gravitl/netmaker/logic"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/netclient/ncutils"
 )
 
 func decryptMsg(msg []byte) ([]byte, error) {
-	logger.Log(0, "found message for decryption: %s \n", string(msg))
-	trafficKey, trafficErr := logic.RetrieveTrafficKey()
+	trafficKey, trafficErr := logic.RetrievePrivateTrafficKey()
 	if trafficErr != nil {
 		return nil, trafficErr
 	}

+ 1 - 1
netclient/ncutils/netclientutils.go

@@ -566,7 +566,7 @@ func DestructMessage(builtMsg string, priv *rsa.PrivateKey) []byte {
 
 // BuildMessage Build a message for publishing
 func BuildMessage(originalMessage []byte, pub *rsa.PublicKey) string {
-	chunks := getSliceChunks(originalMessage, 245)
+	chunks := getSliceChunks(originalMessage, 240)
 	var message = ""
 	for i := 0; i < len(chunks); i++ {
 		var encryptedText, encryptErr = encryptWithPublicKey(chunks[i], pub)