我是靠谱客的博主 高兴御姐,最近开发中收集的这篇文章主要介绍【笔记】openwrt - DDNS 插件研究介绍,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

在这里插入图片描述

文章目录

  • 介绍
    • DDNS 插件文件路径
    • DDNS 插件架构
    • luci-app-ddns
      • /usr/lib/lua/luci/controller/ddns.lua
    • DDNS 插件暴露接口文件 /etc/init.d/ddns
    • DDNS 插件工作接口文件 /usr/lib/ddns/dynamic_dns_updater.sh
    • ddns-scripts-cloudflare 工作

介绍

内核版本

$ cat /proc/version
Linux version 5.15.32 (lawsssscat@lawsssscat-virtual-machine) (x86_64-openwrt-linux-musl-gcc (OpenWrt GCC 8.4.0 r4424-89e39d880) 8.4.0, GNU ld (GNU Binutils) 2.34) #0 SMP Sun Apr 3 11:12:12 2022

编译版本

$ cat /etc/openwrt_release
DISTRIB_ID='OpenWrt'
DISTRIB_RELEASE='SNAPSHOT'
DISTRIB_TARGET='x86/64'
DISTRIB_ARCH='x86_64'
DISTRIB_TAINTS='no-all'
DISTRIB_REVISION='R22.4.1'
DISTRIB_DESCRIPTION='OpenWrt '

编译版本号

$ cat /etc/openwrt_version
r4415-8f2b0b86b

DDNS 插件文件路径

查看 OpenWRT 中所有关于 DDNS 的软件包

$ opkg list-installed  | grep ddns
ddns-scripts - 2.7.8-3
ddns-scripts-cloudflare - 2.8.2-35
ddns-scripts_aliyun - 1.0.3-1
ddns-scripts_dnspod - 1.0.2-1
luci-app-ddns - 2.4.9-8
luci-i18n-ddns-zh-cn - 2.4.9-8

查看这些软件包的文件

$ opkg files ddns-scripts-cloudflare
Package ddns-scripts-cloudflare (2.8.2-35) is installed on root and has the following files:
/usr/share/ddns/default/cloudflare.com-v4.json
/usr/lib/ddns/update_cloudflare_com_v4.sh

$ opkg files ddns-scripts_aliyun
Package ddns-scripts_aliyun (1.0.3-1) is installed on root and has the following files:
/usr/lib/ddns/update_aliyun_com.sh

$ opkg files ddns-scripts_dnspod
Package ddns-scripts_dnspod (1.0.2-1) is installed on root and has the following files:
/usr/lib/ddns/update_dnspod_cn.sh
/usr/lib/ddns/update_dnspod_com.sh

$ opkg files ddns-scripts
Package ddns-scripts (2.7.8-3) is installed on root and has the following files:
/etc/ddns/services
/etc/uci-defaults/ddns
/etc/ddns/services_ipv6
/usr/lib/ddns/dynamic_dns_updater.sh
/etc/init.d/ddns
/etc/hotplug.d/iface/95-ddns
/etc/config/ddns
/usr/lib/ddns/dynamic_dns_functions.sh
/usr/lib/ddns/dynamic_dns_lucihelper.sh

DDNS 插件架构

像其他 OpenWRT 插件一样,DDNS 插件也分 ui 界面和脚本两部分:

  • ui 界面

    luci-app-ddns - 2.4.9-8           ????界面核心
    luci-i18n-ddns-zh-cn - 2.4.9-8    ????ui翻译
    
  • 脚本

    ddns-scripts - 2.7.8-3                 ????脚本核心
    ddns-scripts-cloudflare - 2.8.2-35     ????脚本cloudflare补充功能
    ddns-scripts_aliyun - 1.0.3-1
    ddns-scripts_dnspod - 1.0.2-1
    

luci-app-ddns

????关于 luci(openwrt 的 ui 界面) 的资料: https://lawsssscat.blog.csdn.net/article/details/103609225

先看安装情况

$ opkg info luci-i18n-ddns-zh-cn
Package: luci-i18n-ddns-zh-cn
Version: 2.4.9-8
Depends: libc, luci-app-ddns
Status: install user installed
Architecture: all
Installed-Time: 1648984332

$ opkg files luci-i18n-ddns-zh-cn
Package luci-i18n-ddns-zh-cn (2.4.9-8) is installed on root and has the following files:
/usr/lib/lua/luci/i18n/ddns.zh-cn.lmo
/etc/uci-defaults/luci-i18n-ddns-zh-cn

$ opkg info luci-app-ddns
Package: luci-app-ddns
Version: 2.4.9-8
Depends: libc, luci-mod-admin-full, ddns-scripts
Status: install user installed
Architecture: all
Installed-Time: 1648984332

$ opkg files luci-app-ddns
Package luci-app-ddns (2.4.9-8) is installed on root and has the following files:
/etc/uci-defaults/40_luci-ddns
/usr/lib/lua/luci/tools/ddns.lua
/usr/lib/lua/luci/model/cbi/ddns/detail.lua
/usr/lib/lua/luci/model/cbi/ddns/hints.lua
/usr/lib/lua/luci/model/cbi/ddns/global.lua
/usr/lib/lua/luci/model/cbi/ddns/overview.lua
/usr/lib/lua/luci/view/ddns/detail_value.htm
/usr/lib/lua/luci/view/ddns/global_value.htm
/usr/lib/lua/luci/view/ddns/overview_doubleline.htm
/usr/lib/lua/luci/view/ddns/system_status.htm
/usr/lib/lua/luci/view/ddns/detail_logview.htm
/usr/lib/lua/luci/view/ddns/overview_startstop.htm
/usr/lib/lua/luci/view/ddns/detail_lvalue.htm
/usr/lib/lua/luci/view/ddns/overview_status.htm
/usr/lib/lua/luci/view/ddns/overview_enabled.htm
/usr/lib/lua/luci/view/admin_status/index/ddns.htm
/usr/lib/lua/luci/controller/ddns.lua

