Skip to content

Commit

Permalink
Add support for IDNA
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Pivotto <[email protected]>
  • Loading branch information
roidelapluie committed Jun 15, 2020
1 parent 9bbe703 commit 474f012
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions prober/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,17 @@ func ProbeDNS(ctx context.Context, target string, module config.Module, registry
}
}

idnTarget, err := internationalizeDNSTarget(logger, module.DNS.QueryName)
if err != nil {
level.Error(logger).Log("msg", "IDN conversion failed", "err", err)
return false
}

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(idnTarget), qt, qc}
level.Info(logger).Log("msg", "Making DNS query", "target", target, "dial_protocol", dialProtocol, "query", module.DNS.QueryName, "type", qt, "class", qc)
timeoutDeadline, _ := ctx.Deadline()
client.Timeout = time.Until(timeoutDeadline)
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,12 @@ func chooseProtocol(ctx context.Context, IPProtocol string, fallbackIPProtocol b
fallbackProtocol = "ip6"
}

target, err = internationalizeDNSTarget(logger, target)
if err != nil {
level.Error(logger).Log("msg", "IDN conversion failed", "err", err)
return nil, 0.0, err
}

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

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

func internationalizeDNSTarget(logger log.Logger, target string) (string, error) {
idnaTarget, err := idna.ToASCII(target)
if err != nil {
return target, fmt.Errorf("error while internationalizing target: %w", err)
}
if idnaTarget != target {
level.Info(logger).Log("msg", "Target address internationalized", "ascii", idnaTarget)
}
return idnaTarget, nil
}
20 changes: 20 additions & 0 deletions prober/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,23 @@ func TestChooseProtocol(t *testing.T) {
t.Error("without fallback it should not answer")
}
}

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")
}
}

0 comments on commit 474f012

Please sign in to comment.