swoole学习笔记(三)-UDP radius协议实现
2024年03月18日 · 阅读需 14 分钟又开新坑-swoole 作为 radius 服务器,huanlphp 写业务 php-radius 主要还是为了学习 swoole 和实验我的框架,所以这里记录一下 radius 协议的结构和使用(原来用 python 实现过一次,容易崩溃还写得垃圾),文章中只写了 auth,没有写 account 的记录,openvpn 需要 auth 和 account 才能实现连接成功,可以看我完整的实现python 实现
协议&工具
测试工具我用的 NTRadPing:http://www.winsite.com/internet/server-tools/ntradping/

结构
pack fromat
关于计费和验证的格式都是一样的
验证:https://tools.ietf.org/html/rfc2865#page-13
计费:https://tools.ietf.org/html/rfc2866#page-5
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Code | Identifier | Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Authenticator |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Attributes ...
+-+-+-+-+-+-+-+-+-+-+-+-+-
看得有些隐晦,我将它转换成 c 的 struct 结构吧 char 1 个字节,8bit short 两个字节,16bit
struct radius_format{
char code;//包类型,请求类型根据这个判断
char identifier;//鉴定码,服务器和客户端这个值需要一样
short lenght; //本次数据包的总长度(20-4096)
char authenticator[16];//数据hash,用来校验数据是否正确,一个数据的md5码
};
在响应的时候 authenticator 的计算方式为:MD5(Code+ID+Length+RequestAuth+Attributes+Secret) 在计算中的RequestAuth为请求的时候的 authenticator,Secret记笔记,是双方的一个 key
后面的 Attributes 长度不一定(length-20),里面包含着 用户/NAS 的一些信息,比如账号,密码,ip,之类的,格式看后面,这里也可以用来扩展自己的协议,带上自己的一些奇奇怪怪的值- -
code 意义
主要就是前面 5 个了
RADIUS Codes (decimal) are assigned as follows:
1 Access-Request //验证请求,一般由客户端使用
2 Access-Accept //验证通过,一般由服务端处理返回
3 Access-Reject //验证拒绝,一般由服务端处理返回
4 Accounting-Request //计费请求,客户端发起
5 Accounting-Response //计费返回,服务器发起
11 Access-Challeng //服务端主动请求再次验证,客户端要求必须返回
12 Status-Server (experimental)//服务器状态???没看到说明
13 Status-Client (experimental)//同上
255 Reserved//保留
Attributes
https://tools.ietf.org/html/rfc2865#page-22
0 1 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
| Type | Length | Value ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
struct radius_attr{
char type;//类型,有不少...
char length;//type+length+value的长度
}