Commit 26833b26 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent f75ee542
const radius = require('radius'); const radius = require('radius');
const dgram = require('dgram'); const dgram = require('dgram');
let redis=require('./redis') let redis = require('./redis')
redis.init()
const secret = 'secret'; // Replace with your shared secret const secret = 'secret'; // Replace with your shared secret
const port = 1812; // Default RADIUS authentication port const port = 1812; // Default RADIUS authentication port
let reject='Access-Reject' let reject = 'Access-Reject'
let accept='Access-Accept' let accept = 'Access-Accept'
let maxConnection=2 let maxConnection = 2
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 }); const packet = radius.decode({packet: msg, secret});
if (packet.code !== 'Access-Request') { if (packet.code !== 'Access-Request') {
console.error('Invalid packet type: ' + packet.code); console.error('Invalid packet type: ' + packet.code);
...@@ -19,15 +19,16 @@ server.on('message', (msg, rinfo) => { ...@@ -19,15 +19,16 @@ server.on('message', (msg, rinfo) => {
} }
const username = packet.attributes['User-Name']; const username = packet.attributes['User-Name'];
if (username === 'root')
return;
const password = packet.attributes['User-Password']; const password = packet.attributes['User-Password'];
let ip=packet.attributes['Calling-Station-Id'] let ip = packet.attributes['Calling-Station-Id']
let user={username,password,ip} let user = {username, password, ip}
// console.log('Received access request from:', username); // console.log('Received access request from:', username);
doAuth(packet, user, rinfo)
doAuth(packet,user,rinfo)
}); });
server.on('listening', () => { server.on('listening', () => {
...@@ -37,36 +38,32 @@ server.on('listening', () => { ...@@ -37,36 +38,32 @@ server.on('listening', () => {
server.bind(port); server.bind(port);
async function doAuth(packet,user,rinfo) async function doAuth(packet, user, rinfo) {
{ let existsUser = authenticateUser(user.username)
let existsUser=authenticateUser(user.username) if (!existsUser) {
if (!existsUser) console.log('uuid with ' + user.username + ' with ip:' + user.ip + ' not exist')
{ sendResponsePacket(packet, rinfo, reject)
console.log('uuid with '+user.username +' with ip:'+user.ip +' not exist')
sendResponsePacket(packet,rinfo,reject)
return return
} }
let keys=await redis.getAllKeysByUUID(user.username) let keys = await redis.getAllKeysByUUID(user.username)
if (keys.length>=2) if (keys.length >= 2) {
{ console.log('uuid with ' + user.username + ' with ip:' + user.ip + ' reach limits')
console.log('uuid with '+user.username +' with ip:'+user.ip +' reach limits') sendResponsePacket(packet, rinfo, reject)
sendResponsePacket(packet,rinfo,reject)
return return
} }
console.log('uuid with '+user.username +' with ip:'+user.ip +' granted') console.log('uuid with ' + user.username + ' with ip:' + user.ip + ' granted')
sendResponsePacket(packet,rinfo,accept) sendResponsePacket(packet, rinfo, accept)
redis.addIp(user.username,user.ip) redis.addIp(user.username, user.ip)
} }
function authenticateUser(username) { function authenticateUser(username) {
// console.log(username,password) // console.log(username,password)
// Replace this function with your actual authentication logic // Replace this function with your actual authentication logic
return username === 'ali'; return username === 'ali';
} }
function sendResponsePacket(packet,rinfo,code) function sendResponsePacket(packet, rinfo, code) {
{
let response; let response;
response = radius.encode_response({ response = radius.encode_response({
packet, packet,
......
...@@ -46,7 +46,8 @@ async function addIp(uuid, ip) { ...@@ -46,7 +46,8 @@ async function addIp(uuid, ip) {
module.exports = module.exports =
{ {
getAllKeysByUUID: getAllKeysByUUID, getAllKeysByUUID: getAllKeysByUUID,
addIp:addIp addIp:addIp,
init:init
} }
\ No newline at end of file
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