mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-03-30 17:05:58 +00:00
Compare commits
4 Commits
fix/hashch
...
fix/remove
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
095d50acc4 | ||
|
|
aec178d41a | ||
|
|
f3638a6a0c | ||
|
|
b455e5a594 |
@@ -26,14 +26,13 @@ type MQTTLegacy struct {
|
||||
|
||||
// Config holds the ingestor configuration, compatible with the Node.js config.json format.
|
||||
type Config struct {
|
||||
DBPath string `json:"dbPath"`
|
||||
MQTT *MQTTLegacy `json:"mqtt,omitempty"`
|
||||
MQTTSources []MQTTSource `json:"mqttSources,omitempty"`
|
||||
LogLevel string `json:"logLevel,omitempty"`
|
||||
ChannelKeysPath string `json:"channelKeysPath,omitempty"`
|
||||
ChannelKeys map[string]string `json:"channelKeys,omitempty"`
|
||||
HashChannels []string `json:"hashChannels,omitempty"`
|
||||
Retention *RetentionConfig `json:"retention,omitempty"`
|
||||
DBPath string `json:"dbPath"`
|
||||
MQTT *MQTTLegacy `json:"mqtt,omitempty"`
|
||||
MQTTSources []MQTTSource `json:"mqttSources,omitempty"`
|
||||
LogLevel string `json:"logLevel,omitempty"`
|
||||
ChannelKeysPath string `json:"channelKeysPath,omitempty"`
|
||||
ChannelKeys map[string]string `json:"channelKeys,omitempty"`
|
||||
Retention *RetentionConfig `json:"retention,omitempty"`
|
||||
}
|
||||
|
||||
// RetentionConfig controls how long stale nodes are kept before being moved to inactive_nodes.
|
||||
|
||||
@@ -512,64 +512,34 @@ func firstNonEmpty(vals ...string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// deriveHashtagChannelKey derives an AES-128 key from a channel name.
|
||||
// Same algorithm as Node.js: SHA-256(channelName) → first 32 hex chars (16 bytes).
|
||||
func deriveHashtagChannelKey(channelName string) string {
|
||||
h := sha256.Sum256([]byte(channelName))
|
||||
return hex.EncodeToString(h[:16])
|
||||
}
|
||||
|
||||
// loadChannelKeys loads channel decryption keys from config and/or a JSON file.
|
||||
// Merge priority: rainbow (lowest) → derived from hashChannels → explicit config (highest).
|
||||
// Priority: CHANNEL_KEYS_PATH env var > cfg.ChannelKeysPath > channel-rainbow.json next to config.
|
||||
func loadChannelKeys(cfg *Config, configPath string) map[string]string {
|
||||
keys := make(map[string]string)
|
||||
|
||||
// 1. Rainbow table keys (lowest priority)
|
||||
// Determine file path for rainbow keys
|
||||
keysPath := os.Getenv("CHANNEL_KEYS_PATH")
|
||||
if keysPath == "" {
|
||||
keysPath = cfg.ChannelKeysPath
|
||||
}
|
||||
if keysPath == "" {
|
||||
// Default: look for channel-rainbow.json next to config file
|
||||
keysPath = filepath.Join(filepath.Dir(configPath), "channel-rainbow.json")
|
||||
}
|
||||
|
||||
rainbowCount := 0
|
||||
if data, err := os.ReadFile(keysPath); err == nil {
|
||||
var fileKeys map[string]string
|
||||
if err := json.Unmarshal(data, &fileKeys); err == nil {
|
||||
for k, v := range fileKeys {
|
||||
keys[k] = v
|
||||
}
|
||||
rainbowCount = len(fileKeys)
|
||||
log.Printf("Loaded %d channel keys from %s", rainbowCount, keysPath)
|
||||
log.Printf("Loaded %d channel keys from %s", len(fileKeys), keysPath)
|
||||
} else {
|
||||
log.Printf("Warning: failed to parse channel keys file %s: %v", keysPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Derived keys from hashChannels (middle priority)
|
||||
derivedCount := 0
|
||||
for _, raw := range cfg.HashChannels {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
channelName := trimmed
|
||||
if !strings.HasPrefix(channelName, "#") {
|
||||
channelName = "#" + channelName
|
||||
}
|
||||
// Skip if explicit config already has this key
|
||||
if _, exists := cfg.ChannelKeys[channelName]; exists {
|
||||
continue
|
||||
}
|
||||
keys[channelName] = deriveHashtagChannelKey(channelName)
|
||||
derivedCount++
|
||||
}
|
||||
if derivedCount > 0 {
|
||||
log.Printf("[channels] %d derived from hashChannels", derivedCount)
|
||||
}
|
||||
|
||||
// 3. Explicit config keys (highest priority — overrides rainbow + derived)
|
||||
// Merge inline config keys (override file keys)
|
||||
for k, v := range cfg.ChannelKeys {
|
||||
keys[k] = v
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -494,132 +492,3 @@ func TestAdvertRole(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeriveHashtagChannelKey(t *testing.T) {
|
||||
// Test vectors validated against Node.js server-helpers.js
|
||||
tests := []struct {
|
||||
name string
|
||||
want string
|
||||
}{
|
||||
{"#General", "649af2cab73ed5a890890a5485a0c004"},
|
||||
{"#test", "9cd8fcf22a47333b591d96a2b848b73f"},
|
||||
{"#MeshCore", "dcf73f393fa217f6b28fcec6ffc411ad"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := deriveHashtagChannelKey(tt.name)
|
||||
if got != tt.want {
|
||||
t.Errorf("deriveHashtagChannelKey(%q) = %q, want %q", tt.name, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Deterministic
|
||||
k1 := deriveHashtagChannelKey("#foo")
|
||||
k2 := deriveHashtagChannelKey("#foo")
|
||||
if k1 != k2 {
|
||||
t.Error("deriveHashtagChannelKey should be deterministic")
|
||||
}
|
||||
|
||||
// Returns 32-char hex string (16 bytes)
|
||||
if len(k1) != 32 {
|
||||
t.Errorf("key length = %d, want 32", len(k1))
|
||||
}
|
||||
|
||||
// Different inputs → different keys
|
||||
k3 := deriveHashtagChannelKey("#bar")
|
||||
if k1 == k3 {
|
||||
t.Error("different inputs should produce different keys")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadChannelKeysMergePriority(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.json")
|
||||
|
||||
// Create a rainbow file with two keys: #rainbow (unique) and #override (to be overridden)
|
||||
rainbowPath := filepath.Join(dir, "channel-rainbow.json")
|
||||
t.Setenv("CHANNEL_KEYS_PATH", rainbowPath)
|
||||
rainbow := map[string]string{
|
||||
"#rainbow": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
"#override": "rainbow_value_should_be_overridden",
|
||||
}
|
||||
rainbowJSON, err := json.Marshal(rainbow)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(rainbowPath, rainbowJSON, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := &Config{
|
||||
HashChannels: []string{"General", "#override"},
|
||||
ChannelKeys: map[string]string{"#override": "explicit_wins"},
|
||||
}
|
||||
|
||||
keys := loadChannelKeys(cfg, cfgPath)
|
||||
|
||||
// Rainbow key loaded
|
||||
if keys["#rainbow"] != "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" {
|
||||
t.Errorf("rainbow key missing or wrong: %q", keys["#rainbow"])
|
||||
}
|
||||
|
||||
// HashChannels derived #General
|
||||
expected := deriveHashtagChannelKey("#General")
|
||||
if keys["#General"] != expected {
|
||||
t.Errorf("#General = %q, want %q (derived)", keys["#General"], expected)
|
||||
}
|
||||
|
||||
// Explicit config wins over both rainbow and derived
|
||||
if keys["#override"] != "explicit_wins" {
|
||||
t.Errorf("#override = %q, want explicit_wins", keys["#override"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadChannelKeysHashChannelsNormalization(t *testing.T) {
|
||||
t.Setenv("CHANNEL_KEYS_PATH", "")
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.json")
|
||||
|
||||
cfg := &Config{
|
||||
HashChannels: []string{
|
||||
"NoPound", // should become #NoPound
|
||||
"#HasPound", // stays #HasPound
|
||||
" Spaced ", // trimmed → #Spaced
|
||||
"", // skipped
|
||||
},
|
||||
}
|
||||
|
||||
keys := loadChannelKeys(cfg, cfgPath)
|
||||
|
||||
if _, ok := keys["#NoPound"]; !ok {
|
||||
t.Error("should derive key for #NoPound (auto-prefixed)")
|
||||
}
|
||||
if _, ok := keys["#HasPound"]; !ok {
|
||||
t.Error("should derive key for #HasPound")
|
||||
}
|
||||
if _, ok := keys["#Spaced"]; !ok {
|
||||
t.Error("should derive key for #Spaced (trimmed)")
|
||||
}
|
||||
if len(keys) != 3 {
|
||||
t.Errorf("expected 3 keys, got %d", len(keys))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadChannelKeysSkipExplicit(t *testing.T) {
|
||||
t.Setenv("CHANNEL_KEYS_PATH", "")
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.json")
|
||||
|
||||
cfg := &Config{
|
||||
HashChannels: []string{"General"},
|
||||
ChannelKeys: map[string]string{"#General": "my_explicit_key"},
|
||||
}
|
||||
|
||||
keys := loadChannelKeys(cfg, cfgPath)
|
||||
|
||||
// Explicit key should win — hashChannels derivation should be skipped
|
||||
if keys["#General"] != "my_explicit_key" {
|
||||
t.Errorf("#General = %q, want my_explicit_key", keys["#General"])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user