honeypeer/server.js

60 lines
1.9 KiB
JavaScript
Raw Normal View History

2024-07-10 05:52:41 -04:00
const node = require('hyper-ipc-secure')();
const crypto = require('hypercore-crypto');
const fs = require('fs');
// Generate key pair and save to file if it doesn't exist
if (!fs.existsSync('kp.json')) {
const kp = crypto.keyPair();
fs.writeFileSync('kp.json', JSON.stringify({
publicKey: kp.publicKey.toString('hex'),
secretKey: kp.secretKey.toString('hex')
}));
}
// Read key pair from file
const kp = JSON.parse(fs.readFileSync('kp.json'));
kp.publicKey = Buffer.from(kp.publicKey, 'hex');
kp.secretKey = Buffer.from(kp.secretKey, 'hex');
// Store clients
let clients = [];
// Function to extract IPv4 address from potential IPv6 format
function extractIPv4(ip) {
const ipv4Match = ip.match(/(\d{1,3}\.){3}\d{1,3}/);
return ipv4Match ? ipv4Match[0] : ip;
}
// Define services
node.serve(kp, 'report.attack', async (args) => {
const attackDetails = args.details;
console.log(`Received attack report from client. Details: ${JSON.stringify(attackDetails, null, 2)}`);
// Extract and use IPv4 address
const attackerIP = extractIPv4(attackDetails.ip);
for (const client of clients) {
try {
await node.run(client, 'ban.ip', { ip: attackerIP });
console.log(`Sent ban command to client for IP: ${attackerIP}`);
} catch (e) {
console.error(`Failed to send ban command to client: ${e.message}`);
}
}
return { status: 'ban commands sent' };
});
node.serve(kp, 'register.client', async (args) => {
try {
console.log('Received register client request:', args);
const clientPublicKey = Buffer.from(args.publicKey, 'hex');
clients.push(clientPublicKey);
console.log(`Client registered: ${args.publicKey}`);
return { status: 'registered' };
} catch (e) {
console.error('Failed to register client:', e.message);
return { status: 'error', message: e.message };
}
});
// Start the server
// Add server start logic if required