@panguard-ai/manager 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/db.d.ts ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * SQLite database layer for Panguard Manager
3
+ * Manager SQLite 資料庫持久化層
4
+ *
5
+ * Persists agent registrations, aggregated threats, and policy updates
6
+ * so data survives process restarts.
7
+ *
8
+ * @module @panguard-ai/manager/db
9
+ */
10
+ import type { AgentRegistration, AgentStatus, AggregatedThreat, PolicyUpdate } from './types.js';
11
+ /**
12
+ * ManagerDB - SQLite persistence for the Panguard Manager
13
+ * 基於 SQLite 的 Manager 資料持久化
14
+ *
15
+ * Follows the same pattern as ThreatCloudDB and AuthDB:
16
+ * - WAL mode for concurrent read performance
17
+ * - Prepared statements for all queries (no string interpolation)
18
+ * - JSON serialization for complex fields
19
+ */
20
+ export declare class ManagerDB {
21
+ private readonly db;
22
+ private readonly stmts;
23
+ constructor(dbPath: string);
24
+ private initialize;
25
+ private prepareStatements;
26
+ /** Insert or update an agent registration / 插入或更新代理登錄 */
27
+ upsertAgent(agent: AgentRegistration): void;
28
+ /** Get a single agent by ID / 依 ID 取得單一代理 */
29
+ getAgent(agentId: string): AgentRegistration | undefined;
30
+ /** Get all registered agents / 取得所有已登錄代理 */
31
+ getAllAgents(): AgentRegistration[];
32
+ /** Get agents filtered by status / 依狀態篩選代理 */
33
+ getAgentsByStatus(status: AgentStatus): AgentRegistration[];
34
+ /** Update an agent's status / 更新代理狀態 */
35
+ updateAgentStatus(agentId: string, status: AgentStatus): boolean;
36
+ /** Update an agent's heartbeat timestamp / 更新代理心跳時間戳 */
37
+ updateHeartbeat(agentId: string, timestamp: string): boolean;
38
+ /** Delete an agent from the registry / 從登錄簿中刪除代理 */
39
+ deleteAgent(agentId: string): boolean;
40
+ /** Get total number of registered agents / 取得已登錄代理總數 */
41
+ getAgentCount(): number;
42
+ /** Insert an aggregated threat / 插入聚合威脅 */
43
+ insertThreat(threat: AggregatedThreat): void;
44
+ /** Get all threats from a specific agent / 取得特定代理的所有威脅 */
45
+ getThreatsByAgent(agentId: string): AggregatedThreat[];
46
+ /** Get threats received after a given ISO timestamp / 取得指定時間後接收的威脅 */
47
+ getRecentThreats(since: string): AggregatedThreat[];
48
+ /** Get total number of stored threats / 取得已儲存威脅總數 */
49
+ getThreatCount(): number;
50
+ /** Purge threats older than a given ISO date, returns number deleted / 清除指定日期前的威脅 */
51
+ purgeOldThreats(beforeDate: string): number;
52
+ /**
53
+ * Insert a policy update. If active=true, deactivates all existing policies first.
54
+ * 插入策略更新。若 active=true,先停用所有現有策略。
55
+ */
56
+ insertPolicy(policy: PolicyUpdate, active: boolean): void;
57
+ /** Get the currently active policy, or null if none / 取得當前啟用的策略 */
58
+ getActivePolicy(): PolicyUpdate | null;
59
+ /** Get all policies ordered by version descending / 取得所有策略(依版本降冪) */
60
+ getPolicyHistory(): PolicyUpdate[];
61
+ /** Deactivate all policies / 停用所有策略 */
62
+ deactivateAllPolicies(): void;
63
+ /** Get agents filtered by organization ID / 依組織 ID 篩選代理 */
64
+ getAgentsByOrg(orgId: string): AgentRegistration[];
65
+ /** Get threats filtered by organization ID / 依組織 ID 篩選威脅 */
66
+ getThreatsByOrg(orgId: string): AggregatedThreat[];
67
+ /** Get the active policy for a specific organization / 取得特定組織的啟用策略 */
68
+ getActivePolicyForOrg(orgId: string): PolicyUpdate | null;
69
+ /** Close the database connection / 關閉資料庫連線 */
70
+ close(): void;
71
+ /** Convert a database row to an AgentRegistration / 將資料庫列轉換為 AgentRegistration */
72
+ private rowToAgent;
73
+ /** Convert a database row to an AggregatedThreat / 將資料庫列轉換為 AggregatedThreat */
74
+ private rowToThreat;
75
+ /** Convert a database row to a PolicyUpdate / 將資料庫列轉換為 PolicyUpdate */
76
+ private rowToPolicy;
77
+ }
78
+ //# sourceMappingURL=db.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db.d.ts","sourceR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M;IAS3C;;;OAGG;IACH,YAAY,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI;IAkBzD,mEAAmE;IACnE,eAAe,IAAI,YAAY,GAAG,IAAI;IAKtC,qEAAqE;IACrE,gBAAgB,IAAI,YAAY,EAAE;IAKlC,uCAAuC;IACvC,qBAAqB,IAAI,IAAI;IAQ7B,2DAA2D;IAC3D,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,iBAAiB,EAAE;IAKlD,4DAA4D;IAC5D,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB,EAAE;IAKlD,sEAAsE;IACtE,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI;IASzD,8CAA8C;IAC9C,KAAK,IAAI,IAAI;IAQb,kFAAkF;IAClF,OAAO,CAAC,UAAU;IAkBlB,gFAAgF;IAChF,OAAO,CAAC,WAAW;IAenB,uEAAuE;IACvE,OAAO,CAAC,WAAW;CAapB"}
package/dist/db.js ADDED
@@ -0,0 +1,339 @@
1
+ /**
2
+ * SQLite database layer for Panguard Manager
3
+ * Manager SQLite 資料庫持久化層
4
+ *
5
+ * Persists agent registrations, aggregated threats, and policy updates
6
+ * so data survives process restarts.
7
+ *
8
+ * @module @panguard-ai/manager/db
9
+ */
10
+ import Database from 'better-sqlite3';
11
+ /**
12
+ * ManagerDB - SQLite persistence for the Panguard Manager
13
+ * 基於 SQLite 的 Manager 資料持久化
14
+ *
15
+ * Follows the same pattern as ThreatCloudDB and AuthDB:
16
+ * - WAL mode for concurrent read performance
17
+ * - Prepared statements for all queries (no string interpolation)
18
+ * - JSON serialization for complex fields
19
+ */
20
+ export class ManagerDB {
21
+ db;
22
+ stmts;
23
+ constructor(dbPath) {
24
+ this.db = new Database(dbPath);
25
+ // Enable WAL mode and performance pragmas / 啟用 WAL 模式及效能 pragma
26
+ this.db.pragma('journal_mode = WAL');
27
+ this.db.pragma('foreign_keys = ON');
28
+ this.db.pragma('busy_timeout = 15000');
29
+ this.db.pragma('synchronous = NORMAL');
30
+ this.db.pragma('cache_size = -64000');
31
+ this.db.pragma('temp_store = MEMORY');
32
+ this.initialize();
33
+ this.stmts = this.prepareStatements();
34
+ }
35
+ // =========================================================================
36
+ // Schema initialization / 資料表初始化
37
+ // =========================================================================
38
+ initialize() {
39
+ this.db.exec(`
40
+ CREATE TABLE IF NOT EXISTS agents (
41
+ agent_id TEXT PRIMARY KEY,
42
+ hostname TEXT NOT NULL,
43
+ os TEXT NOT NULL,
44
+ arch TEXT NOT NULL,
45
+ ip TEXT,
46
+ version TEXT NOT NULL,
47
+ status TEXT NOT NULL DEFAULT 'online',
48
+ last_heartbeat TEXT NOT NULL,
49
+ registered_at TEXT NOT NULL,
50
+ org_id TEXT NOT NULL DEFAULT 'default'
51
+ );
52
+
53
+ CREATE TABLE IF NOT EXISTS threats (
54
+ id TEXT PRIMARY KEY,
55
+ agent_id TEXT NOT NULL,
56
+ source_hostname TEXT NOT NULL,
57
+ event_json TEXT NOT NULL,
58
+ verdict_conclusion TEXT NOT NULL,
59
+ verdict_confidence REAL NOT NULL,
60
+ verdict_action TEXT NOT NULL,
61
+ correlated_with TEXT DEFAULT '[]',
62
+ received_at TEXT NOT NULL,
63
+ org_id TEXT NOT NULL DEFAULT 'default',
64
+ FOREIGN KEY (agent_id) REFERENCES agents(agent_id)
65
+ );
66
+
67
+ CREATE TABLE IF NOT EXISTS policies (
68
+ policy_id TEXT PRIMARY KEY,
69
+ version INTEGER NOT NULL,
70
+ rules_json TEXT NOT NULL,
71
+ applied_to TEXT DEFAULT '[]',
72
+ updated_at TEXT NOT NULL,
73
+ active INTEGER NOT NULL DEFAULT 0,
74
+ org_id TEXT NOT NULL DEFAULT 'default'
75
+ );
76
+
77
+ CREATE INDEX IF NOT EXISTS idx_threats_agent_id ON threats(agent_id);
78
+ CREATE INDEX IF NOT EXISTS idx_threats_received_at ON threats(received_at);
79
+ CREATE INDEX IF NOT EXISTS idx_threats_verdict ON threats(verdict_conclusion);
80
+ CREATE INDEX IF NOT EXISTS idx_agents_status ON agents(status);
81
+ CREATE INDEX IF NOT EXISTS idx_policies_active ON policies(active);
82
+ CREATE INDEX IF NOT EXISTS idx_policies_version ON policies(version);
83
+ CREATE INDEX IF NOT EXISTS idx_agents_org_id ON agents(org_id);
84
+ CREATE INDEX IF NOT EXISTS idx_threats_org_id ON threats(org_id);
85
+ CREATE INDEX IF NOT EXISTS idx_policies_org_id ON policies(org_id);
86
+ `);
87
+ }
88
+ // =========================================================================
89
+ // Prepared statements / 預編譯語句
90
+ // =========================================================================
91
+ prepareStatements() {
92
+ return {
93
+ // --- Agent statements ---
94
+ upsertAgent: this.db.prepare(`
95
+ INSERT INTO agents (agent_id, hostname, os, arch, ip, version, status, last_heartbeat, registered_at, org_id)
96
+ VALUES (@agent_id, @hostname, @os, @arch, @ip, @version, @status, @last_heartbeat, @registered_at, @org_id)
97
+ ON CONFLICT(agent_id) DO UPDATE SET
98
+ hostname = excluded.hostname,
99
+ os = excluded.os,
100
+ arch = excluded.arch,
101
+ ip = excluded.ip,
102
+ version = excluded.version,
103
+ status = excluded.status,
104
+ last_heartbeat = excluded.last_heartbeat,
105
+ org_id = excluded.org_id
106
+ `),
107
+ getAgent: this.db.prepare('SELECT * FROM agents WHERE agent_id = ?'),
108
+ getAllAgents: this.db.prepare('SELECT * FROM agents ORDER BY registered_at DESC'),
109
+ getAgentsByStatus: this.db.prepare('SELECT * FROM agents WHERE status = ? ORDER BY last_heartbeat DESC'),
110
+ updateAgentStatus: this.db.prepare('UPDATE agents SET status = ? WHERE agent_id = ?'),
111
+ updateHeartbeat: this.db.prepare('UPDATE agents SET last_heartbeat = ?, status = ? WHERE agent_id = ?'),
112
+ deleteAgent: this.db.prepare('DELETE FROM agents WHERE agent_id = ?'),
113
+ getAgentCount: this.db.prepare('SELECT COUNT(*) as count FROM agents'),
114
+ getAgentsByOrg: this.db.prepare('SELECT * FROM agents WHERE org_id = ? ORDER BY registered_at DESC'),
115
+ // --- Threat statements ---
116
+ insertThreat: this.db.prepare(`
117
+ INSERT INTO threats (id, agent_id, source_hostname, event_json, verdict_conclusion, verdict_confidence, verdict_action, correlated_with, received_at, org_id)
118
+ VALUES (@id, @agent_id, @source_hostname, @event_json, @verdict_conclusion, @verdict_confidence, @verdict_action, @correlated_with, @received_at, @org_id)
119
+ `),
120
+ getThreatsByAgent: this.db.prepare('SELECT * FROM threats WHERE agent_id = ? ORDER BY received_at DESC'),
121
+ getRecentThreats: this.db.prepare('SELECT * FROM threats WHERE received_at >= ? ORDER BY received_at DESC'),
122
+ getThreatCount: this.db.prepare('SELECT COUNT(*) as count FROM threats'),
123
+ purgeOldThreats: this.db.prepare('DELETE FROM threats WHERE received_at < ?'),
124
+ getThreatsByOrg: this.db.prepare('SELECT * FROM threats WHERE org_id = ? ORDER BY received_at DESC'),
125
+ // --- Policy statements ---
126
+ insertPolicy: this.db.prepare(`
127
+ INSERT INTO policies (policy_id, version, rules_json, applied_to, updated_at, active, org_id)
128
+ VALUES (@policy_id, @version, @rules_json, @applied_to, @updated_at, @active, @org_id)
129
+ `),
130
+ getActivePolicy: this.db.prepare('SELECT * FROM policies WHERE active = 1 ORDER BY version DESC LIMIT 1'),
131
+ getPolicyHistory: this.db.prepare('SELECT * FROM policies ORDER BY version DESC'),
132
+ deactivateAllPolicies: this.db.prepare('UPDATE policies SET active = 0'),
133
+ getActivePolicyForOrg: this.db.prepare('SELECT * FROM policies WHERE active = 1 AND org_id = ? ORDER BY version DESC LIMIT 1'),
134
+ };
135
+ }
136
+ // =========================================================================
137
+ // Agent methods / 代理方法
138
+ // =========================================================================
139
+ /** Insert or update an agent registration / 插入或更新代理登錄 */
140
+ upsertAgent(agent) {
141
+ this.stmts.upsertAgent.run({
142
+ agent_id: agent.agentId,
143
+ hostname: agent.hostname,
144
+ os: agent.platform.os,
145
+ arch: agent.platform.arch,
146
+ ip: agent.platform.ip ?? null,
147
+ version: agent.version,
148
+ status: agent.status,
149
+ last_heartbeat: agent.lastHeartbeat,
150
+ registered_at: agent.registeredAt,
151
+ org_id: agent.organizationId ?? 'default',
152
+ });
153
+ }
154
+ /** Get a single agent by ID / 依 ID 取得單一代理 */
155
+ getAgent(agentId) {
156
+ const row = this.stmts.getAgent.get(agentId);
157
+ return row ? this.rowToAgent(row) : undefined;
158
+ }
159
+ /** Get all registered agents / 取得所有已登錄代理 */
160
+ getAllAgents() {
161
+ const rows = this.stmts.getAllAgents.all();
162
+ return rows.map((row) => this.rowToAgent(row));
163
+ }
164
+ /** Get agents filtered by status / 依狀態篩選代理 */
165
+ getAgentsByStatus(status) {
166
+ const rows = this.stmts.getAgentsByStatus.all(status);
167
+ return rows.map((row) => this.rowToAgent(row));
168
+ }
169
+ /** Update an agent's status / 更新代理狀態 */
170
+ updateAgentStatus(agentId, status) {
171
+ const result = this.stmts.updateAgentStatus.run(status, agentId);
172
+ return result.changes > 0;
173
+ }
174
+ /** Update an agent's heartbeat timestamp / 更新代理心跳時間戳 */
175
+ updateHeartbeat(agentId, timestamp) {
176
+ const result = this.stmts.updateHeartbeat.run(timestamp, 'online', agentId);
177
+ return result.changes > 0;
178
+ }
179
+ /** Delete an agent from the registry / 從登錄簿中刪除代理 */
180
+ deleteAgent(agentId) {
181
+ const result = this.stmts.deleteAgent.run(agentId);
182
+ return result.changes > 0;
183
+ }
184
+ /** Get total number of registered agents / 取得已登錄代理總數 */
185
+ getAgentCount() {
186
+ const row = this.stmts.getAgentCount.get();
187
+ return row.count;
188
+ }
189
+ // =========================================================================
190
+ // Threat methods / 威脅方法
191
+ // =========================================================================
192
+ /** Insert an aggregated threat / 插入聚合威脅 */
193
+ insertThreat(threat) {
194
+ this.stmts.insertThreat.run({
195
+ id: threat.id,
196
+ agent_id: threat.sourceAgentId,
197
+ source_hostname: threat.sourceHostname,
198
+ event_json: JSON.stringify(threat.originalThreat),
199
+ verdict_conclusion: threat.originalThreat.verdict.conclusion,
200
+ verdict_confidence: threat.originalThreat.verdict.confidence,
201
+ verdict_action: threat.originalThreat.verdict.action,
202
+ correlated_with: JSON.stringify(threat.correlatedWith),
203
+ received_at: threat.receivedAt,
204
+ org_id: threat.organizationId ?? 'default',
205
+ });
206
+ }
207
+ /** Get all threats from a specific agent / 取得特定代理的所有威脅 */
208
+ getThreatsByAgent(agentId) {
209
+ const rows = this.stmts.getThreatsByAgent.all(agentId);
210
+ return rows.map((row) => this.rowToThreat(row));
211
+ }
212
+ /** Get threats received after a given ISO timestamp / 取得指定時間後接收的威脅 */
213
+ getRecentThreats(since) {
214
+ const rows = this.stmts.getRecentThreats.all(since);
215
+ return rows.map((row) => this.rowToThreat(row));
216
+ }
217
+ /** Get total number of stored threats / 取得已儲存威脅總數 */
218
+ getThreatCount() {
219
+ const row = this.stmts.getThreatCount.get();
220
+ return row.count;
221
+ }
222
+ /** Purge threats older than a given ISO date, returns number deleted / 清除指定日期前的威脅 */
223
+ purgeOldThreats(beforeDate) {
224
+ const result = this.stmts.purgeOldThreats.run(beforeDate);
225
+ return result.changes;
226
+ }
227
+ // =========================================================================
228
+ // Policy methods / 策略方法
229
+ // =========================================================================
230
+ /**
231
+ * Insert a policy update. If active=true, deactivates all existing policies first.
232
+ * 插入策略更新。若 active=true,先停用所有現有策略。
233
+ */
234
+ insertPolicy(policy, active) {
235
+ const insertWithActivation = this.db.transaction(() => {
236
+ if (active) {
237
+ this.stmts.deactivateAllPolicies.run();
238
+ }
239
+ this.stmts.insertPolicy.run({
240
+ policy_id: policy.policyId,
241
+ version: policy.version,
242
+ rules_json: JSON.stringify(policy.rules),
243
+ applied_to: JSON.stringify(policy.appliedTo),
244
+ updated_at: policy.updatedAt,
245
+ active: active ? 1 : 0,
246
+ org_id: policy.organizationId ?? 'default',
247
+ });
248
+ });
249
+ insertWithActivation();
250
+ }
251
+ /** Get the currently active policy, or null if none / 取得當前啟用的策略 */
252
+ getActivePolicy() {
253
+ const row = this.stmts.getActivePolicy.get();
254
+ return row ? this.rowToPolicy(row) : null;
255
+ }
256
+ /** Get all policies ordered by version descending / 取得所有策略(依版本降冪) */
257
+ getPolicyHistory() {
258
+ const rows = this.stmts.getPolicyHistory.all();
259
+ return rows.map((row) => this.rowToPolicy(row));
260
+ }
261
+ /** Deactivate all policies / 停用所有策略 */
262
+ deactivateAllPolicies() {
263
+ this.stmts.deactivateAllPolicies.run();
264
+ }
265
+ // =========================================================================
266
+ // Organization-scoped queries / 組織範圍查詢
267
+ // =========================================================================
268
+ /** Get agents filtered by organization ID / 依組織 ID 篩選代理 */
269
+ getAgentsByOrg(orgId) {
270
+ const rows = this.stmts.getAgentsByOrg.all(orgId);
271
+ return rows.map((row) => this.rowToAgent(row));
272
+ }
273
+ /** Get threats filtered by organization ID / 依組織 ID 篩選威脅 */
274
+ getThreatsByOrg(orgId) {
275
+ const rows = this.stmts.getThreatsByOrg.all(orgId);
276
+ return rows.map((row) => this.rowToThreat(row));
277
+ }
278
+ /** Get the active policy for a specific organization / 取得特定組織的啟用策略 */
279
+ getActivePolicyForOrg(orgId) {
280
+ const row = this.stmts.getActivePolicyForOrg.get(orgId);
281
+ return row ? this.rowToPolicy(row) : null;
282
+ }
283
+ // =========================================================================
284
+ // Lifecycle / 生命週期
285
+ // =========================================================================
286
+ /** Close the database connection / 關閉資料庫連線 */
287
+ close() {
288
+ this.db.close();
289
+ }
290
+ // =========================================================================
291
+ // Row-to-domain converters (private) / 列轉網域物件(私有)
292
+ // =========================================================================
293
+ /** Convert a database row to an AgentRegistration / 將資料庫列轉換為 AgentRegistration */
294
+ rowToAgent(row) {
295
+ return {
296
+ agentId: row.agent_id,
297
+ hostname: row.hostname,
298
+ endpoint: '',
299
+ platform: {
300
+ os: row.os,
301
+ arch: row.arch,
302
+ ip: row.ip ?? undefined,
303
+ },
304
+ version: row.version,
305
+ status: row.status,
306
+ lastHeartbeat: row.last_heartbeat,
307
+ registeredAt: row.registered_at,
308
+ organizationId: row.org_id,
309
+ };
310
+ }
311
+ /** Convert a database row to an AggregatedThreat / 將資料庫列轉換為 AggregatedThreat */
312
+ rowToThreat(row) {
313
+ const originalThreat = JSON.parse(row.event_json);
314
+ const correlatedWith = JSON.parse(row.correlated_with);
315
+ return {
316
+ id: row.id,
317
+ originalThreat,
318
+ sourceAgentId: row.agent_id,
319
+ sourceHostname: row.source_hostname,
320
+ receivedAt: row.received_at,
321
+ correlatedWith,
322
+ organizationId: row.org_id,
323
+ };
324
+ }
325
+ /** Convert a database row to a PolicyUpdate / 將資料庫列轉換為 PolicyUpdate */
326
+ rowToPolicy(row) {
327
+ const rules = JSON.parse(row.rules_json);
328
+ const appliedTo = JSON.parse(row.applied_to);
329
+ return {
330
+ policyId: row.policy_id,
331
+ version: row.version,
332
+ rules,
333
+ updatedAt: row.updated_at,
334
+ appliedTo,
335
+ organizationId: row.org_id,
336
+ };
337
+ }
338
+ }
339
+ //# sourceMappingURL=db.js.map
package/dist/db.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db.js","sourceRoot":"","sources":["../src/db.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAsFtC;;;;;;;;GAQG;AACH,MAAM,OAAO,SAAS;IACH,EAAE,CAAoB;IACtB,KAAK,CAAa;IAEnC,YAAY,MAAc;QACxB,IAAI,CAAC,EAAE,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;QAE/B,gEAAgE;QAChE,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;QACrC,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;QACpC,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC;QACvC,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC;QACvC,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC;QACtC,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAA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AQ;YAC3B,cAAc,EAAE,GAAG,CAAC,eAAe;YACnC,UAAU,EAAE,GAAG,CAAC,WAAW;YAC3B,cAAc;YACd,cAAc,EAAE,GAAG,CAAC,MAAM;SAC3B,CAAC;IACJ,CAAC;IAED,uEAAuE;IAC/D,WAAW,CAAC,GAAc;QAChC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAiB,CAAC;QACzD,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAa,CAAC;QAEzD,OAAO;YACL,QAAQ,EAAE,GAAG,CAAC,SAAS;YACvB,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,KAAK;YACL,SAAS,EAAE,GAAG,CAAC,UAAU;YACzB,SAAS;YACT,cAAc,EAAE,GAAG,CAAC,MAAM;SAC3B,CAAC;IACJ,CAAC;CACF"}
package/dist/index.d.ts CHANGED
@@ -11,12 +11,15 @@
11
11
  * @module @panguard-ai/manager
