hardware.power.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. //validate password using authreq.html
  34. if !AuthValidateSecureRequest(w, r, true) {
  35. return
  36. }
  37. if runtime.GOOS == "windows" {
  38. //Only allow Linux to do power operation
  39. cmd := exec.Command("shutdown", "-s", "-t", "20")
  40. out, err := cmd.CombinedOutput()
  41. if err != nil {
  42. systemWideLogger.PrintAndLog("Power", string(out), err)
  43. utils.SendErrorResponse(w, string(out))
  44. }
  45. systemWideLogger.PrintAndLog("Power", string(out), nil)
  46. }
  47. if runtime.GOOS == "linux" {
  48. //Only allow Linux to do power operation
  49. cmd := exec.Command("/sbin/shutdown")
  50. out, err := cmd.CombinedOutput()
  51. if err != nil {
  52. systemWideLogger.PrintAndLog("Power", string(out), err)
  53. utils.SendErrorResponse(w, string(out))
  54. }
  55. systemWideLogger.PrintAndLog("Power", string(out), nil)
  56. }
  57. if runtime.GOOS == "darwin" {
  58. //Only allow Linux to do power operation
  59. cmd := exec.Command("sudo", "shutdown", "-h", "+1")
  60. out, err := cmd.CombinedOutput()
  61. if err != nil {
  62. systemWideLogger.PrintAndLog("Power", string(out), err)
  63. utils.SendErrorResponse(w, string(out))
  64. }
  65. systemWideLogger.PrintAndLog("Power", string(out), nil)
  66. }
  67. utils.SendOK(w)
  68. }
  69. func hardware_power_restart(w http.ResponseWriter, r *http.Request) {
  70. //Validate password using authreq.html
  71. if !AuthValidateSecureRequest(w, r, true) {
  72. return
  73. }
  74. if runtime.GOOS == "windows" {
  75. //Only allow Linux to do power operation
  76. cmd := exec.Command("shutdown", "-r", "-t", "20")
  77. out, err := cmd.CombinedOutput()
  78. if err != nil {
  79. systemWideLogger.PrintAndLog("Power", string(out), err)
  80. utils.SendErrorResponse(w, string(out))
  81. }
  82. systemWideLogger.PrintAndLog("Power", string(out), nil)
  83. }
  84. if runtime.GOOS == "linux" {
  85. //Only allow Linux to do power operation
  86. cmd := exec.Command("systemctl", "reboot")
  87. out, err := cmd.CombinedOutput()
  88. if err != nil {
  89. systemWideLogger.PrintAndLog("Power", string(out), err)
  90. utils.SendErrorResponse(w, string(out))
  91. }
  92. systemWideLogger.PrintAndLog("Power", string(out), nil)
  93. }
  94. if runtime.GOOS == "darwin" {
  95. //Only allow Linux to do power operation
  96. cmd := exec.Command("shutdown", "-r", "+1")
  97. out, err := cmd.CombinedOutput()
  98. if err != nil {
  99. systemWideLogger.PrintAndLog("Power", string(out), err)
  100. utils.SendErrorResponse(w, string(out))
  101. }
  102. systemWideLogger.PrintAndLog("Power", string(out), nil)
  103. }
  104. utils.SendOK(w)
  105. }