当前位置:   article > 正文

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

openwrt web

 

转自:Openwrt Luci 初探(WEB) 这博客博文题目看起来挺有意思的,不过博文加密了;

追溯到原文是:【玩转开源】BananaPi R2 —— 第四篇 Openwrt Luci 初探 这个博客也很有意思,看下来很有收获


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

 

 

 

概述:

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 节点的注册 :表示添加一个新的模块入口

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

  

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

 

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

  

target : 为调用目标,调用目标分为三种,分别是执行指定方法(Action)、访问指定页面(Views)以及调用CBI Module。
  1.   call:第一种可以直接调用指定的函数,比如点击菜单项就直接重启路由器等等,比如写为"call("function_name")",然后在该lua文件下编写名为function_name的函数就可以调用了。
  2.   template:第二种可以访问指定的页面,比如写为"template("myapp/mymodule")"就可以调用/usr/lib/lua/luci/view/myapp/mymodule.htm文件了。
  3.   cbi: 第三种主要应用在配置界面,比如写为"cbi("myapp/mymodule")"就可以调用/usr/lib/lua/luci/model/cbi/myapp/mymodule.lua文件了。
  4.   title和order: 是针对管理员菜单的,其中的title即是显示在网页上的内容。这里我们创建"/usr/lib/lua/luci/controller/loogson.lua"文件,定义我们的入口为"loogson"
  5.   alias :表示连接到其他某个节点

 

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

  1. root@OpenWrt:/usr/lib/lua/luci/controller/admin# ls -l
  2. -rw-rw-r-- 1 root root 319 Mar 31 07:19 filebrowser.lua
  3. -rw-rw-r-- 1 root root 1140 Mar 31 07:19 index.lua
  4. -rw-rw-r-- 1 root root 11355 Mar 31 07:19 network.lua
  5. -rw-rw-r-- 1 root root 4403 Mar 31 07:19 status.lua
  6. -rw-rw-r-- 1 root root 10235 Mar 31 07:19 system.lua
  7. -rw-rw-r-- 1 root root 1769 Mar 31 07:19 uci.lua
  8. -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 

  1. Section : 创建与配置文件中对应的SectionSection分为两种,NamedSectionTypedSection
  2. NamedSection :根据配置文件中的Section
  3. TypedSection:根据配置文件中的Section类型

 

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

2.4 定义:

  1. 选项:tab
  2. s:tab("led", translate("Control LED"))
  3. 文本框:value
  4. o:value(0, translate("LED0"))
  5. o:value(1, translate("LED1"))
  6. o:value(2, translate("LED2"))
  7. 下拉框:ListValue
  8. o = s:taboption("led", ListValue, "lednum", translate("LED NUM:"))
  9. 选择框:Flag

 

