config.go 6.9 KB

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