OPENWRT 教程第二章 Openwrt Luci 初探(WEB)

Stella981
• 阅读 2741

那么,到底什么是Luci呢?先直观的感受一下,打开web浏览器的网关地址,然后出现了一个web登录界面,这个就是Openwrt Luci的应用。

OPENWRT 教程第二章 Openwrt Luci 初探(WEB)

概述:

OpenWRT的web采取的是luci框架, 在luci的官方网站说明了luci是一个MVC架构的框架,是一个单用户框架,
公用的模块放置在*/luci/controller/下面,
各个用户的模块放置在*/luci/controller/下面对应的文件夹里面,
比如admin登录,最终的页面只显示/luci/controller/admin下面的菜单。这样既有效的管理了不同管理员的权限。

基础知识:

Luci = lua + uci
lua : 脚本语言
uci :(Unified Configuration Interface)是Openwrt的配置框架

Openwrt 的 web 服务器: uhttpd

uhttpd:是一个轻量级的web服务器,由于其可以和Openwrt的配置框架UCI结合到一起,因此默认被用于OpenWrt的Web管理接口LuCI。我们都知道,网站都是被部署在一台台服务器,PC等设备上的,我们的设备访问网站时,先是通过网络访问到部署这个网站的服务器,然后服务器的web服务再返回页面给我们;也就是说如果服务器没有web服务,我们是访问不了网页的哦。

说明:

  1. lua单行注释使用“--”,类似于C语言的“//”,多行注释时,“--[[”类似C语言中的“/*”,“]]--”类似C语言中的“*/”

一:luci的目录

contoller:逻辑控制文件(主要是注册页面)

model :业务上的处理

view : 存放 html 文件

controller在luci框架中的作用是逻辑上的组织,编码时主要分为2块

1 模块的注册 :

module("luci.controller.admin.system", package.seeall)        //在luci/controller/admin/下注册一个system模块

2 节点的注册 :表示添加一个新的模块入口

local fs = require "nixio.fs"
entry({"admin", "system"}, alias("admin", "system", "system"), _("System"), 30).index = true
entry({"admin", "system", "system"}, cbi("admin_system/system"), _("System"), 1)
entry({"admin", "system", "clock_status"}, call("action_clock_status"))
entry({"admin", "system", "admin"}, cbi("admin_system/admin"), _("Administration"), 2)
entry({"admin", "system", "reboot"}, call("action_reboot"), _("Reboot"), 90)

函数原型 : entry(path, target, title=nil, order=nil)

path :是访问的路径,路径是按字符串数组给定的,
    比如路径按如下方式写"{"admin", "loogson", "control"}",那么就可以在浏览器里访问"http://192.168.1.1/cgi-bin/luci/admin/loogson/control"来访问这个脚本。
    其中的“admin”表示为管理员添加脚本,"loogson"即为一级菜单名,"control"为菜单项名。系统会自动在对应的菜单中生成菜单项。比如想在"System"菜单下创建一个菜单项,那么一级菜单名可以写为"system"。

target : 为调用目标,调用目标分为三种,分别是执行指定方法(Action)、访问指定页面(Views)以及调用CBI Module。

  call:第一种可以直接调用指定的函数,比如点击菜单项就直接重启路由器等等,比如写为"call("function_name")",然后在该lua文件下编写名为function_name的函数就可以调用了。

  template:第二种可以访问指定的页面,比如写为"template("myapp/mymodule")"就可以调用/usr/lib/lua/luci/view/myapp/mymodule.htm文件了。

  cbi: 第三种主要应用在配置界面,比如写为"cbi("myapp/mymodule")"就可以调用/usr/lib/lua/luci/model/cbi/myapp/mymodule.lua文件了。

  title和order: 是针对管理员菜单的,其中的title即是显示在网页上的内容。这里我们创建"/usr/lib/lua/luci/controller/loogson.lua"文件,定义我们的入口为"loogson"。
  alias :表示连接到其他某个节点

用户管理:
/luci/controller/admin下面的菜单,一共7个文件

root@OpenWrt:/usr/lib/lua/luci/controller/admin# ls -l
-rw-rw-r--    1 root     root           319 Mar 31 07:19 filebrowser.lua
-rw-rw-r--    1 root     root          1140 Mar 31 07:19 index.lua
-rw-rw-r--    1 root     root         11355 Mar 31 07:19 network.lua
-rw-rw-r--    1 root     root          4403 Mar 31 07:19 status.lua
-rw-rw-r--    1 root     root         10235 Mar 31 07:19 system.lua
-rw-rw-r--    1 root     root          1769 Mar 31 07:19 uci.lua

-rw-rw-r--    1 root     root          1167 Mar 31 07:19 servicectl.lua

前面6个来至Feeds/luci/modules/luci-mod-admin-full/luasrc/controller/admin目录,最后一个来自luci-base目录

二:脚本函数

2.1 Map 函数

m = Map("配置文件存储的文件名,不包含路径", "配置页面标题", "配置页面说明")

2.2 

local m, s, o //定义全局变量

2.3 

Section : 创建与配置文件中对应的Section , Section分为两种,NamedSection 和 TypedSection
NamedSection :根据配置文件中的Section名
TypedSection:根据配置文件中的Section类型

s = m:section(TypedSection, "_dummy", "")
s.addremove = false    //不允许增加或删除Section
s.anonymous = true    //设定不显示Section的名称

2.4 定义:

选项:tab 
s:tab("led", translate("Control LED"))

文本框:value
o:value(0, translate("LED0"))
o:value(1, translate("LED1"))
o:value(2, translate("LED2"))

