├── Makefile ├── README.md ├── files ├── etc │ ├── config │ │ └── pdnsd │ ├── pdnsd_gfw.cfg │ └── pdnsd_init.sh └── usr │ └── lib │ └── lua │ └── luci │ ├── controller │ └── pdnsd.lua │ └── model │ └── cbi │ └── pdnsd.lua ├── i18n └── zh-cn │ └── pdnsd.zh-cn.po └── tools └── po2lmo ├── Makefile └── src ├── po2lmo ├── po2lmo.c ├── po2lmo.o ├── template_lmo.c ├── template_lmo.h └── template_lmo.o /Makefile: -------------------------------------------------------------------------------- 1 | include $(TOPDIR)/rules.mk 2 | 3 | PKG_NAME:=luci-app-pdnsd 4 | PKG_VERSION=1.2 5 | PKG_RELEASE:=1 6 | PKG_MAINTAINER:=Alex Zhuo <1886090@gmail.com> 7 | 8 | PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME) 9 | 10 | include $(INCLUDE_DIR)/package.mk 11 | 12 | define Package/$(PKG_NAME) 13 | CATEGORY:=Utilities 14 | SUBMENU:=Luci 15 | PKGARCH:=all 16 | TITLE:=luci for pdnsd to invoid DNS pollution 17 | DEPENDS:=+pdnsd 18 | endef 19 | 20 | define Package/$(PKG_NAME)/description 21 | A luci app for pdnsd 22 | endef 23 | 24 | define Package/$(PKG_NAME)/postinst 25 | #!/bin/sh 26 | rm -rf /tmp/luci* 27 | 28 | endef 29 | 30 | define Build/Prepare 31 | $(foreach po,$(wildcard ${CURDIR}/i18n/zh-cn/*.po), \ 32 | po2lmo $(po) $(PKG_BUILD_DIR)/$(patsubst %.po,%.lmo,$(notdir $(po)));) 33 | endef 34 | 35 | define Build/Configure 36 | endef 37 | 38 | define Build/Compile 39 | endef 40 | 41 | define Package/$(PKG_NAME)/install 42 | $(INSTALL_DIR) $(1)/usr/lib/lua/luci/i18n 43 | $(INSTALL_DATA) $(PKG_BUILD_DIR)/*.*.lmo $(1)/usr/lib/lua/luci/i18n/ 44 | $(CP) ./files/* $(1)/ 45 | 46 | endef 47 | 48 | $(eval $(call BuildPackage,$(PKG_NAME))) 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # luci-app-pdnsd 2 | 为OpenWRT路由器Pdnsd设计的luci页面和配置文件,用于使用TCP协议请求DNS以方便代理转发或避免投毒,已在[该固件][A]中使用 3 | 4 | 简介 5 | --- 6 | 7 | 本软件包为OpenWRT路由器使用Pdnsd进行TCP协议的DNS请求提供了修改配置文件的luci页面并在应用之后自动配置dnsmasq并重启Pdnsd 8 | 9 | 默认显示的配置文件: /etc/pdnsd_gfw.cfg 10 | 11 | 默认监听端口: `0.0.0.0:5053` 12 | 13 | 默认模式: tcp_only 14 | 15 | 默认上游DNS服务器: `114.114.114.114` 16 | 17 | 默认备用服务器: `OpenDNS` 18 | 19 | 默认备用服务器DNS端口 `53` 20 | 21 | 该配置文件无需修改即可防止DNS污染。点击【保存&应用】后会将此配置文件覆盖"/etc/pdnsd.conf"并重启pdnsd,同时会后台自动修改【DHCP/DNS】的【DNS转发】为【127.0.0.1#5053】并勾选【忽略解析文件】并重启dnsmasq,刷新Pdnsd界面会自动检查这个配置,如果没有配置成功会以红色字提示。在取消启用后会自动恢复【DHCP/DNS】的配置。但是需要你手动去点击【DHCP/DNS】的【保存&应用】否则重启会失效并红字提示。 22 | 使用效果和更多使用方法请[参考这里][A] 23 | 24 | 25 | 依赖 26 | --- 27 | 28 | 显式依赖 `pdnsd`,支持开机自启,但是重启请注意【DHCP/DNS】中的【DNS转发】有没有成功配置成【127.0.0.1#5053】并【忽略解析文件】 29 | 30 | 配置 31 | --- 32 | 33 | 进入luci页面默认读取的是 "/etc/pdnsd_gfw.cfg" ,点击保存&应用后会以此文件覆盖"/etc/pdnsd.conf" 34 | 35 | 编译 36 | --- 37 | 38 | 从 OpenWrt 的 [SDK][openwrt-sdk] 编译 39 | ```bash 40 | # 解压下载好的 SDK 41 | tar xjf OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 42 | cd OpenWrt-SDK-ar71xx-* 43 | # Clone 项目 44 | git clone https://github.com/AlexZhuo/luci-app-pdnsd package/luci-app-pdnsd 45 | # 编译 po2lmo (如果有po2lmo可跳过) 46 | pushd package/luci-app-pdnsd/tools/po2lmo 47 | make && sudo make install 48 | popd 49 | # 选择要编译的包 Utilities -> LuCI -> luci-app-pdnsd 50 | make menuconfig 51 | # 开始编译 52 | make package/luci-app-pdnsd/compile V=99 53 | ``` 54 | 55 | 截图 56 | --- 57 | 58 | ![demo](https://github.com/AlexZhuo/BreakwallOpenWrt/raw/master/screenshots/pdnsd.png) 59 | [A]: http://www.right.com.cn/forum/thread-198649-1-1.html 60 | [openwrt-sdk]: https://wiki.openwrt.org/doc/howto/obtain.firmware.sdk 61 | -------------------------------------------------------------------------------- /files/etc/config/pdnsd: -------------------------------------------------------------------------------- 1 | 2 | config arguments 3 | option enabled '0' 4 | 5 | -------------------------------------------------------------------------------- /files/etc/pdnsd_gfw.cfg: -------------------------------------------------------------------------------- 1 | 2 | global { 3 | perm_cache=512; # dns缓存大小,单位KB,建议不要写的太大 4 | cache_dir="/var/pdnsd"; # 缓存文件的位置 5 | server_ip = 0.0.0.0; # pdnsd监听的网卡,0.0.0.0是全部网卡 6 | server_port=5053; # pdnsd监听的端口,不要和别的服务冲突即可 7 | status_ctl = on; 8 | paranoid=on; # 二次请求模式,如果请求主DNS服务器返回的是垃圾地址,就向备用服务器请求 9 | query_method=tcp_only; # 请求模式,推荐使用仅TCP模式,UDP模式一般需要二次请求 10 | neg_domain_pol = off; 11 | par_queries = 400; # 最多同时请求数 12 | min_ttl = 1d; # DNS结果最短缓存时间 13 | max_ttl = 1w; # DNS结果最长缓存时间 14 | timeout = 10; # DNS请求超时时间,单位秒 15 | } 16 | 17 | /* 上游DNS服务器的配置 */ 18 | server { 19 | label = "routine"; # 这个随便写 20 | ip = 114.114.114.114; # 这里为主要上级 dns 的 ip 地址,建议填写一个当地最快的DNS地址 21 | timeout = 5; # DNS请求超时时间 22 | reject = 74.125.127.102, # 以下是脏IP,也就是DNS污染一般会返回的结果,如果收到如下DNS结果会触发二次请求(TCP协议一般不会碰到脏IP) 23 | 74.125.155.102, 24 | 74.125.39.102, 25 | 74.125.39.113, 26 | 209.85.229.138, 27 | 128.121.126.139, 28 | 159.106.121.75, 29 | 169.132.13.103, 30 | 192.67.198.6, 31 | 202.106.1.2, 32 | 202.181.7.85, 33 | 203.161.230.171, 34 | 203.98.7.65, 35 | 207.12.88.98, 36 | 208.56.31.43, 37 | 209.145.54.50, 38 | 209.220.30.174, 39 | 209.36.73.33, 40 | 211.94.66.147, 41 | 213.169.251.35, 42 | 216.221.188.182, 43 | 216.234.179.13, 44 | 243.185.187.39, 45 | 37.61.54.158, 46 | 4.36.66.178, 47 | 46.82.174.68, 48 | 59.24.3.173, 49 | 64.33.88.161, 50 | 64.33.99.47, 51 | 64.66.163.251, 52 | 65.104.202.252, 53 | 65.160.219.113, 54 | 66.45.252.237, 55 | 69.55.52.253, 56 | 72.14.205.104, 57 | 72.14.205.99, 58 | 78.16.49.15, 59 | 8.7.198.45, 60 | 93.46.8.89, 61 | 37.61.54.158, 62 | 243.185.187.39, 63 | 190.93.247.4, 64 | 190.93.246.4, 65 | 190.93.245.4, 66 | 190.93.244.4, 67 | 65.49.2.178, 68 | 189.163.17.5, 69 | 23.89.5.60, 70 | 49.2.123.56, 71 | 54.76.135.1, 72 | 77.4.7.92, 73 | 118.5.49.6, 74 | 159.24.3.173, 75 | 188.5.4.96, 76 | 197.4.4.12, 77 | 220.250.64.24, 78 | 243.185.187.30, 79 | 249.129.46.48, 80 | 253.157.14.165; 81 | reject_policy = fail; 82 | exclude = ".google.com", 83 | ".cn", #排除国内DNS解析,如果正常翻,则可以在前面加#注释 84 | ".baidu.com", #排除国内DNS解析,如果正常翻,则可以在前面加#注释 85 | ".qq.com", #排除国内DNS解析,如果正常翻,则可以在前面加#注释 86 | ".gstatic.com", 87 | ".googleusercontent.com", 88 | ".googlepages.com", 89 | ".googlevideo.com", 90 | ".googlecode.com", 91 | ".googleapis.com", 92 | ".googlesource.com", 93 | ".googledrive.com", 94 | ".ggpht.com", 95 | ".youtube.com", 96 | ".youtu.be", 97 | ".ytimg.com", 98 | ".twitter.com", 99 | ".facebook.com", 100 | ".fastly.net", 101 | ".akamai.net", 102 | ".akamaiedge.net", 103 | ".akamaihd.net", 104 | ".edgesuite.net", 105 | ".edgekey.net"; 106 | } 107 | 108 | server { 109 | # 以下为备用DNS服务器的配置,也是二次请求服务器的配置 110 | label = "special"; # 这个随便写 111 | ip = 208.67.222.222,208.67.220.220; # 这里为备用DNS服务器的 ip 地址 112 | port = 5353; # 推荐使用53以外的端口(DNS服务器必须支持) 113 | proxy_only = on; 114 | timeout = 5; 115 | } 116 | 117 | source { 118 | owner=localhost; 119 | // serve_aliases=on; 120 | file="/etc/hosts"; 121 | } 122 | 123 | rr { 124 | name=localhost; 125 | reverse=on; 126 | a=127.0.0.1; 127 | owner=localhost; 128 | soa=localhost,root.localhost,42,86400,900,86400,86400; 129 | } 130 | -------------------------------------------------------------------------------- /files/etc/pdnsd_init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh /etc/rc.common 2 | restart(){ 3 | echo luci for pdnsd 4 | sleep 1s 5 | local vt_enabled=`uci get pdnsd.@arguments[0].enabled 2>/dev/null` 6 | echo $vt_enabled 7 | logger -t alex !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!pdnsd is initializing enabled is $vt_enabled 8 | 9 | if [ $vt_enabled = 1 ]; then 10 | logger -t alex restarting pdnsd 11 | echo "restarting pdnsd" 12 | uci delete dhcp.@dnsmasq[0].server 13 | uci add_list dhcp.@dnsmasq[0].server=127.0.0.1#5053 14 | uci delete dhcp.@dnsmasq[0].resolvfile 15 | uci set dhcp.@dnsmasq[0].noresolv=1 16 | uci commit dhcp 17 | cp /etc/pdnsd_gfw.cfg /etc/pdnsd.conf 18 | /etc/init.d/dnsmasq restart 19 | /etc/init.d/pdnsd enable 20 | /etc/init.d/pdnsd restart 21 | 22 | else 23 | logger -t alex stopping pdnsd 24 | echo "stopping pdnsd" 25 | /etc/init.d/pdnsd disable 26 | /etc/init.d/pdnsd stop 27 | uci del_list dhcp.@dnsmasq[0].server=127.0.0.1#5053 28 | uci set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.auto 29 | uci delete dhcp.@dnsmasq[0].noresolv 30 | uci commit dhcp 31 | /etc/init.d/dnsmasq restart 32 | fi 33 | } -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/controller/pdnsd.lua: -------------------------------------------------------------------------------- 1 | module("luci.controller.pdnsd", package.seeall) 2 | function index() 3 | if not nixio.fs.access("/etc/config/pdnsd") then 4 | return 5 | end 6 | entry({"admin", "services", "pdnsd"}, cbi("pdnsd"), _("Pdnsd")).dependent = true 7 | end -------------------------------------------------------------------------------- /files/usr/lib/lua/luci/model/cbi/pdnsd.lua: -------------------------------------------------------------------------------- 1 | --Alex<1886090@gmail.com> 2 | local fs = require "nixio.fs" 3 | local CONFIG_FILE = "/etc/pdnsd_gfw.cfg"; 4 | 5 | function sync_value_to_file(value, file) 6 | value = value:gsub("\r\n?", "\n") 7 | local old_value = nixio.fs.readfile(file) 8 | if value ~= old_value then 9 | nixio.fs.writefile(file, value) 10 | end 11 | 12 | end 13 | local state_msg = "" 14 | 15 | local pdnsd_on = string.len(luci.sys.exec("cat /var/run/pdnsd.pid"))>0 16 | local other_pdnsd = (luci.sys.call("pidof pdnsd > /dev/null") == 0) 17 | 18 | local resolv_file = luci.sys.exec("uci get dhcp.@dnsmasq[0].resolvfile") 19 | if pdnsd_on then 20 | state_msg = "" .. translate("Running") .. "" 21 | else 22 | state_msg = "" .. translate("Not running") .. "" 23 | end 24 | local listen_port = luci.sys.exec("uci get dhcp.@dnsmasq[0].server") 25 | if pdnsd_on and string.sub(listen_port,1,14) == "127.0.0.1#5053" then 26 | state_msg=state_msg .. ",并恭喜你DNS转发正常" 27 | elseif pdnsd_on then 28 | state_msg=state_msg .. "但是当前DNS转发为{" .. listen_port .."}请到【DHCP/DNS】修改为【127.0.0.1#5053】,否则Pdnsd无效果" 29 | elseif other_pdnsd then 30 | state_msg=state_msg .. ",但是有其他程序开启了Pdnsd,如ShadowsocksR" 31 | end 32 | if resolv_file=="" then 33 | 34 | else if pdnsd_on then 35 | state_msg=state_msg .. "请到【DHCP/DNS】【HOSTS和解析文件】勾上【忽略解析文件】的勾,否则Pdnsd可能无效果" 36 | end 37 | end 38 | m=Map("pdnsd",translate("Pdnsd"),translate("Pdnsd可以通过TCP协议进行DNS解析,可以方便的使用iptables进行透明代理,配合ipset、GFWList使用效果更佳。默认上游服务器为114DNS,可以在【DHCP/DNS】中设置【DNS转发】为【127.0.0.1#5053】即可将全局DNS请求交给pdnsd进行解析,由于pdnsd自带缓存,所以可以加快解析速度。另外你可以使用dig来检测DNS解析情况,方法dig @127.0.0.1 -p 5053 www.facebook.com ,使用说明请点击这里").. "

