feat(inputs.whois): Support IDN domains (#16700)
This commit is contained in:
parent
ffb4a8cedb
commit
df6d44e43d
|
|
@ -0,0 +1,2 @@
|
|||
invalid domain format: "*.example.com"
|
||||
invalid domain format: "no-tld"
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
[[inputs.whois]]
|
||||
domains = ["invalid-domain.xyz"]
|
||||
domains = ["invalid-domain.xyz", "*.example.com", "no-tld"]
|
||||
timeout = "5s"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
[[inputs.whois]]
|
||||
domains = ["münchen.de", "xn--mnchn-kva.de"]
|
||||
timeout = "5s"
|
||||
|
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"github.com/likexian/whois"
|
||||
"github.com/likexian/whois-parser"
|
||||
"golang.org/x/net/idna"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/config"
|
||||
|
|
@ -21,6 +22,8 @@ import (
|
|||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
const maxDomainLength = 253
|
||||
|
||||
type Whois struct {
|
||||
Domains []string `toml:"domains"`
|
||||
Server string `toml:"server"`
|
||||
|
|
@ -55,10 +58,26 @@ func (w *Whois) Init() error {
|
|||
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 {
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue