Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dns, http]: support IDNA domains #1192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion prober/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,13 @@ func ProbeDNS(ctx context.Context, target string, module config.Module, registry
}
}

queryName := internationalizeDNSDomain(logger, module.DNS.QueryName)

msg := new(dns.Msg)
msg.Id = dns.Id()
msg.RecursionDesired = module.DNS.Recursion
msg.Question = make([]dns.Question, 1)
msg.Question[0] = dns.Question{dns.Fqdn(module.DNS.QueryName), qt, qc}
msg.Question[0] = dns.Question{dns.Fqdn(queryName), qt, qc}

level.Info(logger).Log("msg", "Making DNS query", "target", targetIP, "dial_protocol", dialProtocol, "query", module.DNS.QueryName, "type", qt, "class", qc)
timeoutDeadline, _ := ctx.Deadline()
Expand Down
2 changes: 1 addition & 1 deletion prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
return false
}

targetHost := targetURL.Hostname()
targetHost := internationalizeDNSDomain(logger, targetURL.Hostname())
targetPort := targetURL.Port()

var ip *net.IPAddr
Expand Down
17 changes: 17 additions & 0 deletions prober/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"golang.org/x/net/idna"

"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -60,6 +61,7 @@ func chooseProtocol(ctx context.Context, IPProtocol string, fallbackIPProtocol b
fallbackProtocol = "ip6"
}

target = internationalizeDNSDomain(logger, target)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

below the target string is used is several places to log results.

Question: do I really want to see the actual target that was used (www.xn--acadmie-franaise-npb1a.fr) or do I want to see the target as entered by the user (www.académie-française.fr)? The first one is useful for debugging, the second one might be less surprising. Maybe this needs documenting?

level.Info(logger).Log("msg", "Resolving target address", "target", target, "ip_protocol", IPProtocol)
resolveStart := time.Now()

Expand Down Expand Up @@ -142,3 +144,18 @@ func ipHash(ip net.IP) float64 {
}
return float64(h.Sum32())
}

func internationalizeDNSDomain(logger log.Logger, domain string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be happier if there was a test for this function.

Basically test these cases:

  • IPv4 address
  • IPv6 address
  • regular domain name
  • domain name with non-ASCII characters in UTF form ("www.académie-française.fr")
  • domain name with non-ASCII characters in ASCII form (punycode) ("www.xn--acadmie-franaise-npb1a.fr")

if net.ParseIP(domain) != nil {
// IP addresses don't need to be internationalized.
return domain
}
idnaDomain, err := idna.Lookup.ToASCII(domain)
if err != nil {
return domain
}
if idnaDomain != domain {
level.Info(logger).Log("msg", "Domain internationalized", "unicode", domain, "ascii", idnaDomain)
}
return idnaDomain
}
20 changes: 20 additions & 0 deletions prober/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,23 @@ func checkMetrics(expected map[string]map[string]map[string]struct{}, mfs []*dto
}
}
}

func TestChooseProtocolIDNA(t *testing.T) {
if testing.Short() {
t.Skip("skipping network dependent test")
}
var (
ctx = context.Background()
registry = prometheus.NewPedanticRegistry()
w = log.NewSyncWriter(os.Stderr)
logger = log.NewLogfmtLogger(w)
)

ip, _, err := chooseProtocol(ctx, "ip4", true, "www.académie-française.fr", registry, logger)
if err != nil {
t.Error(err)
}
if ip == nil || ip.IP.To4() == nil {
t.Error("it should answer")
}
}
Loading