12
12
  */
13
13
  export { Manager } from './manager.js';
14
+ export { ManagerServer } from './server.js';
14
15
  export { AgentRegistry } from './agent-registry.js';
15
16
  export { ThreatAggregator } from './threat-aggregator.js';
16
17
  export { PolicyEngine } from './policy-engine.js';
18
+ export { DashboardRelay } from './dashboard-relay.js';
19
+ export type { DashboardRelayConfig } from './dashboard-relay.js';
17
20
  export { generateAgentId, generateThreatId, generatePolicyId, generateAuthToken, extractSourceIP, extractFileHash, } from './utils.js';
18
- export type { AgentStatus, AgentPlatformInfo, AgentRegistration, AgentRegistrationRequest, AgentHeartbeat, ThreatEvent, ThreatReport, AggregatedThreat, CorrelationMatch, ThreatSummary, PolicyRule, PolicyUpdate, ManagerConfig, AgentOverview, ManagerOverview, PolicyBroadcastResult, } from './types.js';
21
+ export type { AgentStatus, AgentPlatformInfo, AgentRegistration, AgentRegistrationRequest, AgentHeartbeat, ThreatEvent, ThreatReport, AggregatedThreat, CorrelationMatch, ThreatSummary, PolicyRule, PolicyUpdate, ManagerConfig, AgentOverview, ManagerOverview, AgentPushResult, PolicyBroadcastResult, } from './types.js';
19
22
  export { DEFAULT_MANAGER_CONFIG } from './types.js';
