config.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. // we keep map[string]structs.ContainerState in memory to keep track of the containers
  122. // eg if they're running and whether they should be
  123. // modify the desired/actual state of containers
  124. func UpdateContainerState(name string, containerState structs.ContainerState) {
  125. contMutex.Lock()
  126. defer contMutex.Unlock()
  127. GSContainers[name] = containerState
  128. res, _ := json.Marshal(containerState)
  129. logger.Info(fmt.Sprintf("%s:%s", name, string(res)))
  130. }
  131. // get the current container state
  132. func GetContainerState() map[string]structs.ContainerState {
  133. contMutex.Lock()
  134. defer contMutex.Unlock()
  135. return GSContainers
  136. }
  137. // write a default conf to disk
  138. func createDefaultConf() error {
  139. defaultConfig := structs.SysConfig{
  140. Setup: "start",
  141. EndpointUrl: "api.startram.io",
  142. ApiVersion: "v1",
  143. Piers: []string{},
  144. NetCheck: "1.1.1.1:53",
  145. UpdateMode: "auto",
  146. UpdateUrl: "https://version.groundseg.app",
  147. UpdateBranch: "latest",
  148. SwapVal: 16,
  149. SwapFile: filepath.Join(BasePath, "settings", "swapfile"),
  150. KeyFile: filepath.Join(BasePath, "settings", "session.key"),
  151. Sessions: struct {
  152. Authorized map[string]structs.SessionInfo `json:"authorized"`
  153. Unauthorized map[string]structs.SessionInfo `json:"unauthorized"`
  154. }{
  155. Authorized: make(map[string]structs.SessionInfo),
  156. Unauthorized: make(map[string]structs.SessionInfo),
  157. },
  158. LinuxUpdates: struct {
  159. Value int `json:"value"`
  160. Interval string `json:"interval"`
  161. Previous bool `json:"previous"`
  162. }{
  163. Value: 1,
  164. Interval: "week",
  165. Previous: false,
  166. },
  167. DockerData: "/var/lib/docker",
  168. WgOn: false,
  169. WgRegistered: false,
  170. PwHash: "",
  171. C2cInterval: 0,
  172. FirstBoot: true,
  173. GsVersion: Version,
  174. CfgDir: "",
  175. UpdateInterval: 0,
  176. BinHash: "",
  177. Pubkey: "",
  178. Privkey: "",
  179. Salt: "",
  180. }
  181. path := filepath.Join(BasePath, "settings", "system.json")
  182. if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
  183. return err
  184. }
  185. file, err := os.Create(path)
  186. if err != nil {
  187. return err
  188. }
  189. defer file.Close()
  190. encoder := json.NewEncoder(file)
  191. encoder.SetIndent("", " ")
  192. if err := encoder.Encode(&defaultConfig); err != nil {
  193. return err
  194. }
  195. return nil
  196. }
  197. // check outbound tcp connectivity
  198. // takes ip:port
  199. func NetCheck(netCheck string) bool {
  200. logger.Info("Checking internet access")
  201. internet := false
  202. timeout := 3 * time.Second
  203. conn, err := net.DialTimeout("tcp", netCheck, timeout)
  204. if err != nil {
  205. errmsg := fmt.Sprintf("Check internet access error: %v", err)
  206. logger.Error(errmsg)
  207. } else {
  208. internet = true
  209. _ = conn.Close()
  210. }
  211. return internet
  212. }
  213. // check the version server and return unmarshaled result
  214. func CheckVersion() (structs.Version, bool) {
  215. versMutex.Lock()
  216. defer versMutex.Unlock()
  217. const retries = 10
  218. const delay = time.Second
  219. url := globalConfig.UpdateUrl
  220. for i := 0; i < retries; i++ {
  221. resp, err := http.Get(url)
  222. if err != nil {
  223. errmsg := fmt.Sprintf("Unable to connect to update server: %v", err)
  224. logger.Warn(errmsg)
  225. if i < retries-1 {
  226. time.Sleep(delay)
  227. continue
  228. } else {
  229. return VersionInfo, false
  230. }
  231. }
  232. // read the body bytes
  233. body, err := ioutil.ReadAll(resp.Body)
  234. resp.Body.Close()
  235. if err != nil {
  236. errmsg := fmt.Sprintf("Error reading version info: %v", err)
  237. logger.Warn(errmsg)
  238. if i < retries-1 {
  239. time.Sleep(delay)
  240. continue
  241. } else {
  242. return VersionInfo, false
  243. }
  244. }
  245. // unmarshal values into Version struct
  246. err = json.Unmarshal(body, &VersionInfo)
  247. if err != nil {
  248. errmsg := fmt.Sprintf("Error unmarshalling JSON: %v", err)
  249. logger.Warn(errmsg)
  250. if i < retries-1 {
  251. time.Sleep(delay)
  252. continue
  253. } else {
  254. return VersionInfo, false
  255. }
  256. }
  257. // debug: re-marshal and write to disk
  258. confPath := filepath.Join(BasePath, "settings", "version_info.json")
  259. file, err := os.Create(confPath)
  260. if err != nil {
  261. errmsg := fmt.Sprintf("Failed to create file: %v", err)
  262. logger.Error(errmsg)
  263. return VersionInfo, false
  264. }
  265. defer file.Close()
  266. encoder := json.NewEncoder(file)
  267. encoder.SetIndent("", " ")
  268. if err := encoder.Encode(&VersionInfo); err != nil {
  269. errmsg := fmt.Sprintf("Failed to write JSON: %v", err)
  270. logger.Error(errmsg)
  271. }
  272. return VersionInfo, true
  273. }
  274. return VersionInfo, false
  275. }
  276. func CheckVersionLoop() {
  277. ticker := time.NewTicker(checkInterval)
  278. for {
  279. select {
  280. case <-ticker.C:
  281. latestVersion, _ := CheckVersion()
  282. currentVersion := VersionInfo
  283. if latestVersion != currentVersion {
  284. fmt.Printf("New version available! Current: %s, Latest: %s\n", currentVersion, latestVersion)
  285. VersionInfo = latestVersion
  286. // Handle the update logic here
  287. }
  288. }
  289. }
  290. }