Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
77 lines (64 loc) · 2.51 KB
/
Copy pathapi.js
File metadata and controls
77 lines (64 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// api.js — API client & SSE handler
const API = {
BASE: '/api/v1',
async request(method, path, body = null) {
const opts = { method, headers: { 'Content-Type': 'application/json' } };
if (body) opts.body = JSON.stringify(body);
const resp = await fetch(`${API.BASE}${path}`, opts);
const data = await resp.json().catch(() => ({ error: resp.statusText }));
if (!resp.ok) throw new Error(data.error || `HTTP ${resp.status}`);
return data;
},
// Scan CRUD
createScan: (payload) => API.request('POST', '/scans', payload),
listScans: () => API.request('GET', '/scans'),
getScan: (id) => API.request('GET', `/scans/${id}`),
deleteScan: (id) => API.request('DELETE', `/scans/${id}`),
cancelScan: (id) => API.request('POST', `/scans/${id}/cancel`),
// Hosts
listHosts: (id) => API.request('GET', `/scans/${id}/hosts`),
getHost: (id, ip) => API.request('GET', `/scans/${id}/hosts/${ip}`),
// Topology
getTopology: (id) => API.request('GET', `/scans/${id}/topology`),
// Export/Import
exportScan: (id) => { window.open(`${API.BASE}/scans/${id}/export`); },
importScan: (file) => {
return file.text().then(text => {
return API.request('POST', '/import', JSON.parse(text));
});
},
// Config
getPresets: () => API.request('GET', '/config/presets'),
health: () => API.request('GET', '/health'),
};
// SSE Manager
const SSE = {
source: null,
handlers: {},
reconnectDelay: 3000,
connect() {
if (SSE.source) SSE.source.close();
SSE.source = new EventSource(`${API.BASE}/events`);
const events = [
'scan:started', 'scan:progress', 'host:discovered',
'port:open', 'service:detected', 'scan:completed',
'scan:cancelled', 'scan:error',
];
events.forEach(evt => {
SSE.source.addEventListener(evt, (e) => {
const data = JSON.parse(e.data);
if (SSE.handlers[evt]) SSE.handlers[evt](data);
document.dispatchEvent(new CustomEvent(`netsight:${evt}`, { detail: data }));
});
});
SSE.source. => {
SSE.source.close();
setTimeout(SSE.connect, SSE.reconnectDelay);
};
},
on(event, handler) { SSE.handlers[event] = handler; },
off(event) { delete SSE.handlers[event]; },
disconnect() {
if (SSE.source) { SSE.source.close(); SSE.source = null; }
},
};