-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_runner.go
67 lines (61 loc) · 1.97 KB
/
command_runner.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
package main
import (
"log"
"net/http"
"os/exec"
"runtime"
)
// To open URLs or applications only on Windows
func openBrowser(url string) *exec.Cmd {
if runtime.GOOS != "windows" {
log.Println("openBrowser: Unsupported OS. This function only works on Windows.")
return nil
}
// If the OS is Windows, proceed to open the URL
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
}
// To runs the given command and logs the output
func execCommand(cmd *exec.Cmd, w http.ResponseWriter) {
if cmd == nil {
http.Error(w, "Failed to execute command", http.StatusInternalServerError)
return
}
if err := cmd.Start(); err != nil {
log.Printf("Failed to execute command: %v", err)
http.Error(w, "Failed to execute command", http.StatusInternalServerError)
return
}
w.Write([]byte("Executed command"))
}
// To send key press events
func sendKeyPress(keyCombination string, w http.ResponseWriter) {
if runtime.GOOS == "windows" {
// Using nircmd to simulate Ctrl+C
cmd := exec.Command("./helper/nircmd.exe", "sendkeypress", keyCombination)
err := cmd.Run()
if err != nil {
log.Printf("Failed to execute sendkeypress: %v", keyCombination)
http.Error(w, "Failed to execute sendkeypress", http.StatusInternalServerError)
return
}
w.Write([]byte("Executed command"))
} else {
http.Error(w, "sendkeypress is not implemented for this OS", http.StatusNotImplemented)
}
}
// To send key press events
func sendMouseClick(buttonType string, w http.ResponseWriter) {
if runtime.GOOS == "windows" {
// Using nircmd to simulate Ctrl+C
cmd := exec.Command("./helper/nircmd.exe", "sendmouse", buttonType, "click")
err := cmd.Run()
if err != nil {
log.Printf("Failed to execute mouse button click: %v", buttonType)
http.Error(w, "Failed to execute mouse button click", http.StatusInternalServerError)
return
}
w.Write([]byte("Executed command"))
} else {
http.Error(w, "sendkeypress is not implemented for this OS", http.StatusNotImplemented)
}
}