Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

fix(rules): restore policy-group rule naming for mihomo and clean up misattributed data

luoleifa2393eb9e39
diff --git a/apps/collector/src/database/repositories/traffic-writer.test.ts b/apps/collector/src/database/repositories/traffic-writer.test.ts
index 91c823f..a7ffdef 100644
--- a/apps/collector/src/database/repositories/traffic-writer.test.ts
+++ b/apps/collector/src/database/repositories/traffic-writer.test.ts
@@ -248,7 +248,7 @@ describe('TrafficWriterRepository', () => {
expect(summary.totalConnections).toBe(1);
});
- it('should use rule and rulePayload instead of treating the last chain hop as the rule', () => {
+ it('should key mihomo multi-hop traffic by the top-level policy group, not the raw rule type', () => {
db.batchUpdateTrafficStats(backendId, [
{
domain: 'media.example',
@@ -266,17 +266,38 @@ describe('TrafficWriterRepository', () => {
const rules = db.getRuleStats(backendId);
expect(rules).toHaveLength(1);
- expect(rules[0].rule).toBe('RULE-SET(OpenAI)');
+ expect(rules[0].rule).toBe('🔯 大流量节点');
expect(rules[0].finalProxy).toBe('香港 01');
const ruleProxyMap = db.getRuleProxyMap(backendId);
expect(ruleProxyMap).toHaveLength(1);
expect(ruleProxyMap[0]).toEqual({
- rule: 'RULE-SET(OpenAI)',
+ rule: '🔯 大流量节点',
proxies: ['香港 01'],
});
});
+ it('should keep rule detail as the key only for single-hop chains (DIRECT-target rules)', () => {
+ db.batchUpdateTrafficStats(backendId, [
+ {
+ domain: 'cn.example',
+ ip: '114.114.114.114',
+ chain: 'DIRECT',
+ chains: ['DIRECT'],
+ rule: 'RuleSet',
+ rulePayload: 'ChinaDomain',
+ upload: 10,
+ download: 20,
+ sourceIP: '192.168.1.9',
+ timestampMs: Date.now(),
+ },
+ ]);
+
+ const rules = db.getRuleStats(backendId);
+ expect(rules).toHaveLength(1);
+ expect(rules[0].rule).toBe('RuleSet(ChinaDomain)');
+ });
+
it('should preserve Surge rule names when the last chain hop already matches the resolved rule', () => {
db.batchUpdateTrafficStats(backendId, [
{
diff --git a/apps/collector/src/database/rule-name-cleanup.test.ts b/apps/collector/src/database/rule-name-cleanup.test.ts
new file mode 100644
index 0000000..68ddb26
--- /dev/null
+++ b/apps/collector/src/database/rule-name-cleanup.test.ts
@@ -0,0 +1,130 @@
+import { describe, it, expect, beforeEach } from 'vitest';
+import Database from 'better-sqlite3';
+import { getAllSchemaStatements } from './schema.js';
+import { cleanupMisattributedRuleNames } from './rule-name-cleanup.js';
+
+const BACKEND_ID = 1;
+const GROUP_CHAIN = '香港 01 > 🔯 大流量节点';
+
+function createRawDb(): Database.Database {
+ const db = new Database(':memory:');
+ for (const stmt of getAllSchemaStatements()) {
+ db.exec(stmt);
+ }
+ db.prepare(`
+ INSERT INTO backend_configs (name, url, token, enabled, is_active, listening)
+ VALUES ('test', 'http://127.0.0.1:9090', '', 1, 1, 1)
+ `).run();
+ return db;
+}
+
+function seedChainRow(db: Database.Database, rule: string, chain: string, up: number, down: number, conn: number) {
+ db.prepare(`
+ INSERT INTO rule_chain_traffic (backend_id, rule, chain, total_upload, total_download, total_connections, last_seen)
+ VALUES (?, ?, ?, ?, ?, ?, '2026-06-01T00:00:00.000Z')
+ `).run(BACKEND_ID, rule, chain, up, down, conn);
+}
+
+describe('cleanupMisattributedRuleNames', () => {
+ let db: Database.Database;
+
+ beforeEach(() => {
+ db = createRawDb();
+ });
+
+ it('remaps rule-type keys on multi-hop chains back to the policy group and merges totals', () => {
+ // Polluted row written by v1.3.9 naming + pre-existing correct row
+ seedChainRow(db, 'RuleSet(OpenAI)', GROUP_CHAIN, 100, 200, 3);
+ seedChainRow(db, '🔯 大流量节点', GROUP_CHAIN, 10, 20, 1);
+
+ db.prepare(`
+ INSERT INTO rule_stats (backend_id, rule, final_proxy, total_upload, total_download, total_connections, last_seen)
+ VALUES (?, 'RuleSet(OpenAI)', '香港 01', 100, 200, 3, '2026-06-01T00:00:00.000Z'),
+ (?, '🔯 大流量节点', '香港 01', 10, 20, 1, '2026-05-01T00:00:00.000Z')
+ `).run(BACKEND_ID, BACKEND_ID);
+
+ db.prepare(`
+ INSERT INTO rule_domain_traffic (backend_id, rule, domain, total_upload, total_download, total_connections, last_seen)
+ VALUES (?, 'RuleSet(OpenAI)', 'media.example', 100, 200, 3, '2026-06-01T00:00:00.000Z')
+ `).run(BACKEND_ID);
+ db.prepare(`
+ INSERT INTO rule_ip_traffic (backend_id, rule, ip, total_upload, total_download, total_connections, last_seen)
+ VALUES (?, 'RuleSet(OpenAI)', '8.8.8.8', 100, 200, 3, '2026-06-01T00:00:00.000Z')
+ `).run(BACKEND_ID);
+ db.prepare(`
+ INSERT INTO rule_proxy_map (backend_id, rule, proxy)
+ VALUES (?, 'RuleSet(OpenAI)', '香港 01'), (?, '🔯 大流量节点', '香港 01')
+ `).run(BACKEND_ID, BACKEND_ID);
+ db.prepare(`
+ INSERT INTO minute_dim_stats (backend_id, minute, domain, ip, source_ip, chain, rule, upload, download, connections)
+ VALUES (?, '2026-06-01T00:00:00', 'media.example', '8.8.8.8', '192.168.1.8', ?, 'RuleSet(OpenAI)', 100, 200, 3)
+ `).run(BACKEND_ID, GROUP_CHAIN);
+ db.prepare(`
+ INSERT INTO hourly_dim_stats (backend_id, hour, domain, ip, source_ip, chain, rule, upload, download, connections)
+ VALUES (?, '2026-06-01T00:00:00', 'media.example', '8.8.8.8', '192.168.1.8', ?, 'RuleSet(OpenAI)', 100, 200, 3)
+ `).run(BACKEND_ID, GROUP_CHAIN);
+
+ cleanupMisattributedRuleNames(db);
+
+ const chainRows = db.prepare(`SELECT rule, total_upload as up, total_download as down, total_connections as conn FROM rule_chain_traffic`).all() as Array<{ rule: string; up: number; down: number; conn: number }>;
+ expect(chainRows).toHaveLength(1);
+ expect(chainRows[0]).toEqual({ rule: '🔯 大流量节点', up: 110, down: 220, conn: 4 });
+
+ const ruleRows = db.prepare(`SELECT rule, final_proxy as fp, total_upload as up, total_download as down FROM rule_stats`).all() as Array<{ rule: string; fp: string; up: number; down: number }>;
+ expect(ruleRows).toHaveLength(1);
+ expect(ruleRows[0]).toEqual({ rule: '🔯 大流量节点', fp: '香港 01', up: 110, down: 220 });
+
+ const domainRows = db.prepare(`SELECT rule FROM rule_domain_traffic`).all() as Array<{ rule: string }>;
+ expect(domainRows).toEqual([{ rule: '🔯 大流量节点' }]);
+ const ipRows = db.prepare(`SELECT rule FROM rule_ip_traffic`).all() as Array<{ rule: string }>;
+ expect(ipRows).toEqual([{ rule: '🔯 大流量节点' }]);
+
+ const proxyMap = db.prepare(`SELECT rule, proxy FROM rule_proxy_map`).all() as Array<{ rule: string; proxy: string }>;
+ expect(proxyMap).toEqual([{ rule: '🔯 大流量节点', proxy: '香港 01' }]);
+
+ for (const table of ['minute_dim_stats', 'hourly_dim_stats']) {
+ const dimRules = db.prepare(`SELECT DISTINCT rule FROM ${table}`).all() as Array<{ rule: string }>;
+ expect(dimRules).toEqual([{ rule: '🔯 大流量节点' }]);
+ }
+
+ const flag = db.prepare(`SELECT value FROM app_config WHERE key = 'rule_name_cleanup_v1'`).get() as { value: string } | undefined;
+ expect(flag?.value).toBe('1');
+ });
+
+ it('leaves single-hop rows (DIRECT-target rule details) and Surge-style rows untouched', () => {
+ seedChainRow(db, 'RuleSet(ChinaDomain)', 'DIRECT', 10, 20, 1);
+ seedChainRow(db, 'YouTube|Media', 'JP-Sakura > Manual|Select > YouTube|Media', 5, 5, 1);
+
+ cleanupMisattributedRuleNames(db);
+
+ const rules = (db.prepare(`SELECT rule FROM rule_chain_traffic ORDER BY rule`).all() as Array<{ rule: string }>).map(r => r.rule);
+ expect(rules).toEqual(['RuleSet(ChinaDomain)', 'YouTube|Media']);
+ });
+
+ it('is idempotent: second run is a no-op even if polluted-looking rows reappear', () => {
+ seedChainRow(db, 'RuleSet(OpenAI)', GROUP_CHAIN, 100, 200, 3);
+ cleanupMisattributedRuleNames(db);
+ seedChainRow(db, 'RuleSet(Telegram)', GROUP_CHAIN, 1, 1, 1);
+ cleanupMisattributedRuleNames(db);
+
+ const rules = (db.prepare(`SELECT rule FROM rule_chain_traffic ORDER BY rule`).all() as Array<{ rule: string }>).map(r => r.rule);
+ expect(rules).toEqual(['RuleSet(Telegram)', '🔯 大流量节点']);
+ });
+
+ it('assigns rule-only tables to the dominant policy group when a name maps to several', () => {
+ seedChainRow(db, 'Match', '香港 01 > 兜底组', 1000, 2000, 5);
+ seedChainRow(db, 'Match', '美国 01 > 其他组', 1, 2, 1);
+ db.prepare(`
+ INSERT INTO rule_stats (backend_id, rule, final_proxy, total_upload, total_download, total_connections, last_seen)
+ VALUES (?, 'Match', '香港 01', 1001, 2002, 6, '2026-06-01T00:00:00.000Z')
+ `).run(BACKEND_ID);
+
+ cleanupMisattributedRuleNames(db);
+
+ const chainRules = (db.prepare(`SELECT rule FROM rule_chain_traffic`).all() as Array<{ rule: string }>).map(r => r.rule);
+ expect(new Set(chainRules)).toEqual(new Set(['其他组', '兜底组']));
+
+ const ruleRows = db.prepare(`SELECT rule FROM rule_stats`).all() as Array<{ rule: string }>;
+ expect(ruleRows).toEqual([{ rule: '兜底组' }]);
+ });
+});
diff --git a/apps/collector/src/database/rule-name-cleanup.ts b/apps/collector/src/database/rule-name-cleanup.ts
new file mode 100644
index 0000000..b2f080b
Binary files /dev/null and b/apps/collector/src/database/rule-name-cleanup.ts differ
diff --git a/apps/collector/src/modules/db/db.ts b/apps/collector/src/modules/db/db.ts
index 665b6a7..10a470f 100644
--- a/apps/collector/src/modules/db/db.ts
+++ b/apps/collector/src/modules/db/db.ts
@@ -4,6 +4,7 @@ import fs from 'fs';
import { randomBytes } from 'crypto';
import { normalizeGeoIP, type Connection, type DomainStats, type IPStats, type HourlyStats, type ProxyStats, type RuleStats, type ProxyTrafficStats, type DeviceStats } from '@neko-master/shared';
import { getAllSchemaStatements } from '../../database/schema.js';
+import { cleanupMisattributedRuleNames } from '../../database/rule-name-cleanup.js';
import {
AuthRepository,
SurgeRepository,
@@ -167,6 +168,13 @@ export class StatsDatabase {
// Migrate existing data if needed (from single-backend schema)
this.migrateIfNeeded();
+ // One-time cleanup: remap v1.3.9-era rule-type keys back to policy groups
+ try {
+ cleanupMisattributedRuleNames(this.db);
+ } catch (error) {
+ console.error('[DB] Rule name cleanup failed:', error);
+ }
+
// Backfill hourly tables from minute data (one-time)
this.backfillHourlyTables();
}
diff --git a/apps/collector/src/modules/stats/stats.service.test.ts b/apps/collector/src/modules/stats/stats.service.test.ts
index 5893b8b..23fa7ab 100644
--- a/apps/collector/src/modules/stats/stats.service.test.ts
+++ b/apps/collector/src/modules/stats/stats.service.test.ts
@@ -158,8 +158,8 @@ describe('StatsService', () => {
ip: '203.0.113.10',
chain: 'JP-Sakura|IEPL',
chains: ['JP-Sakura|IEPL', 'Manual|Select', 'YouTube|Media'],
- rule: 'YouTube|Media',
- rulePayload: 'RULE-SET',
+ rule: 'RULE-SET',
+ rulePayload: 'YouTube',
upload: 123,
download: 456,
sourceIP: '192.168.1.88',
diff --git a/apps/collector/src/shared/utils/rule-name.ts b/apps/collector/src/shared/utils/rule-name.ts
index bf8c961..da0fe08 100644
--- a/apps/collector/src/shared/utils/rule-name.ts
+++ b/apps/collector/src/shared/utils/rule-name.ts
@@ -9,10 +9,16 @@ export function buildRuleName(input: RuleNameInput): string {
const rulePayload = input.rulePayload.trim();
const lastChain = input.chains[input.chains.length - 1]?.trim() || '';
- // Surge collector already resolves the real rule/group name into `rule`
- // and mirrors it as the last chain hop. Preserve that behavior.
- if (rule && lastChain === rule) {
- return rule;
+ // Product contract: rule stats and the flow-graph entry column key on the
+ // top-level policy group (the last chain hop). The frontend matches these
+ // names against gateway rule targets (rule.proxy), so the raw rule type
+ // mihomo reports in `rule` (RuleSet/IPCIDR/Match...) must not become the
+ // aggregation key for multi-hop chains. Surge resolves the real rule name
+ // into `rule` and mirrors it as the last hop, so this preserves Surge
+ // naming as well. The matched rule detail (rule + payload) is only used
+ // when the chain has no group hop (e.g. rules targeting DIRECT/REJECT).
+ if (input.chains.length > 1 && lastChain) {
+ return lastChain;
}
if (rule) {
diff --git a/apps/web/components/features/rules/interactive-rule-stats.tsx b/apps/web/components/features/rules/interactive-rule-stats.tsx
index eb11b0f..9f47a26 100644
--- a/apps/web/components/features/rules/interactive-rule-stats.tsx
+++ b/apps/web/components/features/rules/interactive-rule-stats.tsx
@@ -187,7 +187,7 @@ export function InteractiveRuleStats({
}));
// Append zero-traffic rules from Gateway API, using the target proxy group name
- // (rule.proxy) which matches how traffic data stores rule names for Surge.
+ // (rule.proxy) which matches how traffic data stores rule names.
// Multiple low-level rules (RuleSet, ProcessName, etc.) can target the same
// proxy group, so we deduplicate by proxy group name.
if (gatewayRules?.rules) {