Skip to content

Commit

Permalink
Merge pull request #20 from soulteary/feat/support-ipv4-ipv6-addr
Browse files Browse the repository at this point in the history
feat: support ipv4 and ipv6 addrs
  • Loading branch information
soulteary authored Dec 23, 2024
2 parents 74960dd + 7319c64 commit 0da0125
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
28 changes: 21 additions & 7 deletions internal/fn/domain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fn

import (
"fmt"
"net"
"regexp"
"strings"
)
Expand All @@ -18,13 +20,25 @@ func GetUniqDomains(s []string) []string {
}

func GetDomainsByString(input string) (result []string) {
var re = regexp.MustCompile(`^([\.\w\*\-\_]+(\,)?){1,}$`)
if len(re.FindAllString(input, -1)) > 0 {
domains := strings.Split(input, ",")
for _, domain := range domains {
s := strings.TrimSpace(domain)
if len(s) > 0 {
result = append(result, strings.ToLower(domain))
domainPattern := `([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}`
ipv4Pattern := `((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`
ipv6Pattern := `(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))`
combinedPattern := fmt.Sprintf(`^(%s|%s|%s)$`, domainPattern, ipv4Pattern, ipv6Pattern)
re := regexp.MustCompile(combinedPattern)

addresses := strings.Split(input, ",")

for _, addr := range addresses {
addr = strings.TrimSpace(addr)
if len(addr) == 0 {
continue
}

if re.MatchString(addr) {
if strings.Contains(addr, ":") || net.ParseIP(addr) != nil {
result = append(result, addr)
} else {
result = append(result, strings.ToLower(addr))
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions internal/fn/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ func TestGetDomainsByString(t *testing.T) {
t.Fatal("test GetDomainsByString failed")
}

ret = fn.GetDomainsByString("192.168.1.1,10.11.12.13")
if !reflect.DeepEqual(ret, []string{"192.168.1.1", "10.11.12.13"}) {
t.Fatal("test GetDomainsByString failed")
}

ret = fn.GetDomainsByString("2001:db8::1,2001:db8::2")
if !reflect.DeepEqual(ret, []string{"2001:db8::1", "2001:db8::2"}) {
t.Fatal("test GetDomainsByString failed")
}

ret = fn.GetDomainsByString("2001:db8::1,2001:db8::2,a.com,192.168.1.1,A.COM")
if !reflect.DeepEqual(ret, []string{"2001:db8::1", "2001:db8::2", "a.com", "192.168.1.1"}) {
t.Fatal("test GetDomainsByString failed")
}

ret = fn.GetDomainsByString("")
if !reflect.DeepEqual(ret, []string{}) {
t.Fatal("test GetDomainsByString failed")
Expand Down

0 comments on commit 0da0125

Please sign in to comment.