20
23
  /** Manager package version / Manager 套件版本 */
21
- export declare const MANAGER_VERSION = "0.2.0";
24
+ export declare const MANAGER_VERSION: string;
22
25
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAGvC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,eAAe,GAChB,MAAM,YAAY,CAAC;AAGpB,YAAY,EACV,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,wBAAwB,EACxB,cAAc,EACd,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,UAAU,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EACb,eAAe,EACf,qBAAqB,GACtB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEpD,6CAA6C;AAC7C,eAAO,MAAM,eAAe,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAGvC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG5C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,YAAY,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAGjE,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,eAAe,GAChB,MAAM,YAAY,CAAC;AAGpB,YAAY,EACV,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,wBAAwB,EACxB,cAAc,EACd,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,UAAU,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EACb,eAAe,EACf,eAAe,EACf,qBAAqB,GACtB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAMpD,6CAA6C;AAC7C,eAAO,MAAM,eAAe,EAAE,MAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -12,14 +12,20 @@
12
12
  */
13
13
  // Main orchestrator / 主協調器
14
14
  export { Manager } from './manager.js';
15
+ // HTTP Server / HTTP 伺服器
16
+ export { ManagerServer } from './server.js';
15
17
  // Components / 元件
16
18
  export { AgentRegistry } from './agent-registry.js';
