Browse Source

trying again

0xdcarns 3 years ago
parent
commit
48d6d57f25
3 changed files with 13 additions and 26 deletions
  1. 7 17
      database/database.go
  2. 3 5
      logic/traffic.go
  3. 3 4
      models/structs.go

+ 7 - 17
database/database.go

@@ -1,14 +1,11 @@
 package database
 package database
 
 
 import (
 import (
-	"bytes"
 	"crypto/rand"
 	"crypto/rand"
 	"crypto/rsa"
 	"crypto/rsa"
-	"encoding/gob"
 	"encoding/json"
 	"encoding/json"
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
-	"strings"
 	"time"
 	"time"
 
 
 	"github.com/google/uuid"
 	"github.com/google/uuid"
@@ -47,9 +44,6 @@ const SERVERCONF_TABLE_NAME = "serverconf"
 // SERVER_UUID_TABLE_NAME - stores
 // SERVER_UUID_TABLE_NAME - stores
 const SERVER_UUID_TABLE_NAME = "serveruuid"
 const SERVER_UUID_TABLE_NAME = "serveruuid"
 
 
-// TRAFFIC_TABLE_NAME - stores stuff to control traffic
-const TRAFFIC_TABLE_NAME = "traffic-table"
-
 // SERVER_UUID_RECORD_KEY - telemetry thing
 // SERVER_UUID_RECORD_KEY - telemetry thing
 const SERVER_UUID_RECORD_KEY = "serveruuid"
 const SERVER_UUID_RECORD_KEY = "serveruuid"
 
 
@@ -122,8 +116,7 @@ func InitializeDatabase() error {
 		time.Sleep(2 * time.Second)
 		time.Sleep(2 * time.Second)
 	}
 	}
 	createTables()
 	createTables()
-	err := initializeUUID()
-	return err
+	return initializeUUID()
 }
 }
 
 
 func createTables() {
 func createTables() {
@@ -138,7 +131,6 @@ func createTables() {
 	createTable(SERVERCONF_TABLE_NAME)
 	createTable(SERVERCONF_TABLE_NAME)
 	createTable(SERVER_UUID_TABLE_NAME)
 	createTable(SERVER_UUID_TABLE_NAME)
 	createTable(GENERATED_TABLE_NAME)
 	createTable(GENERATED_TABLE_NAME)
-	createTable(TRAFFIC_TABLE_NAME)
 }
 }
 
 
 func createTable(tableName string) error {
 func createTable(tableName string) error {
@@ -208,7 +200,7 @@ func FetchRecords(tableName string) (map[string]string, error) {
 func initializeUUID() error {
 func initializeUUID() error {
 	records, err := FetchRecords(SERVER_UUID_TABLE_NAME)
 	records, err := FetchRecords(SERVER_UUID_TABLE_NAME)
 	if err != nil {
 	if err != nil {
-		if !strings.Contains("could not find any records", err.Error()) {
+		if !IsEmptyRecord(err) {
 			return err
 			return err
 		}
 		}
 	} else if len(records) > 0 {
 	} else if len(records) > 0 {
@@ -218,18 +210,16 @@ func initializeUUID() error {
 	if keyErr != nil {
 	if keyErr != nil {
 		return keyErr
 		return keyErr
 	}
 	}
-	var rsaKey bytes.Buffer
-	if err = gob.NewEncoder(&rsaKey).Encode(rsaPrivKey); err != nil {
-		return err
-	}
-	fmt.Printf("adding key %v \n", rsaPrivKey)
+	fmt.Printf("created key %v \n", rsaPrivKey)
+
+	data, _ := json.Marshal(rsaPrivKey)
+	fmt.Printf("priv key data: %s \n", string(data))
 
 
-	telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKey: rsaKey}
+	telemetry := models.Telemetry{UUID: uuid.NewString(), TrafficKey: string(data)}
 	telJSON, err := json.Marshal(&telemetry)
 	telJSON, err := json.Marshal(&telemetry)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	fmt.Printf("added key %v \n", rsaKey)
 
 
 	return Insert(SERVER_UUID_RECORD_KEY, string(telJSON), SERVER_UUID_TABLE_NAME)
 	return Insert(SERVER_UUID_RECORD_KEY, string(telJSON), SERVER_UUID_TABLE_NAME)
 }
 }

+ 3 - 5
logic/traffic.go

@@ -2,7 +2,7 @@ package logic
 
 
 import (
 import (
 	"crypto/rsa"
 	"crypto/rsa"
-	"encoding/gob"
+	"encoding/json"
 	"fmt"
 	"fmt"
 )
 )
 
 
@@ -12,10 +12,8 @@ func RetrieveTrafficKey() (rsa.PrivateKey, error) {
 	if err != nil {
 	if err != nil {
 		return rsa.PrivateKey{}, err
 		return rsa.PrivateKey{}, err
 	}
 	}
-	var key = rsa.PrivateKey{}
-	if err = gob.NewDecoder(&telRecord.TrafficKey).Decode(&key); err != nil {
-		return rsa.PrivateKey{}, err
-	}
+	var key rsa.PrivateKey
+	json.Unmarshal([]byte(telRecord.TrafficKey), &key)
 	fmt.Printf("retrieved key: %v \n", key.PublicKey)
 	fmt.Printf("retrieved key: %v \n", key.PublicKey)
 
 
 	return key, nil
 	return key, nil

+ 3 - 4
models/structs.go

@@ -1,7 +1,6 @@
 package models
 package models
 
 
 import (
 import (
-	"bytes"
 	"crypto/rsa"
 	"crypto/rsa"
 
 
 	jwt "github.com/golang-jwt/jwt/v4"
 	jwt "github.com/golang-jwt/jwt/v4"
@@ -171,9 +170,9 @@ 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"`
-	LastSend   int64        `json:"lastsend" bson:"lastsend"`
-	TrafficKey bytes.Buffer `json:"traffickey" bson:"traffickey"`
+	UUID       string `json:"uuid" bson:"uuid"`
+	LastSend   int64  `json:"lastsend" bson:"lastsend"`
+	TrafficKey string `json:"traffickey" bson:"traffickey"`
 }
 }
 
 
 // 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