mirror of
https://github.com/brianshea2/meshmap.net.git
synced 2025-02-02 08:42:28 -08:00
add per-node rate limiting
This commit is contained in:
parent
b3b44283a7
commit
b55a1d3570
|
@ -25,6 +25,8 @@ const (
|
|||
NeighborExpiration = 7200 // 2 hr
|
||||
MetricsExpiration = 7200 // 2 hr
|
||||
PruneWriteInterval = time.Minute
|
||||
RateLimitCount = 4000
|
||||
RateLimitDuration = time.Hour
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -225,6 +227,15 @@ func main() {
|
|||
log.Fatalf("[error] read blocklist: %v", err)
|
||||
}
|
||||
}
|
||||
// maintain per-node message counters for rate limiting
|
||||
var counters sync.Map // as map[uint32]*uint32
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(RateLimitDuration)
|
||||
log.Print("[info] clearing message counters")
|
||||
counters.Clear()
|
||||
}
|
||||
}()
|
||||
// connect to MQTT
|
||||
client := &meshtastic.MQTTClient{
|
||||
Topics: []string{
|
||||
|
@ -239,8 +250,18 @@ func main() {
|
|||
},
|
||||
TopicRegex: regexp.MustCompile(`^msh(?:/[^/]+)+/2/(?:e/[^/]+/![0-9a-f]+|map/)$`),
|
||||
Accept: func(from uint32) bool {
|
||||
_, found := blocked[from]
|
||||
return !found
|
||||
if _, found := blocked[from]; found {
|
||||
return false
|
||||
}
|
||||
v, _ := counters.LoadOrStore(from, new(uint32))
|
||||
count := atomic.AddUint32(v.(*uint32), 1)
|
||||
if count >= RateLimitCount {
|
||||
if count%100 == 0 {
|
||||
log.Printf("[info] node %v rate limited (%v messages)", from, count)
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
},
|
||||
BlockCipher: meshtastic.NewBlockCipher(meshtastic.DefaultKey),
|
||||
MessageHandler: handleMessage,
|
||||
|
|
Loading…
Reference in a new issue