Skip to content

Commit ebbbbd0

Browse files
committed
Address feedback and linting errors
1 parent 03db674 commit ebbbbd0

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

.golangci.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
text = "(tlsFeatureExtensionOID|ocspMustStapleFeature) is a global variable"
126126
[[issues.exclude-rules]]
127127
path = "challenge/dns01/nameserver.go"
128-
text = "(defaultNameservers|recursiveNameservers|fqdnSoaCache|muFqdnSoaCache) is a global variable"
128+
text = "(defaultNameservers|recursiveNameservers|currentNetworkStack|fqdnSoaCache|muFqdnSoaCache) is a global variable"
129129
[[issues.exclude-rules]]
130130
path = "challenge/dns01/nameserver_.+.go"
131131
text = "dnsTimeout is a global variable"

challenge/dns01/nameserver.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ var defaultNameservers = []string{
2626
// recursiveNameservers are used to pre-check DNS propagation.
2727
var recursiveNameservers = getNameservers(defaultResolvConf, defaultNameservers)
2828

29-
// networkStack is used to define which IP stack will be used. The default is
30-
// both IPv4 and IPv6. Set to "ipv4" for IPv4 only, and "ipv6" for IPv6 only.
31-
var networkStack = "both"
29+
type networkStack int
30+
31+
const (
32+
defaultNetworkStack networkStack = iota
33+
ipv4Only
34+
ipv6Only
35+
)
36+
37+
// currentNetworkStack is used to define which IP stack will be used. The default is
38+
// both IPv4 and IPv6. Set to ipv4Only or ipv6Only to select either version.
39+
var currentNetworkStack = defaultNetworkStack
3240

3341
// soaCacheEntry holds a cached SOA record (only selected fields).
3442
type soaCacheEntry struct {
@@ -74,10 +82,15 @@ func AddRecursiveNameservers(nameservers []string) ChallengeOption {
7482
// SetNetworkStack defines the IP stack that will be used for DNS queries.
7583
// Accepts "both", "ipv4", or "ipv6".
7684
func SetNetworkStack(network string) {
77-
if network == "ipv4" || network == "ipv6" {
78-
networkStack = network
79-
} else {
80-
networkStack = "both"
85+
switch network {
86+
case "ipv4":
87+
currentNetworkStack = ipv4Only
88+
89+
case "ipv6":
90+
currentNetworkStack = ipv6Only
91+
92+
default:
93+
currentNetworkStack = defaultNetworkStack
8194
}
8295
}
8396

@@ -270,10 +283,10 @@ func getNetwork(proto string) string {
270283
// the [net.Dialer] (https://github.com/miekg/dns/blob/fe20d5d/client.go#L119-L141).
271284
// And the [net.Dialer] accepts strings such as "udp4" or "tcp6"
272285
// (https://cs.opensource.google/go/go/+/refs/tags/go1.18.9:src/net/dial.go;l=167-182).
273-
if networkStack == "ipv4" {
286+
if currentNetworkStack == ipv4Only {
274287
return proto + "4"
275288
}
276-
if networkStack == "ipv6" {
289+
if currentNetworkStack == ipv6Only {
277290
return proto + "6"
278291
}
279292
return proto

cmd/flags.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
func CreateFlags(defaultPath string) []cli.Flag {
1010
return []cli.Flag{
1111
&cli.BoolFlag{
12-
Name: "ipv4only",
12+
Name: "ipv4only",
1313
Aliases: []string{"4"},
14-
Usage: "Use IPv4 only. This flag is ignored if ipv6only is also specified.",
14+
Usage: "Use IPv4 only. This flag is ignored if ipv6only is also specified.",
1515
},
1616
&cli.BoolFlag{
17-
Name: "ipv6only",
17+
Name: "ipv6only",
1818
Aliases: []string{"6"},
19-
Usage: "Use IPv6 only. This flag is ignored if ipv4only is also specified.",
19+
Usage: "Use IPv6 only. This flag is ignored if ipv4only is also specified.",
2020
},
2121
&cli.StringSliceFlag{
2222
Name: "domains",

cmd/setup_challenges.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,16 @@ func setupDNS(ctx *cli.Context, client *lego.Client) {
111111
log.Fatal(err)
112112
}
113113

114-
if ctx.IsSet("ipv4only") && ctx.IsSet("ipv6only") {
114+
switch {
115+
case ctx.IsSet("ipv4only") && ctx.IsSet("ipv6only"):
115116
// If both flags are set then it's as good as not providing either flag,
116117
// so we default to the OS choice.
117118
dns01.SetNetworkStack("both")
118-
} else if ctx.IsSet("ipv4only") {
119+
120+
case ctx.IsSet("ipv4only"):
119121
dns01.SetNetworkStack("ipv4")
120-
} else if ctx.IsSet("ipv6only") {
122+
123+
case ctx.IsSet("ipv6only"):
121124
dns01.SetNetworkStack("ipv6")
122125
}
123126

0 commit comments

Comments
 (0)