From 31cf9661424cbd3a4d4940564813d6325408a55d Mon Sep 17 00:00:00 2001 From: root <165865819+brianshea2@users.noreply.github.com> Date: Sat, 9 Nov 2024 19:34:36 +0000 Subject: [PATCH] add blocklist mechanism --- cmd/meshobserv/meshobserv.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/cmd/meshobserv/meshobserv.go b/cmd/meshobserv/meshobserv.go index 99b116d..0395e3b 100644 --- a/cmd/meshobserv/meshobserv.go +++ b/cmd/meshobserv/meshobserv.go @@ -173,8 +173,9 @@ func handleMessage(from uint32, topic string, portNum generated.PortNum, payload } func main() { - var dbPath string + var dbPath, blockedPath string flag.StringVar(&dbPath, "f", "", "node database `file`") + flag.StringVar(&blockedPath, "b", "", "node blocklist `file`") flag.Parse() // load or make NodeDB if len(dbPath) > 0 { @@ -187,9 +188,34 @@ func main() { if Nodes == nil { Nodes = make(meshtastic.NodeDB) } + // load node blocklist + blocked := make(map[uint32]struct{}) + if len(blockedPath) > 0 { + f, err := os.Open(blockedPath) + if err != nil { + log.Fatalf("[error] open blocklist: %v", err) + } + s := bufio.NewScanner(f) + for s.Scan() { + n, err := strconv.ParseUint(s.Text(), 10, 32) + if err == nil { + blocked[uint32(n)] = struct{}{} + log.Printf("[info] node %v blocked", n) + } + } + f.Close() + err = s.Err() + if err != nil { + log.Fatalf("[error] read blocklist: %v", err) + } + } // connect to MQTT client := &meshtastic.MQTTClient{ - TopicRegex: regexp.MustCompile(`/2/e/[^/]+/![0-9a-f]+$|/2/map/$`), + TopicRegex: regexp.MustCompile(`/2/e/[^/]+/![0-9a-f]+$|/2/map/$`), + Accept: func(from uint32) bool { + _, found := blocked[from] + return !found + }, BlockCipher: meshtastic.NewBlockCipher(meshtastic.DefaultKey), MessageHandler: handleMessage, }