handler.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. Checksum string `json:"checksum"`
  42. }
  43. func HandleUpdateCheckSize(w http.ResponseWriter, r *http.Request) {
  44. webpack, err := common.Mv(r, "webpack", true)
  45. if err != nil {
  46. common.SendErrorResponse(w, "Invalid or empty webpack download URL")
  47. return
  48. }
  49. binary, err := common.Mv(r, "binary", true)
  50. if err != nil {
  51. common.SendErrorResponse(w, "Invalid or empty binary download URL")
  52. return
  53. }
  54. bsize, wsize, err := GetUpdateSizes(binary, webpack)
  55. if err != nil {
  56. common.SendErrorResponse(w, "Failed to get update size: "+err.Error())
  57. return
  58. }
  59. js, _ := json.Marshal([]int{bsize, wsize})
  60. common.SendJSONResponse(w, string(js))
  61. }
  62. func HandleUpdateDownloadRequest(w http.ResponseWriter, r *http.Request) {
  63. webpack, err := common.Mv(r, "webpack", false)
  64. if err != nil {
  65. common.SendErrorResponse(w, "Invalid or empty webpack download URL")
  66. return
  67. }
  68. binary, err := common.Mv(r, "binary", false)
  69. if err != nil {
  70. common.SendErrorResponse(w, "Invalid or empty binary download URL")
  71. return
  72. }
  73. checksum, err := common.Mv(r, "checksum", true)
  74. if err != nil {
  75. checksum = ""
  76. }
  77. //Update the connection to websocket
  78. requireWebsocket, _ := common.Mv(r, "ws", false)
  79. if requireWebsocket == "true" {
  80. //Upgrade to websocket
  81. var upgrader = websocket.Upgrader{}
  82. upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  83. c, err := upgrader.Upgrade(w, r, nil)
  84. if err != nil {
  85. common.SendErrorResponse(w, "Upgrade websocket failed: "+err.Error())
  86. return
  87. }
  88. type Progress struct {
  89. Stage int
  90. Progress float64
  91. StatusText string
  92. }
  93. err = DownloadUpdatesFromURL(binary, webpack, checksum, func(stage int, progress float64, statusText string) {
  94. thisProgress := Progress{
  95. Stage: stage,
  96. Progress: progress,
  97. StatusText: statusText,
  98. }
  99. js, _ := json.Marshal(thisProgress)
  100. c.WriteMessage(1, js)
  101. })
  102. if err != nil {
  103. //Finish with error
  104. c.WriteMessage(1, []byte("{\"error\":\""+err.Error()+"\"}"))
  105. } else {
  106. //Done without error
  107. c.WriteMessage(1, []byte("OK"))
  108. }
  109. //Close WebSocket connection after finished
  110. c.WriteControl(8, []byte{}, time.Now().Add(time.Second))
  111. c.Close()
  112. } else {
  113. //Just download and return ok after finish
  114. err = DownloadUpdatesFromURL(binary, webpack, checksum, func(stage int, progress float64, statusText string) {
  115. fmt.Println("Downloading Update, Stage: ", stage, " Progress: ", progress, " Status: ", statusText)
  116. })
  117. if err != nil {
  118. common.SendErrorResponse(w, err.Error())
  119. } else {
  120. common.SendOK(w)
  121. }
  122. }
  123. }
  124. //Handle getting information for vendor update
  125. func HandleGetUpdatePlatformInfo(w http.ResponseWriter, r *http.Request) {
  126. type UpdatePackageInfo struct {
  127. Config UpdateConfig
  128. OS string
  129. ARCH string
  130. }
  131. //Check if update config find. If yes, parse that
  132. updateFileContent, err := ioutil.ReadFile("./system/update.json")
  133. if err != nil {
  134. common.SendErrorResponse(w, "No vendor update config found")
  135. return
  136. }
  137. //Read from the update config
  138. vendorUpdateConfig := UpdateConfig{}
  139. err = json.Unmarshal(updateFileContent, &vendorUpdateConfig)
  140. if err != nil {
  141. log.Println("[Updates] Failed to parse update config file: ", err.Error())
  142. common.SendErrorResponse(w, "Invalid or corrupted update config")
  143. return
  144. }
  145. updateinfo := UpdatePackageInfo{
  146. Config: vendorUpdateConfig,
  147. OS: runtime.GOOS,
  148. ARCH: runtime.GOARCH,
  149. }
  150. js, _ := json.Marshal(updateinfo)
  151. common.SendJSONResponse(w, string(js))
  152. }
  153. //Handle check if there is a pending update
  154. func HandlePendingCheck(w http.ResponseWriter, r *http.Request) {
  155. if common.FileExists("./updates/") && common.FileExists("./updates/web/") && common.FileExists("./updates/system/") {
  156. //Update is pending
  157. common.SendJSONResponse(w, "true")
  158. } else {
  159. common.SendJSONResponse(w, "false")
  160. }
  161. }