17
19
  export { ThreatAggregator } from './threat-aggregator.js';
18
20
  export { PolicyEngine } from './policy-engine.js';
21
+ export { DashboardRelay } from './dashboard-relay.js';
19
22
  // Utilities / 工具函式
20
23
  export { generateAgentId, generateThreatId, generatePolicyId, generateAuthToken, extractSourceIP, extractFileHash, } from './utils.js';
21
24
  // Constants / 常數
22
25
  export { DEFAULT_MANAGER_CONFIG } from './types.js';
26
+ import { createRequire } from 'node:module';
27
+ const _require = createRequire(import.meta.url);
28
+ const _pkg = _require('../package.json');
23
29
  /** Manager package version / Manager 套件版本 */
24
- export const MANAGER_VERSION = '0.2.0';
30
+ export const MANAGER_VERSION = _pkg.version;
25
31
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,2BAA2B;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,kBAAkB;AAClB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,mBAAmB;AACnB,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,eAAe,GAChB,MAAM,YAAY,CAAC;AAsBpB,iBAAiB;AACjB,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEpD,6CAA6C;AAC7C,MAAM,CAAC,MAAM,eAAe,GAAG,OAAO,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,2BAA2B;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,yBAAyB;AACzB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,kBAAkB;AAClB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAGtD,mBAAmB;AACnB,OAAO,EACL,eAAe,EACf,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,eAAe,GAChB,MAAM,YAAY,CAAC;AAuBpB,iBAAiB;AACjB,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAChD,MAAM,IAAI,GAAG,QAAQ,CAAC,iBAAiB,CAAwB,CAAC;AAEhE,6CAA6C;AAC7C,MAAM,CAAC,MAAM,eAAe,GAAW,IAAI,CAAC,OAAO,CAAC"}
package/dist/manager.d.ts CHANGED
@@ -10,7 +10,10 @@
10
10
  *
11
11
  * @module @panguard-ai/manager/manager
12
12
  */
13
- import type { ManagerConfig, AgentRegistrationRequest, AgentRegistration, AgentHeartbeat, ThreatReport, AggregatedThreat, PolicyUpdate, PolicyRule, ManagerOverview, ThreatSummary, PolicyBroadcastResult } from './types.js';
13
+ import { DashboardRelay } from './dashboard-relay.js';
14
+ import type { IncomingMessage } from 'node:http';
15
+ import type { Socket } from 'node:net';
16
+ import type { ManagerConfig, AgentRegistrationRequest, AgentRegistration, AgentHeartbeat, ThreatReport, AggregatedThreat, PolicyUpdate, PolicyRule, ManagerOverview, ThreatSummary, AgentPushResult, PolicyBroadcastResult } from './types.js';
14
17
  /**
15
18
  * Manager is the main orchestrator for the distributed Guard architecture.
16
19
  *
@@ -22,6 +25,8 @@ export declare class Manager {
22
25
  private readonly registry;
23
26
  private readonly aggregator;
24
27
  private readonly policyEngine;
28
+ /** Dashboard relay for proxying remote dashboard connections / 用於代理遠端 dashboard 連接的 relay */
29
+ private readonly relay;
25
30
  private running;
