This repository has been archived by the owner on Jun 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
httpserver.go
537 lines (491 loc) · 14 KB
/
httpserver.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/codeskyblue/heartbeat"
"github.com/codeskyblue/realip"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/koding/websocketproxy"
hb2 "github.com/openatx/atx-server/heartbeat"
"github.com/openatx/atx-server/proto"
"github.com/qiniu/log"
)
var (
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// Time allowed to write message to the client
wsWriteWait = 10 * time.Second
// Send pings to client with this period. Must be less than pongWait.
wsPingPeriod = 10 * time.Second
// Time allowed to read the next pong message from client
wsPongWait = wsPingPeriod * 3
funcMap template.FuncMap
)
func init() {
funcMap = template.FuncMap{
"title": strings.Title,
"urlhash": func(s string) string {
path := strings.TrimPrefix(s, "/")
info, err := os.Stat(path)
if err != nil {
return s + "#no-such-file"
}
return fmt.Sprintf("%s?t=%d", s, info.ModTime().Unix())
},
}
}
func renderHTML(w http.ResponseWriter, filename string, value interface{}) {
tmpl := template.Must(template.New("").Funcs(funcMap).Delims("[[", "]]").ParseGlob("templates/*.html"))
tmpl.ExecuteTemplate(w, filename, value)
// content, _ := ioutil.ReadFile("templates/" + filename)
// template.Must(template.New(filename).Parse(string(content))).Execute(w, nil)
}
func renderJSON(w http.ResponseWriter, data interface{}) {
js, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(js)))
w.Write(js)
}
func newHandler() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
renderJSON(w, map[string]string{
"server": version,
"atx-agent": atxAgentVersion,
})
})
// 设备远程控制
r.HandleFunc("/devices/ip:{ip}/remote", func(w http.ResponseWriter, r *http.Request) {
ip := mux.Vars(r)["ip"]
renderHTML(w, "remote.html", ip)
}).Methods("GET")
r.HandleFunc("/devices/{udid}/remote", func(w http.ResponseWriter, r *http.Request) {
udid := mux.Vars(r)["udid"]
info, err := db.DeviceGet(udid)
if err != nil {
http.Error(w, err.Error(), 404)
return
}
renderHTML(w, "remote.html", map[string]interface{}{
"IP": info.IP,
"Port": info.Port,
"Udid": udid})
}).Methods("GET")
// 设备信息修改
r.HandleFunc("/devices/{udid}/edit", func(w http.ResponseWriter, r *http.Request) {
udid := mux.Vars(r)["udid"]
renderHTML(w, "edit.html", udid)
}).Methods("GET")
// Video-backend starts
videoProxyURL, _ := url.Parse(*videoBackend)
wsProxyURL, _ := url.Parse(*videoBackend)
wsProxyURL.Scheme = "ws"
videoProxy := httputil.NewSingleHostReverseProxy(videoProxyURL)
wsVideoProxy := websocketproxy.NewProxy(wsProxyURL)
r.PathPrefix("/videos").Handler(videoProxy).Methods("GET", "DELETE")
r.Handle("/video/images2video", videoProxy) // not working with POST proxy
r.PathPrefix("/static/videos/").Handler(videoProxy)
r.Handle("/video/convert", wsVideoProxy)
// End of video-backend
r.HandleFunc("/products/{brand}/{model}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
brand, model := vars["brand"], vars["model"]
products, err := db.ProductsFindAll(brand, model)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
renderJSON(w, products)
})
r.HandleFunc("/devices/{udid}/product", func(w http.ResponseWriter, r *http.Request) {
var product proto.Product
err := json.NewDecoder(r.Body).Decode(&product)
if err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
if product.Id == "" {
http.Error(w, "product id is required", http.StatusForbidden)
return
}
if err := db.ProductUpdate(product.Id, product); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
err = db.DeviceUpdate(mux.Vars(r)["udid"], proto.DeviceInfo{
Product: &proto.Product{
Id: product.Id,
},
})
if err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
renderJSON(w, map[string]interface{}{
"success": true,
})
}).Methods("PUT")
r.HandleFunc("/echo", echo)
r.HandleFunc("/feeds", func(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer ws.Close()
feeds, cancel, err := db.WatchDeviceChanges()
if err != nil {
ws.WriteJSON(map[string]string{
"error": err.Error(),
})
return
}
go func() {
defer cancel()
for {
_, _, err := ws.ReadMessage()
if err != nil {
break
}
}
log.Debug("ws read closed")
}()
for change := range feeds {
buf := bytes.NewBuffer(nil)
json.NewEncoder(buf).Encode(map[string]interface{}{
"new": change.NewValue,
"old": change.OldValue,
})
err := ws.WriteMessage(websocket.TextMessage, buf.Bytes()) // []byte(`{"new": "haha", "old": "wowo"}`))
if err != nil {
break
}
}
log.Debug("ws write closed")
})
r.HandleFunc("/providers", func(w http.ResponseWriter, r *http.Request) {
values := r.URL.Query()
if _, ok := values["json"]; ok {
providers, err := db.ProvidersAll()
if err != nil {
renderJSON(w, map[string]interface{}{
"success": false,
"description": err.Error(),
})
return
}
renderJSON(w, providers)
return
}
renderHTML(w, "providers.html", nil)
})
r.HandleFunc("/providers/{id}", func(w http.ResponseWriter, r *http.Request) {
var p proto.Provider
data, _ := ioutil.ReadAll(r.Body)
if err := json.Unmarshal(data, &p); err != nil {
renderJSON(w, map[string]interface{}{
"success": false,
"description": err.Error(),
})
return
}
id := mux.Vars(r)["id"]
db.ProviderUpdate(id, p)
renderJSON(w, map[string]interface{}{
"success": true,
})
}).Methods("PUT")
r.HandleFunc("/api/v1/batch/unlock", func(w http.ResponseWriter, r *http.Request) {
batchRunCommand("am start -W --user 0 -a com.github.uiautomator.ACTION_IDENTIFY; input keyevent HOME")
io.WriteString(w, "Success")
})
r.HandleFunc("/api/v1/batch/lock", func(w http.ResponseWriter, r *http.Request) {
batchRunCommand("input keyevent POWER")
io.WriteString(w, "Success")
})
r.HandleFunc("/api/v1/batch/shell", func(w http.ResponseWriter, r *http.Request) {
command := r.FormValue("command")
batchRunCommand(command)
io.WriteString(w, "Success")
})
// r.HandleFunc("/api/v1/phones/identify")
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
renderHTML(w, "index.html", nil)
})
r.PathPrefix("/assets").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir("./assets"))))
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "assets/favicon.ico")
})
r.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) {
devices, err := db.DeviceList()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
renderJSON(w, devices)
})
r.HandleFunc("/devices/{query}/info", func(w http.ResponseWriter, r *http.Request) {
query := mux.Vars(r)["query"]
udid, err := deviceQueryToUdid(query)
if err != nil {
io.WriteString(w, "Failure, device "+query+" not found")
return
}
if r.Method == "GET" {
info, _ := db.DeviceGet(udid)
renderJSON(w, info)
return
}
// POST
var info proto.DeviceInfo
if err := json.NewDecoder(r.Body).Decode(&info); err != nil {
io.WriteString(w, err.Error())
return
}
db.DeviceUpdate(udid, info)
io.WriteString(w, "Success")
}).Methods("GET", "POST")
r.HandleFunc("/property", func(w http.ResponseWriter, r *http.Request) {
clientIp := realip.FromRequest(r)
udid, err := deviceQueryToUdid("ip:" + clientIp)
if err != nil {
io.WriteString(w, "init with uiautomator2")
return
}
info, err := db.DeviceGet(udid)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if r.Method == "POST" {
var id string = r.FormValue("id")
if id == "" && r.FormValue("id_number") != "" {
id = "HIH-PHO-" + r.FormValue("id_number")
}
db.DeviceUpdate(info.Udid, proto.DeviceInfo{
PropertyId: id,
})
info.PropertyId = id
io.WriteString(w, "<h1>Updated to "+id+"</h1>")
return
}
renderHTML(w, "property.html", info.PropertyId)
}).Methods("GET", "POST")
r.HandleFunc("/devices/{query}/reserved", func(w http.ResponseWriter, r *http.Request) {
query := mux.Vars(r)["query"]
udid, err := deviceQueryToUdid(query)
if err != nil {
http.Error(w, "Device not found "+err.Error(), http.StatusGone)
return
}
info, err := db.DeviceGet(udid)
if err != nil {
http.Error(w, "Device get error "+err.Error(), http.StatusGone)
return
}
// create websocket connection
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
defer ws.Close()
if toBool(info.Using) {
log.Printf("Device %s is using", udid)
return
}
db.DeviceUpdate(info.Udid, proto.DeviceInfo{
Using: newBool(true),
UsingBeganAt: time.Now(),
Owner: &proto.OwnerInfo{
IP: realip.FromRequest(r),
},
})
defer func() {
db.DeviceUpdate(udid, proto.DeviceInfo{
Using: newBool(false),
})
go func() {
port := info.Port
if port == 0 {
port = 7912
}
reqURL := "http://" + info.IP + ":" + strconv.Itoa(port) + "/shell"
req, _ := http.NewRequest("GET", reqURL, nil)
q := req.URL.Query()
q.Add("command", "am start -n com.github.uiautomator/.IdentifyActivity")
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err == nil {
resp.Body.Close()
}
}()
}()
// wait until ws disconnected
for {
if _, _, err := ws.ReadMessage(); err != nil {
return
}
}
}).Methods("GET")
r.HandleFunc("/devices/{query}/reserved", func(w http.ResponseWriter, r *http.Request) {
query := mux.Vars(r)["query"]
udid, err := deviceQueryToUdid(query)
// info := hostsManager.Lookup(query)
if err != nil {
http.Error(w, "Device not found "+err.Error(), http.StatusGone)
return
}
if r.Method == "POST" {
info, err := db.DeviceGet(udid)
if err != nil {
http.Error(w, "Device get error "+err.Error(), http.StatusGone)
return
}
if !toBool(info.Present) {
http.Error(w, "Device offline", http.StatusGone)
return
}
if toBool(info.Using) {
http.Error(w, "Device is using", http.StatusForbidden)
return
}
db.DeviceUpdate(info.Udid, proto.DeviceInfo{
Using: newBool(true),
UsingBeganAt: time.Now(),
Owner: &proto.OwnerInfo{
IP: realip.FromRequest(r),
},
})
io.WriteString(w, "Success")
return
}
// DELETE
db.DeviceUpdate(udid, proto.DeviceInfo{
Using: newBool(false),
})
io.WriteString(w, "Release success")
}).Methods("POST", "DELETE")
r.HandleFunc("/devices/{query}/shell", func(w http.ResponseWriter, r *http.Request) {
query := mux.Vars(r)["query"]
udid, err := deviceQueryToUdid(query)
if err != nil {
http.Error(w, "Device not found", 410)
return
}
info, err := db.DeviceGet(udid)
if err != nil {
http.Error(w, "Device get error "+err.Error(), 500)
return
}
command := r.FormValue("command")
output, err := runAndroidShell(info.IP, command)
if err != nil {
renderJSON(w, map[string]string{
"error": err.Error(),
})
} else {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
io.WriteString(w, output) // the output is already json
}
}).Methods("POST")
// heartbeat for reverse proxies (adb forward device 7912 port)
hbs := heartbeat.NewServer("hello kitty", 15*time.Second)
hbs.OnConnect = func(identifier string, r *http.Request) {
host := realip.FromRequest(r)
db.DeviceUpdateOrInsert(proto.DeviceInfo{
Udid: identifier,
IP: host,
})
log.Println(identifier, "is online")
}
// Called when client ip changes
hbs.OnReconnect = func(identifier string, r *http.Request) {
host := realip.FromRequest(r)
db.DeviceUpdateOrInsert(proto.DeviceInfo{
Udid: identifier,
IP: host,
})
log.Println(identifier, "is reconnected")
}
hbs.OnDisconnect = func(identifier string) {
db.SetDeviceAbsent(identifier)
log.Println(identifier, "is offline")
}
r.Handle("/heartbeat", hbs)
providerhbs := hb2.NewServer(&ProviderReceiver{})
r.Handle("/provider/heartbeat", providerhbs)
return r
}
type ProviderReceiver struct{}
func (p *ProviderReceiver) OnConnect(ctx hb2.Context) error {
port, _ := strconv.Atoi(ctx.Request.FormValue("port"))
if port == 0 {
return errors.New("Request port is required")
}
log.Printf("Provider id:%s ip:%s port:%d is connected", ctx.ID, ctx.IP, port)
return db.ProviderUpdateOrInsert(ctx.ID, ctx.IP, port)
}
func (p *ProviderReceiver) OnDisconnect(id string) {
log.Printf("Provider %s disconnected", id)
db.ProviderOffline(id)
}
/*
POST udid, status=<online|offline>
*/
func (p *ProviderReceiver) OnRequest(ctx hb2.Context) error {
id, req := ctx.ID, ctx.Request
data := req.FormValue("data")
log.Println("Receive data:", data)
var v struct {
Status string `json:"status"`
Udid string `json:"udid"`
ProviderForwardedPort int `json:"providerForwardedPort"`
}
if err := json.Unmarshal([]byte(data), &v); err != nil {
return err
}
status, udid := v.Status, v.Udid
if status == "" || udid == "" {
return errors.New("status or udid is empty")
}
provider, err := db.ProviderGet(id)
if err != nil {
log.Println("Unexpect err:", err)
return err
}
var providerId = &provider.Id
if status == "online" {
log.Printf("Device: %s is plugged-in", udid)
} else if status == "offline" {
log.Printf("Device: %s is plugged-off", udid)
providerId = nil
} else {
log.Printf("Invalid status: %s, only <offline|online> is allowed.", status)
return errors.New("status is required")
}
return db.DeviceUpdate(udid, map[string]interface{}{
"provider_id": providerId,
"providerForwardedPort": v.ProviderForwardedPort,
})
}