首先简单介绍一下什么是Portal认证,Portal认证,通常也会叫Web认证,未认证用户上网时,设备强制用户登录到特定站点,用户可以免费访问其中的服务。当用户需要使用互联网中的其它信息时,必须在门户网站进行认证,只有认证通过后才可以使用互联网资源。现金很多中国移动CMCC、中国联通、中国电信ChinaNet的WIFI都使用这种认证接入方式。
在OpenWRT上实现Portal认证,实际上早已有解决方案:
1. chillispot,但原维护作者停止更新,被chillispot.info接管继续开发;
2.coova-chilli,它是基于chillispot开发拓展的,功能最为强大;可以去官方看一下Coova-chilli;
3.wifidog
前两个由于原维护作者停止更新,笔者也没有深入研究,重点钻研了wifidog,Wifidog也是OpenWRT和DD-WRT中实现Portal比较出名的。
但是,Wifidog只是实现AP认证网关,需要配合外部的Portal服务器才能使用,Portal主要是提供认证所需的WEB页面且实现认证计费等的功能。虽然这也有很多商用解决方案,例如wiwiz、wifiap等,但是这些商业解决方案的目标都是盈利,即使可以免费使用,免费账号的功能和权限都受到了很大的限制,例如不能自定义页面,Web认证页面有广告等等。有条件的人可能打算自己搭建Portal服务器,但是看看Wifidog的官方Wiki,对搭建过程实在是难以理解。后来,笔者发现网络上还有一个authpuppy方案,官方网站www.authpuppy.org,是一个已实现好的Wifidog认证服务器,里面包含各种插件供你使用,官方的安装过程也很简单,如果你懂的HTML和面向对象编程的相关知识且拥有一个服务器,可以自行修改认证页面,使用authpuppy也是一个不错的方案。
但是,即便如此,这些方案还是不够灵活,经过笔者认真钻研,查阅大量资料并经过多次抓包分析,终于理解了Wifidog的工作原理。接下来笔者将会跟你介绍如何自行编写一个轻量级的Web Portal认证服务器。当然,这需要你具有程序设计基础,HTML、CSS当然是少不得的,后端开发语言可以使用PHP或Python或Java等。
首先,需要简单介绍一下Wifidog的工作原理:
1.客户端发出初始化请求,比如访问 www.baidu.com。
2.网关的防火墙规则将这个请求重定向到本地网关的端口上。这个端口是Wifidog监听的端口。
3.Wfidog提供一个HTTP重定向回复,重定向到Web认证页面,重定向的Url的Querystring中包含了Gateway的ID,Gateway的FQDN以及其他的信息。
4.用户向认证服务器发出认证请求
http://portal_server:port/login_script?
gw_id=[GatewayID, default: “default”]
gw_address=[GatewayAddress, internal IP of router]
gw_port=[GatewayPort, port that wifidog Gateway is listening on]
url=[user requested url];
5.网关返回一个(可以是自定义的)splash(也称作“登录”)页面。
6.用户提供他的凭据信息,比如用户名和密码。
7.成功认证的话,客户端将会被重定向到网关的自己的web页面上,并且带有一个认证凭据(一个一次性的token),内容比如:
http://GatewayIP:GatewayPort/wifidog/auth?token=[auth token];
8.用户就是用获取到的凭据访问网关。
9.网关去认证服务器询问token的有效性。
10.认证服务器确认token的有效性。
11.网关发送重定向给客户端,以从认证服务器上获取 成功提示页面,重定向到 http://portal_server:port/portal_script 这个位置。
12.认证服务器通知客户请求成功,可以上网了。
图解:
然后考察一下Wifidog的配置文件/etc/wifidog.conf,关键的配置项是:
AuthServer {
Hostname (Mandatory; Default: NONE) SSLAvailable (Optional; Default: no; Possible values: yes, no) SSLPort (Optional; Default: 443) HTTPPort (Optional; Default: 80) Path (Optional; Default: /wifidog/ Note: The path must be both prefixed and suffixed by /. Use a single / for server root.) LoginScriptPathFragment (Optional; Default: login/? Note: This is the script the user will be sent to for login.) PortalScriptPathFragment (Optional; Default: portal/? Note: This is the script the user will be sent to after a successfull login.) MsgScriptPathFragment (Optional; Default: gw_message.php? Note: This is the script the user will be sent to upon error to read a readable message.) PingScriptPathFragment (Optional; Default: ping/? Note: This is the script the user will be sent to upon error to read a readable message.) AuthScriptPathFragment (Optional; Default: auth/? Note: This is the script the user will be sent to upon error to read a readable message.) } # Listen on this port GatewayPort 2060 # Parameter: CheckInterval # Default: 60 # Optional # # How many seconds should we wait between timeout checks. This is also # how often the gateway will ping the auth server and how often it will # update the traffic counters on the auth server. Setting this too low # wastes bandwidth, setting this too high will cause the gateway to take # a long time to switch to it's backup auth server(s). CheckInterval 60 # Parameter: ClientTimeout # Default: 5 # Optional # # Set this to the desired of number of CheckInterval of inactivity before a client is logged out # The timeout will be INTERVAL * TIMEOUT ClientTimeout 5