-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
95 lines (76 loc) · 2.07 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
package main
import (
"code.google.com/p/go.net/websocket"
"net/http"
"os/exec"
"log"
"os"
"path"
"flag"
)
type Request struct {
Cmd string
}
type Response struct {
Result string
Error string
}
func execHandler(ws *websocket.Conn) {
var data Request
err := websocket.JSON.Receive(ws, &data)
if err != nil {
log.Fatal(err)
}
log.Println("Received request", data)
var out []byte
if len(data.Cmd) > 2 && data.Cmd[0:3] == "cd " {
out, err = changeWorkingDirectory(data.Cmd[3:])
} else {
var cmd = exec.Command("bash", []string{"-c", data.Cmd}...)
out, err = cmd.CombinedOutput()
}
var error = ""
if err != nil {
log.Println("Error ocurred executing command", err)
error = err.Error()
}
err = websocket.JSON.Send(ws, Response{Result:string(out), Error:error})
if err != nil {
log.Fatal("Could not send response", err)
}
}
func changeWorkingDirectory(newPath string) (out []byte, err error) {
wd, wderr := os.Getwd()
if wderr != nil {
// No idea in which cases this happens, so not really sure
// how to recover from it.
log.Fatal("Could not get current working directory", err)
}
newWd := path.Clean(wd + "/" + newPath)
log.Println("Changing directory to", newWd)
err = os.Chdir(newWd)
if err != nil {
log.Println("Error changing directory", err)
} else {
out = []byte("Changed directory to" + newWd)
}
return
}
func main() {
var path string
flag.StringVar(&path, "path", "", "Working directory, i.e. where commands will be executed.")
flag.Parse()
if path == "" {
log.Fatal("Please specify the 'path' option.")
}
err := os.Chdir(path)
if err != nil {
log.Fatal(err)
}
log.Printf("Starting with working directory '%s'", path)
http.Handle("/exec", websocket.Handler(execHandler))
err = http.ListenAndServe("127.0.0.1:8080", nil)
if err != nil {
log.Fatal("ListenAndServe:" + err.Error())
}
}