├── README.md ├── images ├── ddns1.png ├── ddns2.png └── qrcode.png └── tencentcloud_ddns ├── LICENSE ├── Makefile ├── files ├── luci │ ├── controller │ │ └── tencentddns.lua │ ├── i18n │ │ └── tencentddns.zh-cn.po │ └── model │ │ └── cbi │ │ └── tencentddns.lua └── root │ ├── etc │ ├── config │ │ └── tencentddns │ ├── init.d │ │ └── tencentddns │ ├── tencentddnsupload │ └── uci-defaults │ │ └── luci-tencentddns │ └── usr │ └── sbin │ └── tencentddns └── tools └── po2lmo ├── Makefile └── src ├── po2lmo.c ├── template_lmo.c └── template_lmo.h /README.md: -------------------------------------------------------------------------------- 1 | # 腾讯云DDNS插件 2 | 3 | ## 1.插件介绍 4 | > OpenWRT TencentDDNS插件是一款腾讯云研发的,自动映射动态公网IP至用户指定的DNSPod域名解析记录的官方插件。 5 | 6 | | 标题 | 名称 | 7 | | ---------- | ------------------------------------------------------------ | 8 | | 中文名称 | 腾讯云DDNS插件 | 9 | | 英文名称 | luci-app-tencentddns | 10 | | 最新版本 | 0.1.0 (2020.09.17) | 11 | | 适用平台 | [Lean OpenWRT](https://github.com/coolsnowwolf/lede) | 12 | | 适用产品 | [DNSPod](https://www.dnspod.cn/)| 13 | | GitHub项目 | [tencentcloud-openwrt-plugin-ddns](https://github.com/Tencent-Cloud-Plugins/tencentcloud-openwrt-plugin-ddns) | 14 | | 主创团队 | 腾讯云中小企业产品中心(SMB Product Center of Tencent Cloud) | 15 | 16 | 17 | 18 | ## 2.功能特性 19 | 20 | - 可在安装了openwrt的软路由上,将运营商分配的家庭宽带公网IP动态关联到指定的DNSPod域名解析记录上。实现通过域名访问家庭内网服务的目的。 21 | 22 | 23 | 24 | 25 | 26 | ## 3.安装指引 27 | 28 | ### 3.1.安装ipk包(推荐) 29 | 30 | 登录openwrt路由器,进入系统→软件包页面,将下方软件包的地址放入从网络安装的输入框中,点击确认完成安装。 31 | ipk安装地址:http://openwrt-tencentddns-1301800460.cos.ap-guangzhou.myqcloud.com/luci-app-tencentddns_0.1.0-1_all.ipk 32 | ![](./images/ddns1.png) 33 | 34 | 35 | 36 | ### 3.2.源码编译安装 37 | 38 | 1. 安装好openWrt编译环境。 39 | 2. 下载本github的zip文件。 40 | 3. 编译 po2lmo (如果有po2lmo可跳过) 41 | pushd package/tencentcloud-openwrt-plugin-ddns/tencentcloud_ddns/tools/po2lmo 42 | make && sudo make install 43 | popd 44 | 4. 开始编译 45 | make package/feeds/tencentcloud_ddns/compile V=s 46 | 5. 把生成的ipk包上传到路由器,用opkg安装 47 | opkg install luci-app-tencentddns_0.1.0-1_all.ipk 48 | 49 | 50 | 51 | 52 | ## 4.使用指引 53 | 54 | ### 4.1.界面功能介绍 55 | 56 | ![](./images/ddns2.png) 57 | 58 | > 上图主要是配置DDNS插件的腾讯云参数,包括密钥ID、密钥Token、WAN-IP来源、主域名和子域名等 59 | 60 | 61 | 62 | 63 | ### 4.2.名词解释 64 | - **启用**:是否开启腾讯云DDNS 65 | - **清除所有同名记录**:如果一个域名有多个A解析记录,是否一并清除 66 | - **密钥ID**:DNSPod后台密钥管理中的ID,获取地址:[DNSPod密钥管理](https://console.dnspod.cn/account/token) 67 | - **密钥Token**:DNSPod后台密钥管理中的Token。获取地址:[DNSPod密钥管理](https://console.dnspod.cn/account/token) 68 | - **主域名**:主域名,如: github.com 69 | - **子域名**:子域名,如:www 70 | - **检查时间**:后台对域名解析检查的时间间隔 71 | 72 | 73 | 74 | 75 | 76 | ## 5.FAQ 77 | 78 | > 暂无 79 | 80 | ## 6.版本迭代记录 81 | 82 | ### 6.1 tencentcloud-openwrt-plugin-ddns v0.1.0 83 | - 支持在软路由提供DDNS服务 84 | 85 | 86 | ## 7.注意事项 87 | 88 | > 暂无 89 | 90 | 91 | ## 8. 联系我们 92 | 93 |    扫码备注“春雨”来联络到我们
94 | ![](./images/qrcode.png) 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /images/ddns1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tencent-Cloud-Plugins/tencentcloud-openwrt-plugin-ddns/537a537436663cb1595a49db44989fac4314d83c/images/ddns1.png -------------------------------------------------------------------------------- /images/ddns2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tencent-Cloud-Plugins/tencentcloud-openwrt-plugin-ddns/537a537436663cb1595a49db44989fac4314d83c/images/ddns2.png -------------------------------------------------------------------------------- /images/qrcode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tencent-Cloud-Plugins/tencentcloud-openwrt-plugin-ddns/537a537436663cb1595a49db44989fac4314d83c/images/qrcode.png -------------------------------------------------------------------------------- /tencentcloud_ddns/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /tencentcloud_ddns/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (C) 2020 tencentcloud 3 | # 4 | # See /LICENSE for more information. 5 | # 6 | 7 | include $(TOPDIR)/rules.mk 8 | 9 | PKG_NAME:=luci-app-tencentddns 10 | PKG_VERSION:=0.1.0 11 | PKG_RELEASE:=1 12 | 13 | PKG_LICENSE:=MIT 14 | PKG_LICENSE_FILES:=LICENSE 15 | PKG_MAINTAINER:=tencentcloud 16 | 17 | PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME) 18 | 19 | include $(INCLUDE_DIR)/package.mk 20 | 21 | define Package/luci-app-tencentddns 22 | SECTION:=luci 23 | CATEGORY:=LuCI 24 | SUBMENU:=3. Applications 25 | TITLE:=LuCI Support for tencentddns 26 | PKGARCH:=all 27 | DEPENDS:=+openssl-util +curl 28 | endef 29 | 30 | define Package/luci-app-tencentddns/description 31 | LuCI Support for TencentDDNS. 32 | endef 33 | 34 | define Build/Prepare 35 | $(foreach po,$(wildcard ${CURDIR}/files/luci/i18n/*.po), \ 36 | po2lmo $(po) $(PKG_BUILD_DIR)/$(patsubst %.po,%.lmo,$(notdir $(po)));) 37 | endef 38 | 39 | define Build/Configure 40 | endef 41 | 42 | define Build/Compile 43 | endef 44 | 45 | define Package/luci-app-tencentddns/postinst 46 | #!/bin/sh 47 | if [ -z "$${IPKG_INSTROOT}" ]; then 48 | if [ -f /etc/uci-defaults/luci-tencentddns ]; then 49 | ( . /etc/uci-defaults/luci-tencentddns ) && \ 50 | rm -f /etc/uci-defaults/luci-tencentddns 51 | fi 52 | rm -rf /tmp/luci-indexcache /tmp/luci-modulecache 53 | fi 54 | exit 0 55 | endef 56 | 57 | define Package/luci-app-tencentddns/prerm 58 | #!/bin/sh 59 | /etc/init.d/tencentddns stop 60 | exit 0 61 | endef 62 | 63 | define Package/luci-app-tencentddns/conffiles 64 | /etc/config/tencentddns 65 | endef 66 | 67 | define Package/luci-app-tencentddns/install 68 | $(INSTALL_DIR) $(1)/usr/lib/lua/luci/i18n 69 | $(INSTALL_DATA) $(PKG_BUILD_DIR)/tencentddns.*.lmo $(1)/usr/lib/lua/luci/i18n/ 70 | $(INSTALL_DIR) $(1)/usr/lib/lua/luci/controller 71 | $(INSTALL_DATA) ./files/luci/controller/*.lua $(1)/usr/lib/lua/luci/controller/ 72 | $(INSTALL_DIR) $(1)/usr/lib/lua/luci/model/cbi/tencentddns 73 | $(INSTALL_DATA) ./files/luci/model/cbi/*.lua $(1)/usr/lib/lua/luci/model/cbi/ 74 | $(INSTALL_DIR) $(1)/etc/config 75 | $(INSTALL_DATA) ./files/root/etc/config/tencentddns $(1)/etc/config/tencentddns 76 | $(INSTALL_DIR) $(1)/etc/init.d 77 | $(INSTALL_BIN) ./files/root/etc/init.d/tencentddns $(1)/etc/init.d/tencentddns 78 | $(INSTALL_DIR) $(1)/etc/uci-defaults 79 | $(INSTALL_BIN) ./files/root/etc/uci-defaults/luci-tencentddns $(1)/etc/uci-defaults/luci-tencentddns 80 | $(INSTALL_DIR) $(1)/usr/sbin 81 | $(INSTALL_BIN) ./files/root/usr/sbin/tencentddns $(1)/usr/sbin/tencentddns 82 | $(INSTALL_DIR) $(1)/etc 83 | $(INSTALL_BIN) ./files/root/etc/tencentddnsupload $(1)/etc/tencentddnsupload 84 | endef 85 | 86 | $(eval $(call BuildPackage,luci-app-tencentddns)) 87 | -------------------------------------------------------------------------------- /tencentcloud_ddns/files/luci/controller/tencentddns.lua: -------------------------------------------------------------------------------- 1 | module("luci.controller.tencentddns",package.seeall) 2 | function index() 3 | entry({"admin", "tencentcloud"}, firstchild(), "腾讯云设置", 30).dependent=false 4 | entry({"admin", "tencentcloud", "tencentddns"},cbi("tencentddns"),_("TencentDDNS"),2) 5 | end 6 | -------------------------------------------------------------------------------- /tencentcloud_ddns/files/luci/i18n/tencentddns.zh-cn.po: -------------------------------------------------------------------------------- 1 | msgid "" 2 | msgstr "Content-Type: text/plain; charset=UTF-8\n" 3 | 4 | msgid "TencentDDNS" 5 | msgstr "腾讯云DDNS" 6 | 7 | msgid "enable" 8 | msgstr "启用" 9 | 10 | msgid "Clean Before Update" 11 | msgstr "清除所有同名记录" 12 | 13 | msgid "Clean Before Update mean" 14 | msgstr "如果有相同的域名的A解析记录,DDNS会将其覆盖" 15 | 16 | msgid "Base" 17 | msgstr "基本设置" 18 | 19 | msgid "Key ID" 20 | msgstr "密钥ID" 21 | 22 | msgid "Key ID Mean" 23 | msgstr "DNSPod后台密钥管理中的ID" 24 | 25 | msgid "Key Token" 26 | msgstr "密钥Token" 27 | 28 | msgid "Key Token Mean" 29 | msgstr "DNSPod后台密钥管理中的Token" 30 | 31 | 32 | msgid "WAN-IP Source" 33 | msgstr "WAN-IP来源" 34 | 35 | msgid "Select WAN-IP Source" 36 | msgstr "选择WAN-IP来源" 37 | 38 | msgid "Select the WAN-IP Source for TencentDDNS, like wan/internet" 39 | msgstr "动态域名的IP来源,如wan/internet" 40 | 41 | msgid "Main Domain" 42 | msgstr "主域名" 43 | 44 | msgid "Sub Domain" 45 | msgstr "子域名" 46 | 47 | msgid "For example: test.github.com -> github.com" 48 | msgstr "例如: test.github.com 则填: github.com" 49 | 50 | msgid "For example: test.github.com -> test" 51 | msgstr "例如: test.github.com, 则填: test" 52 | 53 | msgid "Inspection Time" 54 | msgstr "检查时间" 55 | 56 | msgid "Unit: Minute, Range: 1-59" 57 | msgstr "域名检查间隔时间,单位分钟,范围1-59" 58 | 59 | msgid "Update Log" 60 | msgstr "更新记录" 61 | -------------------------------------------------------------------------------- /tencentcloud_ddns/files/luci/model/cbi/tencentddns.lua: -------------------------------------------------------------------------------- 1 | local a=require"luci.sys" 2 | local e=luci.model.uci.cursor() 3 | local e=require"nixio.fs" 4 | require("luci.sys") 5 | local t,e,o 6 | 7 | t=Map("tencentddns",translate("TencentDDNS")) 8 | 9 | e=t:section(TypedSection,"base",translate("Base")) 10 | e.anonymous=true 11 | 12 | enable=e:option(Flag,"enable",translate("enable")) 13 | enable.rmempty=false 14 | 15 | enable=e:option(Flag,"clean",translate("Clean Before Update"),translate("Clean Before Update mean")) 16 | enable.rmempty=false 17 | 18 | token=e:option(Value,"key_id",translate("Key ID"),translate("Key ID Mean")) 19 | email=e:option(Value,"key_token",translate("Key Token"),translate("Key Token Mean")) 20 | email.password = true 21 | 22 | iface=e:option(ListValue,"interface",translate("WAN-IP Source"),translate("Select the WAN-IP Source for TencentDDNS, like wan/internet")) 23 | iface:value("",translate("Select WAN-IP Source")) 24 | iface:value("internet") 25 | iface:value("wan") 26 | 27 | iface.rmempty=false 28 | main=e:option(Value,"main_domain",translate("Main Domain"),translate("For example: test.github.com -> github.com")) 29 | main.rmempty=false 30 | sub=e:option(Value,"sub_domain",translate("Sub Domain"),translate("For example: test.github.com -> test")) 31 | sub.rmempty=false 32 | time=e:option(Value,"time",translate("Inspection Time"),translate("Unit: Minute, Range: 1-59")) 33 | time.rmempty=false 34 | 35 | e=t:section(TypedSection,"base",translate("Update Log")) 36 | e.anonymous=true 37 | local a="/var/log/tencentddns.log" 38 | tvlog=e:option(TextValue,"sylogtext") 39 | tvlog.rows=16 40 | tvlog.readonly="readonly" 41 | tvlog.wrap="off" 42 | 43 | function tvlog.cfgvalue(e,e) 44 | sylogtext="" 45 | if a and nixio.fs.access(a) then 46 | sylogtext=luci.sys.exec("tail -n 100 %s"%a) 47 | end 48 | return sylogtext 49 | end 50 | 51 | 52 | tvlog.write=function(e,e,e) 53 | end 54 | local e=luci.http.formvalue("cbi.apply") 55 | if e then 56 | local key, val 57 | local Enable 58 | local Keyid 59 | local Keytoken 60 | local Domain 61 | local Subdomian 62 | for key, val in pairs(luci.http.formvalue()) do 63 | if(string.find(key,"enable")) 64 | then 65 | Enable=val 66 | elseif(string.find(key,"key_id")) 67 | then 68 | Keyid=val 69 | elseif(string.find(key,"key_token")) 70 | then 71 | Keytoken=val 72 | elseif(string.find(key,"main_domain")) 73 | then 74 | Domain=val 75 | elseif(string.find(key,"sub_domain")) 76 | then 77 | Subdomian=val 78 | end 79 | end 80 | io.popen("/etc/tencentddnsupload 1 "..Keyid.." "..Domain.." "..Subdomian.." > /dev/null") 81 | io.popen("/etc/init.d/tencentddns restart") 82 | end 83 | return t 84 | -------------------------------------------------------------------------------- /tencentcloud_ddns/files/root/etc/config/tencentddns: -------------------------------------------------------------------------------- 1 | 2 | config base 'base' 3 | option enable '0' 4 | option time '10' 5 | option siteid '' 6 | -------------------------------------------------------------------------------- /tencentcloud_ddns/files/root/etc/init.d/tencentddns: -------------------------------------------------------------------------------- 1 | #!/bin/sh /etc/rc.common 2 | 3 | START=80 4 | NAME=tencentddns 5 | 6 | NE_TIPS='TencentDDNS NOT ENABLED' 7 | 8 | restart() { 9 | start 10 | } 11 | 12 | uci_get_by_name() { 13 | local ret=$(uci get $NAME.$1.$2 2>/dev/null) 14 | echo ${ret:=$3} 15 | } 16 | 17 | uci_bool_by_name() { 18 | case "$(uci_get_by_name $1 $2)" in 19 | 1|on|true|yes|enabled) return 0;; 20 | esac 21 | return 1 22 | } 23 | 24 | add_hotplug() { 25 | cat < "/etc/hotplug.d/iface/${START}-${NAME}" 26 | #!/bin/sh /etc/rc.common 27 | 28 | [ "Z\$ACTION" = "Zifup" -a "Z\$INTERFACE" = "Zwan" ] || exit 0 29 | 30 | DATE=\$(date +'%Y-%m-%d %H:%M:%S') 31 | ( sleep 10 && ( echo "\$DATE IFUP-WAN" && /usr/sbin/tencentddns ) >> /var/log/tencentddns.log 2>&1 ) & 32 | 33 | exit 0 34 | EOF 35 | chmod +x "/etc/hotplug.d/iface/${START}-${NAME}" 36 | } 37 | 38 | start() { 39 | $(uci_bool_by_name base enable) || { 40 | stop 41 | return 0 42 | } 43 | 44 | add_hotplug 45 | 46 | sed -i "/${NE_TIPS}/d" /var/log/tencentddns.log 47 | 48 | time=$(uci_get_by_name base time 10) 49 | [ 0 -lt $time -a $time -lt 60 ] || time=10 50 | 51 | [ -f /etc/crontabs/root ] || mkdir -p /etc/crontabs && touch /etc/crontabs/root 52 | sed -i '/tencentddns/d' /etc/crontabs/root 53 | echo "*/$time * * * * /usr/sbin/tencentddns >> /var/log/tencentddns.log 2>&1" >> /etc/crontabs/root 54 | /etc/init.d/cron restart 55 | 56 | ( /usr/sbin/tencentddns >> /var/log/tencentddns.log 2>&1 ) & 57 | } 58 | 59 | stop() { 60 | rm -rf "/etc/hotplug.d/iface/${START}-${NAME}" 61 | sed -i '/tencentddns/d' /etc/crontabs/root >/dev/null 2>&1 62 | /etc/init.d/cron restart 63 | echo "${NE_TIPS}" > /var/log/tencentddns.log 64 | } 65 | 66 | -------------------------------------------------------------------------------- /tencentcloud_ddns/files/root/etc/tencentddnsupload: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function rand(){ 4 | min=$1 5 | max=$(($2-$min+1)) 6 | num=$(date +%s%N) 7 | echo $(($num%$max+$min)) 8 | } 9 | 10 | runflag=$1 11 | if [[ $1 -eq 1 ]]; then 12 | uin=$2 13 | domain=$3 14 | subdomain=$4 15 | siteid=`uci get tencentddns.base.siteid` 16 | siteurl="openwrt" 17 | siteapp="openwrt" 18 | plugintype="ddns" 19 | if [[ ! $siteid ]]; then 20 | siteid="openwrt_"$(rand 1 50) 21 | uci set tencentddns.base.siteid=$siteid 22 | uci commit tencentddns 23 | fi 24 | . /usr/share/libubox/jshn.sh 25 | json_init 26 | json_add_string "action" "save_config" 27 | json_add_string "plugin_type" "ddns" 28 | json_add_object "data" 29 | json_add_string "site_id" $siteid 30 | json_add_string "site_url" $siteurl 31 | json_add_string "site_app" $siteapp 32 | json_add_string "uin" $uin 33 | json_add_string "cust_sec_on" "2" 34 | json_add_object "others" 35 | json_add_string "domain" $domain 36 | json_add_string "sub_domain" $subdomain 37 | 38 | 39 | postdata=`json_dump` 40 | `curl -X POST https://openapp.qq.com/api/public/index.php/upload -H "Content-Type: application/json" --data-raw "$postdata"` 41 | fi -------------------------------------------------------------------------------- /tencentcloud_ddns/files/root/etc/uci-defaults/luci-tencentddns: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | uci -q batch <<-EOF >/dev/null 4 | delete ucitrack.@tencentddns[-1] 5 | add ucitrack tencentddns 6 | set ucitrack.@tencentddns[-1].init=tencentddns 7 | commit ucitrack 8 | EOF 9 | 10 | exit 0 11 | -------------------------------------------------------------------------------- /tencentcloud_ddns/files/root/usr/sbin/tencentddns: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | NAME=tencentddns 4 | log_file=/var/log/$NAME.log 5 | 6 | uci_get_by_name() { 7 | local ret=$(uci get $NAME.$1.$2 2>/dev/null) 8 | echo ${ret:=$3} 9 | } 10 | 11 | uci_bool_by_name() { 12 | case "$(uci_get_by_name $1 $2)" in 13 | 1|on|true|yes|enabled) return 0;; 14 | esac 15 | return 1 16 | } 17 | 18 | intelnetip() { 19 | tmp_ip=`curl -sL --connect-timeout 3 ns1.dnspod.net:6666` 20 | if [ "Z$tmp_ip" == "Z" ]; then 21 | tmp_ip=`curl -sL --connect-timeout 3 members.3322.org/dyndns/getip` 22 | fi 23 | if [ "Z$tmp_ip" == "Z" ]; then 24 | tmp_ip=`curl -sL --connect-timeout 3 14.215.150.17:6666` 25 | fi 26 | if [ "Z$tmp_ip" == "Z" ]; then 27 | tmp_ip=`curl -sL --connect-timeout 3 whatismyip.akamai.com` 28 | fi 29 | echo -n $tmp_ip 30 | } 31 | 32 | resolve2ip() { 33 | # resolve2ip domain 34 | domain=$1 35 | tmp_ip=`nslookup $domain f1g1ns1.dnspod.net 2>/dev/null | sed '/^Server/d; /#53$/d' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | tail -n1` 36 | if [ "Z$tmp_ip" == "Z" ]; then 37 | tmp_ip=`nslookup $domain f1g1ns2.dnspod.net 2>/dev/null | sed '/^Server/d; /#53$/d' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | tail -n1` 38 | fi 39 | if [ "Z$tmp_ip" == "Z" ]; then 40 | tmp_ip=`nslookup $domain 114.114.115.115 2>/dev/null | sed '/^Server/d; /#53$/d' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | tail -n1` 41 | fi 42 | if [ "Z$tmp_ip" == "Z" ]; then 43 | tmp_ip=`curl -sL --connect-timeout 3 "119.29.29.29/d?dn=$domain"` 44 | fi 45 | echo -n $tmp_ip 46 | } 47 | 48 | check_tencentddns() { 49 | echo "$DATE WAN-IP: ${ip}" 50 | if [ "Z$ip" == "Z" ]; then 51 | echo "$DATE ERROR, cant get WAN-IP..." 52 | return 0 53 | fi 54 | current_ip=$(resolve2ip "$sub_dm.$main_dm") 55 | if [ "Z$current_ip" == "Z" ]; then 56 | rrid='' # NO Resolve IP Means new Record_ID 57 | fi 58 | echo "$DATE DOMAIN-IP: ${current_ip}" 59 | if [ "Z$ip" == "Z$current_ip" ]; then 60 | echo "$DATE IP dont need UPDATE..." 61 | return 0 62 | else 63 | echo "$DATE UPDATING..." 64 | return 1 65 | fi 66 | } 67 | 68 | urlencode() { 69 | # urlencode url 70 | out='' 71 | for c in $(echo -n $1 | sed 's/[^\n]/&\n/g'); do 72 | case $c in 73 | [a-zA-Z0-9._-]) out="$out$c" ;; 74 | *) out="$out$(printf '%%%02X' "'$c")" ;; 75 | esac 76 | done 77 | echo -n $out 78 | } 79 | 80 | send_request() { 81 | curl -s -X POST https://dnsapi.cn/$1 -d "login_token=$ak_id,$ak_token&format=json&"$2 82 | } 83 | 84 | get_recordid() { 85 | sed 's/"records"/\n/g' | sed -n '2p' | sed 's/ttl/\n/g' | sed -n 's/.*"id[^0-9]*\([0-9]*\).*/\1\n/p' | sort -ru | sed /^$/d 86 | } 87 | 88 | 89 | get_recordid2() { 90 | sed 's/"record"/\n/g' | sed -n '2p' | sed -n 's/.*"id[^0-9]*\([0-9]*\).*/\1\n/p' | sort -ru | sed /^$/d 91 | } 92 | 93 | query_recordid() { 94 | send_request "Record.List" "domain=$main_dm&sub_domain=$sub_dm&record_type=A" 95 | } 96 | 97 | update_record() { 98 | send_request "Record.Modify" "domain=$main_dm&sub_domain=$sub_dm&record_id=$1&record_type=A&record_line=%e9%bb%98%e8%ae%a4&value=$ip" 99 | } 100 | 101 | add_record() { 102 | send_request "Record.Create" "domain=$main_dm&sub_domain=$sub_dm&record_type=A&record_line=%e9%bb%98%e8%ae%a4&value=$ip" 103 | } 104 | 105 | del_record() { 106 | send_request "Record.Remove" "domain=$main_dm&record_id=$1" 107 | } 108 | 109 | do_ddns_record() { 110 | if uci_bool_by_name base clean ; then 111 | query_recordid | get_recordid | while read rr; do 112 | echo "$DATE Clean record $sub_dm.$main_dm: $rr" 113 | del_record $rr >/dev/null 114 | timestamp=$(date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ") 115 | done 116 | rrid='' 117 | fi 118 | if [ "Z$rrid" == "Z" ]; then 119 | rrid=`query_recordid | get_recordid` 120 | fi 121 | if [ "Z$rrid" == "Z" ]; then 122 | rrid=`add_record | get_recordid2` 123 | echo "$DATE ADD record $rrid" 124 | else 125 | update_record $rrid >/dev/null 2>&1 126 | echo "$DATE UPDATE record $rrid" 127 | fi 128 | if [ "Z$rrid" == "Z" ]; then 129 | # failed 130 | echo "$DATE # ERROR, Please Check Config/Time" 131 | else 132 | # save rrid 133 | uci set tencentddns.base.record_id=$rrid 134 | uci commit tencentddns 135 | echo "$DATE # UPDATED($ip)" 136 | fi 137 | } 138 | 139 | clean_log() { 140 | if [ $(cat $log_file 2>/dev/null | wc -l) -ge 16 ]; then 141 | rm -f $log_file && touch $log_file 142 | echo "$DATE Log Cleaned" 143 | fi 144 | } 145 | 146 | [ -x /usr/bin/openssl -a -x /usr/bin/curl -a -x /bin/sed ] || 147 | ( echo "Need [ openssl + curl + sed ]" && exit 1 ) 148 | 149 | ak_id=$(uci_get_by_name base key_id) 150 | ak_token=$(uci_get_by_name base key_token) 151 | rrid=$(uci_get_by_name base record_id) 152 | main_dm=$(uci_get_by_name base main_domain) 153 | sub_dm=$(uci_get_by_name base sub_domain) 154 | 155 | iface=$(uci_get_by_name base interface) 156 | if [ "Z$iface" == "Zinternet" -o "Z$iface" == "Z" ]; then 157 | ip=$(intelnetip) 158 | else 159 | ip=$(ubus call network.interface.$iface status | grep '"address"' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1) 160 | fi 161 | 162 | DATE=$(date +'%Y-%m-%d %H:%M:%S') 163 | timestamp=$(date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ") 164 | 165 | clean_log 166 | check_tencentddns || do_ddns_record 167 | -------------------------------------------------------------------------------- /tencentcloud_ddns/tools/po2lmo/Makefile: -------------------------------------------------------------------------------- 1 | 2 | INSTALL = install 3 | PREFIX = /usr/bin 4 | 5 | po2lmo: src/po2lmo.o src/template_lmo.o 6 | $(CC) $(LDFLAGS) -o src/po2lmo src/po2lmo.o src/template_lmo.o 7 | 8 | install: 9 | $(INSTALL) -m 755 src/po2lmo $(PREFIX) 10 | 11 | clean: 12 | $(RM) src/po2lmo src/*.o 13 | -------------------------------------------------------------------------------- /tencentcloud_ddns/tools/po2lmo/src/po2lmo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lmo - Lua Machine Objects - PO to LMO conversion tool 3 | * 4 | * Copyright (C) 2009-2012 Jo-Philipp Wich 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include "template_lmo.h" 20 | 21 | static void die(const char *msg) 22 | { 23 | fprintf(stderr, "Error: %s\n", msg); 24 | exit(1); 25 | } 26 | 27 | static void usage(const char *name) 28 | { 29 | fprintf(stderr, "Usage: %s input.po output.lmo\n", name); 30 | exit(1); 31 | } 32 | 33 | static void print(const void *ptr, size_t size, size_t nmemb, FILE *stream) 34 | { 35 | if( fwrite(ptr, size, nmemb, stream) == 0 ) 36 | die("Failed to write stdout"); 37 | } 38 | 39 | static int extract_string(const char *src, char *dest, int len) 40 | { 41 | int pos = 0; 42 | int esc = 0; 43 | int off = -1; 44 | 45 | for( pos = 0; (pos < strlen(src)) && (pos < len); pos++ ) 46 | { 47 | if( (off == -1) && (src[pos] == '"') ) 48 | { 49 | off = pos + 1; 50 | } 51 | else if( off >= 0 ) 52 | { 53 | if( esc == 1 ) 54 | { 55 | switch (src[pos]) 56 | { 57 | case '"': 58 | case '\\': 59 | off++; 60 | break; 61 | } 62 | dest[pos-off] = src[pos]; 63 | esc = 0; 64 | } 65 | else if( src[pos] == '\\' ) 66 | { 67 | dest[pos-off] = src[pos]; 68 | esc = 1; 69 | } 70 | else if( src[pos] != '"' ) 71 | { 72 | dest[pos-off] = src[pos]; 73 | } 74 | else 75 | { 76 | dest[pos-off] = '\0'; 77 | break; 78 | } 79 | } 80 | } 81 | 82 | return (off > -1) ? strlen(dest) : -1; 83 | } 84 | 85 | static int cmp_index(const void *a, const void *b) 86 | { 87 | uint32_t x = ((const lmo_entry_t *)a)->key_id; 88 | uint32_t y = ((const lmo_entry_t *)b)->key_id; 89 | 90 | if (x < y) 91 | return -1; 92 | else if (x > y) 93 | return 1; 94 | 95 | return 0; 96 | } 97 | 98 | static void print_uint32(uint32_t x, FILE *out) 99 | { 100 | uint32_t y = htonl(x); 101 | print(&y, sizeof(uint32_t), 1, out); 102 | } 103 | 104 | static void print_index(void *array, int n, FILE *out) 105 | { 106 | lmo_entry_t *e; 107 | 108 | qsort(array, n, sizeof(*e), cmp_index); 109 | 110 | for (e = array; n > 0; n--, e++) 111 | { 112 | print_uint32(e->key_id, out); 113 | print_uint32(e->val_id, out); 114 | print_uint32(e->offset, out); 115 | print_uint32(e->length, out); 116 | } 117 | } 118 | 119 | int main(int argc, char *argv[]) 120 | { 121 | char line[4096]; 122 | char key[4096]; 123 | char val[4096]; 124 | char tmp[4096]; 125 | int state = 0; 126 | int offset = 0; 127 | int length = 0; 128 | int n_entries = 0; 129 | void *array = NULL; 130 | lmo_entry_t *entry = NULL; 131 | uint32_t key_id, val_id; 132 | 133 | FILE *in; 134 | FILE *out; 135 | 136 | if( (argc != 3) || ((in = fopen(argv[1], "r")) == NULL) || ((out = fopen(argv[2], "w")) == NULL) ) 137 | usage(argv[0]); 138 | 139 | memset(line, 0, sizeof(key)); 140 | memset(key, 0, sizeof(val)); 141 | memset(val, 0, sizeof(val)); 142 | 143 | while( (NULL != fgets(line, sizeof(line), in)) || (state >= 2 && feof(in)) ) 144 | { 145 | if( state == 0 && strstr(line, "msgid \"") == line ) 146 | { 147 | switch(extract_string(line, key, sizeof(key))) 148 | { 149 | case -1: 150 | die("Syntax error in msgid"); 151 | case 0: 152 | state = 1; 153 | break; 154 | default: 155 | state = 2; 156 | } 157 | } 158 | else if( state == 1 || state == 2 ) 159 | { 160 | if( strstr(line, "msgstr \"") == line || state == 2 ) 161 | { 162 | switch(extract_string(line, val, sizeof(val))) 163 | { 164 | case -1: 165 | state = 4; 166 | break; 167 | default: 168 | state = 3; 169 | } 170 | } 171 | else 172 | { 173 | switch(extract_string(line, tmp, sizeof(tmp))) 174 | { 175 | case -1: 176 | state = 2; 177 | break; 178 | default: 179 | strcat(key, tmp); 180 | } 181 | } 182 | } 183 | else if( state == 3 ) 184 | { 185 | switch(extract_string(line, tmp, sizeof(tmp))) 186 | { 187 | case -1: 188 | state = 4; 189 | break; 190 | default: 191 | strcat(val, tmp); 192 | } 193 | } 194 | 195 | if( state == 4 ) 196 | { 197 | if( strlen(key) > 0 && strlen(val) > 0 ) 198 | { 199 | key_id = sfh_hash(key, strlen(key)); 200 | val_id = sfh_hash(val, strlen(val)); 201 | 202 | if( key_id != val_id ) 203 | { 204 | n_entries++; 205 | array = realloc(array, n_entries * sizeof(lmo_entry_t)); 206 | entry = (lmo_entry_t *)array + n_entries - 1; 207 | 208 | if (!array) 209 | die("Out of memory"); 210 | 211 | entry->key_id = key_id; 212 | entry->val_id = val_id; 213 | entry->offset = offset; 214 | entry->length = strlen(val); 215 | 216 | length = strlen(val) + ((4 - (strlen(val) % 4)) % 4); 217 | 218 | print(val, length, 1, out); 219 | offset += length; 220 | } 221 | } 222 | 223 | state = 0; 224 | memset(key, 0, sizeof(key)); 225 | memset(val, 0, sizeof(val)); 226 | } 227 | 228 | memset(line, 0, sizeof(line)); 229 | } 230 | 231 | print_index(array, n_entries, out); 232 | 233 | if( offset > 0 ) 234 | { 235 | print_uint32(offset, out); 236 | fsync(fileno(out)); 237 | fclose(out); 238 | } 239 | else 240 | { 241 | fclose(out); 242 | unlink(argv[2]); 243 | } 244 | 245 | fclose(in); 246 | return(0); 247 | } 248 | -------------------------------------------------------------------------------- /tencentcloud_ddns/tools/po2lmo/src/template_lmo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lmo - Lua Machine Objects - Base functions 3 | * 4 | * Copyright (C) 2009-2010 Jo-Philipp Wich 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #include "template_lmo.h" 20 | 21 | /* 22 | * Hash function from http://www.azillionmonkeys.com/qed/hash.html 23 | * Copyright (C) 2004-2008 by Paul Hsieh 24 | */ 25 | 26 | uint32_t sfh_hash(const char *data, int len) 27 | { 28 | uint32_t hash = len, tmp; 29 | int rem; 30 | 31 | if (len <= 0 || data == NULL) return 0; 32 | 33 | rem = len & 3; 34 | len >>= 2; 35 | 36 | /* Main loop */ 37 | for (;len > 0; len--) { 38 | hash += sfh_get16(data); 39 | tmp = (sfh_get16(data+2) << 11) ^ hash; 40 | hash = (hash << 16) ^ tmp; 41 | data += 2*sizeof(uint16_t); 42 | hash += hash >> 11; 43 | } 44 | 45 | /* Handle end cases */ 46 | switch (rem) { 47 | case 3: hash += sfh_get16(data); 48 | hash ^= hash << 16; 49 | hash ^= data[sizeof(uint16_t)] << 18; 50 | hash += hash >> 11; 51 | break; 52 | case 2: hash += sfh_get16(data); 53 | hash ^= hash << 11; 54 | hash += hash >> 17; 55 | break; 56 | case 1: hash += *data; 57 | hash ^= hash << 10; 58 | hash += hash >> 1; 59 | } 60 | 61 | /* Force "avalanching" of final 127 bits */ 62 | hash ^= hash << 3; 63 | hash += hash >> 5; 64 | hash ^= hash << 4; 65 | hash += hash >> 17; 66 | hash ^= hash << 25; 67 | hash += hash >> 6; 68 | 69 | return hash; 70 | } 71 | 72 | uint32_t lmo_canon_hash(const char *str, int len) 73 | { 74 | char res[4096]; 75 | char *ptr, prev; 76 | int off; 77 | 78 | if (!str || len >= sizeof(res)) 79 | return 0; 80 | 81 | for (prev = ' ', ptr = res, off = 0; off < len; prev = *str, off++, str++) 82 | { 83 | if (isspace(*str)) 84 | { 85 | if (!isspace(prev)) 86 | *ptr++ = ' '; 87 | } 88 | else 89 | { 90 | *ptr++ = *str; 91 | } 92 | } 93 | 94 | if ((ptr > res) && isspace(*(ptr-1))) 95 | ptr--; 96 | 97 | return sfh_hash(res, ptr - res); 98 | } 99 | 100 | lmo_archive_t * lmo_open(const char *file) 101 | { 102 | int in = -1; 103 | uint32_t idx_offset = 0; 104 | struct stat s; 105 | 106 | lmo_archive_t *ar = NULL; 107 | 108 | if (stat(file, &s) == -1) 109 | goto err; 110 | 111 | if ((in = open(file, O_RDONLY)) == -1) 112 | goto err; 113 | 114 | if ((ar = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL) 115 | { 116 | memset(ar, 0, sizeof(*ar)); 117 | 118 | ar->fd = in; 119 | ar->size = s.st_size; 120 | 121 | fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC); 122 | 123 | if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED) 124 | goto err; 125 | 126 | idx_offset = ntohl(*((const uint32_t *) 127 | (ar->mmap + ar->size - sizeof(uint32_t)))); 128 | 129 | if (idx_offset >= ar->size) 130 | goto err; 131 | 132 | ar->index = (lmo_entry_t *)(ar->mmap + idx_offset); 133 | ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t); 134 | ar->end = ar->mmap + ar->size; 135 | 136 | return ar; 137 | } 138 | 139 | err: 140 | if (in > -1) 141 | close(in); 142 | 143 | if (ar != NULL) 144 | { 145 | if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED)) 146 | munmap(ar->mmap, ar->size); 147 | 148 | free(ar); 149 | } 150 | 151 | return NULL; 152 | } 153 | 154 | void lmo_close(lmo_archive_t *ar) 155 | { 156 | if (ar != NULL) 157 | { 158 | if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED)) 159 | munmap(ar->mmap, ar->size); 160 | 161 | close(ar->fd); 162 | free(ar); 163 | 164 | ar = NULL; 165 | } 166 | } 167 | 168 | 169 | lmo_catalog_t *_lmo_catalogs = NULL; 170 | lmo_catalog_t *_lmo_active_catalog = NULL; 171 | 172 | int lmo_load_catalog(const char *lang, const char *dir) 173 | { 174 | DIR *dh = NULL; 175 | char pattern[16]; 176 | char path[PATH_MAX]; 177 | struct dirent *de = NULL; 178 | 179 | lmo_archive_t *ar = NULL; 180 | lmo_catalog_t *cat = NULL; 181 | 182 | if (!lmo_change_catalog(lang)) 183 | return 0; 184 | 185 | if (!dir || !(dh = opendir(dir))) 186 | goto err; 187 | 188 | if (!(cat = malloc(sizeof(*cat)))) 189 | goto err; 190 | 191 | memset(cat, 0, sizeof(*cat)); 192 | 193 | snprintf(cat->lang, sizeof(cat->lang), "%s", lang); 194 | snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang); 195 | 196 | while ((de = readdir(dh)) != NULL) 197 | { 198 | if (!fnmatch(pattern, de->d_name, 0)) 199 | { 200 | snprintf(path, sizeof(path), "%s/%s", dir, de->d_name); 201 | ar = lmo_open(path); 202 | 203 | if (ar) 204 | { 205 | ar->next = cat->archives; 206 | cat->archives = ar; 207 | } 208 | } 209 | } 210 | 211 | closedir(dh); 212 | 213 | cat->next = _lmo_catalogs; 214 | _lmo_catalogs = cat; 215 | 216 | if (!_lmo_active_catalog) 217 | _lmo_active_catalog = cat; 218 | 219 | return 0; 220 | 221 | err: 222 | if (dh) closedir(dh); 223 | if (cat) free(cat); 224 | 225 | return -1; 226 | } 227 | 228 | int lmo_change_catalog(const char *lang) 229 | { 230 | lmo_catalog_t *cat; 231 | 232 | for (cat = _lmo_catalogs; cat; cat = cat->next) 233 | { 234 | if (!strncmp(cat->lang, lang, sizeof(cat->lang))) 235 | { 236 | _lmo_active_catalog = cat; 237 | return 0; 238 | } 239 | } 240 | 241 | return -1; 242 | } 243 | 244 | static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash) 245 | { 246 | unsigned int m, l, r; 247 | uint32_t k; 248 | 249 | l = 0; 250 | r = ar->length - 1; 251 | 252 | while (1) 253 | { 254 | m = l + ((r - l) / 2); 255 | 256 | if (r < l) 257 | break; 258 | 259 | k = ntohl(ar->index[m].key_id); 260 | 261 | if (k == hash) 262 | return &ar->index[m]; 263 | 264 | if (k > hash) 265 | { 266 | if (!m) 267 | break; 268 | 269 | r = m - 1; 270 | } 271 | else 272 | { 273 | l = m + 1; 274 | } 275 | } 276 | 277 | return NULL; 278 | } 279 | 280 | int lmo_translate(const char *key, int keylen, char **out, int *outlen) 281 | { 282 | uint32_t hash; 283 | lmo_entry_t *e; 284 | lmo_archive_t *ar; 285 | 286 | if (!key || !_lmo_active_catalog) 287 | return -2; 288 | 289 | hash = lmo_canon_hash(key, keylen); 290 | 291 | for (ar = _lmo_active_catalog->archives; ar; ar = ar->next) 292 | { 293 | if ((e = lmo_find_entry(ar, hash)) != NULL) 294 | { 295 | *out = ar->mmap + ntohl(e->offset); 296 | *outlen = ntohl(e->length); 297 | return 0; 298 | } 299 | } 300 | 301 | return -1; 302 | } 303 | 304 | void lmo_close_catalog(const char *lang) 305 | { 306 | lmo_archive_t *ar, *next; 307 | lmo_catalog_t *cat, *prev; 308 | 309 | for (prev = NULL, cat = _lmo_catalogs; cat; prev = cat, cat = cat->next) 310 | { 311 | if (!strncmp(cat->lang, lang, sizeof(cat->lang))) 312 | { 313 | if (prev) 314 | prev->next = cat->next; 315 | else 316 | _lmo_catalogs = cat->next; 317 | 318 | for (ar = cat->archives; ar; ar = next) 319 | { 320 | next = ar->next; 321 | lmo_close(ar); 322 | } 323 | 324 | free(cat); 325 | break; 326 | } 327 | } 328 | } 329 | -------------------------------------------------------------------------------- /tencentcloud_ddns/tools/po2lmo/src/template_lmo.h: -------------------------------------------------------------------------------- 1 | /* 2 | * lmo - Lua Machine Objects - General header 3 | * 4 | * Copyright (C) 2009-2012 Jo-Philipp Wich 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | #ifndef _TEMPLATE_LMO_H_ 20 | #define _TEMPLATE_LMO_H_ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #if (defined(__GNUC__) && defined(__i386__)) 38 | #define sfh_get16(d) (*((const uint16_t *) (d))) 39 | #else 40 | #define sfh_get16(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ 41 | +(uint32_t)(((const uint8_t *)(d))[0]) ) 42 | #endif 43 | 44 | 45 | struct lmo_entry { 46 | uint32_t key_id; 47 | uint32_t val_id; 48 | uint32_t offset; 49 | uint32_t length; 50 | } __attribute__((packed)); 51 | 52 | typedef struct lmo_entry lmo_entry_t; 53 | 54 | 55 | struct lmo_archive { 56 | int fd; 57 | int length; 58 | uint32_t size; 59 | lmo_entry_t *index; 60 | char *mmap; 61 | char *end; 62 | struct lmo_archive *next; 63 | }; 64 | 65 | typedef struct lmo_archive lmo_archive_t; 66 | 67 | 68 | struct lmo_catalog { 69 | char lang[6]; 70 | struct lmo_archive *archives; 71 | struct lmo_catalog *next; 72 | }; 73 | 74 | typedef struct lmo_catalog lmo_catalog_t; 75 | 76 | 77 | uint32_t sfh_hash(const char *data, int len); 78 | uint32_t lmo_canon_hash(const char *data, int len); 79 | 80 | lmo_archive_t * lmo_open(const char *file); 81 | void lmo_close(lmo_archive_t *ar); 82 | 83 | 84 | extern lmo_catalog_t *_lmo_catalogs; 85 | extern lmo_catalog_t *_lmo_active_catalog; 86 | 87 | int lmo_load_catalog(const char *lang, const char *dir); 88 | int lmo_change_catalog(const char *lang); 89 | int lmo_translate(const char *key, int keylen, char **out, int *outlen); 90 | void lmo_close_catalog(const char *lang); 91 | 92 | #endif 93 | --------------------------------------------------------------------------------