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