Skip to content

Commit

Permalink
dns ping
Browse files Browse the repository at this point in the history
  • Loading branch information
wzv5 committed Oct 3, 2020
1 parent 4c6b4d4 commit 9e774fb
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 1 deletion.
67 changes: 67 additions & 0 deletions cmd/pping/cmd/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cmd

import (
"fmt"
"net"
"strconv"
"time"

"github.com/wzv5/pping/pkg/ping"

"github.com/spf13/cobra"
)

type dnsFlags struct {
port uint16
timeout time.Duration
tcp bool
tls bool
qtype string
domain string
}

var dnsflag dnsFlags

func addDnsCommand() {
var cmd = &cobra.Command{
Use: "dns <host>",
Short: "dns ping",
Long: "dns ping",
Args: cobra.ExactArgs(1),
RunE: rundns,
}

cmd.Flags().DurationVarP(&dnsflag.timeout, "timeout", "w", time.Second*4, "timeout")
cmd.Flags().Uint16VarP(&dnsflag.port, "port", "p", 0, "port")
cmd.Flags().BoolVar(&dnsflag.tcp, "tcp", false, "use TCP")
cmd.Flags().BoolVar(&dnsflag.tls, "tls", false, "use DNS-over-TLS")
cmd.Flags().StringVar(&dnsflag.qtype, "type", "NS", "A, AAAA, NS, ...")
cmd.Flags().StringVar(&dnsflag.domain, "domain", ".", "domain")

rootCmd.AddCommand(cmd)
}

func rundns(cmd *cobra.Command, args []string) error {
host := args[0]
Net := "udp"
if dnsflag.tls {
Net = "tcp-tls"
} else if dnsflag.tcp {
Net = "tcp"
}
if dnsflag.port == 0 {
switch Net {
case "udp", "tcp":
dnsflag.port = 53
case "tcp-tls":
dnsflag.port = 853
}
}
fmt.Printf("Ping %s://%s:\n", Net, net.JoinHostPort(host, strconv.Itoa(int(dnsflag.port))))
p := ping.NewDnsPing(host, dnsflag.timeout)
p.Port = dnsflag.port
p.Net = Net
p.Type = dnsflag.qtype
p.Domain = dnsflag.domain
return RunPing(p)
}
1 change: 1 addition & 0 deletions cmd/pping/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func init() {
addTlsCommand()
addHttpCommand()
addIcmpCommand()
addDnsCommand()
}

func Execute() error {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/wzv5/pping
go 1.14

require (
github.com/miekg/dns v1.1.31
github.com/spf13/cobra v1.0.0
golang.org/x/net v0.0.0-20200904194848-62affa334b73
golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo=
github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down Expand Up @@ -92,34 +94,47 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200904194848-62affa334b73 h1:MXfv8rhZWmFeqX3GNZRsd6vOLoaCHjYEX3qkRo3YBUA=
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c h1:dk0ukUIHmGHqASjP0iue2261isepFCC6XRCSd1nHgDw=
golang.org/x/net v0.0.0-20201002202402-0a1ea396d57c/go.mod h1:iQL9McJNjoIa5mjH6nYTCTZXUN6RP+XW3eib7Ya3XcI=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand Down
114 changes: 114 additions & 0 deletions pkg/ping/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ping

import (
"context"
"errors"
"fmt"
"net"
"strconv"
"time"

"github.com/miekg/dns"
)

type DnsPingResult struct {
Time int
Err error
IP net.IP
}

func (this *DnsPingResult) Result() int {
return this.Time
}

func (this *DnsPingResult) Error() error {
return this.Err
}

func (this *DnsPingResult) String() string {
if this.Err != nil {
return fmt.Sprintf("%s", this.Err)
} else {
return fmt.Sprintf("%s: time=%d ms", this.IP.String(), this.Time)
}
}

type DnsPing struct {
host string
Port uint16
Timeout time.Duration

// udp, tcp, tcp-tls,默认 udp
Net string

// A, AAAA, NS, ...,默认 NS
Type string

// 查询域名,默认 .
Domain string

ip net.IP
}

func (this *DnsPing) SetHost(host string) {
this.host = host
this.ip = net.ParseIP(host)
}

func (this *DnsPing) Host() string {
return this.host
}

func (this *DnsPing) Ping() IPingResult {
return this.PingContext(context.Background())
}

func (this *DnsPing) PingContext(ctx context.Context) IPingResult {
ip := cloneIP(this.ip)
if ip == nil {
var err error
ip, err = LookupFunc(this.host)
if err != nil {
return &DnsPingResult{0, err, nil}
}
}

msg := &dns.Msg{}
qtype, ok := dns.StringToType[this.Type]
if !ok {
return &DnsPingResult{0, errors.New("unknown type"), nil}
}
msg.SetQuestion(this.Domain, qtype)
msg.MsgHdr.RecursionDesired = true

client := &dns.Client{}
client.Net = this.Net
client.Timeout = this.Timeout

t0 := time.Now()
r, _, err := client.ExchangeContext(ctx, msg, net.JoinHostPort(ip.String(), strconv.Itoa(int(this.Port))))
if err != nil {
return &DnsPingResult{0, err, nil}
}
if r == nil || r.Response == false || r.Opcode != dns.OpcodeQuery {
return &DnsPingResult{0, errors.New("response error"), nil}
}
return &DnsPingResult{int(time.Now().Sub(t0).Milliseconds()), nil, ip}
}

func NewDnsPing(host string, timeout time.Duration) *DnsPing {
return &DnsPing{
host: host,
Port: 53,
Timeout: timeout,
Net: "udp",
Type: "NS",
Domain: ".",
ip: net.ParseIP(host),
}
}

var (
_ IPing = (*DnsPing)(nil)
_ IPingResult = (*DnsPingResult)(nil)
)

0 comments on commit 9e774fb

Please sign in to comment.