900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > openwrt luci web解析

openwrt luci web解析

时间:2022-03-27 18:30:41

相关推荐

openwrt luci web解析

路由器:newifi mini

硬件信息:mt7620a +mt7612e+128M DDR+16M flash

固件: Pandorabox

luci theme:lafite

主界面:

winSCP登录

securieCRT 串口登录

/www/index.html:

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="/1999/xhtml"><head><meta http-equiv="Cache-Control" content="no-cache" /><meta http-equiv="refresh" content="0; URL=/cgi-bin/luci" /></head><body style="background-color: white"><a style="color: black; font-family: arial, helvetica, sans-serif;" href="/cgi-bin/luci">LuCI for PandoraBox</a></body></html>

在 uhttpd web server 中的 cgi-bin 目录下,运行 luci 文件(权限一般是 755 ) , luci 的代码如下:

#!/usr/bin/lua #lua脚本,lua执行命令的路径require "luci.cacheloader"#导入 cacheloader 包require "luci.sgi.cgi" # 导入 sgi.cgi 包luci.dispatcher.indexcache = "/tmp/luci-indexcache"#cache 缓存路径地址luci.sgi.cgi.run() #执行run方法,此方法位于 /usr/lib/lua/luci/sgi/cgi.lua中

找到lcgi.lua运行文件, /usr/lib/lua/luci/sgi/cgi.lua :

exectime=os.clock()module("luci.sgi.cgi",package.seeall)local a=require("luci.ltn12")require("nixio.util")require("luci.http")require("luci.sys")require("luci.dispatcher")local function o(t,e)e=e or 0local a=a.BLOCKSIZEreturn function()if e<1 thent:close()return nilelselocal a=(e>a)and a or ee=e-alocal e=t:read(a)if not e then t:close()endreturn eendendendfunction run()local t=luci.http.Request(luci.sys.getenv(),o(io.stdin,tonumber(luci.sys.getenv("CONTENT_LENGTH"))),a.sink.file(io.stderr))local e=coroutine.create(luci.dispatcher.httpdispatch) //开启协助线程---->调用/usr/lib/lua/luci/dispatcher.lua里的httpdispatch函数local o=""local i=truewhile coroutine.status(e)~="dead"dolocal n,e,t,a=coroutine.resume(e,t)if not n thenprint("Status: 500 Internal Server Error")print("Content-Type: text/plain\n")print(e)break;endif i thenif e==1 thenio.write("Status: "..tostring(t).." "..a.."\r\n")elseif e==2 theno=o..t..": "..a.."\r\n"elseif e==3 thenio.write(o)io.write("\r\n")elseif e==4 thenio.write(tostring(t or""))elseif e==5 thenio.flush()io.close()i=falseelseif e==6 thent:copyz(nixio.stdout,a)t:close()endendendend

连接路由器,浏览器下输入192.168.1.1,转入192.168.1.1/cgi-bin/luci,转入登录页面,调用了/usr/lua/luci/controller/admin/index.html文件:

module("luci.controller.admin.index", package.seeall)function index ()local root = node()if not root.target thenroot.target = alias("admin")root.index = trueendlocal page = node("admin")page.target= firstchild() //从dispatcher.lua调firstchild()page.title = _("Administration")page.order = 10page.sysauth= "root"page.sysauth_authenticator = "htmlauth"//从dispatcher.lua调htmlauth()page.ucidata= truepage.index = true-- Empty services menu to be populated by addonsentry({"admin", "services"}, firstchild(), _("Services"), 40).index = trueentry({"admin", "logout"}, call("action_logout"), _("Logout"), 90)endfunction action_logout ()local dsp = require "luci.dispatcher"local utl = require "luci.util"local sid = dsp.context.authsessionif sid thenutl.ubus("session", "destroy", { ubus_rpc_session = sid })luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s/" %{sid, 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()})endluci.http.redirect(dsp.build_url())end

查看htmlauth()函数:

function authenticator.htmlauth(a,t,o)local t=e.formvalue("luci_username")local i=e.formvalue("luci_password")if t and a(t,i)thenreturn tendrequire("luci.i18n")require("luci.template")context.path={}e.status(403,"Forbidden")luci.template.render("sysauth",{duser=o,fuser=t})return falseend

在dispatcher.lua读取并解析了/etc/config下的luci配置,可以看到读取了theme:

if(t and t.index)or not i.notemplate thenlocal s=require("luci.template")local t=i.mediaurlbase or luci.config.main.mediaurlbaseif not pcall(s.Template,"themes/%s/header"%n.basename(t))thent=nilfor a,e in pairs(luci.config.themes)doif a:sub(1,1)~="."and pcall(s.Template,"themes/%s/header"%n.basename(e))thent=eend

使用了/usr/lib/lua/luci/viem/theme/lafite

查看/usr/lib/lua/luci/controller/admin/lafite.lua :

module("luci.controller.admin.lafite", package.seeall)function index()local root = node()if not root.target thenroot.target = alias("lafite")root.index = trueendfunction jump ()local uci = require("luci.model.uci").cursor()local themename = uci:get('luci', 'main', 'mediaurlbase') or nilif not themename thenreturn template("jump")endthemename = themename:match('([%da-zA-Z]*)$')if themename == 'lafite' thenreturn template("jump")elselocal list = {}for _, v in ipairs(require('luci.fs').dir('/www/luci-static')) doif v ~= '.' and v ~= '..' thenlist[#list+1] = vendendif #list > 2 thenreturn alias("admin")endendreturn template("jump")endlocal page = entry({"lafite"}, jump(), nil, 0)page.sysauth = falsepage.ucidata = truepage.index = trueentry({"lafite", "change"}, call("action_change"), _("index"), 0)endfunction action_change()local sauth = require "luci.controller.api.user"local PBUtil = require "luci.pb.PBUtil"local code = PBUtil.message()local _apiAuth = sauth.checkApiAuth()if _apiAuth then return _apiAuth endlocal V = PBUtil.getFormValue({ 'change' })if not V.change thenreturn code.miss('Change+')endif V.change ~= '0' and V.change ~= '1' thenreturn code.error('Change+')endlocal themename = 'lafite'if V.change == '1' thenlocal list = {}for _, v in ipairs(require('luci.fs').dir('/www/luci-static')) doif v ~= '.' and v ~= '..' thenlist[#list+1] = vendendif #list > 2 thenfor _, v in ipairs(list) doif v ~= 'lafite' and v ~= 'resources' thenthemename = vendendendendPBUtil.setUCISection('luci', 'main', {mediaurlbase = '/luci-static/' .. themename})uci:commit('luci')return code.ok()end

Luci则会calling /luci/admin/目录下的lafite.lua脚本。

模块入口文件lafite.lu中index()函数中,使用entry函数来完成每个模块函数的注册:

local page = entry({"lafite"}, jump(), nil, 0)

转入/www/web/

entry({"lafite", "change"}, call("action_change"), _("index"), 0)

change函数中加入了/usr/lib/lua/luci/pb/目录下文件

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。