三:web 访问流程

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

  1. root@OpenWrt:/etc/config# cat uhttpd #也可以通过uci 命令查看 :uci show uhttpd
  2. config uhttpd 'main'
  3. list listen_http '0.0.0.0:8080' #http协议IPV4的监听端口80
  4. list listen_http '[::]:8080' #http协议IPV6的监听端口80
  5. list listen_https '0.0.0.0:443' #https协议的监听端口为443
  6. list listen_https '[::]:443'
  7. option redirect_https '1'
  8. option home '/www' #指定根路径
  9. option rfc1918_filter '1'
  10. option max_requests '3' #最大请求数
  11. option max_connections '100' #最大TCP连接数
  12. option cert '/etc/uhttpd.crt' #HTTPS连接的证书路径
  13. option key '/etc/uhttpd.key' #HTTPS连接的私钥路径
  14. option cgi_prefix '/cgi-bin' #cgi脚本的路径,这个路径又是home的相对路径,即/www/cgi-bin
  15. option script_timeout '60'
  16. option network_timeout '30'
  17. option http_keepalive '20'
  18. option tcp_keepalive '1'
  19. option ubus_prefix '/ubus'
  20. config cert 'px5g'
  21. option days '730'
  22. option bits '1024'
  23. option country 'ZZ'
  24. option state 'Somewhere'
  25. option location ''
  26. option commonname 'OpenWrt'

 

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

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

  

  1. root@OpenWrt:/www# cat index.html
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <meta http-equiv="Cache-Control" content="no-cache" />
  7. <meta http-equiv="refresh" content="0; URL=/cgi-bin/luci" />
  8. </head>
  9. <body style="background-color: white">
  10. <a style="color: black; font-family: arial, helvetica, sans-serif;" href="/cgi-bin/luci">LuCI - Lua Configuration Interface</a>
  11. </body>
  12. </html>
  13. root@OpenWrt:/www#

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

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

  

  1. root@OpenWrt:/www/cgi-bin# cat luci
  2. #!/usr/bin/lua
  3. require "luci.cacheloader"
  4. require "luci.sgi.cgi"
  5. luci.dispatcher.indexcache = "/tmp/luci-indexcache"
  6. luci.sgi.cgi.run()
  7. root@OpenWrt:/www/cgi-bin#

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

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

  

  1. root@OpenWrt:/usr/lib/lua/luci/sgi# cat cgi.lua
  2. -- Copyright 2008 Steven Barth <steven@midlink.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. exectime = os.clock()
  5. module("luci.sgi.cgi", package.seeall)
  6. local ltn12 = require("luci.ltn12")
  7. require("nixio.util")
  8. require("luci.http")
  9. require("luci.sys")
  10. require("luci.dispatcher")
  11. -- Limited source to avoid endless blocking
  12. local function limitsource(handle, limit)
  13. limit = limit or 0
  14. local BLOCKSIZE = ltn12.BLOCKSIZE
  15. return function()
  16. if limit < 1 then
  17. handle:close()
  18. return nil
  19. else
  20. local read = (limit > BLOCKSIZE) and BLOCKSIZE or limit
  21. limit = limit - read
  22. local chunk = handle:read(read)
  23. if not chunk then handle:close() end
  24. return chunk
  25. end
  26. end
  27. end
  28. function run()
  29. local r = luci.http.Request(
  30. luci.sys.getenv(),
  31. limitsource(io.stdin, tonumber(luci.sys.getenv("CONTENT_LENGTH"))),
  32. ltn12.sink.file(io.stderr)
  33. )
  34. local x = coroutine.create(luci.dispatcher.httpdispatch)
  35. local hcache = ""
  36. local active = true
  37. while coroutine.status(x) ~= "dead" do
  38. local res, id, data1, data2 = coroutine.resume(x, r)
  39. if not res then
  40. print("Status: 500 Internal Server Error")
  41. print("Content-Type: text/plain\n")
  42. print(id)
  43. break;
  44. end
  45. if active then
  46. if id == 1 then
  47. io.write("Status: " .. tostring(data1) .. " " .. data2 .. "\r\n")
  48. elseif id == 2 then
  49. hcache = hcache .. data1 .. ": " .. data2 .. "\r\n"
  50. elseif id == 3 then
  51. io.write(hcache)
  52. io.write("\r\n")
  53. elseif id == 4 then
  54. io.write(tostring(data1 or ""))
  55. elseif id == 5 then
  56. io.flush()
  57. io.close()
  58. active = false
  59. elseif id == 6 then
  60. data1:copyz(nixio.stdout, data2)
  61. data1:close()
  62. end
  63. end
  64. end
  65. end
  66. root@OpenWrt:/usr/lib/lua/luci/sgi#

  

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

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

web(输入网关地址)==> uhttpd调用"/www/index.html" ==> index.html重定向到"/cgi-bin/luci" ==> luci被启动。

讲到这里可能大家对Luci有一个初步的认识了,由于Luci涉及lua语言,这门语言不像Java,C普及率那么高,我这里暂时也点到为止,更多Luci的知识后续有时间我会继续和大家一起研究。

  Luci Github:https://github.com/openwrt/luci

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/290787
推荐阅读
相关标签
  

闽ICP备14008679号