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

Add support for IDNA #640

Open
wants to merge 1 commit 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 @@ -248,11 +248,13 @@ func ProbeDNS(ctx context.Context, target string, module config.Module, registry
}
}

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

Choose a reason for hiding this comment

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

I'm not sure this is correct, let's leave this out.


msg := new(dns.Msg)
msg.Id = dns.Id()
msg.RecursionDesired = true
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
18 changes: 18 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/kit/log"
"github.com/go-kit/kit/log/level"
"golang.org/x/net/idna"

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

target = internationalizeDNSDomain(logger, target)

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

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

func internationalizeDNSDomain(logger log.Logger, domain string) string {
if net.ParseIP(domain) != nil {
// IP addresses don't need to be internationalized.
return domain
}
idnaDomain, err := idna.Lookup.ToASCII(domain)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should be silently ignoring errors.

Copy link
Member Author

Choose a reason for hiding this comment

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

Somehow we pass :: in some tests, which creates an error disallowed rune U+003A. I need to investigate why, but I was thinking that it would be safe to ignore those errors.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see what's going on here.

You have to try to handle the hostname as an IP address (either IPv4 or IPv6), net.ParseIP will do this for you, and if that fails handle it as a hostname and apply a punycode transformation to it.

Basically:

if net.ParseIP(domain) != nil {
    return domain
}

idnaDomain, err := idna.Lookup.ToASCII(domain)
...

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks

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 @@ -251,3 +251,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")
}
}