config.go 7.1 KB

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