-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (79 loc) · 2.15 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"os/exec"
)
const (
PORT = "7070"
)
func main() {
fmt.Println("Gontroller Active on port " + PORT)
http.HandleFunc("/r", restart)
http.HandleFunc("/s", shutdown)
http.HandleFunc("/d", display_restart)
err := http.ListenAndServe(":"+PORT, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Done")
}
func restart(w http.ResponseWriter, r *http.Request) {
exec_respond(w, r, "/bin/sh", "-c", "sleep 5; shutdown -r now")
//exec_respond(w, r, "shutdown --help")
}
func shutdown(w http.ResponseWriter, r *http.Request) {
exec_respond(w, r, "/bin/sh", "-c", `"sleep 5; shutdown now"`)
}
func display_restart(w http.ResponseWriter, r *http.Request) {
exec_respond(w, r, "systemctl", "restart", "display-manager")
}
//func getHello(w http.ResponseWriter, r *http.Request) {
// fmt.Printf("got /hello request\n")
// io.WriteString(w, "Hello, HTTP!\n")
// // popup()
//}
func exec_respond(w http.ResponseWriter, r *http.Request, s string, args ...string) {
out := execute(s, args...)
fmt.Println(out)
io.WriteString(w, "success: "+out)
}
func wexecute(s string) {
command := s
err := exec.Command("powershell", "-NoProfile", command).Run()
if err != nil {
log.Fatal(err)
}
}
func execute(s string, args ...string) string {
cmd := exec.Command(s, args...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return "failed"
}
fmt.Println("Exec Result: " + out.String() + " (" + stderr.String() + ")")
return out.String()
}
/* func popup() {
command := `
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::YesNoCancel
$MessageIcon = [System.Windows.MessageBoxImage]::Error
$MessageBody = "Are you sure you want to delete the log file?"
$MessageTitle = "Confirm Deletion"
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
Write-Host "Your choice is $Result"
`
err := exec.Command("powershell", "-NoProfile", command).Run()
if err != nil {
log.Fatal(err)
}
}*/