main.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "context"
  6. "encoding/binary"
  7. "encoding/json"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. syslog "log"
  12. "net"
  13. "net/http"
  14. "net/http/httputil"
  15. "os"
  16. "os/exec"
  17. "os/signal"
  18. "os/user"
  19. "path/filepath"
  20. "strconv"
  21. "strings"
  22. "sync"
  23. "syscall"
  24. "time"
  25. "github.com/alecthomas/kingpin"
  26. "github.com/dustin/go-broadcast"
  27. "github.com/gorilla/websocket"
  28. "github.com/openatx/androidutils"
  29. "github.com/openatx/atx-agent/cmdctrl"
  30. "github.com/openatx/atx-agent/logger"
  31. "github.com/openatx/atx-agent/subcmd"
  32. "github.com/pkg/errors"
  33. "github.com/sevlyar/go-daemon"
  34. )
  35. var (
  36. service = cmdctrl.New()
  37. downManager = newDownloadManager()
  38. upgrader = websocket.Upgrader{
  39. ReadBufferSize: 1024,
  40. WriteBufferSize: 1024,
  41. CheckOrigin: func(r *http.Request) bool {
  42. return true
  43. },
  44. }
  45. version = "dev"
  46. owner = "openatx"
  47. repo = "atx-agent"
  48. listenAddr string
  49. daemonLogPath = "/sdcard/atx-agent.daemon.log"
  50. rotationPublisher = broadcast.NewBroadcaster(1)
  51. minicapSocketPath = "@minicap"
  52. minitouchSocketPath = "@minitouch"
  53. log = logger.Default
  54. )
  55. const (
  56. apkVersionCode = 4
  57. apkVersionName = "1.0.4"
  58. )
  59. // singleFight for http request
  60. // - minicap
  61. // - minitouch
  62. var muxMutex = sync.Mutex{}
  63. var muxLocks = make(map[string]bool)
  64. var muxConns = make(map[string]*websocket.Conn)
  65. func singleFightWrap(handleFunc func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
  66. return func(w http.ResponseWriter, r *http.Request) {
  67. muxMutex.Lock()
  68. if _, ok := muxLocks[r.RequestURI]; ok {
  69. muxMutex.Unlock()
  70. log.Println("singlefight conflict", r.RequestURI)
  71. http.Error(w, "singlefight conflicts", http.StatusTooManyRequests) // code: 429
  72. return
  73. }
  74. muxLocks[r.RequestURI] = true
  75. muxMutex.Unlock()
  76. handleFunc(w, r) // handle requests
  77. muxMutex.Lock()
  78. delete(muxLocks, r.RequestURI)
  79. muxMutex.Unlock()
  80. }
  81. }
  82. func singleFightNewerWebsocket(handleFunc func(http.ResponseWriter, *http.Request, *websocket.Conn)) func(http.ResponseWriter, *http.Request) {
  83. return func(w http.ResponseWriter, r *http.Request) {
  84. muxMutex.Lock()
  85. if oldWs, ok := muxConns[r.RequestURI]; ok {
  86. oldWs.Close()
  87. delete(muxConns, r.RequestURI)
  88. }
  89. wsConn, err := upgrader.Upgrade(w, r, nil)
  90. if err != nil {
  91. http.Error(w, "websocket upgrade error", 500)
  92. muxMutex.Unlock()
  93. return
  94. }
  95. muxConns[r.RequestURI] = wsConn
  96. muxMutex.Unlock()
  97. handleFunc(w, r, wsConn) // handle request
  98. muxMutex.Lock()
  99. if muxConns[r.RequestURI] == wsConn { // release connection
  100. delete(muxConns, r.RequestURI)
  101. }
  102. muxMutex.Unlock()
  103. }
  104. }
  105. // Get preferred outbound ip of this machine
  106. func getOutboundIP() (ip net.IP, err error) {
  107. conn, err := net.Dial("udp", "8.8.8.8:80")
  108. if err != nil {
  109. return
  110. }
  111. defer conn.Close()
  112. localAddr := conn.LocalAddr().(*net.UDPAddr)
  113. return localAddr.IP, nil
  114. }
  115. func mustGetOoutboundIP() net.IP {
  116. ip, err := getOutboundIP()
  117. if err != nil {
  118. return net.ParseIP("127.0.0.1")
  119. // panic(err)
  120. }
  121. return ip
  122. }
  123. func renderJSON(w http.ResponseWriter, data interface{}) {
  124. js, err := json.Marshal(data)
  125. if err != nil {
  126. http.Error(w, err.Error(), http.StatusInternalServerError)
  127. return
  128. }
  129. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  130. w.Header().Set("Content-Length", fmt.Sprintf("%d", len(js)))
  131. w.Write(js)
  132. }
  133. func cmdError2Code(err error) int {
  134. if err == nil {
  135. return 0
  136. }
  137. if exiterr, ok := err.(*exec.ExitError); ok {
  138. // The program has exited with an exit code != 0
  139. // This works on both Unix and Windows. Although package
  140. // syscall is generally platform dependent, WaitStatus is
  141. // defined for both Unix and Windows and in both cases has
  142. // an ExitStatus() method with the same signature.
  143. if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
  144. return status.ExitStatus()
  145. }
  146. }
  147. return 128
  148. }
  149. func GoFunc(f func() error) chan error {
  150. ch := make(chan error)
  151. go func() {
  152. ch <- f()
  153. }()
  154. return ch
  155. }
  156. type MinicapInfo struct {
  157. Width int `json:"width"`
  158. Height int `json:"height"`
  159. Rotation int `json:"rotation"`
  160. Density float32 `json:"density"`
  161. }
  162. var (
  163. deviceRotation int
  164. displayMaxWidthHeight = 800
  165. )
  166. func updateMinicapRotation(rotation int) {
  167. running := service.Running("minicap")
  168. if running {
  169. service.Stop("minicap")
  170. killProcessByName("minicap") // kill not controlled minicap
  171. }
  172. devInfo := getDeviceInfo()
  173. width, height := devInfo.Display.Width, devInfo.Display.Height
  174. service.UpdateArgs("minicap", "/data/local/tmp/minicap", "-S", "-P",
  175. fmt.Sprintf("%dx%d@%dx%d/%d", width, height, displayMaxWidthHeight, displayMaxWidthHeight, rotation))
  176. if running {
  177. service.Start("minicap")
  178. }
  179. }
  180. func checkUiautomatorInstalled() (ok bool) {
  181. pi, err := androidutils.StatPackage("com.github.uiautomator")
  182. if err != nil {
  183. return
  184. }
  185. if pi.Version.Code < apkVersionCode {
  186. return
  187. }
  188. _, err = androidutils.StatPackage("com.github.uiautomator.test")
  189. return err == nil
  190. }
  191. type DownloadManager struct {
  192. db map[string]*downloadProxy
  193. mu sync.Mutex
  194. n int
  195. }
  196. func newDownloadManager() *DownloadManager {
  197. return &DownloadManager{
  198. db: make(map[string]*downloadProxy, 10),
  199. }
  200. }
  201. func (m *DownloadManager) Get(id string) *downloadProxy {
  202. m.mu.Lock()
  203. defer m.mu.Unlock()
  204. return m.db[id]
  205. }
  206. func (m *DownloadManager) Put(di *downloadProxy) (id string) {
  207. m.mu.Lock()
  208. defer m.mu.Unlock()
  209. m.n += 1
  210. id = strconv.Itoa(m.n)
  211. m.db[id] = di
  212. // di.Id = id
  213. return id
  214. }
  215. func (m *DownloadManager) Del(id string) {
  216. m.mu.Lock()
  217. defer m.mu.Unlock()
  218. delete(m.db, id)
  219. }
  220. func (m *DownloadManager) DelayDel(id string, sleep time.Duration) {
  221. go func() {
  222. time.Sleep(sleep)
  223. m.Del(id)
  224. }()
  225. }
  226. func currentUserName() string {
  227. if u, err := user.Current(); err == nil {
  228. return u.Name
  229. }
  230. if name := os.Getenv("USER"); name != "" {
  231. return name
  232. }
  233. output, err := exec.Command("whoami").Output()
  234. if err == nil {
  235. return strings.TrimSpace(string(output))
  236. }
  237. return ""
  238. }
  239. func renderHTML(w http.ResponseWriter, filename string) {
  240. file, err := Assets.Open(filename)
  241. if err != nil {
  242. http.Error(w, "404 page not found", 404)
  243. return
  244. }
  245. content, _ := ioutil.ReadAll(file)
  246. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  247. w.Header().Set("Content-Length", strconv.Itoa(len(content)))
  248. w.Write(content)
  249. }
  250. var (
  251. ErrJpegWrongFormat = errors.New("jpeg format error, not starts with 0xff,0xd8")
  252. // target, _ := url.Parse("http://127.0.0.1:9008")
  253. // uiautomatorProxy := httputil.NewSingleHostReverseProxy(target)
  254. uiautomatorTimer = NewSafeTimer(time.Hour * 3)
  255. uiautomatorProxy = &httputil.ReverseProxy{
  256. Director: func(req *http.Request) {
  257. req.URL.RawQuery = "" // ignore http query
  258. req.URL.Scheme = "http"
  259. req.URL.Host = "127.0.0.1:9008"
  260. if req.URL.Path == "/jsonrpc/0" {
  261. uiautomatorTimer.Reset()
  262. }
  263. },
  264. Transport: &http.Transport{
  265. // Ref: https://golang.org/pkg/net/http/#RoundTripper
  266. Dial: func(network, addr string) (net.Conn, error) {
  267. conn, err := (&net.Dialer{
  268. Timeout: 5 * time.Second,
  269. KeepAlive: 30 * time.Second,
  270. DualStack: true,
  271. }).Dial(network, addr)
  272. return conn, err
  273. },
  274. MaxIdleConns: 100,
  275. IdleConnTimeout: 180 * time.Second,
  276. TLSHandshakeTimeout: 10 * time.Second,
  277. ExpectContinueTimeout: 1 * time.Second,
  278. },
  279. }
  280. )
  281. type errorBinaryReader struct {
  282. rd io.Reader
  283. err error
  284. }
  285. func (r *errorBinaryReader) ReadInto(datas ...interface{}) error {
  286. if r.err != nil {
  287. return r.err
  288. }
  289. for _, data := range datas {
  290. r.err = binary.Read(r.rd, binary.LittleEndian, data)
  291. if r.err != nil {
  292. return r.err
  293. }
  294. }
  295. return nil
  296. }
  297. // read from @minicap and send jpeg raw data to channel
  298. func translateMinicap(conn net.Conn, jpgC chan []byte, ctx context.Context) error {
  299. var pid, rw, rh, vw, vh uint32
  300. var version, unused, orientation, quirkFlag uint8
  301. rd := bufio.NewReader(conn)
  302. binRd := errorBinaryReader{rd: rd}
  303. err := binRd.ReadInto(&version, &unused, &pid, &rw, &rh, &vw, &vh, &orientation, &quirkFlag)
  304. if err != nil {
  305. return err
  306. }
  307. for {
  308. var size uint32
  309. if err = binRd.ReadInto(&size); err != nil {
  310. break
  311. }
  312. lr := &io.LimitedReader{R: rd, N: int64(size)}
  313. buf := bytes.NewBuffer(nil)
  314. _, err = io.Copy(buf, lr)
  315. if err != nil {
  316. break
  317. }
  318. if string(buf.Bytes()[:2]) != "\xff\xd8" {
  319. err = ErrJpegWrongFormat
  320. break
  321. }
  322. select {
  323. case jpgC <- buf.Bytes(): // Maybe should use buffer instead
  324. case <-ctx.Done():
  325. return nil
  326. default:
  327. // TODO(ssx): image should not wait or it will stuck here
  328. }
  329. }
  330. return err
  331. }
  332. func runDaemon() (cntxt *daemon.Context) {
  333. cntxt = &daemon.Context{ // remove pid to prevent resource busy
  334. PidFilePerm: 0644,
  335. LogFilePerm: 0640,
  336. WorkDir: "./",
  337. Umask: 022,
  338. }
  339. // log might be no auth
  340. if f, err := os.OpenFile(daemonLogPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644); err == nil { // |os.O_APPEND
  341. f.Close()
  342. cntxt.LogFileName = daemonLogPath
  343. }
  344. child, err := cntxt.Reborn()
  345. if err != nil {
  346. log.Fatal("Unale to run: ", err)
  347. }
  348. if child != nil {
  349. return nil // return nil indicate program run in parent
  350. }
  351. return cntxt
  352. }
  353. func setupLogrotate() {
  354. logger.SetOutputFile("/sdcard/atx-agent.log")
  355. }
  356. func stopSelf() {
  357. // kill previous daemon first
  358. log.Println("stop server self")
  359. listenPort, _ := strconv.Atoi(strings.Split(listenAddr, ":")[1])
  360. client := http.Client{Timeout: 3 * time.Second}
  361. _, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/stop", listenPort))
  362. if err == nil {
  363. log.Println("wait server stopped")
  364. time.Sleep(500 * time.Millisecond) // server will quit in 0.5s
  365. } else {
  366. log.Println("already stopped")
  367. }
  368. // to make sure stopped
  369. killAgentProcess()
  370. }
  371. func init() {
  372. syslog.SetFlags(syslog.Lshortfile | syslog.LstdFlags)
  373. // Set timezone.
  374. //
  375. // Note that Android zoneinfo is stored in /system/usr/share/zoneinfo,
  376. // but it is in some kind of packed TZiff file that we do not support
  377. // yet. To make it simple, we use FixedZone instead
  378. zones := map[string]int{
  379. "Asia/Shanghai": 8,
  380. "CST": 8, // China Standard Time
  381. }
  382. tz := getCachedProperty("persist.sys.timezone")
  383. if tz != "" {
  384. offset, ok := zones[tz]
  385. if !ok {
  386. // get offset from date command, example date output: +0800\n
  387. output, _ := runShell("date", "+%z")
  388. if len(output) != 6 {
  389. return
  390. }
  391. offset, _ = strconv.Atoi(string(output[1:3]))
  392. if output[0] == '-' {
  393. offset *= -1
  394. }
  395. }
  396. time.Local = time.FixedZone(tz, offset*3600)
  397. }
  398. }
  399. // lazyInit will be called in func:main
  400. func lazyInit() {
  401. // watch rotation and send to rotatinPublisher
  402. go _watchRotation()
  403. if !isMinicapSupported() {
  404. minicapSocketPath = "@minicapagent"
  405. }
  406. if !fileExists("/data/local/tmp/minitouch") {
  407. minitouchSocketPath = "@minitouchagent"
  408. } else if sdk, _ := strconv.Atoi(getCachedProperty("ro.build.version.sdk")); sdk > 28 { // Android Q..
  409. minitouchSocketPath = "@minitouchagent"
  410. }
  411. }
  412. func _watchRotation() {
  413. for {
  414. conn, err := net.Dial("unix", "@rotationagent")
  415. if err != nil {
  416. time.Sleep(2 * time.Second)
  417. continue
  418. }
  419. func() {
  420. defer conn.Close()
  421. scanner := bufio.NewScanner(conn)
  422. for scanner.Scan() {
  423. rotation, err := strconv.Atoi(scanner.Text())
  424. if err != nil {
  425. continue
  426. }
  427. deviceRotation = rotation
  428. if minicapSocketPath == "@minicap" {
  429. updateMinicapRotation(deviceRotation)
  430. }
  431. rotationPublisher.Submit(rotation)
  432. log.Println("Rotation -->", rotation)
  433. }
  434. }()
  435. time.Sleep(1 * time.Second)
  436. }
  437. }
  438. func killAgentProcess() error {
  439. // kill process by process cmdline
  440. procs, err := listAllProcs()
  441. if err != nil {
  442. return err
  443. }
  444. for _, p := range procs {
  445. if os.Getpid() == p.Pid {
  446. // do not kill self
  447. continue
  448. }
  449. if len(p.Cmdline) >= 2 {
  450. // cmdline: /data/local/tmp/atx-agent server -d
  451. if filepath.Base(p.Cmdline[0]) == "atx-agent" && p.Cmdline[1] == "server" {
  452. log.Infof("kill running atx-agent (pid=%d)", p.Pid)
  453. p.Kill()
  454. }
  455. }
  456. }
  457. return nil
  458. }
  459. func main() {
  460. kingpin.Version(version)
  461. kingpin.CommandLine.HelpFlag.Short('h')
  462. kingpin.CommandLine.VersionFlag.Short('v')
  463. // CMD: curl
  464. cmdCurl := kingpin.Command("curl", "curl command")
  465. subcmd.RegisterCurl(cmdCurl)
  466. // CMD: server
  467. cmdServer := kingpin.Command("server", "start server")
  468. fDaemon := cmdServer.Flag("daemon", "daemon mode").Short('d').Bool()
  469. fStop := cmdServer.Flag("stop", "stop server").Bool()
  470. cmdServer.Flag("addr", "listen port").Default(":7912").StringVar(&listenAddr) // Create on 2017/09/12
  471. cmdServer.Flag("log", "log file path when in daemon mode").StringVar(&daemonLogPath)
  472. // fServerURL := cmdServer.Flag("server", "server url").Short('t').String()
  473. fNoUiautomator := cmdServer.Flag("nouia", "do not start uiautoamtor when start").Bool()
  474. // CMD: version
  475. kingpin.Command("version", "show version")
  476. // CMD: install
  477. cmdIns := kingpin.Command("install", "install apk")
  478. apkStart := cmdIns.Flag("start", "start when installed").Short('s').Bool()
  479. apkPath := cmdIns.Arg("apkPath", "apk path").Required().String()
  480. // CMD: info
  481. os.Setenv("COLUMNS", "160")
  482. kingpin.Command("info", "show device info")
  483. switch kingpin.Parse() {
  484. case "curl":
  485. subcmd.DoCurl()
  486. return
  487. case "version":
  488. println(version)
  489. return
  490. case "install":
  491. am := &APKManager{Path: *apkPath}
  492. if err := am.ForceInstall(); err != nil {
  493. log.Fatal(err)
  494. }
  495. if *apkStart {
  496. am.Start(StartOptions{})
  497. }
  498. return
  499. case "info":
  500. data, _ := json.MarshalIndent(getDeviceInfo(), "", " ")
  501. println(string(data))
  502. return
  503. case "server":
  504. // continue
  505. }
  506. if *fStop {
  507. stopSelf()
  508. if !*fDaemon {
  509. return
  510. }
  511. }
  512. // serverURL := *fServerURL
  513. // if serverURL != "" {
  514. // if !regexp.MustCompile(`https?://`).MatchString(serverURL) {
  515. // serverURL = "http://" + serverURL
  516. // }
  517. // u, err := url.Parse(serverURL)
  518. // if err != nil {
  519. // log.Fatal(err)
  520. // }
  521. // _ = u
  522. // }
  523. if _, err := os.Stat("/sdcard/tmp"); err != nil {
  524. os.MkdirAll("/sdcard/tmp", 0755)
  525. }
  526. os.Setenv("TMPDIR", "/sdcard/tmp")
  527. if *fDaemon {
  528. log.Println("run atx-agent in background")
  529. cntxt := runDaemon()
  530. if cntxt == nil {
  531. log.Printf("atx-agent listening on %v", listenAddr)
  532. return
  533. }
  534. defer cntxt.Release()
  535. log.Println("- - - - - - - - - - - - - - -")
  536. log.Println("daemon started")
  537. setupLogrotate()
  538. }
  539. log.Printf("atx-agent version %s\n", version)
  540. lazyInit()
  541. // show ip
  542. outIp, err := getOutboundIP()
  543. if err == nil {
  544. fmt.Printf("Device IP: %v\n", outIp)
  545. } else {
  546. fmt.Printf("Internet is not connected.")
  547. }
  548. listener, err := net.Listen("tcp", listenAddr)
  549. if err != nil {
  550. log.Fatal(err)
  551. }
  552. // minicap + minitouch
  553. devInfo := getDeviceInfo()
  554. width, height := devInfo.Display.Width, devInfo.Display.Height
  555. service.Add("minicap", cmdctrl.CommandInfo{
  556. Environ: []string{"LD_LIBRARY_PATH=/data/local/tmp"},
  557. Args: []string{"/data/local/tmp/minicap", "-S", "-P",
  558. fmt.Sprintf("%dx%d@%dx%d/0", width, height, displayMaxWidthHeight, displayMaxWidthHeight)},
  559. })
  560. service.Add("apkagent", cmdctrl.CommandInfo{
  561. MaxRetries: 2,
  562. Shell: true,
  563. OnStart: func() error {
  564. log.Println("killProcessByName apk-agent.cli")
  565. killProcessByName("apkagent.cli")
  566. return nil
  567. },
  568. ArgsFunc: func() ([]string, error) {
  569. packagePath, err := getPackagePath("com.github.uiautomator")
  570. if err != nil {
  571. return nil, err
  572. }
  573. return []string{"CLASSPATH=" + packagePath, "exec", "app_process", "/system/bin", "com.github.uiautomator.Console"}, nil
  574. },
  575. })
  576. service.Start("apkagent")
  577. service.Add("minitouch", cmdctrl.CommandInfo{
  578. MaxRetries: 2,
  579. Args: []string{"/data/local/tmp/minitouch"},
  580. Shell: true,
  581. })
  582. // uiautomator 1.0
  583. service.Add("uiautomator-1.0", cmdctrl.CommandInfo{
  584. Args: []string{"sh", "-c",
  585. "uiautomator runtest uiautomator-stub.jar bundle.jar -c com.github.uiautomatorstub.Stub"},
  586. // Args: []string{"uiautomator", "runtest", "/data/local/tmp/uiautomator-stub.jar", "bundle.jar","-c", "com.github.uiautomatorstub.Stub"},
  587. Stdout: os.Stdout,
  588. Stderr: os.Stderr,
  589. MaxRetries: 3,
  590. RecoverDuration: 30 * time.Second,
  591. StopSignal: os.Interrupt,
  592. OnStart: func() error {
  593. uiautomatorTimer.Reset()
  594. return nil
  595. },
  596. OnStop: func() {
  597. uiautomatorTimer.Stop()
  598. },
  599. })
  600. // uiautomator 2.0
  601. service.Add("uiautomator", cmdctrl.CommandInfo{
  602. Args: []string{"am", "instrument", "-w", "-r",
  603. "-e", "debug", "false",
  604. "-e", "class", "com.github.uiautomator.stub.Stub",
  605. "com.github.uiautomator.test/androidx.test.runner.AndroidJUnitRunner"}, // update for android-uiautomator-server.apk>=2.3.2
  606. //"com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner"},
  607. Stdout: os.Stdout,
  608. Stderr: os.Stderr,
  609. MaxRetries: 1, // only once
  610. RecoverDuration: 30 * time.Second,
  611. StopSignal: os.Interrupt,
  612. OnStart: func() error {
  613. uiautomatorTimer.Reset()
  614. // log.Println("service uiautomator: startservice com.github.uiautomator/.Service")
  615. // runShell("am", "startservice", "-n", "com.github.uiautomator/.Service")
  616. return nil
  617. },
  618. OnStop: func() {
  619. uiautomatorTimer.Stop()
  620. // log.Println("service uiautomator: stopservice com.github.uiautomator/.Service")
  621. // runShell("am", "stopservice", "-n", "com.github.uiautomator/.Service")
  622. // runShell("am", "force-stop", "com.github.uiautomator")
  623. },
  624. })
  625. // stop uiautomator when 3 minutes not requests
  626. go func() {
  627. for range uiautomatorTimer.C {
  628. log.Println("uiautomator has not activity for 3 minutes, closed")
  629. service.Stop("uiautomator")
  630. service.Stop("uiautomator-1.0")
  631. }
  632. }()
  633. if !*fNoUiautomator {
  634. if err := service.Start("uiautomator"); err != nil {
  635. log.Println("uiautomator start failed:", err)
  636. }
  637. }
  638. server := NewServer()
  639. sigc := make(chan os.Signal, 1)
  640. signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
  641. go func() {
  642. for sig := range sigc {
  643. needStop := false
  644. switch sig {
  645. case syscall.SIGTERM:
  646. needStop = true
  647. case syscall.SIGHUP:
  648. case syscall.SIGINT:
  649. if !*fDaemon {
  650. needStop = true
  651. }
  652. }
  653. if needStop {
  654. log.Println("Catch signal", sig)
  655. service.StopAll()
  656. server.httpServer.Shutdown(context.TODO())
  657. return
  658. }
  659. log.Println("Ignore signal", sig)
  660. }
  661. }()
  662. service.Start("minitouch")
  663. // run server forever
  664. if err := server.Serve(listener); err != nil {
  665. log.Println("server quit:", err)
  666. }
  667. }