Skip to content

Commit d4ccfbf

Browse files
authored
Add custom dns to resolve host (#3)
Add custom dns to resolve host
1 parent 4223849 commit d4ccfbf

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ var (
2626
httpHead bool
2727
httpPost bool
2828
httpUA string
29+
30+
dnsServer []string
2931
)
3032

3133
var rootCmd = cobra.Command{
@@ -110,6 +112,9 @@ var rootCmd = cobra.Command{
110112
return
111113
}
112114
}
115+
if len(dnsServer) != 0 {
116+
ping.UseCustomeDNS(dnsServer)
117+
}
113118
target := ping.Target{
114119
Timeout: timeoutDuration,
115120
Interval: intervalDuration,
@@ -163,6 +168,8 @@ func init() {
163168
rootCmd.Flags().BoolVar(&httpPost, "post", false, `Use HEAD instead of GET in http mode.`)
164169
rootCmd.Flags().StringVar(&httpUA, "user-agent", "tcping", `Use custom UA in http mode.`)
165170

171+
rootCmd.Flags().StringArrayVarP(&dnsServer, "dns-server", "D", nil, `Use the specified dns resolve server.`)
172+
166173
}
167174

168175
func main() {

ping/http.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66
"io"
77
"io/ioutil"
8+
"net"
89
"net/http"
10+
"net/http/httptrace"
911
"time"
1012
)
1113

@@ -47,15 +49,15 @@ func (ping *HTTPing) Start() <-chan struct{} {
4749
ping.Stop()
4850
return
4951
}
50-
duration, resp, err := ping.ping()
52+
duration, resp, remoteAddr, err := ping.ping()
5153
ping.result.Counter++
5254

5355
if err != nil {
5456
fmt.Printf("Ping %s - failed: %s\n", ping.target, err)
5557
} else {
5658
defer resp.Body.Close()
5759
length, _ := io.Copy(ioutil.Discard, resp.Body)
58-
fmt.Printf("Ping %s - %s is open - time=%s method=%s status=%d bytes=%d\n", ping.target, ping.target.Protocol, duration, ping.Method, resp.StatusCode, length)
60+
fmt.Printf("Ping %s(%s) - %s is open - time=%s method=%s status=%d bytes=%d\n", ping.target, remoteAddr, ping.target.Protocol, duration, ping.Method, resp.StatusCode, length)
5961
if ping.result.MinDuration == 0 {
6062
ping.result.MinDuration = duration
6163
}
@@ -88,7 +90,7 @@ func (ping *HTTPing) Stop() {
8890
ping.done <- struct{}{}
8991
}
9092

91-
func (ping HTTPing) ping() (time.Duration, *http.Response, error) {
93+
func (ping HTTPing) ping() (time.Duration, *http.Response, net.Addr, error) {
9294
var resp *http.Response
9395
var body io.Reader
9496
if ping.Method == "POST" {
@@ -97,17 +99,23 @@ func (ping HTTPing) ping() (time.Duration, *http.Response, error) {
9799
req, err := http.NewRequest(ping.Method, ping.target.String(), body)
98100
req.Header.Set(http.CanonicalHeaderKey("User-Agent"), "tcping")
99101
if err != nil {
100-
return 0, nil, err
102+
return 0, nil, nil, err
101103
}
102-
104+
var remoteAddr net.Addr
105+
trace := &httptrace.ClientTrace{
106+
GotConn: func(connInfo httptrace.GotConnInfo) {
107+
remoteAddr = connInfo.Conn.RemoteAddr()
108+
},
109+
}
110+
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
103111
duration, errIfce := timeIt(func() interface{} {
104112
client := http.Client{Timeout: ping.target.Timeout}
105113
resp, err = client.Do(req)
106114
return err
107115
})
108116
if errIfce != nil {
109117
err := errIfce.(error)
110-
return 0, nil, err
118+
return 0, nil, nil, err
111119
}
112-
return time.Duration(duration), resp, nil
120+
return time.Duration(duration), resp, remoteAddr, nil
113121
}

ping/tcp.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,21 @@ func (tcping TCPing) Result() *Result {
4040
func (tcping TCPing) Start() <-chan struct{} {
4141
go func() {
4242
t := time.NewTicker(tcping.target.Interval)
43+
defer t.Stop()
4344
for {
4445
select {
4546
case <-t.C:
4647
if tcping.result.Counter >= tcping.target.Counter && tcping.target.Counter != 0 {
4748
tcping.Stop()
4849
return
4950
}
50-
duration, err := tcping.ping()
51+
duration, remoteAddr, err := tcping.ping()
5152
tcping.result.Counter++
5253

5354
if err != nil {
5455
fmt.Printf("Ping %s - failed: %s\n", tcping.target, err)
5556
} else {
56-
fmt.Printf("Ping %s - Connected - time=%s\n", tcping.target, duration)
57+
fmt.Printf("Ping %s(%s) - Connected - time=%s\n", tcping.target, remoteAddr, duration)
5758

5859
if tcping.result.MinDuration == 0 {
5960
tcping.result.MinDuration = duration
@@ -82,18 +83,20 @@ func (tcping *TCPing) Stop() {
8283
tcping.done <- struct{}{}
8384
}
8485

85-
func (tcping TCPing) ping() (time.Duration, error) {
86+
func (tcping TCPing) ping() (time.Duration, net.Addr, error) {
87+
var remoteAddr net.Addr
8688
duration, errIfce := timeIt(func() interface{} {
8789
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", tcping.target.Host, tcping.target.Port), tcping.target.Timeout)
8890
if err != nil {
8991
return err
9092
}
93+
remoteAddr = conn.RemoteAddr()
9194
conn.Close()
9295
return nil
9396
})
9497
if errIfce != nil {
9598
err := errIfce.(error)
96-
return 0, err
99+
return 0, remoteAddr, err
97100
}
98-
return time.Duration(duration), nil
101+
return time.Duration(duration), remoteAddr, nil
99102
}

ping/utils.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
package ping
22

3-
import "time"
3+
import (
4+
"context"
5+
"net"
6+
"time"
7+
)
48

59
func timeIt(f func() interface{}) (int64, interface{}) {
610
startAt := time.Now()
711
res := f()
812
endAt := time.Now()
913
return endAt.UnixNano() - startAt.UnixNano(), res
1014
}
15+
16+
// UseCustomeDNS will set the dns to default DNS resolver for global
17+
func UseCustomeDNS(dns []string) {
18+
resolver := net.Resolver{
19+
PreferGo: true,
20+
Dial: func(ctx context.Context, network, address string) (conn net.Conn, err error) {
21+
for _, addr := range dns {
22+
if conn, err = net.Dial("udp", addr+":53"); err != nil {
23+
continue
24+
} else {
25+
return conn, nil
26+
}
27+
}
28+
return
29+
},
30+
}
31+
net.DefaultResolver = &resolver
32+
}

0 commit comments

Comments
 (0)