Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
radius
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
radius
Commits
f75ee542
Commit
f75ee542
authored
May 03, 2023
by
Ahmad Nemati
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
614743af
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
102 additions
and
29 deletions
+102
-29
app.js
app.js
+46
-28
package.json
package.json
+2
-1
redis.js
redis.js
+52
-0
test_array.js
test_array.js
+2
-0
No files found.
app.js
View file @
f75ee542
const
radius
=
require
(
'
radius
'
);
const
dgram
=
require
(
'
dgram
'
);
let
redis
=
require
(
'
./redis
'
)
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
const
server
=
dgram
.
createSocket
(
'
udp4
'
);
server
.
on
(
'
message
'
,
(
msg
,
rinfo
)
=>
{
const
packet
=
radius
.
decode
({
packet
:
msg
,
secret
});
console
.
log
(
packet
.
attributes
)
if
(
packet
.
code
!==
'
Access-Request
'
)
{
console
.
error
(
'
Invalid packet type:
'
+
packet
.
code
);
return
;
...
...
@@ -16,35 +20,14 @@ console.log(packet.attributes)
const
username
=
packet
.
attributes
[
'
User-Name
'
];
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
);
const
inputOctets
=
packet
.
attributes
[
'
Acct-Input-Octets
'
];
const
outputOctets
=
packet
.
attributes
[
'
Acct-Output-Octets
'
];
console
.
log
(
'
RX:
'
,
inputOctets
,
'
TX:
'
,
outputOctets
);
// console.log('Received access request from:', username);
// Perform your user authentication logic here
const
isAuthenticated
=
authenticateUser
(
username
,
password
);
let
response
;
if
(
isAuthenticated
)
{
response
=
radius
.
encode_response
({
packet
,
code
:
'
Access-Accept
'
,
secret
,
});
console
.
log
(
'
Access granted for:
'
,
username
);
}
else
{
response
=
radius
.
encode_response
({
packet
,
code
:
'
Access-Reject
'
,
secret
,
});
console
.
log
(
'
Access denied for:
'
,
username
);
}
server
.
send
(
response
,
0
,
response
.
length
,
rinfo
.
port
,
rinfo
.
address
);
doAuth
(
packet
,
user
,
rinfo
)
});
server
.
on
(
'
listening
'
,
()
=>
{
...
...
@@ -54,8 +37,43 @@ server.on('listening', () => {
server
.
bind
(
port
);
function
authenticateUser
(
username
,
password
)
{
console
.
log
(
username
,
password
)
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
)
return
}
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)
// Replace this function with your actual authentication logic
return
username
===
'
ali
'
;
}
function
sendResponsePacket
(
packet
,
rinfo
,
code
)
{
let
response
;
response
=
radius
.
encode_response
({
packet
,
code
:
code
,
secret
,
});
server
.
send
(
response
,
0
,
response
.
length
,
rinfo
.
port
,
rinfo
.
address
);
}
package.json
View file @
f75ee542
...
...
@@ -10,6 +10,7 @@
"author"
:
""
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"
radius
"
:
"
^1.1.4
"
"
radius
"
:
"
^1.1.4
"
,
"
redis
"
:
"
^4.6.6
"
}
}
redis.js
0 → 100644
View file @
f75ee542
const
redis
=
require
(
"
redis
"
);
let
client
=
redis
.
createClient
({
url
:
'
redis://default:p@admin.fcfglobal.co:7001
'
});
async
function
init
()
{
client
.
on
(
'
error
'
,
(
err
)
=>
console
.
log
(
'
Redis Client Error
'
,
err
));
await
client
.
connect
();
}
async
function
getAllKeysByUUID
(
uuid
)
{
let
prefix
=
uuid
+
'
_*
'
const
[
keys
]
=
await
client
.
multi
()
.
keys
(
prefix
)
.
exec
();
return
keys
}
async
function
addIp
(
uuid
,
ip
)
{
let
key
=
uuid
+
'
_
'
+
ip
let
ttl
=
1
*
60
return
await
client
.
multi
()
.
incr
(
key
)
.
expire
(
key
,
ttl
)
.
exec
();
}
// test()
//
// async function test() {
// let uuid='1137dd67-45b3-4e8b-b1f5-5bec9caefad1'
// let ip='192.168.1.1'
// await init()
// let d = await getAllKeysByUUID(uuid)
// console.log(d)
// // addIp(uuid,ip)
//
// }
module
.
exports
=
{
getAllKeysByUUID
:
getAllKeysByUUID
,
addIp
:
addIp
}
\ No newline at end of file
test_array.js
0 → 100644
View file @
f75ee542
let
arr
=
[
1
,
2
,
3
]
console
.
log
(
arr
.
length
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment