Skip to content

Commit

Permalink
fix: DNS scope ID
Browse files Browse the repository at this point in the history
Closes: 41
  • Loading branch information
natesales committed Mar 30, 2023
1 parent 6159058 commit ac997ce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
35 changes: 29 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type optsTemplate struct {

UDPBuffer uint16 `long:"udp-buffer" description:"Set EDNS0 UDP size in query" default:"1232"`
Verbose bool `short:"v" long:"verbose" description:"Show verbose log messages"`
Trace bool `long:"trace" description:"Show trace log messages"`
ShowVersion bool `short:"V" long:"version" description:"Show version and exit"`
}

Expand Down Expand Up @@ -251,7 +252,7 @@ func parsePlusFlags(args []string) {

// parseServer parses opts.Server into a protocol and host:port
func parseServer() (string, string, error) {
var scheme, host, port string
var scheme, host, port, scopeId string

// Set default protocol
if !strings.Contains(opts.Server, "://") {
Expand Down Expand Up @@ -284,8 +285,7 @@ func parseServer() (string, string, error) {
host = strings.ReplaceAll(opts.Server, scheme+"://", "")

// Remove port from host
if strings.Contains(host, "[") && !strings.Contains(host, "]") ||
!strings.Contains(host, "[") && strings.Contains(host, "]") {
if strings.Contains(host, "[") && !strings.Contains(host, "]") || !strings.Contains(host, "[") && strings.Contains(host, "]") {
return "", "", fmt.Errorf("invalid IPv6 bracket notation")
} else if strings.Contains(host, "[") && strings.Contains(host, "]") { // IPv6 in bracket notation
portSuffix := strings.Split(host, "]:")
Expand All @@ -294,23 +294,38 @@ func parseServer() (string, string, error) {
} else {
port = ""
}
host = "[" + strings.Split(strings.Split(host, "[")[1], "]")[0] + "]"

host = strings.Split(strings.Split(host, "[")[1], "]")[0]

// Remove IPv6 scope ID
if strings.Contains(host, "%") {
parts := strings.Split(host, "%")
host = parts[0]
scopeId = parts[1]
}

host = "[" + host + "]"
log.Tracef("host contains ], treating as v6 with port. host: %s port: %s", host, port)
} else if strings.Contains(host, ".") && strings.Contains(host, ":") { // IPv4 or hostname with port
parts := strings.Split(host, ":")
host = parts[0]
port = parts[1]
log.Tracef("host contains . and :, treating as (v4 or host) with explicit port. host %s port %s", host, port)
} else if strings.Contains(host, ":") { // IPv6 no port
// Remove IPv6 scope ID
if strings.Contains(host, "%") {
parts := strings.Split(host, "%")
host = parts[0]
scopeId = parts[1]
}

host = "[" + host + "]"
log.Tracef("host contains :, treating as v6 without port. host %s", host)
} else {
log.Tracef("no cases matched for host %s port %s", host, port)
}
}

log.Debugf("Using scheme: %s host: %s port: %s", scheme, host, port)

// Validate ODoH
if opts.ODoHProxy != "" {
if !strings.HasPrefix(opts.ODoHProxy, "https://") {
Expand All @@ -337,6 +352,7 @@ func parseServer() (string, string, error) {
log.Tracef("Port is %s, not overriding", port)
}

log.Infof("Using DNS server: %s://%s:%s", scheme, host, port)
fqdn := scheme + "://" + host
if scheme != "https" {
fqdn += ":" + port
Expand All @@ -361,6 +377,11 @@ func parseServer() (string, string, error) {
}
}

// Insert scope ID before ']'
if scopeId != "" {
server = strings.Replace(server, "]", "%"+scopeId+"]", 1)
}

return scheme, server, nil
}

Expand All @@ -381,6 +402,8 @@ All long form (--) flags can be toggled with the dig-standard +[no]flag notation

if opts.Verbose {
log.SetLevel(log.DebugLevel)
} else if opts.Trace {
log.SetLevel(log.TraceLevel)
}

if opts.ShowVersion {
Expand Down
5 changes: 5 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ func TestMainParseServer(t *testing.T) {
ExpectedProtocol: "quic",
ExpectedHost: "dns.adguard.com:8530",
},
{ // IPv6 with scope ID
Server: "plain://[fe80::1%en0]:53",
ExpectedProtocol: "plain",
ExpectedHost: "[fe80::1%en0]:53",
},
} {
clearOpts()
opts.Server = tc.Server
Expand Down

0 comments on commit ac997ce

Please sign in to comment.