forked from openatx/atx-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtunnelproxy.go
154 lines (140 loc) · 3.85 KB
/
tunnelproxy.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
package main
import (
"fmt"
"log"
"math"
"strconv"
"strings"
"time"
"github.com/codeskyblue/goreq"
"github.com/codeskyblue/heartbeat"
"github.com/openatx/androidutils"
"github.com/openatx/atx-server/proto"
)
var currentDeviceInfo *proto.DeviceInfo
func getDeviceInfo() *proto.DeviceInfo {
if currentDeviceInfo != nil {
return currentDeviceInfo
}
devInfo := &proto.DeviceInfo{
Serial: getCachedProperty("ro.serialno"),
Brand: getCachedProperty("ro.product.brand"),
Model: getCachedProperty("ro.product.model"),
Version: getCachedProperty("ro.build.version.release"),
AgentVersion: version,
}
devInfo.Sdk, _ = strconv.Atoi(getCachedProperty("ro.build.version.sdk"))
devInfo.HWAddr, _ = androidutils.HWAddrWLAN()
display, _ := androidutils.WindowSize()
devInfo.Display = &display
battery := androidutils.Battery{}
battery.Update()
devInfo.Battery = &battery
devInfo.Port = listenPort
memory, err := androidutils.MemoryInfo()
if err != nil {
log.Println("get memory error:", err)
} else {
total := memory["MemTotal"]
around := int(math.Ceil(float64(total-512*1024) / 1024.0 / 1024.0)) // around GB
devInfo.Memory = &proto.MemoryInfo{
Total: total,
Around: fmt.Sprintf("%d GB", around),
}
}
hardware, processors, err := androidutils.ProcessorInfo()
if err != nil {
log.Println("get cpuinfo error:", err)
} else {
devInfo.Cpu = &proto.CpuInfo{
Hardware: hardware,
Cores: len(processors),
}
}
// Udid is ${Serial}-${MacAddress}-${model}
udid := fmt.Sprintf("%s-%s-%s",
getCachedProperty("ro.serialno"),
devInfo.HWAddr,
strings.Replace(getCachedProperty("ro.product.model"), " ", "_", -1))
devInfo.Udid = udid
currentDeviceInfo = devInfo
return currentDeviceInfo
}
type versionResponse struct {
ServerVersion string `json:"version"`
AgentVersion string `json:"atx-agent"`
}
type TunnelProxy struct {
ServerAddr string
Secret string
udid string
}
// Need test. Connect with server use github.com/codeskyblue/heartbeat
func (t *TunnelProxy) Heratbeat() {
dinfo := getDeviceInfo()
t.udid = dinfo.Udid
client := &heartbeat.Client{
Secret: t.Secret,
ServerAddr: "http://" + t.ServerAddr + "/heartbeat",
Identifier: t.udid,
}
lostCnt := 0
client.OnConnect = func() {
lostCnt = 0
t.checkUpdate()
// send device info on first connect
dinfo.Battery.Update()
if err := t.UpdateInfo(dinfo); err != nil {
log.Println("Update info:", err)
}
}
client.OnError = func(err error) {
if lostCnt == 0 {
// open identify to make WIFI reconnected when disconnected
runShellTimeout(time.Minute, "am", "start", "-n", "com.github.uiautomator/.IdentifyActivity")
}
lostCnt++
}
// send heartbeat to server every 10s
client.Beat(10 * time.Second)
}
func (t *TunnelProxy) checkUpdate() error {
res, err := goreq.Request{Uri: "http://" + t.ServerAddr + "/version"}.Do()
if err != nil {
return err
}
defer res.Body.Close()
verResp := new(versionResponse)
if err := res.Body.FromJsonTo(verResp); err != nil {
return err
}
log.Println("Disable upgrade, until code fixed")
// if verResp.AgentVersion != version {
// if version == "dev" {
// log.Printf("dev version, skip version upgrade")
// } else {
// log.Printf("server require agent version: %v, but current %s, going to upgrade", verResp.AgentVersion, version)
// if err := doUpdate(verResp.AgentVersion); err != nil {
// log.Printf("upgrade error: %v", err)
// return err
// }
// log.Printf("restarting server")
// os.Setenv(daemon.MARK_NAME, daemon.MARK_VALUE+":reset")
// runDaemon()
// os.Exit(0)
// }
// }
return nil
}
func (t *TunnelProxy) UpdateInfo(devInfo *proto.DeviceInfo) error {
res, err := goreq.Request{
Method: "POST",
Uri: "http://" + t.ServerAddr + "/devices/" + t.udid + "/info",
Body: devInfo,
}.Do()
if err != nil {
return err
}
res.Body.Close()
return nil
}