rework address mapping

This commit is contained in:
Konstantin Demin 2024-07-07 16:11:02 +03:00
parent ddaf00f0e7
commit 4094f97a2d
Signed by: krd
GPG Key ID: 4D56F87A8BA65FD0
2 changed files with 72 additions and 52 deletions

View File

@ -79,13 +79,20 @@ func addrMapGet(srcIp net.IP, dstCidr *net.IPNet, ttl uint32) net.IP {
log.Fatalf("addrMapGet(): src/dst size mismatch: %v vs %v", addrlen, len(dstCidr.IP))
}
var hkey any
switch addrlen {
case net.IPv4len:
hkey = binary.NativeEndian.Uint32(srcIp)
case net.IPv6len:
hkey = srcIp.To16().String()
}
var curr AddrMap
curr.SrcAddr = make([]byte, addrlen)
curr.DstAddr = make([]byte, addrlen)
copy(curr.DstAddr, srcIp)
curr.Ttl = ttl
for {
_, err := rand.Read(curr.SrcAddr)
if err != nil {
log.Fatalf("rand.Read(): error %v", err)
@ -102,14 +109,12 @@ func addrMapGet(srcIp net.IP, dstCidr *net.IPNet, ttl uint32) net.IP {
curr.Created = time.Now()
var hkey, xprev any
var xprev any
var loaded bool
switch addrlen {
case net.IPv4len:
hkey = binary.NativeEndian.Uint32(curr.SrcAddr)
xprev, loaded = addr4.LoadOrStore(hkey, curr)
case net.IPv6len:
hkey = binary.NativeEndian.Uint64(curr.SrcAddr[net.IPv6len/2:])
xprev, loaded = addr6.LoadOrStore(hkey, curr)
}
if !loaded {
@ -122,15 +127,7 @@ func addrMapGet(srcIp net.IP, dstCidr *net.IPNet, ttl uint32) net.IP {
log.Fatalf("addrMapGet(): wrong value type from sync.Map")
}
if !net.IP.Equal(curr.SrcAddr, prev.SrcAddr) {
// generate next random address
continue
}
if !net.IP.Equal(curr.DstAddr, prev.DstAddr) {
// generate next random address
continue
}
copy(curr.SrcAddr, prev.SrcAddr)
if prev.GetTtl() < int32(curr.Ttl) {
switch addrlen {
case net.IPv4len:
@ -140,8 +137,5 @@ func addrMapGet(srcIp net.IP, dstCidr *net.IPNet, ttl uint32) net.IP {
}
}
break
}
return curr.SrcAddr
}

View File

@ -16,11 +16,35 @@ var (
_promRegistry *prometheus.Registry
_promHttpHandler http.Handler
opsProcessed = prometheus.NewCounter(prometheus.CounterOpts{
promOpsProcessed = prometheus.NewCounter(prometheus.CounterOpts{
Name: "processed_ops_total",
Help: "The total number of processed requests",
})
promAddr4Count = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "ipv4_mapped_addr_count",
Help: "The total number of IPv4-mapped addresses",
}, func() float64 {
var x uint32
addr4.Range(func(key, value any) bool {
x++
return true
})
return float64(x)
})
promAddr6Count = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "ipv6_mapped_addr_count",
Help: "The total number of IPv6-mapped addresses",
}, func() float64 {
var x uint32
addr6.Range(func(key, value any) bool {
x++
return true
})
return float64(x)
})
labelStringReplacer *strings.Replacer = strings.NewReplacer(
"\"", "",
"'", "",
@ -29,7 +53,9 @@ var (
func setupPrometheus(r *gin.Engine) {
_promRegistry = prometheus.NewRegistry()
_promRegistry.MustRegister(opsProcessed)
_promRegistry.MustRegister(promOpsProcessed)
_promRegistry.MustRegister(promAddr4Count)
_promRegistry.MustRegister(promAddr6Count)
_promHttpHandler = promhttp.HandlerFor(_promRegistry, promhttp.HandlerOpts{
Registry: _promRegistry,
@ -55,7 +81,7 @@ func promSanitizeLabel(str string, fallback string) string {
}
func promHttpHandler(c *gin.Context) {
opsProcessed.Inc()
promOpsProcessed.Inc()
_promHttpHandler.ServeHTTP(c.Writer, c.Request)
}