Skip to content

Commit b1ece11

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 b1ece11

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

internal/db/db.go

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package db
22

33
import (
44
"log"
5+
"net"
6+
"encoding/binary"
57

68
"github.com/spf13/viper"
79

@@ -72,6 +74,24 @@ func Find(typ dbif.QueryType, query string) *Result {
7274
if result, found := queryCache.Load(query); found {
7375
return result.(*Result)
7476
}
77+
// Convert NAT64 64:ff9b::/96 to IPv4
78+
if typ == dbif.TypeIPv6 {
79+
// Parse IPv6 address
80+
ip := net.ParseIP(query)
81+
if ip != nil {
82+
ip6 := ip.To16()
83+
ipu64 := binary.BigEndian.Uint64(ip6[:8])
84+
ipu64_2 := binary.BigEndian.Uint32(ip6[8:12])
85+
if ipu64 == 0x64ff9b00000000 && ipu64_2 == 0 {
86+
// Convert to IPv4
87+
ipu32 := binary.BigEndian.Uint32(ip6[12:16])
88+
ip4 := make(net.IP, 4)
89+
binary.BigEndian.PutUint32(ip4, ipu32)
90+
query = ip4.String()
91+
typ = dbif.TypeIPv4
92+
}
93+
}
94+
}
7595
db := GetDB(typ)
7696
result, err := db.Find(query)
7797
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)