Browse Source

adjusted to byte buffer

0xdcarns 3 years ago
parent
commit
7be2b0e09d
4 changed files with 23 additions and 14 deletions
  1. 1 1
      controllers/node_grpc.go
  2. 7 5
      database/database.go
  3. 11 5
      logic/traffic.go
  4. 4 3
      models/structs.go

+ 1 - 1
controllers/node_grpc.go

@@ -87,7 +87,7 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object)
 
 	node.TrafficKeys = models.TrafficKeys{
 		Mine:   node.TrafficKeys.Mine,
-		Server: key.PublicKey,
+		Server: key,
 	}
 
 	fmt.Printf("finished created node: %v \n", node)

+ 7 - 5
database/database.go

@@ -1,11 +1,12 @@
 package database
 
 import (
+	"bytes"
 	"crypto/rand"
 	"crypto/rsa"
+	"encoding/gob"
 	"encoding/json"
 	"errors"
-	"fmt"
 	"strings"
 	"time"
 
@@ -216,11 +217,12 @@ func initializeUUID() error {
 	if keyErr != nil {
 		return keyErr
 	}
+	var rsaKey bytes.Buffer
+	if err = gob.NewEncoder(&rsaKey).Encode(rsaPrivKey); err != nil {
+		return err
+	}
 
-	fmt.Printf("key generated: %v \n", rsaPrivKey)
-	fmt.Printf("pub key generate: %v \n", rsaPrivKey.PublicKey)
-
-	telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKey: *rsaPrivKey}
+	telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKey: rsaKey}
 	telJSON, err := json.Marshal(&telemetry)
 	if err != nil {
 		return err

+ 11 - 5
logic/traffic.go

@@ -2,15 +2,21 @@ package logic
 
 import (
 	"crypto/rsa"
+	"encoding/gob"
 	"fmt"
 )
 
-// RetrieveTrafficKey - retrieves key based on node
-func RetrieveTrafficKey() (rsa.PrivateKey, error) {
+// RetrieveTrafficKey - retrieves public key based on node
+func RetrieveTrafficKey() (rsa.PublicKey, error) {
 	var telRecord, err = fetchTelemetryRecord()
 	if err != nil {
-		return rsa.PrivateKey{}, err
+		return rsa.PublicKey{}, err
 	}
-	fmt.Printf("retrieved key: %v \n", telRecord.TrafficKey)
-	return telRecord.TrafficKey, nil
+	var key = rsa.PrivateKey{}
+	if err = gob.NewDecoder(&telRecord.TrafficKey).Decode(&key); err != nil {
+		return rsa.PublicKey{}, err
+	}
+	fmt.Printf("retrieved key: %v \n", key.PublicKey)
+
+	return key.PublicKey, nil
 }

+ 4 - 3
models/structs.go

@@ -1,6 +1,7 @@
 package models
 
 import (
+	"bytes"
 	"crypto/rsa"
 
 	jwt "github.com/golang-jwt/jwt/v4"
@@ -170,9 +171,9 @@ 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"`
+	TrafficKey bytes.Buffer `json:"traffickey" bson:"traffickey"`
 }
 
 // ServerAddr - to pass to clients to tell server addresses and if it's the leader or not