trying again

This commit is contained in:
0xdcarns 2022-01-28 18:17:02 -05:00
parent 50c95f66cb
commit 48d6d57f25
3 changed files with 13 additions and 26 deletions

View file

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

View file

@ -2,7 +2,7 @@ package logic
import (
"crypto/rsa"
"encoding/gob"
"encoding/json"
"fmt"
)
@ -12,10 +12,8 @@ func RetrieveTrafficKey() (rsa.PrivateKey, error) {
if err != nil {
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)
return key, nil

View file

@ -1,7 +1,6 @@
package models
import (
"bytes"
"crypto/rsa"
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
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