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

init

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