-
Notifications
You must be signed in to change notification settings - Fork 50
/
webcommand.go
92 lines (75 loc) · 1.91 KB
/
webcommand.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
package main
import (
"encoding/json"
"github.com/jordic/file_server/cmdwebstream"
"log"
"net/http"
"os/exec"
"strings"
)
type WebCommand struct {
Command string `json:"action"`
Params map[string]string `json:"params"`
ParamsList []string `json:"paramslist"`
}
type WebCommandResp struct {
Status int `json:"status"`
Msg string `json:"message"`
}
func WebCommandHandler(w http.ResponseWriter, r *http.Request) {
wc := &WebCommand{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&wc)
if err != nil {
http.Error(w, "Error decoding json", http.StatusInternalServerError)
log.Printf("e decoding %s", err)
return
}
if wc.Command == "syscmd" && disable_sys_command == true {
http.NotFound(w, r)
log.Printf("commands explicity disabled by commandline %s", wc.Command)
return
}
if wc.Command == "syscmd" {
//HandlerStreamCommand(w, wc)
path := strings.TrimRight(dir, "/") + "/"
source := strings.Trim(wc.Params["source"], "/")
args := wc.ParamsList
cmd := exec.Command(wc.Params["command"], args...)
cmd.Dir = path + source
cw := &cmdwebstream.Cmd{
Command: cmd,
}
cw.ServeHTTP(w, r)
//cmdwebstream.Handler(w, r, cmd)
return
}
cmd := GetCommand(wc.Command, dir)
if cmd == nil {
//http.Error(w, "Command Not found", http.StatusInternalServerError)
http.NotFound(w, r)
log.Printf("command not found %s", wc.Command)
return
}
resp := &WebCommandResp{}
cmd.Params = wc.Params
cmd.ParamsList = wc.ParamsList
res := cmd.Run()
if res != 0 {
resp.Status = res
resp.Msg = cmd.Stderr
} else {
resp.Status = 0
resp.Msg = cmd.Stdout
}
out, err := json.Marshal(resp)
if err != nil {
http.Error(w, "Error encoding json", http.StatusInternalServerError)
log.Printf("e encoding json %s", err)
return
}
log.Printf("command executed %s", wc.Command)
w.Header().Set("Content-Type", "application/json")
w.Write(out)
return
}