Commit 0ec12962 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent 23e3e390
Pipeline #163 canceled with stages
const radius = require('radius');
const dgram = require('dgram'); const dgram = require('dgram');
const radius = require('radius');
const secret = 'secret'; // Replace with your shared secret const RADIUS_PORT = 1812; // Default RADIUS authentication port
const port = 1812; // Default RADIUS authentication port const RADIUS_SECRET = 'secret';
const server = dgram.createSocket('udp4'); const server = dgram.createSocket('udp4');
server.on('message', (msg, rinfo) => { server.on('message', (msg, rinfo) => {
const packet = radius.decode({ packet: msg, secret }); try {
console.log(packet) const packet = radius.decode({ packet: msg, secret: RADIUS_SECRET });
// 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.error('Invalid packet type: ' + packet.code); console.log(`Unknown packet type: ${packet.code}`);
return; return;
} }
const username = packet.attributes['User-Name']; const username = packet.attributes['User-Name'];
const password = packet.attributes['User-Password'];
console.log('Received access request from:', username); // Decrypt the password
const encryptedPassword = packet.attributes['User-Password'];
const password = radius.decrypt({ packet: packet, secret: RADIUS_SECRET, attribute: encryptedPassword });
// Perform your user authentication logic here console.log(`Username: ${username}, Password: ${password}`);
// Implement your authentication logic here
const isAuthenticated = authenticateUser(username, password); const isAuthenticated = authenticateUser(username, password);
let response; const response = isAuthenticated ? 'Access-Accept' : 'Access-Reject';
if (isAuthenticated) { const responsePacket = radius.encode_response({
response = radius.encode_response({ packet: packet,
packet, code: response,
code: 'Access-Accept', secret: RADIUS_SECRET,
secret,
}); });
console.log('Access granted for:', username);
} else { server.send(responsePacket, 0, responsePacket.length, rinfo.port, rinfo.address, (err, bytes) => {
response = radius.encode_response({ if (err) {
packet, console.error('Error sending response:', err);
code: 'Access-Reject', }
secret,
}); });
console.log('Access denied for:', username); } catch (err) {
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 port', address.port); console.log(`RADIUS server listening on ${address.address}:${address.port}`);
}); });
server.bind(port); server.bind(RADIUS_PORT);
function authenticateUser(username, password) { function authenticateUser(username, password) {
console.log(username,password) // Replace this with your authentication logic (e.g., checking against a database)
// Replace this function with your actual authentication logic return username === 'ali' && password === 'ahmad';
return username === 'ali' && password === '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