luci实现的wifidog认证服务

利用luci写了个wifidog认证服务,实现直接openwrt路由器本地认证。直接安装ipk安装包,然后修改/etc/wifidog.conf文件(需要先安装wifidog),如下:

AuthServer {
	Hostname 192.168.1.1
	Path /cgi-bin/luci/wdas/
	MsgScriptPathFragment gw_message/?
}


主要的源码如下:

--[[
wdas -- wifidog auth server
mail: xzm2@qq.com
QQ: 529698939
]]--
module("luci.controller.wifidog.wdas", package.seeall)

local session = require "luci.wifidogsession"
local http = luci.http
local translate = luci.i18n.translate
local wdcfg = require("luci.wifidogconfig")

function index()
	local page    = node("wdas")
	page.target   = alias("wdas", "login")
	page.order    = 90
	page.i18n = "wifidogauth"
	page.setuser  = "nobody"
	page.setgroup = "nogroup"

	entry({"wdas", "login"}, call("login"))
	entry({"wdas", "logincheck"}, call("logincheck"))
	entry({"wdas", "auth"}, call("auth"))
	entry({"wdas", "ping"}, call("ping"))
	entry({"wdas", "portal"}, call("portal"))
	entry({"wdas", "gw_message"}, call("gw_message"))
end

function login()
	--login/?gw_id=&gw_address=&gw_port=&mac=&url=
	luci.template.render("wifidog/wdas_login")
end

function logincheck()
	local username, password = wdcfg.auth.username, wdcfg.auth.password
	local user, pwd, id, addr, port, mac, url, token
	user = http.formvalue("user")
	pwd = http.formvalue("pwd")
	id = http.formvalue("gw_id")
	addr = http.formvalue("gw_address")
	port = http.formvalue("gw_port")
	mac = http.formvalue("mac")
	url = http.formvalue("url")
	token = http.getcookie("wdastok")
	http.prepare_content("application/json")
	if addr and port and mac and (user == username) and (pwd == password) then
		token = token and token:match("^[a-f0-9]*$") or luci.sys.uniqueid(16)
		local sdt = {id=id, addr=addr, port=port, mac=mac, url=url, timestamp=luci.sys.uptime()}
		local path = (http.getenv("SCRIPT_NAME") or "") .. "/wdas"
		session.write(token, sdt)
		http.header("Set-Cookie", "wdastok=" .. token .. "; path=" .. path)
		http.write('{url:"http://' .. addr .. ':' .. port ..
			'/wifidog/auth?token=' .. token .. '"}')
	else
		if addr and port and mac then
			http.write('{error: "' .. translate("Invalid username or password.") .. '"}')
		else
			http.write('{error: "' .. translate("Invalid parameter.") .. '"}')
		end
	end
end

function auth()
	--auth/?stage=&ip=&mac=&token=&incoming=&outgoing=
	local stage, ip, mac, token, incoming, outgoing
	stage = http.formvalue("stage")
	ip = http.formvalue("ip")
	mac = http.formvalue("mac")
	token = http.formvalue("token")
	incoming = http.formvalue("incoming")
	outgoing = http.formvalue("outgoing")
	token = token and token:match("^[a-f0-9]*$")
	local sdt = token and session.read(token)
	if token and sdt and (mac == sdt.mac) then
		http.write("Auth: 1")
	else
		http.write("Auth: 0")
	end
end

function ping()
	--ping/?gw_id=&sys_load=&sys_memfree=&sys_load=&wifidog_uptime=
	local id, sys_uptime, sys_memfree, sys_load, wifidog_uptime
	id = http.formvalue("gw_id")
	sys_uptime = http.formvalue("sys_uptime")
	sys_memfree = http.formvalue("sys_memfree")
	sys_load = http.formvalue("sys_load")
	wifidog_uptime = http.formvalue("wifidog_uptime")
	if id and sys_uptime and sys_memfree and sys_load and wifidog_uptime then
		http.write("Pong")
	else
		http.write("{error:2}")
	end
end

function portal()
	--portal/?gw_id=%s
	local token, sdt, url
	token = http.getcookie("wdastok")
	sdt = token and session.read(token)
	url = sdt and sdt.url or "http://www.baidu.com"
	http.redirect(url)
end

function gw_message()
	local msg = http.formvalue("message")
	http.write(msg)
end


源码编译说明:

modules目录下的wifidogauth目录放到./feeds/luci/modules/下。

po/zh_CN/wifidogauth.po文件放到./feeds/luci/po/zh_CN/下。


./feeds/luci/contrib/package/luci/Makefile增加如下语句:

$(eval $(call module,wifidogauth,wifidog auth server,+luci-base))


然后 make menuconfig在luci-->Modules下找到luci-mod-wifidogauth选上。


make package/feeds/luci/luci/compile V=s


认证的用户名、密码的配置文件路径/etc/config/wifidogauth

效果图:

105914_f2Op_82631.jpg

安装包下载地址:luci-mod-wifidogauth_0.12.ipk

完整源码下载地址:luci-mod-wifidogauth.tar.gz

转载于:https://my.oschina.net/osbin/blog/360779

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LuCI是Openwrt中的一个项目,它是由UCI(统一配置接口)和Lua语言实现的Openwrt网页系统配置接口。具体来说,LuCI是将UCI和Lua合并在一起,用于实现路由器的网页配置界面。UCI是Openwrt中用于实现系统配置的统一接口,而Lua是一种轻量级的解释性脚本语言。通过LuCI,开发者可以使用简单的Lua脚本来开发新的功能页面,而剩下的部分则由Openwrt自动完成。这种开发模式遵循MVC(模型-视图-控制器)框架。总之,LuCI是Openwrt中一种方便的方式,用于通过网页界面进行系统配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Openwrt:LuCI入门(一)](https://blog.csdn.net/qq_28812525/article/details/103870169)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [【OpenWrt】(Luci)OpenWrt Web GUI 开发之 Luci 框架粗解](https://blog.csdn.net/qq_29757283/article/details/101056612)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值