config.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "goseg/structs"
  6. "io/ioutil"
  7. "log/slog"
  8. "net"
  9. "net/http"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. "sync"
  14. "time"
  15. )
  16. var (
  17. globalConfig structs.SysConfig
  18. logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
  19. BasePath = "/opt/nativeplanet/groundseg"
  20. Version = "v2.0.0"
  21. Architecture = getArchitecture()
  22. DebugMode = false
  23. Ready = false
  24. VersionServerReady = false
  25. VersionInfo structs.Version
  26. GSContainers = make(map[string]structs.ContainerState)
  27. checkInterval = 5 * time.Minute
  28. confMutex sync.Mutex
  29. contMutex sync.Mutex
  30. versMutex sync.Mutex
  31. )
  32. // try initializing from system.json on disk
  33. func init() {
  34. for _, arg := range os.Args[1:] {
  35. // trigger this with `./groundseg dev`
  36. if arg == "dev" {
  37. logger.Info("Starting GroundSeg in debug mode")
  38. DebugMode = true
  39. }
  40. }
  41. pathMsg := fmt.Sprintf("Loading configs from %s", BasePath)
  42. logger.Info(pathMsg)
  43. confPath := filepath.Join(BasePath, "settings", "system.json")
  44. file, err := os.Open(confPath)
  45. if err != nil {
  46. // create a default if it doesn't exist
  47. err = createDefaultConf()
  48. if err != nil {
  49. // panic if we can't create it
  50. errmsg := fmt.Sprintf("Unable to create config! %v", err)
  51. logger.Error(errmsg)
  52. panic(errmsg)
  53. }
  54. }
  55. defer file.Close()
  56. decoder := json.NewDecoder(file)
  57. err = decoder.Decode(&globalConfig)
  58. if err != nil {
  59. errmsg := fmt.Sprintf("Error decoding JSON: %v", err)
  60. logger.Error(errmsg)
  61. }
  62. }
  63. // return the global conf var
  64. func Conf() structs.SysConfig {
  65. confMutex.Lock()
  66. defer confMutex.Unlock()
  67. return globalConfig
  68. }
  69. // tell if we're amd64 or arm64
  70. func getArchitecture() string {
  71. switch runtime.GOARCH {
  72. case "arm64", "aarch64":
  73. return "arm64"
  74. default:
  75. return "amd64"
  76. }
  77. }
  78. // update by passing in a map of key:values you want to modify
  79. func UpdateConf(values map[string]interface{}) error {
  80. // mutex lock to avoid race conditions
  81. confMutex.Lock()
  82. defer confMutex.Unlock()
  83. confPath := filepath.Join(BasePath, "settings", "system.json")
  84. file, err := ioutil.ReadFile(confPath)
  85. if err != nil {
  86. errmsg := fmt.Sprintf("Unable to load config: %v", err)
  87. logger.Error(errmsg)
  88. return err
  89. }
  90. // unmarshal the config to struct
  91. var configMap map[string]interface{}
  92. if err := json.Unmarshal(file, &configMap); err != nil {
  93. errmsg := fmt.Sprintf("Error decoding JSON: %v", err)
  94. logger.Error(errmsg)
  95. return err
  96. }
  97. // update our unmarshaled struct
  98. for key, value := range values {
  99. configMap[key] = value
  100. }
  101. // marshal and persist it
  102. updatedJSON, err := json.MarshalIndent(configMap, "", " ")
  103. if err != nil {
  104. errmsg := fmt.Sprintf("Error encoding JSON: %v", err)
  105. logger.Error(errmsg)
  106. return err
  107. }
  108. // update the globalConfig var
  109. if err := json.Unmarshal(updatedJSON, &globalConfig); err != nil {
  110. errmsg := fmt.Sprintf("Error updating global config: %v", err)
  111. logger.Error(errmsg)
  112. return err
  113. }
  114. if err := ioutil.WriteFile(confPath, updatedJSON, 0644); err != nil {
  115. errmsg := fmt.Sprintf("Error writing to file: %v", err)
  116. logger.Error(errmsg)
  117. return err
  118. }
  119. return nil
  120. }
  121. // modify the desired/actual state of containers
  122. func UpdateContainerState(name string, containerState structs.ContainerState) {
  123. contMutex.Lock()
  124. defer contMutex.Unlock()
  125. GSContainers[name] = containerState
  126. res, _ := json.Marshal(containerState)
  127. logger.Info(fmt.Sprintf("%s:%s", name, string(res)))
  128. }
  129. // get the current container state
  130. func GetContainerState() map[string]structs.ContainerState {
  131. contMutex.Lock()
  132. defer contMutex.Unlock()
  133. return GSContainers
  134. }
  135. // write a default conf to disk
  136. func createDefaultConf() error {
  137. defaultConfig := structs.SysConfig{
  138. Setup: "start",
  139. EndpointUrl: "api.startram.io",
  140. ApiVersion: "v1",
  141. Piers: []string{},
  142. NetCheck: "1.1.1.1:53",
  143. UpdateMode: "auto",
  144. UpdateUrl: "https://version.groundseg.app",
  145. UpdateBranch: "latest",
  146. SwapVal: 16,
  147. SwapFile: filepath.Join(BasePath, "settings", "swapfile"),
  148. KeyFile: filepath.Join(BasePath, "settings", "session.key"),
  149. Sessions: struct {
  150. Authorized map[string]structs.SessionInfo `json:"authorized"`
  151. Unauthorized map[string]structs.SessionInfo `json:"unauthorized"`
  152. }{
  153. Authorized: make(map[string]structs.SessionInfo),
  154. Unauthorized: make(map[string]structs.SessionInfo),
  155. },
  156. LinuxUpdates: struct {
  157. Value int `json:"value"`
  158. Interval string `json:"interval"`
  159. Previous bool `json:"previous"`
  160. }{
  161. Value: 1,
  162. Interval: "week",
  163. Previous: false,
  164. },
  165. DockerData: "/var/lib/docker",
  166. WgOn: false,
  167. WgRegistered: false,
  168. PwHash: "",
  169. C2cInterval: 0,
  170. FirstBoot: true,
  171. GsVersion: Version,
  172. CfgDir: "",
  173. UpdateInterval: 0,
  174. BinHash: "",
  175. Pubkey: "",
  176. Privkey: "",
  177. Salt: "",
  178. }
  179. path := filepath.Join(BasePath, "settings", "system.json")
  180. if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
  181. return err
  182. }
  183. file, err := os.Create(path)
  184. if err != nil {
  185. return err
  186. }
  187. defer file.Close()
  188. encoder := json.NewEncoder(file)
  189. encoder.SetIndent("", " ")
  190. if err := encoder.Encode(&defaultConfig); err != nil {
  191. return err
  192. }
  193. return nil
  194. }
  195. // check outbound tcp connectivity
  196. // takes ip:port
  197. func NetCheck(netCheck string) bool {
  198. logger.Info("Checking internet access")
  199. internet := false
  200. timeout := 3 * time.Second
  201. conn, err := net.DialTimeout("tcp", netCheck, timeout)
  202. if err != nil {
  203. errmsg := fmt.Sprintf("Check internet access error: %v", err)
  204. logger.Error(errmsg)
  205. } else {
  206. internet = true
  207. _ = conn.Close()
  208. }
  209. return internet
  210. }
  211. // check the version server and return unmarshaled result
  212. func CheckVersion() (structs.Version, bool) {
  213. versMutex.Lock()
  214. defer versMutex.Unlock()
  215. const retries = 10
  216. const delay = time.Second
  217. url := globalConfig.UpdateUrl
  218. for i := 0; i < retries; i++ {
  219. resp, err := http.Get(url)
  220. if err != nil {
  221. errmsg := fmt.Sprintf("Unable to connect to update server: %v", err)
  222. logger.Warn(errmsg)
  223. if i < retries-1 {
  224. time.Sleep(delay)
  225. continue
  226. } else {
  227. return VersionInfo, false
  228. }
  229. }
  230. // read the body bytes
  231. body, err := ioutil.ReadAll(resp.Body)
  232. resp.Body.Close()
  233. if err != nil {
  234. errmsg := fmt.Sprintf("Error reading version info: %v", err)
  235. logger.Warn(errmsg)
  236. if i < retries-1 {
  237. time.Sleep(delay)
  238. continue
  239. } else {
  240. return VersionInfo, false
  241. }
  242. }
  243. // unmarshal values into Version struct
  244. err = json.Unmarshal(body, &VersionInfo)
  245. if err != nil {
  246. errmsg := fmt.Sprintf("Error unmarshalling JSON: %v", err)
  247. logger.Warn(errmsg)
  248. if i < retries-1 {
  249. time.Sleep(delay)
  250. continue
  251. } else {
  252. return VersionInfo, false
  253. }
  254. }
  255. // debug: re-marshal and write to disk
  256. confPath := filepath.Join(BasePath, "settings", "version_info.json")
  257. file, err := os.Create(confPath)
  258. if err != nil {
  259. errmsg := fmt.Sprintf("Failed to create file: %v", err)
  260. logger.Error(errmsg)
  261. return VersionInfo, false
  262. }
  263. defer file.Close()
  264. encoder := json.NewEncoder(file)
  265. encoder.SetIndent("", " ")
  266. if err := encoder.Encode(&VersionInfo); err != nil {
  267. errmsg := fmt.Sprintf("Failed to write JSON: %v", err)
  268. logger.Error(errmsg)
  269. }
  270. return VersionInfo, true
  271. }
  272. return VersionInfo, false
  273. }
  274. func CheckVersionLoop() {
  275. ticker := time.NewTicker(checkInterval)
  276. for {
  277. select {
  278. case <-ticker.C:
  279. latestVersion, _ := CheckVersion()
  280. currentVersion := VersionInfo
  281. if latestVersion != currentVersion {
  282. fmt.Printf("New version available! Current: %s, Latest: %s\n", currentVersion, latestVersion)
  283. VersionInfo = latestVersion
  284. // Handle the update logic here
  285. }
  286. }
  287. }
  288. }