-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |