forked from ycrash/yc-data-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
270 lines (255 loc) · 5.61 KB
/
server.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package shell
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"shell/config"
"shell/logger"
"strconv"
"strings"
)
type Server struct {
*http.Server
ProcessPids func(pids []int, pid2Name map[int]string, hd bool, tags string) (rUrls []string, err error)
ln net.Listener
}
type Req struct {
Key string
Actions []string
WaitFor bool
Hd *bool
Tags string
}
type Resp struct {
Code int
Msg string
DashboardReportURLs []string `json:",omitempty"`
Output [][]string `json:",omitempty"`
}
func (s *Server) Action(writer http.ResponseWriter, request *http.Request) {
encoder := json.NewEncoder(writer)
encoder.SetEscapeHTML(false)
resp := &Resp{}
var err error
defer func() {
err = encoder.Encode(resp)
if err != nil {
logger.Log("failed to encode response(%#v): %v", resp, err)
}
}()
forward := request.Header.Get("ycrash-forward")
if len(forward) > 0 {
fr := request.Clone(context.Background())
fr.URL, err = url.Parse(forward)
if err != nil {
resp.Code = -1
resp.Msg = err.Error()
return
}
fr.RequestURI = ""
fr.Header.Del("ycrash-forward")
fr.Close = true
client := http.Client{}
r, err := client.Do(fr)
if err != nil {
resp.Code = -2
resp.Msg = err.Error()
return
}
defer func() {
if err := r.Body.Close(); err != nil {
logger.Log("failed to close response body: %v", err)
}
}()
for key, v := range r.Header {
for _, value := range v {
writer.Header().Add(key, value)
}
}
writer.WriteHeader(r.StatusCode)
_, err = io.Copy(writer, r.Body)
if err != nil {
resp.Code = -1
resp.Msg = err.Error()
return
}
return
}
decoder := json.NewDecoder(request.Body)
req := &Req{}
err = decoder.Decode(req)
if err != nil {
resp.Code = -1
resp.Msg = err.Error()
return
}
logger.Info().Msgf("action request: %#v", req)
if config.GlobalConfig.ApiKey != req.Key {
resp.Code = -1
resp.Msg = "invalid key passed"
return
}
result, pid2Name, hasCmd, err := parseActions(req.Actions)
if err != nil {
resp.Code = -1
resp.Msg = err.Error()
return
}
var needHeapDump bool
if req.Hd != nil {
needHeapDump = *req.Hd
} else {
logger.Info().Msg("no hd passed in the request, using global config")
needHeapDump = config.GlobalConfig.HeapDump
}
if req.WaitFor || hasCmd {
var rUrls []string
if !hasCmd {
var pids []int
for _, i := range result {
pids = append(pids, i.(int))
}
rUrls, err = s.ProcessPids(pids, pid2Name, needHeapDump, req.Tags)
if err != nil {
resp.Code = -1
resp.Msg = err.Error()
return
}
resp.DashboardReportURLs = rUrls
} else {
var pid int
for _, i := range result {
var output []string
if p, ok := i.(int); ok {
pid = p
output = append(output, strconv.Itoa(p))
rUrls, err = s.ProcessPids([]int{p}, pid2Name, needHeapDump, req.Tags)
if err == nil {
resp.DashboardReportURLs = append(resp.DashboardReportURLs, rUrls...)
output = append(output, rUrls...)
} else {
output = append(output, err.Error())
}
} else if cmd, ok := i.(string); ok {
output = append(output, cmd)
out, err := RunCaptureCmd(pid, cmd)
if err == nil {
output = append(output, string(out))
} else {
output = append(output, err.Error())
}
}
resp.Output = append(resp.Output, output)
}
}
return
}
if !hasCmd {
var pids []int
for _, i := range result {
pids = append(pids, i.(int))
}
go func() {
_, err := s.ProcessPids(pids, pid2Name, needHeapDump, req.Tags)
if err != nil {
logger.Log("failed to process pids in background: %v", err)
}
}()
}
}
func parseActions(actions []string) (result []interface{}, pid2Name map[int]string, hasCmd bool, err error) {
for _, s := range actions {
if strings.HasPrefix(s, "capture ") {
ss := strings.Split(s, " ")
if len(ss) == 2 {
id := strings.TrimSpace(ss[1])
var pid int
switch id {
case "PROCESS_HIGH_CPU":
pid, err = GetTopCpu()
if err != nil {
return
}
case "PROCESS_HIGH_MEMORY":
pid, err = GetTopMem()
if err != nil {
return
}
case "PROCESS_UNKNOWN":
pid, err = GetTopCpu()
if err != nil {
return
}
if pid > 0 {
result = append(result, pid)
}
pid, err = GetTopMem()
if err != nil {
return
}
default:
var e error
pid, e = strconv.Atoi(id)
// "actions": ["capture buggyApp.jar"]
if e != nil {
p2n, e := GetProcessIds(config.ProcessTokens{config.ProcessToken(id)}, nil)
if e != nil {
continue
}
for pid, name := range p2n {
if pid > 0 {
if pid2Name == nil {
pid2Name = make(map[int]string, len(p2n))
}
result = append(result, pid)
pid2Name[pid] = name
}
}
continue
}
}
if pid > 0 {
result = append(result, pid)
}
}
} else if s == "attendance" {
msg, ok := attend("api")
fmt.Printf(
`api attendance task
Is completed: %t
Resp: %s
--------------------------------
`, ok, msg)
} else {
hasCmd = true
result = append(result, s)
}
}
return
}
func NewServer(address string, port int) (s *Server, err error) {
addr := net.JoinHostPort(address, strconv.Itoa(port))
ln, err := net.Listen("tcp", addr)
if err != nil {
return
}
mux := http.NewServeMux()
s = &Server{
Server: &http.Server{
Handler: mux,
},
ln: ln,
}
mux.HandleFunc("/action", s.Action)
return
}
func (s *Server) Serve() error {
return s.Server.Serve(s.ln)
}
func (s *Server) Addr() net.Addr {
return s.ln.Addr()
}