-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
638 lines (574 loc) · 15.3 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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
package main
import (
"bufio"
"bytes"
"context"
"encoding/binary"
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"os"
"os/exec"
"os/signal"
"os/user"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/alecthomas/kingpin"
"github.com/gorilla/websocket"
"github.com/openatx/androidutils"
"github.com/openatx/atx-agent/cmdctrl"
"github.com/openatx/atx-agent/subcmd"
"github.com/pkg/errors"
"github.com/qiniu/log"
"github.com/sevlyar/go-daemon"
)
var (
service = cmdctrl.New()
downManager = newDownloadManager()
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
version = "dev"
owner = "openatx"
repo = "atx-agent"
listenPort int
ip string
daemonLogPath = "/sdcard/atx-agent.log"
)
const (
apkVersionCode = 4
apkVersionName = "1.0.4"
)
// singleFight for http request
// - minicap
// - minitouch
var muxMutex = sync.Mutex{}
var muxLocks = make(map[string]bool)
var muxConns = make(map[string]*websocket.Conn)
func singleFightWrap(handleFunc func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
muxMutex.Lock()
if _, ok := muxLocks[r.RequestURI]; ok {
muxMutex.Unlock()
log.Println("singlefight conflict", r.RequestURI)
http.Error(w, "singlefight conflicts", http.StatusTooManyRequests) // code: 429
return
}
muxLocks[r.RequestURI] = true
muxMutex.Unlock()
handleFunc(w, r) // handle requests
muxMutex.Lock()
delete(muxLocks, r.RequestURI)
muxMutex.Unlock()
}
}
//socket 连接
func singleFightNewerWebsocket(handleFunc func(http.ResponseWriter, *http.Request, *websocket.Conn)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
muxMutex.Lock()
if oldWs, ok := muxConns[r.RequestURI]; ok {
oldWs.Close()
delete(muxConns, r.RequestURI)
}
wsConn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
http.Error(w, "websocket upgrade error", 500)
muxMutex.Unlock()
return
}
muxConns[r.RequestURI] = wsConn
muxMutex.Unlock()
handleFunc(w, r, wsConn) // handle request
muxMutex.Lock()
if muxConns[r.RequestURI] == wsConn { // release connection
delete(muxConns, r.RequestURI)
}
muxMutex.Unlock()
}
}
// Get preferred outbound ip of this machine
func getOutboundIP() (ip net.IP, err error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}
func mustGetOoutboundIP() net.IP {
ip, err := getOutboundIP()
if err != nil {
return net.ParseIP("127.0.0.1")
// panic(err)
}
return ip
}
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 cmdError2Code(err error) int {
if err == nil {
return 0
}
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus()
}
}
return 128
}
func GoFunc(f func() error) chan error {
ch := make(chan error)
go func() {
ch <- f()
}()
return ch
}
type MinicapInfo struct {
Width int `json:"width"`
Height int `json:"height"`
Rotation int `json:"rotation"`
Density float32 `json:"density"`
}
var (
deviceRotation int
displayMaxWidthHeight = 800
)
func updateMinicapRotation(rotation int) {
//获取设备信息
devInfo := getDeviceInfo()
//获取设备宽高
width, height := devInfo.Display.Width, devInfo.Display.Height
service.UpdateArgs("minicap", "/data/local/tmp/minicap", "-S", "-P",
fmt.Sprintf("%dx%d@%dx%d/%d", width, height, displayMaxWidthHeight, displayMaxWidthHeight, rotation),
"-Q", "50")
}
func checkUiautomatorInstalled() (ok bool) {
pi, err := androidutils.StatPackage("com.github.uiautomator")
if err != nil {
return
}
if pi.Version.Code < apkVersionCode {
return
}
_, err = androidutils.StatPackage("com.github.uiautomator.test")
return err == nil
}
func Screenshot(filename string, thumbnailSize string) (err error) {
output, err := runShellOutput("LD_LIBRARY_PATH=/data/local/tmp", "/data/local/tmp/minicap", "-i")
if err != nil {
return
}
var f MinicapInfo
if er := json.Unmarshal([]byte(output), &f); er != nil {
err = fmt.Errorf("minicap not supported: %v", er)
return
}
if thumbnailSize == "" {
thumbnailSize = fmt.Sprintf("%dx%d", f.Width, f.Height)
}
if _, err = runShell(
"LD_LIBRARY_PATH=/data/local/tmp",
"/data/local/tmp/minicap",
"-P", fmt.Sprintf("%dx%d@%s/%d", f.Width, f.Height, thumbnailSize, f.Rotation),
"-s", ">"+filename); err != nil {
return
}
return nil
}
type DownloadManager struct {
db map[string]*downloadProxy
mu sync.Mutex
n int
}
func newDownloadManager() *DownloadManager {
return &DownloadManager{
db: make(map[string]*downloadProxy, 10),
}
}
func (m *DownloadManager) Get(id string) *downloadProxy {
m.mu.Lock()
defer m.mu.Unlock()
return m.db[id]
}
func (m *DownloadManager) Put(di *downloadProxy) (id string) {
m.mu.Lock()
defer m.mu.Unlock()
m.n += 1
id = strconv.Itoa(m.n)
m.db[id] = di
// di.Id = id
return id
}
func (m *DownloadManager) Del(id string) {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.db, id)
}
func (m *DownloadManager) DelayDel(id string, sleep time.Duration) {
go func() {
time.Sleep(sleep)
m.Del(id)
}()
}
func currentUserName() string {
if u, err := user.Current(); err == nil {
return u.Name
}
if name := os.Getenv("USER"); name != "" {
return name
}
output, err := exec.Command("whoami").Output()
if err == nil {
return strings.TrimSpace(string(output))
}
return ""
}
func renderHTML(w http.ResponseWriter, filename string) {
file, err := Assets.Open(filename)
if err != nil {
http.Error(w, "404 page not found", 404)
return
}
content, _ := ioutil.ReadAll(file)
template.Must(template.New(filename).Parse(string(content))).Execute(w, nil)
}
var (
ErrJpegWrongFormat = errors.New("jpeg format error, not starts with 0xff,0xd8")
// target, _ := url.Parse("http://127.0.0.1:9008")
// uiautomatorProxy := httputil.NewSingleHostReverseProxy(target)
uiautomatorProxy = &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.RawQuery = "" // ignore http query
req.URL.Scheme = "http"
req.URL.Host = "127.0.0.1:9008"
},
Transport: &http.Transport{
// Ref: https://golang.org/pkg/net/http/#RoundTripper
Dial: func(network, addr string) (net.Conn, error) {
conn, err := (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).Dial(network, addr)
return conn, err
},
MaxIdleConns: 100,
IdleConnTimeout: 180 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
)
type errorBinaryReader struct {
rd io.Reader
err error
}
func (r *errorBinaryReader) ReadInto(datas ...interface{}) error {
if r.err != nil {
return r.err
}
for _, data := range datas {
r.err = binary.Read(r.rd, binary.LittleEndian, data)
if r.err != nil {
return r.err
}
}
return nil
}
// read from @minicap and send jpeg raw data to channel 读取minicap返回的数据并存到管道中
func translateMinicap(conn net.Conn, jpgC chan []byte, quitC chan bool) error {
var pid, rw, rh, vw, vh uint32
var version, unused, orientation, quirkFlag uint8
rd := bufio.NewReader(conn)
binRd := errorBinaryReader{rd: rd}
err := binRd.ReadInto(&version, &unused, &pid, &rw, &rh, &vw, &vh, &orientation, &quirkFlag)
if err != nil {
return err
}
for {
var size uint32
if err = binRd.ReadInto(&size); err != nil {
break
}
lr := &io.LimitedReader{R: rd, N: int64(size)}
buf := bytes.NewBuffer(nil)
_, err = io.Copy(buf, lr)
if err != nil {
break
}
if string(buf.Bytes()[:2]) != "\xff\xd8" {
err = ErrJpegWrongFormat
break
}
select {
case jpgC <- buf.Bytes(): // Maybe should use buffer instead
case <-quitC:
return nil
default:
// TODO(ssx): image should not wait or it will stuck here
}
}
return err
}
func runDaemon() (cntxt *daemon.Context) {
cntxt = &daemon.Context{ // remove pid to prevent resource busy
PidFilePerm: 0644,
LogFileName: daemonLogPath,
LogFilePerm: 0640,
WorkDir: "./",
Umask: 022,
}
child, err := cntxt.Reborn()
if err != nil {
log.Fatal("Unale to run: ", err)
}
if child != nil {
return nil // return nil indicate program run in parent
}
return cntxt
}
func stopSelf() {
// kill previous daemon first
log.Println("stop server self")
_, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/stop", listenPort))
if err == nil {
log.Println("wait server stopped")
time.Sleep(1000 * time.Millisecond) // server will quit in 0.1s
} else {
log.Println("already stopped")
}
}
func init() {
// Set timezone.
//
// Note that Android zoneinfo is stored in /system/usr/share/zoneinfo,
// but it is in some kind of packed TZiff file that we do not support
// yet. To make it simple, we use FixedZone instead
zones := map[string]int{
"Asia/Shanghai": 8,
"CST": 8, // China Standard Time
}
tz := getCachedProperty("persist.sys.timezone")
if tz != "" {
offset, ok := zones[tz]
if !ok {
// get offset from date command, example date output: +0800\n
output, _ := runShell("date", "+%z")
if len(output) != 6 {
return
}
offset, _ = strconv.Atoi(string(output[1:3]))
if output[0] == '-' {
offset *= -1
}
}
time.Local = time.FixedZone(tz, offset*3600)
}
}
//获取本机地址
func getAddr() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println("获取本机IP地址失败")
}
for _, value := range addrs {
if ipnet, ok := value.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
ip := ipnet.IP
list_temp := strings.Split(ip.String(), ".")
if ip.To4() != nil && list_temp[len(list_temp)-1] != "1" {
return ipnet.IP.String()
}
}
}
return ""
}
func main() {
kingpin.Version(version)
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.CommandLine.VersionFlag.Short('v')
// CMD: curl
cmdCurl := kingpin.Command("curl", "curl command")
subcmd.RegisterCurl(cmdCurl)
// CMD: server
cmdServer := kingpin.Command("server", "start server")
fDaemon := cmdServer.Flag("daemon", "daemon mode").Short('d').Bool()
fStop := cmdServer.Flag("stop", "stop server").Bool()
cmdServer.Flag("port", "listen port").Default("7912").Short('p').IntVar(&listenPort) // Create on 2017/09/12
cmdServer.Flag("log", "log file path when in daemon mode").StringVar(&daemonLogPath)
fTunnelServer := cmdServer.Flag("server", "server url").Default(getAddr() + "8000").Short('t').String()
fNoUiautomator := cmdServer.Flag("nouia", "do not start uiautoamtor when start").Bool()
// CMD: version
kingpin.Command("version", "show version")
// CMD: install
cmdIns := kingpin.Command("install", "install apk")
apkStart := cmdIns.Flag("start", "start when installed").Short('s').Bool()
apkPath := cmdIns.Arg("apkPath", "apk path").Required().String()
// CMD: info
kingpin.Command("info", "show device info")
switch kingpin.Parse() {
case "curl":
subcmd.DoCurl()
return
case "version":
println(version)
return
case "install":
am := &APKManager{Path: *apkPath}
if err := am.ForceInstall(); err != nil {
log.Fatal(err)
}
if *apkStart {
am.Start(StartOptions{})
}
return
case "info":
data, _ := json.MarshalIndent(getDeviceInfo(), "", " ")
println(string(data))
return
case "server":
// continue
}
if *fStop {
stopSelf()
return
}
//if *fRequirements {
// log.Println("check dependencies")
// if err := installRequirements(); err != nil {
// // panic(err)
// log.Println("requirements not ready:", err)
// return
// }
//}
if _, err := os.Stat("/sdcard/tmp"); err != nil {
os.MkdirAll("/sdcard/tmp", 0755)
}
os.Setenv("TMPDIR", "/sdcard/tmp")
if *fDaemon {
cntxt := runDaemon()
if cntxt == nil {
log.Printf("atx-agent listening on %v:%d", mustGetOoutboundIP(), listenPort)
return
}
defer cntxt.Release()
log.Print("- - - - - - - - - - - - - - -")
log.Print("daemon started")
}
fmt.Printf("atx-agent version %s\n", version)
// show ip
outIp, err := getOutboundIP()
if err == nil {
fmt.Printf("Listen on http://%v:%d\n", outIp, listenPort)
} else {
fmt.Printf("Internet is not connected.")
}
listener, err := net.Listen("tcp", ":"+strconv.Itoa(listenPort))
if err != nil {
log.Fatal(err)
}
// minicap + minitouch
devInfo := getDeviceInfo()
width, height := devInfo.Display.Width, devInfo.Display.Height
//降低图片画质
service.Add("minicap", cmdctrl.CommandInfo{
Environ: []string{"LD_LIBRARY_PATH=/data/local/tmp"},
Args: []string{"/data/local/tmp/minicap", "-S", "-P",
fmt.Sprintf("%dx%d@%dx%d/0", width, height, displayMaxWidthHeight, displayMaxWidthHeight),
"-Q", "50"},
})
service.Add("minitouch", cmdctrl.CommandInfo{
Args: []string{"/data/local/tmp/minitouch"},
})
// uiautomator
service.Add("uiautomator", cmdctrl.CommandInfo{
Args: []string{"am", "instrument", "-w", "-r",
"-e", "debug", "false",
"-e", "class", "com.github.uiautomator.stub.Stub",
"com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner"},
Stdout: os.Stdout,
Stderr: os.Stderr,
MaxRetries: 3,
RecoverDuration: 30 * time.Second,
OnStart: func() error {
log.Println("service uiautomator: startservice com.github.uiautomator/.Service")
runShell("am", "startservice", "-n", "com.github.uiautomator/.Service")
return nil
},
OnStop: func() {
log.Println("service uiautomator: stopservice com.github.uiautomator/.Service")
runShell("am", "stopservice", "-n", "com.github.uiautomator/.Service")
},
})
if !*fNoUiautomator {
if err := service.Start("uiautomator"); err != nil {
log.Println("uiautomator start failed:", err)
}
}
//用于持续发送心跳包到服务端
tunnel := &TunnelProxy{
ServerAddr: *fTunnelServer,
Secret: "hello kitty",
}
if *fTunnelServer != "" {
devInfo.ServerURL = *fTunnelServer
if !regexp.MustCompile(`https?://`).MatchString(devInfo.ServerURL) {
devInfo.ServerURL = "http://" + devInfo.ServerURL
}
go tunnel.Heratbeat()
}
server := NewServer(tunnel)
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
for sig := range sigc {
needStop := false
switch sig {
case syscall.SIGTERM:
needStop = true
case syscall.SIGHUP:
case syscall.SIGINT:
if !*fDaemon {
needStop = true
}
}
if needStop {
log.Println("Catch signal", sig)
service.StopAll()
server.httpServer.Shutdown(context.TODO())
return
}
log.Println("Ignore signal", sig)
}
}()
// run server forever
if err := server.Serve(listener); err != nil {
log.Println("server quit:", err)
}
}