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

Commit

Permalink
fix /install error when resolve dns(only happend in network change
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Feb 2, 2018
1 parent 609e64b commit 1e058ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
13 changes: 10 additions & 3 deletions cmdctrl/cmdctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,24 @@ func (cc *CommandCtrl) Start(name string) error {
return pkeeper.start()
}

// Stop send stop signal and quit immediately
func (cc *CommandCtrl) Stop(name string) error {
// Stop send stop signal
// Stop("demo") will quit immediately
// Stop("demo", true) will quit until command really killed
func (cc *CommandCtrl) Stop(name string, waits ...bool) error {
cc.rl.RLock()
defer cc.rl.RUnlock()
pkeeper, ok := cc.cmds[name]
if !ok {
return errors.New("cmdctl not found: " + name)
}
return pkeeper.stop(false)
wait := false
if len(waits) > 0 {
wait = waits[0]
}
return pkeeper.stop(wait)
}

// StopAll command and wait until all program quited
func (cc *CommandCtrl) StopAll() {
for _, pkeeper := range cc.cmds {
pkeeper.stop(true)
Expand Down
20 changes: 12 additions & 8 deletions initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ var (
httpServer *http.Server
)

func dnsLookupHost(hostname string, dnsServer string) (ip net.IP, err error) {
func dnsLookupHost(hostname string) (ip net.IP, err error) {
dnsServer := getProperty("net.dns1")
if dnsServer == "" {
dnsServer = "8.8.8.8"
}
return dnsLookupHostWithDNS(hostname, dnsServer)
}

func dnsLookupHostWithDNS(hostname string, dnsServer string) (ip net.IP, err error) {
if !strings.HasSuffix(hostname, ".") {
hostname += "."
}
Expand All @@ -46,7 +54,7 @@ func dnsLookupHost(hostname string, dnsServer string) (ip net.IP, err error) {
return t.A, nil
}
if t, ok := in.Answer[0].(*dns.CNAME); ok {
return dnsLookupHost(t.Target, dnsServer)
return dnsLookupHostWithDNS(t.Target, dnsServer)
}
return nil, errors.New("dns resolve failed: " + hostname)
}
Expand All @@ -57,16 +65,12 @@ func init() {
KeepAlive: 30 * time.Second,
DualStack: true,
}
// get default dns server
dnsServer := getProperty("net.dns1")
if dnsServer == "" {
dnsServer = "8.8.8.8"
}

// manualy dns resolve
newDialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, _ := net.SplitHostPort(addr)
if net.ParseIP(host) == nil {
ip, err := dnsLookupHost(host, dnsServer)
ip, err := dnsLookupHost(host)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {
}).Methods("POST")

m.HandleFunc("/uiautomator", func(w http.ResponseWriter, r *http.Request) {
err := service.Stop("uiautomator")
err := service.Stop("uiautomator", true) // wait until program quit
if err == nil {
io.WriteString(w, "Success")
} else {
Expand Down

0 comments on commit 1e058ed

Please sign in to comment.