simplify key management in address mapping
This commit is contained in:
parent
426bd5a659
commit
dd278b7374
18
addr-map.go
18
addr-map.go
@ -8,8 +8,6 @@ import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cespare/xxhash/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -92,6 +90,13 @@ func addrMapGet(srcIp net.IP, dstCidr *net.IPNet, ttl uint32) net.IP {
|
||||
if err != nil {
|
||||
log.Fatalf("rand.Read(): error %v", err)
|
||||
}
|
||||
var hkey any
|
||||
switch addrlen {
|
||||
case net.IPv4len:
|
||||
hkey = binary.NativeEndian.Uint32(curr.SrcAddr)
|
||||
case net.IPv6len:
|
||||
hkey = binary.NativeEndian.Uint64(curr.SrcAddr[net.IPv6len/2:])
|
||||
}
|
||||
|
||||
// adjust random bytes to dstCidr
|
||||
for i := range addrlen / 4 {
|
||||
@ -101,7 +106,6 @@ func addrMapGet(srcIp net.IP, dstCidr *net.IPNet, ttl uint32) net.IP {
|
||||
a += (b & ^m)
|
||||
binary.NativeEndian.PutUint32(curr.SrcAddr[i*4:], a)
|
||||
}
|
||||
hsum := xxhash.Sum64(curr.SrcAddr)
|
||||
|
||||
curr.Created = time.Now()
|
||||
|
||||
@ -109,9 +113,9 @@ func addrMapGet(srcIp net.IP, dstCidr *net.IPNet, ttl uint32) net.IP {
|
||||
var loaded bool
|
||||
switch addrlen {
|
||||
case net.IPv4len:
|
||||
xprev, loaded = addr4.LoadOrStore(hsum, curr)
|
||||
xprev, loaded = addr4.LoadOrStore(hkey, curr)
|
||||
case net.IPv6len:
|
||||
xprev, loaded = addr6.LoadOrStore(hsum, curr)
|
||||
xprev, loaded = addr6.LoadOrStore(hkey, curr)
|
||||
}
|
||||
if !loaded {
|
||||
// early return
|
||||
@ -135,9 +139,9 @@ func addrMapGet(srcIp net.IP, dstCidr *net.IPNet, ttl uint32) net.IP {
|
||||
if prev.GetTtl() < int32(curr.Ttl) {
|
||||
switch addrlen {
|
||||
case net.IPv4len:
|
||||
addr4.Store(hsum, curr)
|
||||
addr4.Store(hkey, curr)
|
||||
case net.IPv6len:
|
||||
addr6.Store(hsum, curr)
|
||||
addr6.Store(hkey, curr)
|
||||
}
|
||||
}
|
||||
|
||||
|
2
go.mod
2
go.mod
@ -3,7 +3,6 @@ module git.krd.sh/krd/powerdns-remote-http-example
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/google/nftables v0.2.0
|
||||
github.com/miekg/dns v1.1.59
|
||||
@ -14,6 +13,7 @@ require (
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bytedance/sonic v1.11.8 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
|
||||
|
Loading…
Reference in New Issue
Block a user