hardware.power.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package main
  2. import (
  3. "net/http"
  4. "os/exec"
  5. "runtime"
  6. "imuslab.com/arozos/mod/utils"
  7. )
  8. func HardwarePowerInit() {
  9. if *allow_power_management {
  10. //Only register these paths when hardware management is enabled
  11. http.HandleFunc("/system/power/shutdown", hardware_power_poweroff)
  12. http.HandleFunc("/system/power/restart", hardware_power_restart)
  13. //Register a power handler in system setting menu
  14. registerSetting(settingModule{
  15. Name: "Power",
  16. Desc: "Set the power state of the host device",
  17. IconPath: "SystemAO/boot/img/boot.png",
  18. Group: "Info",
  19. StartDir: "SystemAO/boot/poweroff.html",
  20. RequireAdmin: true,
  21. })
  22. }
  23. http.HandleFunc("/system/power/accessCheck", hardware_power_checkIfHardware)
  24. }
  25. func hardware_power_checkIfHardware(w http.ResponseWriter, r *http.Request) {
  26. if *allow_power_management {
  27. utils.SendJSONResponse(w, "true")
  28. } else {
  29. utils.SendJSONResponse(w, "false")
  30. }
  31. }
  32. func hardware_power_poweroff(w http.ResponseWriter, r *http.Request) {
  33. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  34. if err != nil {
  35. w.WriteHeader(http.StatusUnauthorized)
  36. w.Write([]byte("401 Unauthorized"))
  37. return
  38. }
  39. if !userinfo.IsAdmin() {
  40. utils.SendErrorResponse(w, "Permission Denied")
  41. return
  42. }
  43. if !sudo_mode {
  44. utils.SendErrorResponse(w, "Sudo mode required")
  45. return
  46. }
  47. //Double check password for this user
  48. password, err := utils.PostPara(r, "password")
  49. if err != nil {
  50. utils.SendErrorResponse(w, "Password Incorrect")
  51. return
  52. }
  53. passwordCorrect, rejectionReason := authAgent.ValidateUsernameAndPasswordWithReason(userinfo.Username, password)
  54. if !passwordCorrect {
  55. utils.SendErrorResponse(w, rejectionReason)
  56. return
  57. }
  58. if runtime.GOOS == "windows" {
  59. //Only allow Linux to do power operation
  60. cmd := exec.Command("shutdown", "-s", "-t", "20")
  61. out, err := cmd.CombinedOutput()
  62. if err != nil {
  63. systemWideLogger.PrintAndLog("Power", string(out), err)
  64. utils.SendErrorResponse(w, string(out))
  65. }
  66. systemWideLogger.PrintAndLog("Power", string(out), nil)
  67. }
  68. if runtime.GOOS == "linux" {
  69. //Only allow Linux to do power operation
  70. cmd := exec.Command("/sbin/shutdown")
  71. out, err := cmd.CombinedOutput()
  72. if err != nil {
  73. systemWideLogger.PrintAndLog("Power", string(out), err)
  74. utils.SendErrorResponse(w, string(out))
  75. }
  76. systemWideLogger.PrintAndLog("Power", string(out), nil)
  77. }
  78. if runtime.GOOS == "darwin" {
  79. //Only allow Linux to do power operation
  80. cmd := exec.Command("sudo", "shutdown", "-h", "+1")
  81. out, err := cmd.CombinedOutput()
  82. if err != nil {
  83. systemWideLogger.PrintAndLog("Power", string(out), err)
  84. utils.SendErrorResponse(w, string(out))
  85. }
  86. systemWideLogger.PrintAndLog("Power", string(out), nil)
  87. }
  88. utils.SendOK(w)
  89. }
  90. func hardware_power_restart(w http.ResponseWriter, r *http.Request) {
  91. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  92. if err != nil {
  93. w.WriteHeader(http.StatusUnauthorized)
  94. w.Write([]byte("401 Unauthorized"))
  95. return
  96. }
  97. if !userinfo.IsAdmin() {
  98. utils.SendErrorResponse(w, "Permission Denied")
  99. return
  100. }
  101. if !sudo_mode {
  102. utils.SendErrorResponse(w, "Sudo mode required")
  103. return
  104. }
  105. //Double check password for this user
  106. password, err := utils.PostPara(r, "password")
  107. if err != nil {
  108. utils.SendErrorResponse(w, "Password Incorrect")
  109. return
  110. }
  111. passwordCorrect, rejectionReason := authAgent.ValidateUsernameAndPasswordWithReason(userinfo.Username, password)
  112. if !passwordCorrect {
  113. utils.SendErrorResponse(w, rejectionReason)
  114. return
  115. }
  116. if runtime.GOOS == "windows" {
  117. //Only allow Linux to do power operation
  118. cmd := exec.Command("shutdown", "-r", "-t", "20")
  119. out, err := cmd.CombinedOutput()
  120. if err != nil {
  121. systemWideLogger.PrintAndLog("Power", string(out), err)
  122. utils.SendErrorResponse(w, string(out))
  123. }
  124. systemWideLogger.PrintAndLog("Power", string(out), nil)
  125. }
  126. if runtime.GOOS == "linux" {
  127. //Only allow Linux to do power operation
  128. cmd := exec.Command("systemctl", "reboot")
  129. out, err := cmd.CombinedOutput()
  130. if err != nil {
  131. systemWideLogger.PrintAndLog("Power", string(out), err)
  132. utils.SendErrorResponse(w, string(out))
  133. }
  134. systemWideLogger.PrintAndLog("Power", string(out), nil)
  135. }
  136. if runtime.GOOS == "darwin" {
  137. //Only allow Linux to do power operation
  138. cmd := exec.Command("shutdown", "-r", "+1")
  139. out, err := cmd.CombinedOutput()
  140. if err != nil {
  141. systemWideLogger.PrintAndLog("Power", string(out), err)
  142. utils.SendErrorResponse(w, string(out))
  143. }
  144. systemWideLogger.PrintAndLog("Power", string(out), nil)
  145. }
  146. utils.SendOK(w)
  147. }