broadcast.go 11 KB

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