config.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package config
  2. // code for managing groundseg and container configurations
  3. import (
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "goseg/defaults"
  8. "goseg/structs"
  9. "io/ioutil"
  10. "log/slog"
  11. "math/rand"
  12. "net"
  13. "os"
  14. "path/filepath"
  15. "runtime"
  16. "sync"
  17. "time"
  18. )
  19. var (
  20. Logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
  21. // global settings config (accessed via funcs)
  22. globalConfig structs.SysConfig
  23. // base path for installation (override default with env var)
  24. BasePath = os.Getenv("GS_BASE_PATH")
  25. // only amd64 or arm64
  26. Architecture = getArchitecture()
  27. // struct of /retrieve blob
  28. StartramConfig structs.StartramRetrieve
  29. // unused for now, set with `./groundseg dev`
  30. DebugMode = false
  31. Ready = false
  32. // representation of desired/actual container states
  33. GSContainers = make(map[string]structs.ContainerState)
  34. DockerDir = "/var/lib/docker/volumes/"
  35. // version server check
  36. checkInterval = 5 * time.Minute
  37. confPath = filepath.Join(BasePath, "settings", "system.json")
  38. confMutex sync.Mutex
  39. contMutex sync.Mutex
  40. versMutex sync.Mutex
  41. )
  42. // try initializing from system.json on disk
  43. func init() {
  44. Logger.Info("Starting GroundSeg")
  45. Logger.Info("Urbit is love <3")
  46. for _, arg := range os.Args[1:] {
  47. // trigger this with `./groundseg dev`
  48. if arg == "dev" {
  49. Logger.Info("Starting GroundSeg in debug mode")
  50. DebugMode = true
  51. }
  52. }
  53. if BasePath == "" {
  54. // default base path
  55. BasePath = "/opt/nativeplanet/groundseg"
  56. }
  57. pathMsg := fmt.Sprintf("Loading configs from %s", BasePath)
  58. Logger.Info(pathMsg)
  59. confPath := filepath.Join(BasePath, "settings", "system.json")
  60. file, err := os.Open(confPath)
  61. if err != nil {
  62. // create a default if it doesn't exist
  63. err = createDefaultConf()
  64. if err != nil {
  65. // panic if we can't create it
  66. errmsg := fmt.Sprintf("Unable to create config! Please elevate permissions. %v", err)
  67. Logger.Error(errmsg)
  68. panic(errmsg)
  69. }
  70. // generate and insert wireguard keys
  71. wgPriv, wgPub, err := WgKeyGen()
  72. salt := RandString(32)
  73. if err != nil {
  74. Logger.Error(fmt.Sprintf("%v", err))
  75. } else {
  76. err = UpdateConf(map[string]interface{}{
  77. "Pubkey": wgPub,
  78. "Privkey": wgPriv,
  79. "Salt": salt,
  80. })
  81. if err != nil {
  82. Logger.Error(fmt.Sprintf("%v", err))
  83. }
  84. }
  85. }
  86. defer file.Close()
  87. // read the sysconfig to memory
  88. decoder := json.NewDecoder(file)
  89. err = decoder.Decode(&globalConfig)
  90. if err != nil {
  91. errmsg := fmt.Sprintf("Error decoding JSON: %v", err)
  92. Logger.Error(errmsg)
  93. }
  94. // wipe the sessions on each startup
  95. globalConfig.Sessions.Authorized = make(map[string]structs.SessionInfo)
  96. globalConfig.Sessions.Unauthorized = make(map[string]structs.SessionInfo)
  97. configMap := make(map[string]interface{})
  98. configBytes, err := json.Marshal(globalConfig)
  99. if err != nil {
  100. errmsg := fmt.Sprintf("Error marshaling JSON: %v", err)
  101. Logger.Error(errmsg)
  102. }
  103. err = json.Unmarshal(configBytes, &configMap)
  104. if err != nil {
  105. errmsg := fmt.Sprintf("Error unmarshaling JSON: %v", err)
  106. Logger.Error(errmsg)
  107. }
  108. err = persistConf(configMap)
  109. if err != nil {
  110. errmsg := fmt.Sprintf("Error persisting JSON: %v", err)
  111. Logger.Error(errmsg)
  112. }
  113. file, err = os.Open(confPath)
  114. if err != nil {
  115. errmsg := fmt.Sprintf("Error opening JSON: %v", err)
  116. Logger.Error(errmsg)
  117. }
  118. decoder = json.NewDecoder(file)
  119. err = decoder.Decode(&globalConfig)
  120. if err != nil {
  121. errmsg := fmt.Sprintf("Error decoding JSON: %v", err)
  122. Logger.Error(errmsg)
  123. }
  124. }
  125. // return the global conf var
  126. func Conf() structs.SysConfig {
  127. confMutex.Lock()
  128. defer confMutex.Unlock()
  129. return globalConfig
  130. }
  131. // tell if we're amd64 or arm64
  132. func getArchitecture() string {
  133. switch runtime.GOARCH {
  134. case "arm64", "aarch64":
  135. return "arm64"
  136. default:
  137. return "amd64"
  138. }
  139. }
  140. // update by passing in a map of key:values you want to modify
  141. func UpdateConf(values map[string]interface{}) error {
  142. // mutex lock to avoid race conditions
  143. confMutex.Lock()
  144. defer confMutex.Unlock()
  145. file, err := ioutil.ReadFile(confPath)
  146. if err != nil {
  147. return fmt.Errorf("Unable to load config: %v", err)
  148. }
  149. // unmarshal the config to struct
  150. var configMap map[string]interface{}
  151. if err := json.Unmarshal(file, &configMap); err != nil {
  152. return fmt.Errorf("Error decoding JSON: %v", err)
  153. }
  154. // update our unmarshaled struct
  155. for key, value := range values {
  156. configMap[key] = value
  157. }
  158. if err = persistConf(configMap); err != nil {
  159. return fmt.Errorf("Unable to persist config update: %v", err)
  160. }
  161. return nil
  162. }
  163. func persistConf(configMap map[string]interface{}) error {
  164. if BasePath == "" {
  165. // default base path
  166. BasePath = "/opt/nativeplanet/groundseg"
  167. }
  168. // marshal and persist it
  169. updatedJSON, err := json.MarshalIndent(configMap, "", " ")
  170. if err != nil {
  171. return fmt.Errorf("Error encoding JSON: %v", err)
  172. }
  173. // update the globalConfig var
  174. if err := json.Unmarshal(updatedJSON, &globalConfig); err != nil {
  175. return fmt.Errorf("Error updating global config: %v", err)
  176. }
  177. // write to disk
  178. Logger.Info("Persisting configuration to disk %v",string(updatedJson))
  179. if err := ioutil.WriteFile(confPath, updatedJSON, 0644); err != nil {
  180. return fmt.Errorf("Error writing to file: %v", err)
  181. }
  182. return nil
  183. }
  184. // we keep map[string]structs.ContainerState in memory to keep track of the containers
  185. // eg if they're running and whether they should be
  186. // modify the desired/actual state of containers
  187. func UpdateContainerState(name string, containerState structs.ContainerState) {
  188. contMutex.Lock()
  189. defer contMutex.Unlock()
  190. GSContainers[name] = containerState
  191. logMsg := "<hidden>"
  192. if DebugMode {
  193. res, _ := json.Marshal(containerState)
  194. logMsg = string(res)
  195. }
  196. Logger.Info(fmt.Sprintf("%s state:%s", name, logMsg))
  197. }
  198. // get the current container state
  199. func GetContainerState() map[string]structs.ContainerState {
  200. contMutex.Lock()
  201. defer contMutex.Unlock()
  202. return GSContainers
  203. }
  204. // write a default conf to disk
  205. func createDefaultConf() error {
  206. defaultConfig := defaults.SysConfig(BasePath)
  207. path := filepath.Join(BasePath, "settings", "system.json")
  208. if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
  209. return err
  210. }
  211. file, err := os.Create(path)
  212. if err != nil {
  213. return err
  214. }
  215. defer file.Close()
  216. encoder := json.NewEncoder(file)
  217. encoder.SetIndent("", " ")
  218. if err := encoder.Encode(&defaultConfig); err != nil {
  219. return err
  220. }
  221. return nil
  222. }
  223. // check outbound tcp connectivity
  224. // takes ip:port
  225. func NetCheck(netCheck string) bool {
  226. internet := false
  227. timeout := 3 * time.Second
  228. conn, err := net.DialTimeout("tcp", netCheck, timeout)
  229. if err != nil {
  230. errmsg := fmt.Sprintf("Check internet access error: %v", err)
  231. Logger.Error(errmsg)
  232. } else {
  233. internet = true
  234. _ = conn.Close()
  235. }
  236. return internet
  237. }
  238. // generates a random secret string of the input length
  239. func RandString(length int) string {
  240. randBytes := make([]byte, length)
  241. _, err := rand.Read(randBytes)
  242. if err != nil {
  243. Logger.Warn("Random error :s")
  244. return ""
  245. }
  246. return base64.URLEncoding.EncodeToString(randBytes)
  247. }