???? 关于 luci 的运行流程大致是:

  1. 用户访问页面 => 通过 Controller 进行路由
  2. Controller 选择 model(数据) 和 view(页面)
  3. 渲染 view 页面返回给用户

在这里插入图片描述

先看 controller

/usr/lib/lua/luci/controller/ddns.lua

????luci 进来只看 index 方法,通过该方法对整个插件进行注册,然后就可以在 web 界面上看到了。
在这里插入图片描述

index 方法:(简略)

function index()
	
	-- ????说明基于该文件
	-- no config create an empty one
	if not nxfs.access("/etc/config/ddns") then
		nxfs.writefile("/etc/config/ddns", "")
	end

	-- preset new option "lookup_host" if not already defined
	local uci = muci.cursor()
	local commit = false
	uci:foreach("ddns", "service", function (s) -- ????遍历/etc/config/ddns文件内容
		-- ????lookup_host ── 查询主机名
		-- ????domain ── 需要解析的域名
		-- ????一般 lookup_host 的 ip 和 domain 的 ip 一致时,认为动态域名绑定成功
		if not s["lookup_host"] and s["domain"] then
			uci:set("ddns", s[".name"], "lookup_host", s["domain"])
			commit = true
		end
	end)
	if commit then uci:commit("ddns") end -- ????提交更改
	uci:unload("ddns")

	-- ???? 前面的 {path1, path2, ...} 是路径 => http(s)://<host>/cgi-bin/luci/path1/path2/...
	-- ???? cbi 结合 view、moduel 渲染页面,并返回 /usr/lib/lua/luci/model/cbi/<str>
	-- ???? call 调用 lua 方法
	-- ???? post 请求
	entry( {"admin", "services", "ddns"}, cbi("ddns/overview"), _("Dynamic DNS"), 59)
	entry( {"admin", "services", "ddns", "detail"}, cbi("ddns/detail"), nil ).leaf = true
	entry( {"admin", "services", "ddns", "hints"}, cbi("ddns/hints",
		{hideapplybtn=true, hidesavebtn=true, hideresetbtn=true}), nil ).leaf = true
	entry( {"admin", "services", "ddns", "global"}, cbi("ddns/global"), nil ).leaf = true
	entry( {"admin", "services", "ddns", "logview"}, call("logread") ).leaf = true
	entry( {"admin", "services", "ddns", "startstop"}, post("startstop") ).leaf = true
	entry( {"admin", "services", "ddns", "status"}, call("status") ).leaf = true
end

DDNS 插件暴露接口文件 /etc/init.d/ddns

查看 /etc/init.d/ddns 文件

#!/bin/sh /etc/rc.common
START=95
STOP=10
boot() {
return 0
}
reload() {
/usr/lib/ddns/dynamic_dns_updater.sh -- reload
return 0
}
restart() {
/usr/lib/ddns/dynamic_dns_updater.sh -- stop
sleep 1
/usr/lib/ddns/dynamic_dns_updater.sh -- start
}
start() {
/usr/lib/ddns/dynamic_dns_updater.sh -- start
}
stop() {
/usr/lib/ddns/dynamic_dns_updater.sh -- stop
return 0
}

DDNS 插件工作接口文件 /usr/lib/ddns/dynamic_dns_updater.sh

查看 /usr/lib/ddns/dynamic_dns_updater.sh 文件

help

#!/bin/sh
#.Distributed under the terms of the GNU General Public License (GPL) version 2.0
#.2014-2018 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
. $(dirname $0)/dynamic_dns_functions.sh
usage() {
cat << EOF
Usage:
$MYPROG [options] -- command
Commands:
start                Start SECTION or NETWORK or all
stop                 Stop NETWORK or all
Parameters:
-n NETWORK          Start/Stop sections in background monitoring NETWORK, force VERBOSE=0
-S SECTION          SECTION to start
use either -N NETWORK or -S SECTION
-h                  show this help and exit
-V                  show version and exit
-v LEVEL            VERBOSE=LEVEL (default 1)
'0' NO output to console
'1' output to console
'2' output to console AND logfile
+ run once WITHOUT retry on error
'3' output to console AND logfile
+ run once WITHOUT retry on error
+ NOT sending update to DDNS service
EOF
}

首先会引用几个库

  1. /usr/lib/ddns/dynamic_dns_functions.sh
    1. /lib/functions.sh
    2. /lib/functions/network.sh

ddns-scripts-cloudflare 工作

$ opkg list-installed  | grep ddns
ddns-scripts - 2.7.8-3
ddns-scripts-cloudflare - 2.8.2-35
ddns-scripts_aliyun - 1.0.3-1
ddns-scripts_dnspod - 1.0.2-1
luci-app-ddns - 2.4.9-8
luci-i18n-ddns-zh-cn - 2.4.9-8

最后

以上就是高兴御姐为你收集整理的【笔记】openwrt - DDNS 插件研究介绍的全部内容,希望文章能够帮你解决【笔记】openwrt - DDNS 插件研究介绍所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部