Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
fix dns always failed error
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed May 10, 2018
1 parent bba1cbe commit e6b3e2c
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 35 deletions.
15 changes: 10 additions & 5 deletions initialize.go → dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions dns_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
18 changes: 2 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ type MinicapInfo struct {
}

var (
propOnce sync.Once
properties map[string]string
deviceRotation int
displayMaxWidthHeight = 800
)
Expand All @@ -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"
Expand All @@ -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}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions tunnelproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
15 changes: 12 additions & 3 deletions vendor/github.com/openatx/androidutils/getprop.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/openatx/androidutils/other.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -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=",
Expand Down

0 comments on commit e6b3e2c

Please sign in to comment.