Skip to content

Commit 7805045

Browse files
committed
dns ping:修复 bug
1. 修复没有正确设置 tls ServerName 的问题 2. 允许跳过 tls 证书验证 3. 手动指定域名时,不必带有末尾的 "."
1 parent 9e774fb commit 7805045

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

cmd/pping/cmd/dns.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
)
1313

1414
type dnsFlags struct {
15-
port uint16
16-
timeout time.Duration
17-
tcp bool
18-
tls bool
19-
qtype string
20-
domain string
15+
port uint16
16+
timeout time.Duration
17+
tcp bool
18+
tls bool
19+
qtype string
20+
domain string
21+
insecure bool
2122
}
2223

2324
var dnsflag dnsFlags
@@ -37,6 +38,7 @@ func addDnsCommand() {
3738
cmd.Flags().BoolVar(&dnsflag.tls, "tls", false, "use DNS-over-TLS")
3839
cmd.Flags().StringVar(&dnsflag.qtype, "type", "NS", "A, AAAA, NS, ...")
3940
cmd.Flags().StringVar(&dnsflag.domain, "domain", ".", "domain")
41+
cmd.Flags().BoolVarP(&dnsflag.insecure, "insecure", "k", false, "allow insecure server connections")
4042

4143
rootCmd.AddCommand(cmd)
4244
}
@@ -63,5 +65,6 @@ func rundns(cmd *cobra.Command, args []string) error {
6365
p.Net = Net
6466
p.Type = dnsflag.qtype
6567
p.Domain = dnsflag.domain
68+
p.Insecure = dnsflag.insecure
6669
return RunPing(p)
6770
}

pkg/ping/dns.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package ping
22

33
import (
44
"context"
5+
"crypto/tls"
56
"errors"
67
"fmt"
78
"net"
89
"strconv"
10+
"strings"
911
"time"
1012

1113
"github.com/miekg/dns"
@@ -47,6 +49,9 @@ type DnsPing struct {
4749
// 查询域名,默认 .
4850
Domain string
4951

52+
// Net 为 tcp-tls 时,是否跳过证书验证
53+
Insecure bool
54+
5055
ip net.IP
5156
}
5257

@@ -78,12 +83,19 @@ func (this *DnsPing) PingContext(ctx context.Context) IPingResult {
7883
if !ok {
7984
return &DnsPingResult{0, errors.New("unknown type"), nil}
8085
}
86+
if !strings.HasSuffix(this.Domain, ".") {
87+
this.Domain += "."
88+
}
8189
msg.SetQuestion(this.Domain, qtype)
8290
msg.MsgHdr.RecursionDesired = true
8391

8492
client := &dns.Client{}
8593
client.Net = this.Net
8694
client.Timeout = this.Timeout
95+
client.TLSConfig = &tls.Config{
96+
ServerName: this.host,
97+
InsecureSkipVerify: this.Insecure,
98+
}
8799

88100
t0 := time.Now()
89101
r, _, err := client.ExchangeContext(ctx, msg, net.JoinHostPort(ip.String(), strconv.Itoa(int(this.Port))))
@@ -98,13 +110,14 @@ func (this *DnsPing) PingContext(ctx context.Context) IPingResult {
98110

99111
func NewDnsPing(host string, timeout time.Duration) *DnsPing {
100112
return &DnsPing{
101-
host: host,
102-
Port: 53,
103-
Timeout: timeout,
104-
Net: "udp",
105-
Type: "NS",
106-
Domain: ".",
107-
ip: net.ParseIP(host),
113+
host: host,
114+
Port: 53,
115+
Timeout: timeout,
116+
Net: "udp",
117+
Type: "NS",
118+
Domain: ".",
119+
Insecure: false,
120+
ip: net.ParseIP(host),
108121
}
109122
}
110123

0 commit comments

Comments
 (0)