feat(inputs.whois): Support IDN domains (#16700)

This commit is contained in:
skartikey 2025-04-09 16:08:11 +01:00 committed by GitHub
parent ffb4a8cedb
commit df6d44e43d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 43 additions and 3 deletions

View File

@ -0,0 +1,2 @@
invalid domain format: "*.example.com"
invalid domain format: "no-tld"

View File

@ -1,3 +1,3 @@
[[inputs.whois]] [[inputs.whois]]
domains = ["invalid-domain.xyz"] domains = ["invalid-domain.xyz", "*.example.com", "no-tld"]
timeout = "5s" timeout = "5s"

View File

@ -0,0 +1,2 @@
whois,domain=münchen.de,status=unknown registrant="not set",registrar="DENIC eG (the German domain registry)",name_servers="ns01e.muenchen.de,ns02e.muenchen.de",dnssec_enabled=false,creation_timestamp=808358400i,expiration_timestamp=1912896000i,updated_timestamp=1704067200i,expiry=172283583i
whois,domain=xn--mnchn-kva.de,status=unknown registrant="not set",registrar="DENIC eG",name_servers="ns01e.muenchen.de,ns02e.muenchen.de",dnssec_enabled=false,creation_timestamp=808358400i,expiration_timestamp=1912896000i,updated_timestamp=1704067200i,expiry=172283583i

View File

@ -0,0 +1,7 @@
Domain Name: münchen.de
Registrar: DENIC eG (the German domain registry)
Updated Date: 2024-01-01T00:00:00Z
Creation Date: 1995-08-14T00:00:00Z
Registry Expiry Date: 2030-08-14T00:00:00Z
Name Server: ns01e.muenchen.de
Name Server: ns02e.muenchen.de

View File

@ -0,0 +1,7 @@
Domain Name: xn--mnchn-kva.de
Registrar: DENIC eG
Updated Date: 2024-01-01T00:00:00Z
Creation Date: 1995-08-14T00:00:00Z
Registry Expiry Date: 2030-08-14T00:00:00Z
Name Server: ns01e.muenchen.de
Name Server: ns02e.muenchen.de

View File

@ -0,0 +1,3 @@
[[inputs.whois]]
domains = ["münchen.de", "xn--mnchn-kva.de"]
timeout = "5s"

View File

@ -12,6 +12,7 @@ import (
"github.com/likexian/whois" "github.com/likexian/whois"
"github.com/likexian/whois-parser" "github.com/likexian/whois-parser"
"golang.org/x/net/idna"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/config"
@ -21,6 +22,8 @@ import (
//go:embed sample.conf //go:embed sample.conf
var sampleConfig string var sampleConfig string
const maxDomainLength = 253
type Whois struct { type Whois struct {
Domains []string `toml:"domains"` Domains []string `toml:"domains"`
Server string `toml:"server"` Server string `toml:"server"`
@ -55,10 +58,26 @@ func (w *Whois) Init() error {
return nil return nil
} }
var domainRegex = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9-]{0,253}[a-zA-Z0-9]\.[a-zA-Z]{2,}$`) var asciiDomainRegex = regexp.MustCompile(`^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,63}$`)
func isValidDomain(domain string) bool { func isValidDomain(domain string) bool {
return domainRegex.MatchString(domain) if len(domain) > maxDomainLength {
return false
}
// Handle standard ASCII domains
if asciiDomainRegex.MatchString(domain) {
return true
}
// Try to convert to Punycode (handles IDNs)
p := idna.New(idna.MapForLookup(), idna.StrictDomainName(true))
punycodeVersion, err := p.ToASCII(domain)
if err != nil {
return false
}
return asciiDomainRegex.MatchString(punycodeVersion)
} }
func (w *Whois) Gather(acc telegraf.Accumulator) error { func (w *Whois) Gather(acc telegraf.Accumulator) error {