diff --git a/initialize.go b/dns.go similarity index 86% rename from initialize.go rename to dns.go index 95504e6..252831e 100644 --- a/initialize.go +++ b/dns.go @@ -22,11 +22,16 @@ var ( ) func dnsLookupHost(hostname string) (ip net.IP, err error) { - dnsServer := getProperty("net.dns1") - if dnsServer == "" { - dnsServer = "8.8.8.8" + for _, dnsServer := range []string{getProperty("net.dns1"), "114.114.114.114", "8.8.4.4"} { + if dnsServer == "" { + continue + } + ip, err = dnsLookupHostWithDNS(hostname, dnsServer) + if err == nil { + return + } } - return dnsLookupHostWithDNS(hostname, dnsServer) + return } func dnsLookupHostWithDNS(hostname string, dnsServer string) (ip net.IP, err error) { @@ -49,7 +54,7 @@ func dnsLookupHostWithDNS(hostname string, dnsServer string) (ip net.IP, err err if len(in.Answer) == 0 { return nil, errors.New("dns return empty answer") } - log.Println(in.Answer[0]) + log.Println("dns:"+dnsServer, in.Answer[0]) if t, ok := in.Answer[0].(*dns.A); ok { return t.A, nil } diff --git a/dns_test.go b/dns_test.go new file mode 100644 index 0000000..35a2c2f --- /dev/null +++ b/dns_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDNSLookup(t *testing.T) { + ip, err := dnsLookupHost("www.netease.com") + assert.Nil(t, err) + t.Logf("www.netease.com -> %s", ip) +} diff --git a/main.go b/main.go index 21d983d..d17181c 100644 --- a/main.go +++ b/main.go @@ -149,8 +149,6 @@ type MinicapInfo struct { } var ( - propOnce sync.Once - properties map[string]string deviceRotation int displayMaxWidthHeight = 800 ) @@ -162,18 +160,6 @@ func updateMinicapRotation(rotation int) { fmt.Sprintf("%dx%d@%dx%d/%d", width, height, displayMaxWidthHeight, displayMaxWidthHeight, rotation)) } -func getProperty(name string) string { - propOnce.Do(func() { - var err error - properties, err = androidutils.Properties() - if err != nil { - log.Println("getProperty err:", err) - properties = make(map[string]string) - } - }) - return properties[name] -} - const ( apkVersionCode = 4 apkVersionName = "1.0.4" @@ -195,7 +181,7 @@ func installAPK(path string) error { // -g: grant all runtime permissions // -d: allow version code downgrade // -r: replace existing application - sdk, _ := strconv.Atoi(getProperty("ro.build.version.sdk")) + sdk, _ := strconv.Atoi(getCachedProperty("ro.build.version.sdk")) cmds := []string{"pm", "install", "-d", "-r", path} if sdk >= 23 { // android 6.0 cmds = []string{"pm", "install", "-d", "-r", "-g", path} @@ -1193,7 +1179,7 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error { m.Handle("/jsonrpc/0", uiautomatorProxy) m.Handle("/ping", uiautomatorProxy) m.HandleFunc("/screenshot/0", func(w http.ResponseWriter, r *http.Request) { - if r.FormValue("minicap") == "false" || strings.ToLower(getProperty("ro.product.manufacturer")) == "meizu" { + if r.FormValue("minicap") == "false" || strings.ToLower(getCachedProperty("ro.product.manufacturer")) == "meizu" { uiautomatorProxy.ServeHTTP(w, r) return } diff --git a/requirements.go b/requirements.go index 5fcd4e5..a1cd948 100644 --- a/requirements.go +++ b/requirements.go @@ -89,7 +89,7 @@ func installMinicap() error { func installMinitouch() error { baseURL := "https://github.com/codeskyblue/stf-binaries/raw/master/node_modules/minitouch-prebuilt/prebuilt" - abi := getProperty("ro.product.cpu.abi") + abi := getCachedProperty("ro.product.cpu.abi") binURL := strings.Join([]string{baseURL, abi, "bin/minitouch"}, "/") _, err := httpDownload("/data/local/tmp/minitouch", binURL, 0755) return err diff --git a/tunnelproxy.go b/tunnelproxy.go index d1402f5..cae2f33 100644 --- a/tunnelproxy.go +++ b/tunnelproxy.go @@ -20,13 +20,13 @@ var currentDeviceInfo *proto.DeviceInfo func getDeviceInfo() *proto.DeviceInfo { if currentDeviceInfo == nil { devInfo := &proto.DeviceInfo{ - Serial: getProperty("ro.serialno"), - Brand: getProperty("ro.product.brand"), - Model: getProperty("ro.product.model"), - Version: getProperty("ro.build.version.release"), + 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(getProperty("ro.build.version.sdk")) + devInfo.Sdk, _ = strconv.Atoi(getCachedProperty("ro.build.version.sdk")) devInfo.HWAddr, _ = androidutils.HWAddrWLAN() display, _ := androidutils.WindowSize() devInfo.Display = &display @@ -57,7 +57,10 @@ func getDeviceInfo() *proto.DeviceInfo { } // Udid is ${Serial}-${MacAddress}-${model} - udid := getProperty("ro.serialno") + "-" + devInfo.HWAddr + "-" + strings.Replace(getProperty("ro.product.model"), " ", "_", -1) + udid := fmt.Sprintf("%s-%s-%s", + getCachedProperty("ro.serialno"), + devInfo.HWAddr, + strings.Replace(getCachedProperty("ro.product.model"), " ", "_", -1)) devInfo.Udid = udid currentDeviceInfo = devInfo } diff --git a/utils.go b/utils.go index 3a1a272..ebe8e8d 100644 --- a/utils.go +++ b/utils.go @@ -21,6 +21,7 @@ import ( "github.com/codeskyblue/procfs" "github.com/franela/goreq" shellquote "github.com/kballard/go-shellquote" + "github.com/openatx/androidutils" "github.com/shogo82148/androidbinary/apk" ) @@ -257,3 +258,11 @@ func newHijackReadWriteCloser(conn *net.TCPConn, bufrw *bufio.ReadWriter) net.Co TCPConn: conn, } } + +func getCachedProperty(name string) string { + return androidutils.CachedProperty(name) +} + +func getProperty(name string) string { + return androidutils.Property(name) +} diff --git a/vendor/github.com/openatx/androidutils/getprop.go b/vendor/github.com/openatx/androidutils/getprop.go index 57d14ec..dae1f6d 100644 --- a/vendor/github.com/openatx/androidutils/getprop.go +++ b/vendor/github.com/openatx/androidutils/getprop.go @@ -5,6 +5,7 @@ import ( "log" "os/exec" "regexp" + "strings" "sync" "time" @@ -48,9 +49,8 @@ var ( ErrGetprop = errors.New("error call getprop") ) -// Return property by name -// if something went wrong, return "" -func GetProperty(name string) string { +// Return property by name from cache +func CachedProperty(name string) string { propOnce.Do(func() { var err error properties, err = Properties() @@ -60,3 +60,12 @@ func GetProperty(name string) string { }) return properties[name] } + +// Return property by name +func Property(name string) string { + propOutput, err := runShell("getprop", name) + if err != nil { + return "" + } + return strings.TrimSpace(propOutput) +} diff --git a/vendor/github.com/openatx/androidutils/other.go b/vendor/github.com/openatx/androidutils/other.go index 01dc3eb..b937a46 100644 --- a/vendor/github.com/openatx/androidutils/other.go +++ b/vendor/github.com/openatx/androidutils/other.go @@ -17,7 +17,7 @@ func HWAddrWLAN() (string, error) { // Thanks to this article https://android.stackexchange.com/questions/142606/how-can-i-find-my-mac-address/142630#142630 func getHWAddrWLAN() (string, error) { // method 1 - if macaddr := GetProperty("ro.boot.wifimacaddr"); macaddr != "" { + if macaddr := CachedProperty("ro.boot.wifimacaddr"); macaddr != "" { return macaddr, nil } // method 2 diff --git a/vendor/vendor.json b/vendor/vendor.json index 31c854a..fddfab2 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -183,10 +183,10 @@ "revisionTime": "2017-03-13T01:07:58Z" }, { - "checksumSHA1": "BNPuistkwwZwaz+vMgo7QhruiYs=", + "checksumSHA1": "duzq88Iqk8mTza18jf6lzqbqmyA=", "path": "github.com/openatx/androidutils", - "revision": "ae25fc8bf1157dacc0664f108c3edf152c85900e", - "revisionTime": "2018-02-07T05:20:18Z" + "revision": "24690fcfdb86b129b1f309de53e31724abe29484", + "revisionTime": "2018-05-10T02:30:39Z" }, { "checksumSHA1": "kPCCYHtl3MleEvLPhb6LsjAWRe4=",