-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix on prometheus logger and move publicsuffix to the package transfo…
…rmers (#173) * HTTPS qtype added to the common list * move publicsuffix to transformers and fixes
- Loading branch information
Showing
11 changed files
with
243 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package transformers | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/dmachard/go-dnscollector/dnsutils" | ||
publicsuffixlist "golang.org/x/net/publicsuffix" | ||
) | ||
|
||
type PublicSuffixProcessor struct { | ||
config *dnsutils.ConfigTransformers | ||
} | ||
|
||
func NewPublicSuffixSubprocessor(config *dnsutils.ConfigTransformers) PublicSuffixProcessor { | ||
s := PublicSuffixProcessor{ | ||
config: config, | ||
} | ||
|
||
return s | ||
} | ||
|
||
func (s *PublicSuffixProcessor) IsEnabled() bool { | ||
return s.config.PublicSuffix.Enable | ||
} | ||
|
||
func (s *PublicSuffixProcessor) GetEffectiveTld(qname string) (string, error) { | ||
|
||
// PublicSuffix is case sensitive. | ||
// remove ending dot ? | ||
qname = strings.ToLower(qname) | ||
qname = strings.TrimSuffix(qname, ".") | ||
|
||
// search | ||
etld, icann := publicsuffixlist.PublicSuffix(qname) | ||
if icann { | ||
return etld, nil | ||
} | ||
return "", errors.New("ICANN Unmanaged") | ||
} | ||
|
||
func (s *PublicSuffixProcessor) GetEffectiveTldPlusOne(qname string) (string, error) { | ||
// PublicSuffix is case sensitive. | ||
// remove ending dot ? | ||
qname = strings.ToLower(qname) | ||
qname = strings.TrimSuffix(qname, ".") | ||
|
||
return publicsuffixlist.EffectiveTLDPlusOne(qname) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package transformers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dmachard/go-dnscollector/dnsutils" | ||
) | ||
|
||
func TestPublicSuffixAddTLD(t *testing.T) { | ||
// enable feature | ||
config := dnsutils.GetFakeConfigTransformers() | ||
config.PublicSuffix.Enable = true | ||
config.PublicSuffix.AddTld = true | ||
|
||
// init the processor | ||
psl := NewPublicSuffixSubprocessor(config) | ||
|
||
tt := []struct { | ||
name string | ||
qname string | ||
want string | ||
}{ | ||
{ | ||
name: "get tld", | ||
qname: "www.amazon.fr", | ||
want: "fr", | ||
}, | ||
{ | ||
name: "get tld insensitive", | ||
qname: "www.Google.Com", | ||
want: "com", | ||
}, | ||
{ | ||
name: "get tld with dot trailing", | ||
qname: "www.amazon.fr.", | ||
want: "fr", | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tld, err := psl.GetEffectiveTld(tc.qname) | ||
if err != nil { | ||
t.Errorf("Bad TLD with error: %s", err.Error()) | ||
} | ||
if tld != tc.want { | ||
t.Errorf("Bad TLD, got: %s, expected: com", tld) | ||
|
||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPublicSuffixAddTldPlusOne(t *testing.T) { | ||
// enable feature | ||
config := dnsutils.GetFakeConfigTransformers() | ||
config.PublicSuffix.Enable = true | ||
config.PublicSuffix.AddTld = true | ||
|
||
// init the processor | ||
psl := NewPublicSuffixSubprocessor(config) | ||
|
||
tt := []struct { | ||
name string | ||
qname string | ||
want string | ||
}{ | ||
{ | ||
name: "get tld", | ||
qname: "www.amazon.fr", | ||
want: "amazon.fr", | ||
}, | ||
{ | ||
name: "get tld insensitive", | ||
qname: "books.amazon.co.uk", | ||
want: "amazon.co.uk", | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tld, err := psl.GetEffectiveTldPlusOne(tc.qname) | ||
if err != nil { | ||
t.Errorf("Bad TLD with error: %s", err.Error()) | ||
} | ||
if tld != tc.want { | ||
t.Errorf("Bad TLD, got: %s, expected: com", tld) | ||
|
||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.