状态 - " .. state_msg) 39 | s=m:section(TypedSection,"arguments","") 40 | s.addremove=false 41 | s.anonymous=true 42 | view_enable = s:option(Flag,"enabled",translate("Enable")) 43 | view_cfg = s:option(TextValue, "1", nil) 44 | view_cfg.rmempty = false 45 | view_cfg.rows = 43 46 | 47 | function view_cfg.cfgvalue() 48 | return nixio.fs.readfile(CONFIG_FILE) or "" 49 | end 50 | function view_cfg.write(self, section, value) 51 | sync_value_to_file(value, CONFIG_FILE) 52 | end 53 | -- --------------------------------------------------- 54 | local apply = luci.http.formvalue("cbi.apply") 55 | if apply then 56 | os.execute("/etc/pdnsd_init.sh restart >/dev/null 2>&1 &") 57 | end 58 | 59 | return m 60 | -------------------------------------------------------------------------------- /i18n/zh-cn/pdnsd.zh-cn.po: -------------------------------------------------------------------------------- 1 | msgid "Enable" 2 | msgstr "启用" 3 | 4 | msgid "Running" 5 | msgstr "运行中" 6 | 7 | msgid "Not running" 8 | msgstr "未运行" 9 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tools/po2lmo/src/po2lmo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexZhuo/luci-app-pdnsd/a03b315c6f6d0cffb2321c3daefad130782fa777/tools/po2lmo/src/po2lmo -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tools/po2lmo/src/po2lmo.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexZhuo/luci-app-pdnsd/a03b315c6f6d0cffb2321c3daefad130782fa777/tools/po2lmo/src/po2lmo.o -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tools/po2lmo/src/template_lmo.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexZhuo/luci-app-pdnsd/a03b315c6f6d0cffb2321c3daefad130782fa777/tools/po2lmo/src/template_lmo.o --------------------------------------------------------------------------------