-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (60 loc) · 2.22 KB
/
server.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
const WebSocket = require('ws');
const fs = require('fs');
const path = require('path');
const mongoose = require('mongoose');
const logFilePath = "C:/Squid/var/log/squid/access.log";
let filePosition = 0; // Track the last read position in the file
// Create a WebSocket server on port 8080
const wss = new WebSocket.Server({ port: 8080 });
function extractIPAndLink(data) {
const lines = data.split('\n'); // Split log data into lines
const filteredData = lines.map(line => {
const parts = line.split(/\s+/);
const ipAddress = parts[2]; // Assuming IP address is at index 2
const httpLink = parts[6]; // Assuming HTTP link is at index 6
return { ipAddress, httpLink };
});
return filteredData;
}
// Function to read new updates from the log file and send to clients
function sendNewLogUpdates(ws) {
const fileStream = fs.createReadStream(logFilePath, { start: filePosition });
fileStream.on('data', (chunk) => {
const logData = chunk.toString(); // Convert buffer to string
const filteredData = extractIPAndLink(logData); // Filter and extract data
ws.send(JSON.stringify(filteredData)); // Send filtered data to client
});
fileStream.on('error', (err) => {
console.error('Error reading log file:', err);
});
fileStream.on('end', () => {
filePosition = fs.statSync(logFilePath).size; // Update file position after reading
});
}
// WebSocket server event handling
wss.on('connection', (ws) => {
console.log('Client connected');
// Send initial log file contents when a client connects
sendNewLogUpdates(ws);
// WebSocket message handling (optional)
ws.on('message', (message) => {
console.log(`Received message from client: ${message}`);
// You can handle client messages here if needed
});
// WebSocket close handling
ws.on('close', () => {
console.log('Client disconnected');
});
});
// Watch the log file for changes
fs.watch(logFilePath, (eventType, filename) => {
if (eventType === 'change') {
console.log('Log file changed, sending update to clients');
// Broadcast new log updates to all connected clients
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
sendNewLogUpdates(client);
}
});
}
});