rework address mapping
This commit is contained in:
parent
ddaf00f0e7
commit
4094f97a2d
26
addr-map.go
26
addr-map.go
@ -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))
|
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
|
var curr AddrMap
|
||||||
curr.SrcAddr = make([]byte, addrlen)
|
curr.SrcAddr = make([]byte, addrlen)
|
||||||
curr.DstAddr = make([]byte, addrlen)
|
curr.DstAddr = make([]byte, addrlen)
|
||||||
copy(curr.DstAddr, srcIp)
|
copy(curr.DstAddr, srcIp)
|
||||||
curr.Ttl = ttl
|
curr.Ttl = ttl
|
||||||
|
|
||||||
for {
|
|
||||||
_, err := rand.Read(curr.SrcAddr)
|
_, err := rand.Read(curr.SrcAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("rand.Read(): error %v", err)
|
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()
|
curr.Created = time.Now()
|
||||||
|
|
||||||
var hkey, xprev any
|
var xprev any
|
||||||
var loaded bool
|
var loaded bool
|
||||||
switch addrlen {
|
switch addrlen {
|
||||||
case net.IPv4len:
|
case net.IPv4len:
|
||||||
hkey = binary.NativeEndian.Uint32(curr.SrcAddr)
|
|
||||||
xprev, loaded = addr4.LoadOrStore(hkey, curr)
|
xprev, loaded = addr4.LoadOrStore(hkey, curr)
|
||||||
case net.IPv6len:
|
case net.IPv6len:
|
||||||
hkey = binary.NativeEndian.Uint64(curr.SrcAddr[net.IPv6len/2:])
|
|
||||||
xprev, loaded = addr6.LoadOrStore(hkey, curr)
|
xprev, loaded = addr6.LoadOrStore(hkey, curr)
|
||||||
}
|
}
|
||||||
if !loaded {
|
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")
|
log.Fatalf("addrMapGet(): wrong value type from sync.Map")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !net.IP.Equal(curr.SrcAddr, prev.SrcAddr) {
|
copy(curr.SrcAddr, prev.SrcAddr)
|
||||||
// generate next random address
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !net.IP.Equal(curr.DstAddr, prev.DstAddr) {
|
|
||||||
// generate next random address
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if prev.GetTtl() < int32(curr.Ttl) {
|
if prev.GetTtl() < int32(curr.Ttl) {
|
||||||
switch addrlen {
|
switch addrlen {
|
||||||
case net.IPv4len:
|
case net.IPv4len:
|
||||||
@ -140,8 +137,5 @@ func addrMapGet(srcIp net.IP, dstCidr *net.IPNet, ttl uint32) net.IP {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
return curr.SrcAddr
|
return curr.SrcAddr
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,35 @@ var (
|
|||||||
_promRegistry *prometheus.Registry
|
_promRegistry *prometheus.Registry
|
||||||
_promHttpHandler http.Handler
|
_promHttpHandler http.Handler
|
||||||
|
|
||||||
opsProcessed = prometheus.NewCounter(prometheus.CounterOpts{
|
promOpsProcessed = prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
Name: "processed_ops_total",
|
Name: "processed_ops_total",
|
||||||
Help: "The total number of processed requests",
|
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(
|
labelStringReplacer *strings.Replacer = strings.NewReplacer(
|
||||||
"\"", "",
|
"\"", "",
|
||||||
"'", "",
|
"'", "",
|
||||||
@ -29,7 +53,9 @@ var (
|
|||||||
|
|
||||||
func setupPrometheus(r *gin.Engine) {
|
func setupPrometheus(r *gin.Engine) {
|
||||||
_promRegistry = prometheus.NewRegistry()
|
_promRegistry = prometheus.NewRegistry()
|
||||||
_promRegistry.MustRegister(opsProcessed)
|
_promRegistry.MustRegister(promOpsProcessed)
|
||||||
|
_promRegistry.MustRegister(promAddr4Count)
|
||||||
|
_promRegistry.MustRegister(promAddr6Count)
|
||||||
|
|
||||||
_promHttpHandler = promhttp.HandlerFor(_promRegistry, promhttp.HandlerOpts{
|
_promHttpHandler = promhttp.HandlerFor(_promRegistry, promhttp.HandlerOpts{
|
||||||
Registry: _promRegistry,
|
Registry: _promRegistry,
|
||||||
@ -55,7 +81,7 @@ func promSanitizeLabel(str string, fallback string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func promHttpHandler(c *gin.Context) {
|
func promHttpHandler(c *gin.Context) {
|
||||||
opsProcessed.Inc()
|
promOpsProcessed.Inc()
|
||||||
|
|
||||||
_promHttpHandler.ServeHTTP(c.Writer, c.Request)
|
_promHttpHandler.ServeHTTP(c.Writer, c.Request)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user