Commit 6171c736 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent eead6b45
Pipeline #165 failed with stages
const dgram = require('dgram');
const radius = require('radius'); const radius = require('radius');
const dgram = require('dgram');
const RADIUS_PORT = 1812; // Default RADIUS authentication port const secret = 'secret'; // Replace with your shared secret
const RADIUS_SECRET = 'secret'; const port = 1812; // Default RADIUS authentication port
const server = dgram.createSocket('udp4'); const server = dgram.createSocket('udp4');
server.on('message', (msg, rinfo) => { server.on('message', (msg, rinfo) => {
try { const packet = radius.decode({ packet: msg, secret });
const packet = radius.decode({ packet: msg, secret: RADIUS_SECRET }); console.log(packet)
// Log the client's IP address
console.log(`Received RADIUS packet from ${rinfo.address}:${rinfo.port}`);
if (packet.code !== 'Access-Request') { if (packet.code !== 'Access-Request') {
console.log(`Unknown packet type: ${packet.code}`); console.error('Invalid packet type: ' + packet.code);
return; return;
} }
const username = packet.attributes['User-Name']; const username = packet.attributes['User-Name'];
const password = packet.attributes['User-Password']; const password = packet.attributes['User-Password'];
console.log(`Username: ${username}, Password: ${password}`); console.log('Received access request from:', username);
// Implement your authentication logic here // Perform your user authentication logic here
const isAuthenticated = authenticateUser(username, password); const isAuthenticated = authenticateUser(username, password);
const response = isAuthenticated ? 'Access-Accept' : 'Access-Reject'; let response;
const responsePacket = radius.encode_response({ if (isAuthenticated) {
packet: packet, response = radius.encode_response({
code: response, packet,
secret: RADIUS_SECRET, code: 'Access-Accept',
secret,
}); });
console.log('Access granted for:', username);
server.send(responsePacket, 0, responsePacket.length, rinfo.port, rinfo.address, (err, bytes) => { } else {
if (err) { response = radius.encode_response({
console.error('Error sending response:', err); packet,
} code: 'Access-Reject',
secret,
}); });
} catch (err) { console.log('Access denied for:', username);
console.error('Error decoding RADIUS packet:', err);
} }
server.send(response, 0, response.length, rinfo.port, rinfo.address);
}); });
server.on('listening', () => { server.on('listening', () => {
const address = server.address(); const address = server.address();
console.log(`RADIUS server listening on ${address.address}:${address.port}`); console.log('RADIUS server listening on port', address.port);
}); });
server.bind(RADIUS_PORT); server.bind(port);
function authenticateUser(username, password) { function authenticateUser(username, password) {
// Replace this with your authentication logic (e.g., checking against a database) console.log(username,password)
return username === 'ali' && password === 'ahmad'; // Replace this function with your actual authentication logic
return username === 'ali';
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment