NameToA() accommodates hyphens ("-") in hostnames

This commit is contained in:
Brian Cunnie
2020-08-16 19:02:02 -07:00
parent 5af7186566
commit 65bb857041
2 changed files with 9 additions and 2 deletions

View File

@@ -5,10 +5,11 @@ import (
"golang.org/x/net/dns/dnsmessage"
"net"
"regexp"
"strings"
)
// https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
var ipv4RE= regexp.MustCompile(`(^|\.)(((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]))($|\.)`)
var ipv4RE= regexp.MustCompile(`(^|[.-])(((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]))($|[.-])`)
func NameToA (fqdnString string) (dnsmessage.AResource, error) {
fqdn:=[]byte(fqdnString)
@@ -17,6 +18,7 @@ func NameToA (fqdnString string) (dnsmessage.AResource, error) {
}
match := string(ipv4RE.FindSubmatch(fqdn)[2])
match = strings.Replace(match, "-", ".", -1)
ipv4address := net.ParseIP(match).To4()
return dnsmessage.AResource{A: [4]byte{ipv4address[0], ipv4address[1], ipv4address[2], ipv4address[3]}}, nil

View File

@@ -17,13 +17,17 @@ var _ = Describe("Xip", func() {
Expect(err).To(Not(HaveOccurred()))
Expect(ipv4Answer).To(Equal(expectedA))
},
// dots
Entry("loopback", "127.0.0.1", dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}}),
Entry("loopback with domain", "127.0.0.1.com", dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}}),
Entry("loopback with domain and www", "www.127.0.0.1.com", dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}}),
Entry("pre and post", "nono.io.10.0.9.30.sslip.io", dnsmessage.AResource{A: [4]byte{10, 0, 9, 30}}),
Entry("pre and post", "nono.io.10.0.9.30.sslip.io", dnsmessage.AResource{A: [4]byte{10, 0, 9, 30}}),
Entry("two IPs, grabs the leftmost", "nono.io.10.0.9.30.172.16.0.30.sslip.io", dnsmessage.AResource{A: [4]byte{10, 0, 9, 30}}),
Entry("two IPs, grabs the leftmost", "nono.io.10.0.9.30.172.16.0.30.sslip.io", dnsmessage.AResource{A: [4]byte{10, 0, 9, 30}}),
// dashes
Entry("loopback", "127-0-0-1", dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}}),
Entry("loopback with domain", "127-0-0-1-com", dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}}),
Entry("loopback with domain and www", "www-127-0-0-1-com", dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}}),
)
DescribeTable("when it does not match an IP address",
func(fqdn string) {
@@ -37,6 +41,7 @@ var _ = Describe("Xip", func() {
Entry("canonical domain", "sslip.io"),
Entry("www", "www.sslip.io"),
Entry("a lone number", "538.sslip.io"),
Entry("too big", "256.255.255.255"),
)
})
})