26
31
  private startTime;
27
32
  private staleCheckTimer;
@@ -38,6 +43,16 @@ export declare class Manager {
38
43
  * Clears all periodic timers.
39
44
  */
40
45
  stop(): void;
46
+ /**
47
+ * Handle WebSocket upgrade for dashboard relay paths.
48
+ * Routes /api/dashboard/ paths to the DashboardRelay.
49
+ * 處理 dashboard relay 路徑的 WebSocket 升級。
50
+ *
51
+ * @returns true if the path was handled, false if not a dashboard path
52
+ */
53
+ handleDashboardUpgrade(req: IncomingMessage, socket: Socket, head: Buffer): boolean;
54
+ /** Get the dashboard relay instance / 取得 dashboard relay 實例 */
55
+ getDashboardRelay(): DashboardRelay;
41
56
  /**
42
57
  * Handle a new agent registration request.
43
58
  *
@@ -71,19 +86,38 @@ export declare class Manager {
71
86
  handleDeregistration(agentId: string): boolean;
72
87
  /**
73
88
  * Create and optionally broadcast a new security policy.
89
+ * 建立並選擇性廣播新的安全策略。
74
90
  *
75
91
  * @param rules - Policy rules to include
76
- * @param broadcast - Whether to queue broadcast to all active agents
92
+ * @param broadcast - Whether to push broadcast to all active agents
77
93
  * @returns The created policy
78
94
  */
79
- createPolicy(rules: readonly PolicyRule[], broadcast?: boolean): PolicyUpdate;
95
+ createPolicy(rules: readonly PolicyRule[], broadcast?: boolean): Promise<PolicyUpdate>;
96
+ /**
97
+ * Push a policy update to a single agent via HTTP POST.
98
+ * 透過 HTTP POST 將策略更新推送至單一代理。
99
+ *
100
+ * Looks up the agent's endpoint from the registry, sends a POST to
101
+ * `{agent.endpoint}/api/policy/push` with JSON body `{ policy, timestamp }`.
102
+ * Retries once on network failure. Timeout is 5 seconds per attempt.
103
+ *
104
+ * @param agentId - The target agent's unique identifier
105
+ * @param policy - The policy update to push
106
+ * @returns Push result indicating success or failure
107
+ */
108
+ pushPolicyToAgent(agentId: string, policy: PolicyUpdate): Promise<AgentPushResult>;
80
109
  /**
81
- * Queue a policy update for broadcast to all active agents.
110
+ * Broadcast a policy update to active agents via HTTP POST push.
111
+ * 透過 HTTP POST 推送將策略更新廣播至活躍代理。
112
+ *
113
+ * Gets target agents (specified IDs or all active), pushes policy to each,
114
+ * and collects results into the broadcast queue.
82
115
  *
83
116
  * @param policy - The policy to broadcast
84
- * @returns Broadcast result with target agent list
117
+ * @param targetAgentIds - Optional list of specific agent IDs to target
118
+ * @returns Broadcast result with per-agent outcomes
85
119
  */
86
- broadcastPolicy(policy: PolicyUpdate): PolicyBroadcastResult;
120
+ broadcastPolicy(policy: PolicyUpdate, targetAgentIds?: string[]): Promise<PolicyBroadcastResult>;
87
121
  /**
88
122
  * Get the active global policy.
89
123
  *
@@ -1 +1 @@
1
- {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAMH,OAAO,KAAK,EACV,aAAa,EACb,wBAAwB,EACxB,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,UAAU,EACV,eAAe,EAEf,aAAa,EACb,qBAAqB,EACtB,MAAM,YAAY,CAAC;AAIpB;;;;;GAKG;AACH,qBAAa,OAAO;IAClB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgB;IACzC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAmB;IAC9C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAE5C,OAAO,CAAC,OAAO,CAAU;IACzB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,eAAe,CAAwC;IAC/D,OAAO,CAAC,UAAU,CAAwC;IAG1D,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA0B;gBAE7C,MAAM,EAAE,aAAa;IAsBjC;;;OAGG;IACH,KAAK,IAAI,IAAI;IA6Bb;;;OAGG;IACH,IAAI,IAAI,IAAI;IAsBZ;;;;;;OAMG;IACH,kBAAkB,CAChB,OAAO,EAAE,wBAAwB,GAChC,iBAAiB;IAWpB;;;;;;OAMG;IACH,eAAe,CACb,SAAS,EAAE,cAAc,GACxB,iBAAiB,GAAG,SAAS;IAIhC;;;;;;OAMG;IACH,kBAAkB,CAChB,MAAM,EAAE,YAAY,GACnB,SAAS,gBAAgB,EAAE;IAoB9B;;;;;OAKG;IACH,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAM9C;;;;;;OAMG;IAC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