handler.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package updates
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "runtime"
  9. "time"
  10. "github.com/gorilla/websocket"
  11. "imuslab.com/arozos/mod/common"
  12. )
  13. type UpdateConfig struct {
  14. Vendor string `json:"vendor"`
  15. Binary struct {
  16. Windows struct {
  17. Amd64 string `json:"amd64"`
  18. Arm string `json:"arm"`
  19. Arm64 string `json:"arm64"`
  20. I386 string `json:"i386"`
  21. } `json:"windows"`
  22. Linux struct {
  23. Arm string `json:"arm"`
  24. Armv7 string `json:"armv7"`
  25. Arm64 string `json:"arm64"`
  26. Amd64 string `json:"amd64"`
  27. Mipsle string `json:"mipsle"`
  28. } `json:"linux"`
  29. Darwin struct {
  30. Amd64 string `json:"amd64"`
  31. Arm64 string `json:"arm64"`
  32. } `json:"darwin"`
  33. Freebsd struct {
  34. Amd64 string `json:"amd64"`
  35. Arm string `json:"arm"`
  36. Arm64 string `json:"arm64"`
  37. I386 string `json:"i386"`
  38. } `json:"freebsd"`
  39. } `json:"binary"`
  40. Webpack string `json:"webpack"`
  41. }
  42. func HandleUpdateCheckSize(w http.ResponseWriter, r *http.Request) {
  43. webpack, err := common.Mv(r, "webpack", true)
  44. if err != nil {
  45. common.SendErrorResponse(w, "Invalid or empty webpack download URL")
  46. return
  47. }
  48. binary, err := common.Mv(r, "binary", true)
  49. if err != nil {
  50. common.SendErrorResponse(w, "Invalid or empty binary download URL")
  51. return
  52. }
  53. bsize, wsize, err := GetUpdateSizes(binary, webpack)
  54. if err != nil {
  55. common.SendErrorResponse(w, "Failed to get update size: "+err.Error())
  56. return
  57. }
  58. js, _ := json.Marshal([]int{bsize, wsize})
  59. common.SendJSONResponse(w, string(js))
  60. }
  61. func HandleUpdateDownloadRequest(w http.ResponseWriter, r *http.Request) {
  62. webpack, err := common.Mv(r, "webpack", false)
  63. if err != nil {
  64. common.SendErrorResponse(w, "Invalid or empty webpack download URL")
  65. return
  66. }
  67. binary, err := common.Mv(r, "binary", false)
  68. if err != nil {
  69. common.SendErrorResponse(w, "Invalid or empty binary download URL")
  70. return
  71. }
  72. //Update the connection to websocket
  73. requireWebsocket, _ := common.Mv(r, "ws", false)
  74. if requireWebsocket == "true" {
  75. //Upgrade to websocket
  76. var upgrader = websocket.Upgrader{}
  77. upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  78. c, err := upgrader.Upgrade(w, r, nil)
  79. if err != nil {
  80. common.SendErrorResponse(w, "Upgrade websocket failed: "+err.Error())
  81. return
  82. }
  83. type Progress struct {
  84. Stage int
  85. Progress float64
  86. StatusText string
  87. }
  88. err = DownloadUpdatesFromURL(binary, webpack, func(stage int, progress float64, statusText string) {
  89. thisProgress := Progress{
  90. Stage: stage,
  91. Progress: progress,
  92. StatusText: statusText,
  93. }
  94. js, _ := json.Marshal(thisProgress)
  95. c.WriteMessage(1, js)
  96. })
  97. if err != nil {
  98. //Finish with error
  99. c.WriteMessage(1, []byte("{\"error\":\""+err.Error()+"\""))
  100. } else {
  101. //Done without error
  102. c.WriteMessage(1, []byte("OK"))
  103. }
  104. //Close WebSocket connection after finished
  105. c.WriteControl(8, []byte{}, time.Now().Add(time.Second))
  106. c.Close()
  107. } else {
  108. //Just download and return ok after finish
  109. err = DownloadUpdatesFromURL(binary, webpack, func(stage int, progress float64, statusText string) {
  110. fmt.Println("Downloading Update, Stage: ", stage, " Progress: ", progress, " Status: ", statusText)
  111. })
  112. if err != nil {
  113. common.SendErrorResponse(w, err.Error())
  114. } else {
  115. common.SendOK(w)
  116. }
  117. }
  118. }
  119. //Handle getting information for vendor update
  120. func HandleGetUpdatePlatformInfo(w http.ResponseWriter, r *http.Request) {
  121. type UpdatePackageInfo struct {
  122. Config UpdateConfig
  123. OS string
  124. ARCH string
  125. }
  126. //Check if update config find. If yes, parse that
  127. updateFileContent, err := ioutil.ReadFile("./system/update.json")
  128. if err != nil {
  129. common.SendErrorResponse(w, "No vendor update config found")
  130. return
  131. }
  132. //Read from the update config
  133. vendorUpdateConfig := UpdateConfig{}
  134. err = json.Unmarshal(updateFileContent, &vendorUpdateConfig)
  135. if err != nil {
  136. log.Println("[Updates] Failed to parse update config file: ", err.Error())
  137. common.SendErrorResponse(w, "Invalid or corrupted update config")
  138. return
  139. }
  140. updateinfo := UpdatePackageInfo{
  141. Config: vendorUpdateConfig,
  142. OS: runtime.GOOS,
  143. ARCH: runtime.GOARCH,
  144. }
  145. js, _ := json.Marshal(updateinfo)
  146. common.SendJSONResponse(w, string(js))
  147. }
  148. //Handle check if there is a pending update
  149. func HandlePendingCheck(w http.ResponseWriter, r *http.Request) {
  150. if common.FileExists("./updates/") && common.FileExists("./updates/web/") && common.FileExists("./updates/system/") {
  151. //Update is pending
  152. common.SendJSONResponse(w, "true")
  153. } else {
  154. common.SendJSONResponse(w, "false")
  155. }
  156. }