Initial restructure of server
This commit is contained in:
26
utils/utils.go
Normal file
26
utils/utils.go
Normal file
@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/miekg/dns"
|
||||
"net"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func IsDomain(domain string) bool {
|
||||
if IsIP(domain) {
|
||||
return false
|
||||
}
|
||||
match, _ := regexp.MatchString(`^([a-zA-Z0-9\*]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$`, domain)
|
||||
return match
|
||||
}
|
||||
|
||||
func IsIP(ip string) bool {
|
||||
return net.ParseIP(ip) != nil
|
||||
}
|
||||
|
||||
func UnFqdn(s string) string {
|
||||
if dns.IsFqdn(s) {
|
||||
return s[:len(s)-1]
|
||||
}
|
||||
return s
|
||||
}
|
32
utils/utils_test.go
Normal file
32
utils/utils_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestHostDomainAndIP(t *testing.T) {
|
||||
Convey("Test Host File Domain and IP regex", t, func() {
|
||||
Convey("1.1.1.1 should be IP and not domain", func() {
|
||||
So(isIP("1.1.1.1"), ShouldEqual, true)
|
||||
So(isDomain("1.1.1.1"), ShouldEqual, false)
|
||||
})
|
||||
|
||||
Convey("2001:470:20::2 should be IP and not domain", func() {
|
||||
So(isIP("2001:470:20::2"), ShouldEqual, true)
|
||||
So(isDomain("2001:470:20::2"), ShouldEqual, false)
|
||||
})
|
||||
|
||||
Convey("`host` should not be domain and not IP", func() {
|
||||
So(isDomain("host"), ShouldEqual, false)
|
||||
So(isIP("host"), ShouldEqual, false)
|
||||
})
|
||||
|
||||
Convey("`123.test` should be domain and not IP", func() {
|
||||
So(isDomain("123.test"), ShouldEqual, true)
|
||||
So(isIP("123.test"), ShouldEqual, false)
|
||||
})
|
||||
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user