下拉框:ListValue
o = s:taboption("led", ListValue, "lednum", translate("LED NUM:"))

选择框:Flag

三:web 访问流程

 3.1 首先只有当web 服务器起来后,才可能访问到页面,所以首先看一下web 服务器的配置,前文说过,openwrt 的web 服务器使用的是uhttpd

root@OpenWrt:/etc/config# cat uhttpd     #也可以通过uci 命令查看 :uci show uhttpd

config uhttpd 'main'
        list listen_http '0.0.0.0:8080'    #http协议IPV4的监听端口80
        list listen_http '[::]:8080'     #http协议IPV6的监听端口80
        list listen_https '0.0.0.0:443'        #https协议的监听端口为443
        list listen_https '[::]:443'        
        option redirect_https '1'
        option home '/www'                #指定根路径
        option rfc1918_filter '1'
        option max_requests '3'            #最大请求数
        option max_connections '100'    #最大TCP连接数
        option cert '/etc/uhttpd.crt'    #HTTPS连接的证书路径
        option key '/etc/uhttpd.key'    #HTTPS连接的私钥路径
        option cgi_prefix '/cgi-bin'    #cgi脚本的路径,这个路径又是home的相对路径,即/www/cgi-bin
        option script_timeout '60'
        option network_timeout '30'
        option http_keepalive '20'
        option tcp_keepalive '1'
        option ubus_prefix '/ubus'

config cert 'px5g'
        option days '730'
        option bits '1024'
        option country 'ZZ'
        option state 'Somewhere'
        option location ''
        option commonname 'OpenWrt'

3.2 由上面uhttpd的配置来看,当我们通过web的方式访问时,uhttpd会导向"/www"的路径,那么我们来看看"/www"里面有什么。

root@OpenWrt:/www# ls
cgi-bin      index.html   luci-static

root@OpenWrt:/www# cat 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="https://my.oschina.net/cgi-bin/luci">LuCI - Lua Configuration Interface</a>
</body>
</html>
root@OpenWrt:/www#

  从 index.html 可以看到这个内容“href="https://my.oschina.net/cgi-bin/luci”,原来是这里把网关导向了“/cgi-bin/luci”;那么我们再来看看这个路径里面又有什么?

root@OpenWrt:/www/cgi-bin# ls         #这里有个luci的脚本
luci

root@OpenWrt:/www/cgi-bin# cat luci 
#!/usr/bin/lua
require "luci.cacheloader"
require "luci.sgi.cgi"
luci.dispatcher.indexcache = "/tmp/luci-indexcache"
luci.sgi.cgi.run()

root@OpenWrt:/www/cgi-bin#

  这个路径下面放着一个lua的脚本,脚本里面调用了这个接口“luci.sgi.cgi.run()”,那么这个接口执行的函数在哪里呢,在这里:

root@OpenWrt:/usr/lib/lua/luci/sgi# ls
cgi.lua     uhttpd.lua

root@OpenWrt:/usr/lib/lua/luci/sgi# cat cgi.lua 
-- Copyright 2008 Steven Barth <steven@midlink.org>
-- Licensed to the public under the Apache License 2.0.

exectime = os.clock()
module("luci.sgi.cgi", package.seeall)
local ltn12 = require("luci.ltn12")
require("nixio.util")
require("luci.http")
require("luci.sys")
require("luci.dispatcher")

-- Limited source to avoid endless blocking
local function limitsource(handle, limit)
        limit = limit or 0
        local BLOCKSIZE = ltn12.BLOCKSIZE

        return function()
                if limit < 1 then
                        handle:close()
                        return nil
                else
                        local read = (limit > BLOCKSIZE) and BLOCKSIZE or limit
                        limit = limit - read

                        local chunk = handle:read(read)
                        if not chunk then handle:close() end
                        return chunk
                end
        end
end

function run()
        local r = luci.http.Request(
                luci.sys.getenv(),
                limitsource(io.stdin, tonumber(luci.sys.getenv("CONTENT_LENGTH"))),
                ltn12.sink.file(io.stderr)
        )

        local x = coroutine.create(luci.dispatcher.httpdispatch)
        local hcache = ""
        local active = true

        while coroutine.status(x) ~= "dead" do
                local res, id, data1, data2 = coroutine.resume(x, r)

                if not res then
                        print("Status: 500 Internal Server Error")
                        print("Content-Type: text/plain\n")
                        print(id)
                        break;
                end

                if active then
                        if id == 1 then
                                io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
                        elseif id == 2 then
                                hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
                        elseif id == 3 then
                                io.write(hcache)
                                io.write("\r\n")
                        elseif id == 4 then
                                io.write(tostring(data1 or ""))
                        elseif id == 5 then
                                io.flush()
                                io.close()
                                active = false
                        elseif id == 6 then
                                data1:copyz(nixio.stdout, data2)
                                data1:close()
                        end
                end
        end
end
root@OpenWrt:/usr/lib/lua/luci/sgi#

现在我们可以初步了解到,lua语言就是这样被Luci使用的。

那么我们再整体看一下整个访问流程:

web(输入网关地址)==> uhttpd调用"/www/index.html" ==> index.html重定向到"/cgi-bin/luci" ==> luci被启动。
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Java修道之路,问鼎巅峰,我辈代码修仙法力齐天
<center<fontcolor00FF7Fsize5face"黑体"代码尽头谁为峰,一见秃头道成空。</font<center<fontcolor00FF00size5face"黑体"编程修真路破折,一步一劫渡飞升。</font众所周知,编程修真有八大境界:1.Javase练气筑基2.数据库结丹3.web前端元婴4.Jav
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这