-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.go
More file actions
68 lines (61 loc) · 1.72 KB
/
shell.go
File metadata and controls
68 lines (61 loc) · 1.72 KB
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
package embedweb
import (
"bytes"
"context"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"net/http"
"os/exec"
"strings"
"time"
)
// Handler log view 入口
func ShellHandler(title string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
cmd := req.FormValue("cmd")
data := X{
"host": req.FormValue("host"),
"cmd": cmd,
}
if cmd != "" {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
defer cancel()
args := strings.Fields("/C " + cmd)
buf, err := exec.CommandContext(ctx, "cmd", args...).CombinedOutput()
if err != nil {
data["result"] = err.Error() + GBK2UTF8(buf)
} else {
data["result"] = GBK2UTF8(buf)
}
}
if err := toHtml(w, `
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{{.title}}</title>
</head>
<body>
<h2>执行系统shell,请小心使用!</h2>
<form action="{{.routepath}}" method="post" enctype="application/x-www-form-urlencoded">
<input name="cmd" type="text" value="" maxlength="255" style="width:89%" />
<input name="host" type="hidden" value="{{.host}}" />
<input type="submit" value="执行" style="width:10%" />
</form>
<h4># {{.cmd}}<br></h4>
<pre>{{.result}}</pre>
</body>
</html>
`, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func GBK2UTF8(gbk []byte) string {
out := bytes.Buffer{}
enc := transform.NewReader(bytes.NewBuffer(gbk), simplifiedchinese.GB18030.NewDecoder())
if _, err := io.Copy(&out, enc); err != nil {
return "N/A"
}
return out.String()
}