reid 2 роки тому
батько
коміт
52104c5a4f
5 змінених файлів з 71 додано та 35 видалено
  1. 12 11
      config/config.go
  2. 22 18
      main.go
  3. 31 0
      startram/startram.go
  4. 0 6
      structs/broadcast.go
  5. 6 0
      structs/startram.go

+ 12 - 11
config/config.go

@@ -16,17 +16,18 @@ import (
 )
 
 var (
-	globalConfig  structs.SysConfig
-	logger        = slog.New(slog.NewJSONHandler(os.Stdout, nil))
-	BasePath      = os.Getenv("GS_BASE_PATH")
-	Architecture  = getArchitecture()
-	DebugMode     = false
-	Ready         = false
-	GSContainers  = make(map[string]structs.ContainerState)
-	checkInterval = 5 * time.Minute
-	confMutex     sync.Mutex
-	contMutex     sync.Mutex
-	versMutex     sync.Mutex
+	globalConfig   structs.SysConfig
+	logger         = slog.New(slog.NewJSONHandler(os.Stdout, nil))
+	BasePath       = os.Getenv("GS_BASE_PATH")
+	Architecture   = getArchitecture()
+	StartramConfig structs.StartramRetrieve
+	DebugMode      = false
+	Ready          = false
+	GSContainers   = make(map[string]structs.ContainerState)
+	checkInterval  = 5 * time.Minute
+	confMutex      sync.Mutex
+	contMutex      sync.Mutex
+	versMutex      sync.Mutex
 )
 
 // try initializing from system.json on disk

+ 22 - 18
main.go

@@ -1,11 +1,26 @@
 package main
 
+// NativePlanet GroundSeg: Go Edition (goseg)
+// 🄯 2023 ~nallux-dozryl & ~sitful-hatred
+// This is a Golang rewrite of GroundSeg that serves the v2 json
+// object via websocket.
+// The v2 rewrite decouples the frontend and backend, which makes it
+// straightforward to implement alternative backends.
+//
+// Under development: reimplementing all pyseg functionality.
+// Advantages:
+// - Really, really fast
+// - Event-driven
+// - First-class support for concurrent operations
+// - Very good golang Docker libraries
+
 import (
 	"encoding/json"
 	"fmt"
 	"goseg/config"
 	"goseg/docker"
 	"goseg/rectify"
+	"goseg/startram"
 	"goseg/structs"
 	"goseg/ws"
 	"io/ioutil"
@@ -13,25 +28,12 @@ import (
 	"net/http"
 	"os"
 	"path/filepath"
+	"strings"
 	// "time"
 
 	"github.com/gorilla/mux"
 )
 
-// NativePlanet GroundSeg: Go Edition (goseg)
-// 🄯 2023 ~nallux-dozryl & ~sitful-hatred
-// This is a Golang rewrite of GroundSeg that serves the v2 json
-// object via websocket.
-// The v2 rewrite decouples the frontend and backend, which makes it
-// straightforward to implement alternative backends.
-//
-// Under development: reimplementing all pyseg functionality.
-// Advantages:
-// - Really, really fast
-// - Event-driven
-// - First-class support for concurrent operations
-// - Very good golang Docker libraries
-
 var (
 	logger  = slog.New(slog.NewJSONHandler(os.Stdout, nil))
 	DevMode = false
@@ -100,15 +102,17 @@ func main() {
 	// digest docker events from eventbus
 	go rectify.DockerSubscriptionHandler()
 	// just making sure we can parse (debug)
-	var pierList string
-	for _, pier := range conf.Piers {
-		pierList = pierList + ", " + pier
+	if len(conf.Piers) > 0 {
+		pierList := strings.Join(conf.Piers, ", ")
+		logger.Info(fmt.Sprintf("Loaded piers: %s", pierList))
 	}
-	logger.Info(pierList)
 	// block until returns
 	if remoteVersion == true {
 		<-versionUpdateChannel
 	}
+	if conf.WgRegistered == true {
+		startram.Retrieve()
+	}
 	// Load Netdata
 	loadService(docker.LoadNetdata, "Unable to load Netdata!")
 	// Load Wireguard

+ 31 - 0
startram/startram.go

@@ -42,3 +42,34 @@ func GetRegions() (map[string]structs.StartramRegion, error) {
 	}
 	return regions, nil
 }
+
+func Retrieve() (structs.StartramRetrieve, error) {
+	var retrieve structs.StartramRetrieve
+	conf := config.Conf()
+	regionUrl := "https://" + conf.EndpointUrl + "/v1/retrieve?pubkey=" + conf.Pubkey
+	resp, err := http.Get(regionUrl)
+	if err != nil {
+		errmsg := fmt.Sprintf("Unable to connect to API server: %v", err)
+		logger.Warn(errmsg)
+		return retrieve, err
+	}
+	// read response body
+	body, err := ioutil.ReadAll(resp.Body)
+	resp.Body.Close()
+	if err != nil {
+		errmsg := fmt.Sprintf("Error reading retrieve info: %v", err)
+		logger.Warn(errmsg)
+		return retrieve, err
+	}
+	// unmarshal values into struct
+	err = json.Unmarshal(body, &retrieve)
+	if err != nil {
+		errmsg := fmt.Sprintf("Error unmarshalling retrieve json: %v", err)
+		fmt.Println(string(body))
+		logger.Warn(errmsg)
+		return retrieve, err
+	}
+	config.StartramConfig = retrieve
+	logger.Info(fmt.Sprintf("StarTram info retrieved: %s", string(body)))
+	return retrieve, nil
+}

+ 0 - 6
structs/broadcast.go

@@ -135,12 +135,6 @@ type SetupBroadcast struct {
 	Regions   map[string]StartramRegion `json:"regions"`
 }
 
-// startram region server subobject
-type StartramRegion struct {
-	Country string `json:"country"`
-	Desc    string `json:"desc"`
-}
-
 // broadcast subobject
 type LoginStatus struct {
 	Locked   bool

+ 6 - 0
structs/startram.go

@@ -19,3 +19,9 @@ type StartramRetrieve struct {
 		URL     string `json:"url"`
 	} `json:"subdomains"`
 }
+
+// startram region server subobject
+type StartramRegion struct {
+	Country string `json:"country"`
+	Desc    string `json:"desc"`
+}