system.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package v1
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "unsafe"
  13. http2 "github.com/IceWhaleTech/CasaOS-Common/utils/http"
  14. "github.com/IceWhaleTech/CasaOS/model"
  15. "github.com/IceWhaleTech/CasaOS/pkg/config"
  16. "github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
  17. port2 "github.com/IceWhaleTech/CasaOS/pkg/utils/port"
  18. "github.com/IceWhaleTech/CasaOS/pkg/utils/version"
  19. "github.com/IceWhaleTech/CasaOS/service"
  20. model2 "github.com/IceWhaleTech/CasaOS/service/model"
  21. "github.com/IceWhaleTech/CasaOS/types"
  22. "github.com/gin-gonic/gin"
  23. )
  24. // @Summary check version
  25. // @Produce application/json
  26. // @Accept application/json
  27. // @Tags sys
  28. // @Security ApiKeyAuth
  29. // @Success 200 {string} string "ok"
  30. // @Router /sys/version/check [get]
  31. func GetSystemCheckVersion(c *gin.Context) {
  32. need, version := version.IsNeedUpdate(service.MyService.Casa().GetCasaosVersion())
  33. if need {
  34. installLog := model2.AppNotify{}
  35. installLog.State = 0
  36. installLog.Message = "New version " + version.Version + " is ready, ready to upgrade"
  37. installLog.Type = types.NOTIFY_TYPE_NEED_CONFIRM
  38. installLog.CreatedAt = strconv.FormatInt(time.Now().Unix(), 10)
  39. installLog.UpdatedAt = strconv.FormatInt(time.Now().Unix(), 10)
  40. installLog.Name = "CasaOS System"
  41. service.MyService.Notify().AddLog(installLog)
  42. }
  43. data := make(map[string]interface{}, 3)
  44. data["need_update"] = need
  45. data["version"] = version
  46. data["current_version"] = types.CURRENTVERSION
  47. c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
  48. }
  49. // @Summary 系统信息
  50. // @Produce application/json
  51. // @Accept application/json
  52. // @Tags sys
  53. // @Security ApiKeyAuth
  54. // @Success 200 {string} string "ok"
  55. // @Router /sys/update [post]
  56. func SystemUpdate(c *gin.Context) {
  57. need, version := version.IsNeedUpdate(service.MyService.Casa().GetCasaosVersion())
  58. if need {
  59. service.MyService.System().UpdateSystemVersion(version.Version)
  60. }
  61. c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
  62. }
  63. // @Summary get logs
  64. // @Produce application/json
  65. // @Accept application/json
  66. // @Tags sys
  67. // @Security ApiKeyAuth
  68. // @Success 200 {string} string "ok"
  69. // @Router /sys/error/logs [get]
  70. func GetCasaOSErrorLogs(c *gin.Context) {
  71. line, _ := strconv.Atoi(c.DefaultQuery("line", "100"))
  72. c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: service.MyService.System().GetCasaOSLogs(line)})
  73. }
  74. // 系统配置
  75. func GetSystemConfigDebug(c *gin.Context) {
  76. array := service.MyService.System().GetSystemConfigDebug()
  77. disk := service.MyService.System().GetDiskInfo()
  78. sys := service.MyService.System().GetSysInfo()
  79. version := service.MyService.Casa().GetCasaosVersion()
  80. var bugContent string = fmt.Sprintf(`
  81. - OS: %s
  82. - CasaOS Version: %s
  83. - Disk Total: %v
  84. - Disk Used: %v
  85. - System Info: %s
  86. - Remote Version: %s
  87. - Browser: $Browser$
  88. - Version: $Version$
  89. `, sys.OS, types.CURRENTVERSION, disk.Total>>20, disk.Used>>20, array, version.Version)
  90. // array = append(array, fmt.Sprintf("disk,total:%v,used:%v,UsedPercent:%v", disk.Total>>20, disk.Used>>20, disk.UsedPercent))
  91. c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: bugContent})
  92. }
  93. // @Summary get casaos server port
  94. // @Produce application/json
  95. // @Accept application/json
  96. // @Tags sys
  97. // @Security ApiKeyAuth
  98. // @Success 200 {string} string "ok"
  99. // @Router /sys/port [get]
  100. func GetCasaOSPort(c *gin.Context) {
  101. c.JSON(common_err.SUCCESS,
  102. model.Result{
  103. Success: common_err.SUCCESS,
  104. Message: common_err.GetMsg(common_err.SUCCESS),
  105. Data: config.ServerInfo.HttpPort,
  106. })
  107. }
  108. // @Summary edit casaos server port
  109. // @Produce application/json
  110. // @Accept application/json
  111. // @Tags sys
  112. // @Security ApiKeyAuth
  113. // @Param port json string true "port"
  114. // @Success 200 {string} string "ok"
  115. // @Router /sys/port [put]
  116. func PutCasaOSPort(c *gin.Context) {
  117. json := make(map[string]string)
  118. c.ShouldBind(&json)
  119. portStr := json["port"]
  120. port, err := strconv.Atoi(portStr)
  121. if err != nil {
  122. c.JSON(common_err.SERVICE_ERROR,
  123. model.Result{
  124. Success: common_err.SERVICE_ERROR,
  125. Message: err.Error(),
  126. })
  127. return
  128. }
  129. isAvailable := port2.IsPortAvailable(port, "tcp")
  130. if !isAvailable {
  131. c.JSON(common_err.SERVICE_ERROR,
  132. model.Result{
  133. Success: common_err.PORT_IS_OCCUPIED,
  134. Message: common_err.GetMsg(common_err.PORT_IS_OCCUPIED),
  135. })
  136. return
  137. }
  138. service.MyService.System().UpSystemPort(strconv.Itoa(port))
  139. c.JSON(common_err.SUCCESS,
  140. model.Result{
  141. Success: common_err.SUCCESS,
  142. Message: common_err.GetMsg(common_err.SUCCESS),
  143. })
  144. }
  145. // @Summary active killing casaos
  146. // @Produce application/json
  147. // @Accept application/json
  148. // @Tags sys
  149. // @Security ApiKeyAuth
  150. // @Success 200 {string} string "ok"
  151. // @Router /sys/restart [post]
  152. func PostKillCasaOS(c *gin.Context) {
  153. os.Exit(0)
  154. }
  155. func GetSystemAppsStatus(c *gin.Context) {
  156. systemAppList := service.MyService.App().GetSystemAppList()
  157. appList := []model2.MyAppList{}
  158. for _, v := range systemAppList {
  159. name := strings.ReplaceAll(v.Names[0], "/", "")
  160. if len(v.Labels["name"]) > 0 {
  161. name = v.Labels["name"]
  162. }
  163. appList = append(appList, model2.MyAppList{
  164. Name: name,
  165. Icon: v.Labels["icon"],
  166. State: v.State,
  167. CustomId: v.Labels["custom_id"],
  168. Id: v.ID,
  169. Port: v.Labels["web"],
  170. Index: v.Labels["index"],
  171. // Order: m.Labels["order"],
  172. Image: v.Image,
  173. Latest: false,
  174. // Type: m.Labels["origin"],
  175. // Slogan: m.Slogan,
  176. // Rely: m.Rely,
  177. Host: v.Labels["host"],
  178. Protocol: v.Labels["protocol"],
  179. })
  180. }
  181. c.JSON(common_err.SUCCESS,
  182. model.Result{
  183. Success: common_err.SUCCESS,
  184. Message: common_err.GetMsg(common_err.SUCCESS),
  185. Data: appList,
  186. })
  187. }
  188. // @Summary get system hardware info
  189. // @Produce application/json
  190. // @Accept application/json
  191. // @Tags sys
  192. // @Security ApiKeyAuth
  193. // @Success 200 {string} string "ok"
  194. // @Router /sys/hardware/info [get]
  195. func GetSystemHardwareInfo(c *gin.Context) {
  196. data := make(map[string]string, 1)
  197. data["drive_model"] = service.MyService.System().GetDeviceTree()
  198. c.JSON(common_err.SUCCESS,
  199. model.Result{
  200. Success: common_err.SUCCESS,
  201. Message: common_err.GetMsg(common_err.SUCCESS),
  202. Data: data,
  203. })
  204. }
  205. // @Summary system utilization
  206. // @Produce application/json
  207. // @Accept application/json
  208. // @Tags sys
  209. // @Security ApiKeyAuth
  210. // @Success 200 {string} string "ok"
  211. // @Router /sys/utilization [get]
  212. func GetSystemUtilization(c *gin.Context) {
  213. data := make(map[string]interface{})
  214. cpu := service.MyService.System().GetCpuPercent()
  215. num := service.MyService.System().GetCpuCoreNum()
  216. cpuData := make(map[string]interface{})
  217. cpuData["percent"] = cpu
  218. cpuData["num"] = num
  219. cpuData["temperature"] = service.MyService.System().GetCPUTemperature()
  220. cpuData["power"] = service.MyService.System().GetCPUPower()
  221. data["cpu"] = cpuData
  222. data["mem"] = service.MyService.System().GetMemInfo()
  223. // 拼装网络信息
  224. netList := service.MyService.System().GetNetInfo()
  225. newNet := []model.IOCountersStat{}
  226. nets := service.MyService.System().GetNet(true)
  227. for _, n := range netList {
  228. for _, netCardName := range nets {
  229. if n.Name == netCardName {
  230. item := *(*model.IOCountersStat)(unsafe.Pointer(&n))
  231. item.State = strings.TrimSpace(service.MyService.System().GetNetState(n.Name))
  232. item.Time = time.Now().Unix()
  233. newNet = append(newNet, item)
  234. break
  235. }
  236. }
  237. }
  238. data["net"] = newNet
  239. for k, v := range service.MyService.Notify().GetSystemTempMap() {
  240. data[k] = v
  241. }
  242. c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
  243. }
  244. // @Summary Get notification port
  245. // @Produce application/json
  246. // @Accept application/json
  247. // @Tags sys
  248. // @Security ApiKeyAuth
  249. // @Success 200 {string} string "ok"
  250. // @Router /sys/socket/port [get]
  251. func GetSystemSocketPort(c *gin.Context) {
  252. c.JSON(common_err.SUCCESS,
  253. model.Result{
  254. Success: common_err.SUCCESS,
  255. Message: common_err.GetMsg(common_err.SUCCESS),
  256. Data: config.ServerInfo.SocketPort, // @tiger 这里最好封装成 {'port': ...} 的形式,来体现出参的上下文
  257. })
  258. }
  259. // @Summary get cpu info
  260. // @Produce application/json
  261. // @Accept application/json
  262. // @Tags sys
  263. // @Security ApiKeyAuth
  264. // @Success 200 {string} string "ok"
  265. // @Router /sys/cpu [get]
  266. func GetSystemCupInfo(c *gin.Context) {
  267. cpu := service.MyService.System().GetCpuPercent()
  268. num := service.MyService.System().GetCpuCoreNum()
  269. data := make(map[string]interface{})
  270. data["percent"] = cpu
  271. data["num"] = num
  272. c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
  273. }
  274. // @Summary get mem info
  275. // @Produce application/json
  276. // @Accept application/json
  277. // @Tags sys
  278. // @Security ApiKeyAuth
  279. // @Success 200 {string} string "ok"
  280. // @Router /sys/mem [get]
  281. func GetSystemMemInfo(c *gin.Context) {
  282. mem := service.MyService.System().GetMemInfo()
  283. c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: mem})
  284. }
  285. // @Summary get disk info
  286. // @Produce application/json
  287. // @Accept application/json
  288. // @Tags sys
  289. // @Security ApiKeyAuth
  290. // @Success 200 {string} string "ok"
  291. // @Router /sys/disk [get]
  292. func GetSystemDiskInfo(c *gin.Context) {
  293. disk := service.MyService.System().GetDiskInfo()
  294. c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: disk})
  295. }
  296. // @Summary get Net info
  297. // @Produce application/json
  298. // @Accept application/json
  299. // @Tags sys
  300. // @Security ApiKeyAuth
  301. // @Success 200 {string} string "ok"
  302. // @Router /sys/net [get]
  303. func GetSystemNetInfo(c *gin.Context) {
  304. netList := service.MyService.System().GetNetInfo()
  305. newNet := []model.IOCountersStat{}
  306. for _, n := range netList {
  307. for _, netCardName := range service.MyService.System().GetNet(true) {
  308. if n.Name == netCardName {
  309. item := *(*model.IOCountersStat)(unsafe.Pointer(&n))
  310. item.State = strings.TrimSpace(service.MyService.System().GetNetState(n.Name))
  311. item.Time = time.Now().Unix()
  312. newNet = append(newNet, item)
  313. break
  314. }
  315. }
  316. }
  317. c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: newNet})
  318. }
  319. func GetSystemProxy(c *gin.Context) {
  320. url := c.Query("url")
  321. resp, err := http2.Get(url, 30*time.Second)
  322. if err != nil {
  323. return
  324. }
  325. defer resp.Body.Close()
  326. for k, v := range c.Request.Header {
  327. c.Header(k, v[0])
  328. }
  329. rda, _ := ioutil.ReadAll(resp.Body)
  330. // json.NewEncoder(c.Writer).Encode(json.RawMessage(string(rda)))
  331. // 响应状态码
  332. c.Writer.WriteHeader(resp.StatusCode)
  333. // 复制转发的响应Body到响应Body
  334. io.Copy(c.Writer, ioutil.NopCloser(bytes.NewBuffer(rda)))
  335. }