| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 | package mainimport (	"net/http"	"os/exec"	"runtime"	"imuslab.com/arozos/mod/common")func HardwarePowerInit() {	if *allow_hardware_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_hardware_management {		common.SendJSONResponse(w, "true")	} else {		common.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() {		common.SendErrorResponse(w, "Permission Denied")		return	}	if !sudo_mode {		common.SendErrorResponse(w, "Sudo mode required")		return	}	//Double check password for this user	password, err := common.Mv(r, "password", true)	if err != nil {		common.SendErrorResponse(w, "Password Incorrect")		return	}	passwordCorrect, rejectionReason := authAgent.ValidateUsernameAndPasswordWithReason(userinfo.Username, password)	if !passwordCorrect {		common.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)			common.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)			common.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)			common.SendErrorResponse(w, string(out))		}		systemWideLogger.PrintAndLog("Power", string(out), nil)	}	common.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() {		common.SendErrorResponse(w, "Permission Denied")		return	}	if !sudo_mode {		common.SendErrorResponse(w, "Sudo mode required")		return	}	//Double check password for this user	password, err := common.Mv(r, "password", true)	if err != nil {		common.SendErrorResponse(w, "Password Incorrect")		return	}	passwordCorrect, rejectionReason := authAgent.ValidateUsernameAndPasswordWithReason(userinfo.Username, password)	if !passwordCorrect {		common.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)			common.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)			common.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)			common.SendErrorResponse(w, string(out))		}		systemWideLogger.PrintAndLog("Power", string(out), nil)	}	common.SendOK(w)}
 |