-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhepop.js
217 lines (187 loc) · 6.03 KB
/
hepop.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import hepjs from 'hep-js';
import { getSIP } from 'parsip';
import { InfluxDBClient } from '@influxdata/influxdb3-client';
class BufferManager {
constructor(flushInterval = 2000, bufferSize = 100) {
this.buffers = new Map();
this.flushInterval = flushInterval;
this.bufferSize = bufferSize;
this.host = process.env.INFLUX_HOST || 'http://localhost:8181';
const token = process.env.INFLUX_TOKEN || '';
this.database = process.env.INFLUX_DATABASE || 'hep';
console.log(`Initializing InfluxDB client: ${this.host}/${this.database}`);
this.client = new InfluxDBClient({
host: this.host,
token: token,
database: this.database
});
this.retryAttempts = 3;
this.startFlushInterval();
}
add(type, data) {
if (!this.buffers.has(type)) {
this.buffers.set(type, []);
}
this.buffers.get(type).push(data);
if (this.buffers.get(type).length >= this.bufferSize) {
this.flush(type);
}
}
startFlushInterval() {
setInterval(() => {
for (const type of this.buffers.keys()) {
this.flush(type);
}
}, this.flushInterval);
}
getRetryInterval(retry) {
const token = retry.toString();
let result = parseInt(token);
if (isNaN(result)) {
const now = new Date();
const exp = new Date(token);
if (isNaN(exp.valueOf())) {
throw new Error(`Failed to parse retry value: ${retry}`);
}
result = exp.getTime() - now.getTime();
} else {
result *= 1000;
}
return result;
}
async writeWithRetry(points, retryCount = this.retryAttempts) {
try {
console.log(`Attempting to write points to InfluxDB (${this.host}/${this.database})`);
await this.client.write(points, this.database);
return true;
} catch (error) {
if (error.headers?.['retry-after']) {
console.log(`Warning: Write failed - ${error.message}`);
if (retryCount > 0) {
const interval = this.getRetryInterval(error.headers['retry-after']) + 1000;
console.log(`Retrying in ${interval}ms`);
await new Promise(resolve => setTimeout(resolve, interval));
return this.writeWithRetry(points, retryCount - 1);
}
}
console.error('Write error:', error);
return false;
}
}
async flush(type) {
const buffer = this.buffers.get(type);
if (!buffer?.length) return;
try {
const points = buffer.map(data => {
const clean = str => str?.replace(/["\\\r\n]/g, '') || '';
const rcinfo = data.protocol_header || {};
const tags = [
`type=${type}`
].join(',');
const fields = [
`ip_family=${rcinfo.protocolFamily || 0}i`,
`protocol=${rcinfo.protocol || 0}i`,
`proto_type=${rcinfo.proto_type || 0}i`,
`capture_id=${rcinfo.captureId || 0}i`,
`correlation_id="${clean(rcinfo.correlation_id)}"`,
`capture_pass="${clean(rcinfo.capturePass)}"`,
`src_ip="${clean(rcinfo.srcIp)}"`,
`dst_ip="${clean(rcinfo.dstIp)}"`,
`src_port=${rcinfo.srcPort || 0}i`,
`dst_port=${rcinfo.dstPort || 0}i`,
`time_sec=${rcinfo.timeSeconds || 0}i`,
`time_usec=${rcinfo.timeUseconds || 0}i`,
`payload="${clean(data.raw)}"`
].join(',');
const timestamp = new Date(data.create_date).getTime() * 1000000;
return `hep_${type},${tags} ${fields} ${timestamp}`;
});
console.log(`Writing ${points.length} points, sample:`, points[0]);
await this.writeWithRetry(points.join('\n'));
this.buffers.set(type, []);
} catch (error) {
console.error(`Buffer flush error:`, error);
}
}
async close() {
for (const type of this.buffers.keys()) {
await this.flush(type);
}
await this.client.close();
}
}
class HEPServer {
constructor(config = {}) {
this.debug = config.debug || false;
this.buffer = new BufferManager();
this.startServers();
}
startServers() {
const port = parseInt(process.env.PORT) || 9069;
const host = process.env.HOST || "0.0.0.0";
// TCP Server
Bun.listen({
hostname: host,
port: port,
socket: {
data: (socket, data) => this.handleData(data, socket),
error: (socket, error) => console.error('TCP error:', error),
}
});
// UDP Server
Bun.udpSocket({
hostname: host,
port: port,
udp: true,
socket: {
data: (socket, data) => this.handleData(data, socket),
error: (socket, error) => console.error('UDP error:', error),
}
});
console.log(`HEP Server listening on ${host}:${port} (TCP/UDP)`);
// Handle graceful shutdown
process.on('SIGTERM', this.shutdown.bind(this));
process.on('SIGINT', this.shutdown.bind(this));
}
async shutdown() {
console.log('Shutting down HEP server...');
await this.buffer.close();
process.exit(0);
}
handleData(data, socket) {
try {
console.log(`Received ${data.length} bytes from ${socket.remoteAddress}`);
const processed = this.processHep(data, socket);
const type = processed.type;
console.log(`Processed HEP type ${type}, adding to buffer`);
this.buffer.add(type, processed);
} catch (error) {
console.error('Handle data error:', error);
}
}
processHep(data, socket) {
try {
const decoded = hepjs.decapsulate(data);
const insert = {
protocol_header: decoded.rcinfo,
create_date: this.getHepTimestamp(decoded.rcinfo),
raw: decoded.payload || "",
type: decoded.rcinfo.payload_type || decoded.rcinfo.payloadType || 0
};
return insert;
} catch(err) {
console.error('HEP Processing Error:', err);
throw err;
}
}
getHepTimestamp(rcinfo) {
if (!rcinfo.timeSeconds) return new Date();
return new Date(
(rcinfo.timeSeconds * 1000) +
(((100000 + rcinfo.timeUseconds) / 1000) - 100)
);
}
}
// Start the server
const server = new HEPServer({ debug: true });
export { HEPServer, hepjs };