radius模块地址:https://www.npmjs.com/package/radius
1.radius模块中的实例
var radius = require('radius');
var dgram = require("dgram");
var secret = 'radius_secret';
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
var code, username, password, packet;
packet = radius.decode({packet: msg, secret: secret});
if (packet.code != 'Access-Request') {
console.log('unknown packet type: ', packet.code);
return;
}
username = packet.attributes['User-Name'];
password = packet.attributes['User-Password'];
console.log('Access-Request for ' + username);
if (username == 'jlpicard' && password == 'beverly123') {
code = 'Access-Accept';
} else {
code = 'Access-Reject';
}
var response = radius.encode_response({
packet: packet,
code: code,
secret: secret
});
console.log('Sending ' + code + ' for user ' + username);
server.send(response, 0, response.length, rinfo.port, rinfo.address, function(err, bytes) {
if (err) {
console.log('Error sending response to ', rinfo);
}
});
});
server.on("listening", function () {
var address = server.address();
console.log("radius server listening " +
address.address + ":" + address.port);
});
//监听1812端口
server.bind(1812);
2.在虚拟机上输入登录命令
echo "User-Name=reco,User-Password=rita" |radclient 127.0.0.1:1812 auth sharedkey -x
127.0.0.1换成你本机的ip地址就行
代码运行的时候可以看到你登录的用户名和密码
3.在虚拟机上输入开始命令(acct start)
首先把radius库的监听端口改为1813
然后在你的Linux桌面创建一个start.txt文件
再在start.txt文件里写入以下内容:
Packet-Type=4
Packet-Dst-Port=1813
Acct-Session-Id = "1"
Acct-Status-Type = Start
Acct-Authentic = RADIUS
User-Name = "reco"
NAS-Port = 0
Called-Station-Id = "mac1"
Calling-Station-Id = "mac2"
NAS-Port-Type = Wireless-802.11
Connect-Info = "CONNECT 48Mbps 802.11b"
然后在linux上输入以下内容:
radclient 127.0.0.1 auto sharedkey -f start.txt
127.0.0.1换成你本机的ip地址就行
代码运行的时候就可以看到start.txt里面的东西了
4.同理,停止(acct stop)和更新(acct update)一样操作
创建一个stop.txt文件
start.txt文件里写入以下内容:
Packet-Type=4
Packet-Dst-Port=1813
Acct-Session-Id = "1"
Acct-Status-Type = Stop
Acct-Authentic = RADIUS
User-Name = "reco"
NAS-Port = 0
Called-Station-Id = "mac1"
Calling-Station-Id = "mac2"
NAS-Port-Type = Wireless-802.11
Connect-Info = "CONNECT 48Mbps 802.11b"
Acct-Session-Time = 30
Acct-Input-Packets = 25
Acct-Output-Packets = 7
Acct-Input-Octets = 3407
Acct-Output-Octets = 867
Acct-Terminate-Cause = User-Request
然后在linux上输入以下内容:
radclient 127.0.0.1 auto sharedkey -f stop.txt
127.0.0.1换成你本机的ip地址就行
创建一个update.txt文件
update.txt文件里写入以下内容:
Packet-Type=4
Packet-Dst-Port=1813
Acct-Session-Id = "1"
Acct-Status-Type = Interim-Update
Acct-Authentic = RADIUS
User-Name = "reco"
NAS-Port = 0
Called-Station-Id = "mac1"
Calling-Station-Id = "mac2"
NAS-Port-Type = Wireless-802.11
Connect-Info = "CONNECT 48Mbps 802.11b"
Acct-Session-Time = 11
Acct-Input-Packets = 15
Acct-Output-Packets = 3
Acct-Input-Octets = 1407
Acct-Output-Octets = 467
然后在linux上输入以下内容:
radclient 127.0.0.1 auto sharedkey -f update.txt
127.0.0.1换成你本机的ip地址就行