Skip to content

Commit 89c700f

Browse files
committed
feat: Handle NAT64 address query
We can query the NAT64 address by the IPv4 address part of the NAT64 address. This is useful when we want to know the NAT64 address of a specific IPv4 address. We also add a new regex pattern to match the NAT64 address like 64:ff9b::1.1.1.1 . Signed-off-by: Yangyu Chen <[email protected]>
1 parent 2e758d3 commit 89c700f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

internal/db/db.go

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package db
22

33
import (
44
"log"
5+
"net"
56

67
"github.com/spf13/viper"
78

@@ -72,6 +73,19 @@ func Find(typ dbif.QueryType, query string) *Result {
7273
if result, found := queryCache.Load(query); found {
7374
return result.(*Result)
7475
}
76+
// Convert NAT64 64:ff9b::/96 to IPv4
77+
if typ == dbif.TypeIPv6 {
78+
ip := net.ParseIP(query)
79+
if ip != nil {
80+
_, NAT64, _ := net.ParseCIDR("64:ff9b::/96")
81+
if NAT64.Contains(ip) {
82+
ip4 := make(net.IP, 4)
83+
copy(ip4, ip[12:16])
84+
query = ip4.String()
85+
typ = dbif.TypeIPv4
86+
}
87+
}
88+
}
7589
db := GetDB(typ)
7690
result, err := db.Find(query)
7791
if err != nil {

pkg/re/re.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var (
99
DomainRe = regexp.MustCompile(`([a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+([a-zA-Z][-a-zA-Z]{0,62})`)
1010

1111
IPv4Re = regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`)
12-
IPv6Re = regexp.MustCompile(`fe80:(:[0-9a-fA-F]{1,4}){0,4}(%\w+)?|([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::[fF]{4}:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?`)
12+
IPv6Re = regexp.MustCompile(`fe80:(:[0-9a-fA-F]{1,4}){0,4}(%\w+)?|([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|64:ff9b::(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|::[fF]{4}:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?`)
1313
)
1414

1515
func MaybeRegexp(s string) bool {

0 commit comments

Comments
 (0)