File tree 2 files changed +35
-5
lines changed
2 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
+ "net"
5
6
"os"
6
7
"runtime"
7
8
"sort"
29
30
qtype string
30
31
)
31
32
33
+ // EnsureDNSAddress formats the DNS server address properly.
34
+ func EnsureDNSAddress (server string ) string {
35
+ if strings .Contains (server , "]" ) || strings .Contains (server , ":" ) && net .ParseIP (server ) == nil {
36
+ return server
37
+ }
38
+
39
+ ip := net .ParseIP (server )
40
+ if ip != nil && ip .To4 () == nil { // It's IPv6 (and not IPv4)
41
+ return "[" + server + "]:53"
42
+ }
43
+ // Otherwise, assume IPv4 or hostname, so append port normally.
44
+ return server + ":53"
45
+ }
46
+
32
47
func NewRootCommand () * cobra.Command {
33
48
cmd := & cobra.Command {
34
49
Use : "zns" ,
@@ -138,11 +153,7 @@ func NewRootCommand() *cobra.Command {
138
153
}
139
154
}
140
155
141
- // If the server address does not already include a port,
142
- // append the default DNS port (53) to it.
143
- if ! strings .Contains (server , ":" ) {
144
- server = fmt .Sprintf ("%s:53" , server )
145
- }
156
+ server = EnsureDNSAddress (server )
146
157
147
158
querier := query .NewQueryClient (server , new (dns.Client ), logger )
148
159
Original file line number Diff line number Diff line change @@ -187,3 +187,22 @@ func Test_Cmd_LogFile_Debug(t *testing.T) {
187
187
assert .Contains (t , string (logFile ), "A |example.com. |01m00s |93.184.216.34" )
188
188
assert .Contains (t , string (logFile ), "CNAME |example.com. |01m00s |example.org." )
189
189
}
190
+
191
+ func TestEnsureDNSAddress (t * testing.T ) {
192
+ testCases := []struct {
193
+ input string
194
+ expected string
195
+ }{
196
+ {"127.0.0.1" , "127.0.0.1:53" },
197
+ {"2001:558:feed::1" , "[2001:558:feed::1]:53" },
198
+ {"[2001:558:feed::1]:53" , "[2001:558:feed::1]:53" },
199
+ {"example.com" , "example.com:53" },
200
+ }
201
+
202
+ for _ , tc := range testCases {
203
+ t .Run (fmt .Sprintf ("input=%s" , tc .input ), func (t * testing.T ) {
204
+ result := EnsureDNSAddress (tc .input )
205
+ assert .Equal (t , tc .expected , result )
206
+ })
207
+ }
208
+ }
You can’t perform that action at this time.
0 commit comments