Add device traffic counters and ambiguous MAC detection
This commit is contained in:
@@ -5,6 +5,7 @@ import { settings } from './config.js';
|
||||
import { createSingboxRuntime } from './singboxRuntime.js';
|
||||
import { buildVersionInfo } from './version.js';
|
||||
import { readNeighborSnapshot } from './adapters/neighbors.js';
|
||||
import { createDeviceTrafficService } from './services/deviceTrafficService.js';
|
||||
|
||||
const socketPath = settings.dataplaneSocket;
|
||||
const runtime = createSingboxRuntime({
|
||||
@@ -13,7 +14,14 @@ const runtime = createSingboxRuntime({
|
||||
tproxyChain: settings.tproxyChain,
|
||||
});
|
||||
const versionInfo = buildVersionInfo('gateway');
|
||||
const traffic = createDeviceTrafficService({
|
||||
observe: () => readNeighborSnapshot(),
|
||||
uploadChain: settings.trafficUploadChain,
|
||||
downloadChain: settings.trafficDownloadChain,
|
||||
bypassCidrs: settings.bypassCidrs,
|
||||
});
|
||||
let ready = false;
|
||||
let trafficTimer = null;
|
||||
|
||||
function sendJson(res, statusCode, payload) {
|
||||
res.writeHead(statusCode, { 'content-type': 'application/json; charset=utf-8' });
|
||||
@@ -33,6 +41,9 @@ const server = http.createServer(async (req, res) => {
|
||||
if (req.method === 'GET' && req.url === '/devices') {
|
||||
return sendJson(res, 200, readNeighborSnapshot());
|
||||
}
|
||||
if (req.method === 'GET' && req.url === '/device-traffic') {
|
||||
return sendJson(res, 200, traffic.snapshot());
|
||||
}
|
||||
if (req.method === 'POST' && req.url === '/apply') {
|
||||
return sendJson(res, 200, await runtime.apply());
|
||||
}
|
||||
@@ -58,6 +69,14 @@ server.listen(socketPath, async () => {
|
||||
console.warn(`[dataplane] sing-box не запущен: ${error.message}`);
|
||||
} finally {
|
||||
ready = true;
|
||||
setImmediate(() => {
|
||||
traffic.refresh()
|
||||
.catch((error) => console.warn(`[dataplane] traffic counters не запущены: ${error.message}`));
|
||||
});
|
||||
trafficTimer = setInterval(() => {
|
||||
traffic.refresh().catch((error) => console.warn(`[dataplane] traffic counters не обновлены: ${error.message}`));
|
||||
}, 15_000);
|
||||
trafficTimer.unref();
|
||||
console.log(`[dataplane] control socket: ${socketPath}`);
|
||||
}
|
||||
});
|
||||
@@ -67,6 +86,7 @@ async function shutdown() {
|
||||
if (shuttingDown) return;
|
||||
shuttingDown = true;
|
||||
ready = false;
|
||||
if (trafficTimer) clearInterval(trafficTimer);
|
||||
await runtime.shutdown();
|
||||
server.close(() => {
|
||||
fs.rmSync(socketPath, { force: true });
|
||||
|
||||
Reference in New Issue
Block a user