broadcast.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package broadcast
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "goseg/auth"
  6. "goseg/config"
  7. "goseg/docker"
  8. "goseg/startram"
  9. "goseg/structs"
  10. "goseg/system"
  11. "math"
  12. "os"
  13. "reflect"
  14. "strings"
  15. "sync"
  16. "time"
  17. "github.com/gorilla/websocket"
  18. )
  19. var (
  20. clients = make(map[*websocket.Conn]bool)
  21. hostInfoInterval = 3 * time.Second // how often we refresh system info
  22. shipInfoInterval = 3 * time.Second // how often we refresh ship info
  23. broadcastState structs.AuthBroadcast
  24. unauthState structs.UnauthBroadcast
  25. mu sync.RWMutex // synchronize access to broadcastState
  26. )
  27. func init() {
  28. // initialize broadcastState global var
  29. conf := config.Conf()
  30. broadcast, err := bootstrapBroadcastState(conf)
  31. if err != nil {
  32. errmsg := fmt.Sprintf("Unable to initialize broadcast: %v", err)
  33. panic(errmsg)
  34. }
  35. broadcastState = broadcast
  36. }
  37. // take in config file and addt'l info to initialize broadcast
  38. func bootstrapBroadcastState(conf structs.SysConfig) (structs.AuthBroadcast, error) {
  39. config.Logger.Info("Bootstrapping state")
  40. var res structs.AuthBroadcast
  41. // get a list of piers from config
  42. piers := conf.Piers
  43. // this returns a map of ship:running status
  44. config.Logger.Info("Resolving pier status")
  45. updates, err := constructPierInfo(piers)
  46. if err != nil {
  47. return res, err
  48. }
  49. // update broadcastState
  50. err = UpdateBroadcastState(map[string]interface{}{
  51. "Urbits": updates,
  52. })
  53. if err != nil {
  54. errmsg := fmt.Sprintf("Unable to update broadcast state: %v", err)
  55. config.Logger.Error(errmsg)
  56. return res, err
  57. }
  58. // wgRegistered := config.WgRegistered
  59. // wgOn := config.WgOn
  60. // get startram regions
  61. config.Logger.Info("Retrieving StarTram region info")
  62. regions, err := startram.GetRegions()
  63. if err != nil {
  64. config.Logger.Warn("Couldn't get StarTram regions")
  65. } else {
  66. updates := map[string]interface{}{
  67. "Profile": map[string]interface{}{
  68. "Startram": map[string]interface{}{
  69. "Info": map[string]interface{}{
  70. "Regions": regions,
  71. },
  72. },
  73. },
  74. }
  75. err := UpdateBroadcastState(updates)
  76. if err != nil {
  77. errmsg := fmt.Sprintf("Error updating broadcast state:", err)
  78. config.Logger.Error(errmsg)
  79. }
  80. }
  81. // update with system state
  82. sysInfo := constructSystemInfo()
  83. err = UpdateBroadcastState(sysInfo)
  84. if err != nil {
  85. errmsg := fmt.Sprintf("Error updating broadcast state:", err)
  86. config.Logger.Error(errmsg)
  87. }
  88. // start looping info refreshes
  89. go hostStatusLoop()
  90. go shipStatusLoop()
  91. // return the boostrapped result
  92. res = GetState()
  93. return res, nil
  94. }
  95. // this is for building the broadcast objects describing piers
  96. func constructPierInfo(piers []string) (map[string]structs.Urbit, error) {
  97. updates := make(map[string]structs.Urbit)
  98. // load fresh broadcast state
  99. currentState := GetState()
  100. // get the networks containers are attached to
  101. shipNetworks := GetContainerNetworks(piers)
  102. // find out whether they're running
  103. pierStatus, err := docker.GetShipStatus(piers)
  104. if err != nil {
  105. errmsg := fmt.Sprintf("Unable to bootstrap urbit states: %v", err)
  106. config.Logger.Error(errmsg)
  107. return updates, err
  108. }
  109. hostName, err := os.Hostname()
  110. if err != nil {
  111. errmsg := fmt.Sprintf("Error getting hostname, defaulting to `nativeplanet`: %v", err)
  112. config.Logger.Warn(errmsg)
  113. hostName = "nativeplanet"
  114. }
  115. // convert the running status into bools
  116. for pier, status := range pierStatus {
  117. // pull urbit info from json
  118. err := config.LoadUrbitConfig(pier)
  119. if err != nil {
  120. errmsg := fmt.Sprintf("Unable to load %s config: %v", pier, err)
  121. config.Logger.Error(errmsg)
  122. continue
  123. }
  124. dockerConfig := config.UrbitConf(pier)
  125. // get container stats from docker
  126. var dockerStats structs.ContainerStats
  127. dockerStats, err = docker.GetContainerStats(pier)
  128. if err != nil {
  129. errmsg := fmt.Sprintf("Unable to load %s stats: %v", pier, err)
  130. config.Logger.Error(errmsg)
  131. continue
  132. }
  133. urbit := structs.Urbit{}
  134. if existingUrbit, exists := currentState.Urbits[pier]; exists {
  135. // If the ship already exists in broadcastState, use its current state
  136. urbit = existingUrbit
  137. }
  138. isRunning := (status == "Up" || strings.HasPrefix(status, "Up "))
  139. bootStatus := true
  140. if dockerConfig.BootStatus == "ignore" {
  141. bootStatus = false
  142. }
  143. setRemote := false
  144. if dockerConfig.Network == "wireguard" {
  145. setRemote = true
  146. }
  147. // collate all the info from our sources into the struct
  148. urbit.Info.Running = isRunning
  149. urbit.Info.Network = shipNetworks[pier]
  150. urbit.Info.URL = fmt.Sprintf("http://%s.local:%d", hostName, dockerConfig.HTTPPort)
  151. urbit.Info.LoomSize = int(math.Pow(2, float64(dockerConfig.LoomSize)) / math.Pow(1024, 2))
  152. urbit.Info.DiskUsage = dockerStats.DiskUsage
  153. urbit.Info.MemUsage = dockerStats.MemoryUsage
  154. urbit.Info.DevMode = dockerConfig.DevMode
  155. urbit.Info.Vere = dockerConfig.UrbitVersion
  156. urbit.Info.DetectBootStatus = bootStatus
  157. urbit.Info.Remote = setRemote
  158. urbit.Info.Vere = dockerConfig.UrbitVersion
  159. // and insert the struct into the map we will use as input for the broadcast struct
  160. updates[pier] = urbit
  161. }
  162. return updates, nil
  163. }
  164. // put together the system[usage] subobject
  165. func constructSystemInfo() map[string]interface{} {
  166. var res map[string]interface{}
  167. var ramObj []uint64
  168. var diskObj []uint64
  169. usedRam, totalRam := system.GetMemory()
  170. ramObj = append(ramObj, usedRam, totalRam)
  171. cpuUsage := system.GetCPU()
  172. cpuTemp := system.GetTemp()
  173. usedDisk, freeDisk := system.GetDisk()
  174. diskObj = append(diskObj, usedDisk, freeDisk)
  175. swapVal := system.HasSwap()
  176. res = map[string]interface{}{
  177. "System": map[string]interface{}{
  178. "Usage": map[string]interface{}{
  179. "RAM": ramObj,
  180. "CPU": cpuUsage,
  181. "CPUTemp": cpuTemp,
  182. "Disk": diskObj,
  183. "SwapFile": swapVal,
  184. },
  185. },
  186. }
  187. return res
  188. }
  189. // return a map of ships and their networks
  190. func GetContainerNetworks(containers []string) map[string]string {
  191. res := make(map[string]string)
  192. for _, container := range containers {
  193. network, err := docker.GetContainerNetwork(container)
  194. if err != nil {
  195. errmsg := fmt.Sprintf("Error getting container network: %v", err)
  196. config.Logger.Error(errmsg)
  197. continue
  198. } else {
  199. res[container] = network
  200. }
  201. }
  202. return res
  203. }
  204. // update broadcastState with a map of items
  205. func UpdateBroadcastState(values map[string]interface{}) error {
  206. mu.Lock()
  207. v := reflect.ValueOf(&broadcastState).Elem()
  208. for key, value := range values {
  209. field := v.FieldByName(key)
  210. if !field.IsValid() || !field.CanSet() {
  211. mu.Unlock()
  212. return fmt.Errorf("field %s does not exist or is not settable", key)
  213. }
  214. val := reflect.ValueOf(value)
  215. if val.Kind() == reflect.Interface {
  216. val = val.Elem() // Extract the underlying value from the interface
  217. }
  218. if err := recursiveUpdate(field, val); err != nil {
  219. mu.Unlock()
  220. return fmt.Errorf("error updating field %s: %v", key, err)
  221. return err
  222. }
  223. }
  224. mu.Unlock()
  225. BroadcastToClients()
  226. return nil
  227. }
  228. // this allows us to insert stuff into nested structs/keys and not overwrite the existing contents
  229. func recursiveUpdate(dst, src reflect.Value) error {
  230. if !dst.CanSet() {
  231. return fmt.Errorf("field (type: %s, kind: %s) is not settable", dst.Type(), dst.Kind())
  232. }
  233. // If dst is a struct and src is a map, handle them field by field
  234. if dst.Kind() == reflect.Struct && src.Kind() == reflect.Map {
  235. for _, key := range src.MapKeys() {
  236. dstField := dst.FieldByName(key.String())
  237. if !dstField.IsValid() {
  238. return fmt.Errorf("field %s does not exist in the struct", key.String())
  239. }
  240. // Initialize the map if it's nil and we're trying to set a map
  241. if dstField.Kind() == reflect.Map && dstField.IsNil() && src.MapIndex(key).Kind() == reflect.Map {
  242. dstField.Set(reflect.MakeMap(dstField.Type()))
  243. }
  244. if !dstField.CanSet() {
  245. return fmt.Errorf("field %s is not settable in the struct", key.String())
  246. }
  247. srcVal := src.MapIndex(key)
  248. if srcVal.Kind() == reflect.Interface {
  249. srcVal = srcVal.Elem()
  250. }
  251. if err := recursiveUpdate(dstField, srcVal); err != nil {
  252. return err
  253. }
  254. }
  255. return nil
  256. }
  257. // If both dst and src are maps, handle them recursively
  258. if dst.Kind() == reflect.Map && src.Kind() == reflect.Map {
  259. for _, key := range src.MapKeys() {
  260. srcVal := src.MapIndex(key)
  261. // If the key doesn't exist in dst, initialize it
  262. dstVal := dst.MapIndex(key)
  263. if !dstVal.IsValid() {
  264. dstVal = reflect.New(dst.Type().Elem()).Elem()
  265. }
  266. // Recursive call to handle potential nested maps or structs
  267. if err := recursiveUpdate(dstVal, srcVal); err != nil {
  268. return err
  269. }
  270. // Initialize the map if it's nil
  271. if dst.IsNil() {
  272. dst.Set(reflect.MakeMap(dst.Type()))
  273. }
  274. dst.SetMapIndex(key, dstVal)
  275. }
  276. return nil
  277. }
  278. // For non-map or non-struct fields, or for direct updates
  279. if dst.Type() != src.Type() {
  280. return fmt.Errorf("type mismatch: expected %s, got %s", dst.Type(), src.Type())
  281. }
  282. dst.Set(src)
  283. return nil
  284. }
  285. // return broadcast state
  286. func GetState() structs.AuthBroadcast {
  287. mu.Lock()
  288. defer mu.Unlock()
  289. return broadcastState
  290. }
  291. // return json string of current broadcast state
  292. func GetStateJson() ([]byte, error) {
  293. bState := GetState()
  294. //temp
  295. bState.Type = "structure"
  296. bState.AuthLevel = "authorized"
  297. //end temp
  298. broadcastJson, err := json.Marshal(bState)
  299. if err != nil {
  300. errmsg := fmt.Sprintf("Error marshalling response: %v", err)
  301. config.Logger.Error(errmsg)
  302. return nil, err
  303. }
  304. return broadcastJson, nil
  305. }
  306. // broadcast the global state to auth'd clients
  307. func BroadcastToClients() error {
  308. authJson, err := GetStateJson()
  309. if err != nil {
  310. errmsg := fmt.Errorf("Error marshalling auth broadcast:", err)
  311. return errmsg
  312. }
  313. auth.AuthenticatedClients.Lock()
  314. defer auth.AuthenticatedClients.Unlock()
  315. for client := range auth.AuthenticatedClients.Conns {
  316. if err := auth.AuthenticatedClients.Conns[client].WriteMessage(websocket.TextMessage, authJson); err != nil {
  317. continue
  318. }
  319. }
  320. // for debug, remove me
  321. // for client := range clients {
  322. // if err := client.WriteMessage(websocket.TextMessage, authJson); err != nil {
  323. // config.Logger.Error(fmt.Sprintf("Error writing response: %v", err))
  324. // return err
  325. // }
  326. // }
  327. return nil
  328. }
  329. // refresh loop for host info
  330. func hostStatusLoop() {
  331. ticker := time.NewTicker(hostInfoInterval)
  332. for {
  333. select {
  334. case <-ticker.C:
  335. update := constructSystemInfo()
  336. err := UpdateBroadcastState(update)
  337. if err != nil {
  338. config.Logger.Warn(fmt.Sprintf("Error updating system status: %v", err))
  339. }
  340. }
  341. }
  342. }
  343. // refresh loop for ship info
  344. // for reasons beyond my iq we can't do this through the normal update func
  345. func shipStatusLoop() {
  346. ticker := time.NewTicker(hostInfoInterval)
  347. for {
  348. select {
  349. case <-ticker.C:
  350. conf := config.Conf()
  351. piers := conf.Piers
  352. updates, err := constructPierInfo(piers)
  353. if err != nil {
  354. errmsg := fmt.Sprintf("Unable to build pier info: %v", err)
  355. config.Logger.Warn(errmsg)
  356. continue
  357. }
  358. mu.Lock() // Locking the mutex
  359. for key, urbit := range updates {
  360. broadcastState.Urbits[key] = urbit
  361. }
  362. mu.Unlock() // Unlocking the mutex
  363. BroadcastToClients()
  364. }
  365. }
  366. }