openwrt luci web解析
阿新 • • 發佈:2019-01-27
路由器: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" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns ="http://www.w3.org/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 0
local a=a.BLOCKSIZE
return function()
if e<1 then
t:close()
return nil
else
local a=(e>a)and a or e
e=e-a
local e=t:read(a)
if not e then t:close()end
return e
end
end
end
function 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=true
while coroutine.status(e)~="dead"do
local n,e,t,a=coroutine.resume(e,t)
if not n then
print("Status: 500 Internal Server Error")
print("Content-Type: text/plain\n")
print(e)
break;
end
if i then
if e==1 then
io.write("Status: "..tostring(t).." "..a.."\r\n")
elseif e==2 then
o=o..t..": "..a.."\r\n"
elseif e==3 then
io.write(o)
io.write("\r\n")
elseif e==4 then
io.write(tostring(t or""))
elseif e==5 then
io.flush()
io.close()
i=false
elseif e==6 then
t:copyz(nixio.stdout,a)
t:close()
end
end
end
end
連線路由器,瀏覽器下輸入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 then
root.target = alias("admin")
root.index = true
end
local page = node("admin")
page.target = firstchild() //從dispatcher.lua調firstchild()
page.title = _("Administration")
page.order = 10
page.sysauth = "root"
page.sysauth_authenticator = "htmlauth" //從dispatcher.lua調htmlauth()
page.ucidata = true
page.index = true
-- Empty services menu to be populated by addons
entry({"admin", "services"}, firstchild(), _("Services"), 40).index = true
entry({"admin", "logout"}, call("action_logout"), _("Logout"), 90)
end
function action_logout ()
local dsp = require "luci.dispatcher"
local utl = require "luci.util"
local sid = dsp.context.authsession
if sid then
utl.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()
})
end
luci.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)then
return t
end
require("luci.i18n")
require("luci.template")
context.path={}
e.status(403,"Forbidden")
luci.template.render("sysauth",{duser=o,fuser=t})
return false
end
在dispatcher.lua讀取並解析了/etc/config下的luci配置,可以看到讀取了theme:
if(t and t.index)or not i.notemplate then
local s=require("luci.template")
local t=i.mediaurlbase or luci.config.main.mediaurlbase
if not pcall(s.Template,"themes/%s/header"%n.basename(t))then
t=nil
for a,e in pairs(luci.config.themes)do
if a:sub(1,1)~="."and pcall(s.Template,
"themes/%s/header"%n.basename(e))then
t=e
end
使用了/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 then
root.target = alias("lafite")
root.index = true
end
function jump ()
local uci = require("luci.model.uci").cursor()
local themename = uci:get('luci', 'main', 'mediaurlbase') or nil
if not themename then
return template("jump")
end
themename = themename:match('([%da-zA-Z]*)$')
if themename == 'lafite' then
return template("jump")
else
local list = {}
for _, v in ipairs(require('luci.fs').dir('/www/luci-static')) do
if v ~= '.' and v ~= '..' then
list[#list+1] = v
end
end
if #list > 2 then
return alias("admin")
end
end
return template("jump")
end
local page = entry({"lafite"}, jump(), nil, 0)
page.sysauth = false
page.ucidata = true
page.index = true
entry({"lafite", "change"}, call("action_change"), _("index"), 0)
end
function 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 end
local V = PBUtil.getFormValue({ 'change' })
if not V.change then
return code.miss('Change+')
end
if V.change ~= '0' and V.change ~= '1' then
return code.error('Change+')
end
local themename = 'lafite'
if V.change == '1' then
local list = {}
for _, v in ipairs(require('luci.fs').dir('/www/luci-static')) do
if v ~= '.' and v ~= '..' then
list[#list+1] = v
end
end
if #list > 2 then
for _, v in ipairs(list) do
if v ~= 'lafite' and v ~= 'resources' then
themename = v
end
end
end
end
PBUtil.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/目錄下檔案