refactored struct

This commit is contained in:
0xdcarns
2022-01-29 09:37:53 -05:00
parent 3483e45beb
commit ac632a75b7
7 changed files with 29 additions and 17 deletions

View File

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

View File

@@ -210,8 +210,9 @@ func initializeUUID() error {
if keyErr != nil { if keyErr != nil {
return keyErr 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) telJSON, err := json.Marshal(&telemetry)
if err != nil { if err != nil {
return err return err

View File

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

View File

@@ -5,13 +5,24 @@ import (
"fmt" "fmt"
) )
// RetrieveTrafficKey - retrieves public key based on node // RetrievePrivateTrafficKey - retrieves private key of server
func RetrieveTrafficKey() (rsa.PrivateKey, error) { func RetrievePrivateTrafficKey() (rsa.PrivateKey, error) {
var telRecord, err = fetchTelemetryRecord() var telRecord, err = fetchTelemetryRecord()
if err != nil { if err != nil {
return rsa.PrivateKey{}, err 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
} }

View File

@@ -170,9 +170,10 @@ type ServerUpdateData struct {
// Telemetry - contains UUID of the server and timestamp of last send to posthog // Telemetry - contains UUID of the server and timestamp of last send to posthog
type Telemetry struct { type Telemetry struct {
UUID string `json:"uuid" bson:"uuid"` UUID string `json:"uuid" bson:"uuid"`
LastSend int64 `json:"lastsend" bson:"lastsend"` LastSend int64 `json:"lastsend" bson:"lastsend"`
TrafficKey rsa.PrivateKey `json:"traffickey" bson:"traffickey"` 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 // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not

View File

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

View File

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