From 9c08a676a50d74772556a6a76fced7acb1796e32 Mon Sep 17 00:00:00 2001 From: CN_SZTL Date: Sat, 20 Jul 2019 21:04:28 +0800 Subject: [PATCH] add chinadns for test --- package/cnsztl/ChinaDNS/Makefile | 53 +++ package/cnsztl/luci-app-chinadns/Makefile | 83 +++++ .../files/luci/controller/chinadns.lua | 13 + .../files/luci/i18n/chinadns.zh-cn.po | 35 ++ .../files/luci/model/cbi/chinadns.lua | 48 +++ .../files/root/etc/config/chinadns | 6 + .../files/root/etc/init.d/chinadns | 62 ++++ .../files/root/etc/uci-defaults/luci-chinadns | 8 + .../luci-app-chinadns/tools/po2lmo/Makefile | 12 + .../tools/po2lmo/src/po2lmo.c | 247 +++++++++++++ .../tools/po2lmo/src/template_lmo.c | 328 ++++++++++++++++++ .../tools/po2lmo/src/template_lmo.h | 92 +++++ package/jsda/luci-app-ssr-plus-jsda/Makefile | 5 + package/jsda/openwrt-chinadns/Makefile | 48 +++ 14 files changed, 1040 insertions(+) create mode 100644 package/cnsztl/ChinaDNS/Makefile create mode 100644 package/cnsztl/luci-app-chinadns/Makefile create mode 100644 package/cnsztl/luci-app-chinadns/files/luci/controller/chinadns.lua create mode 100644 package/cnsztl/luci-app-chinadns/files/luci/i18n/chinadns.zh-cn.po create mode 100644 package/cnsztl/luci-app-chinadns/files/luci/model/cbi/chinadns.lua create mode 100644 package/cnsztl/luci-app-chinadns/files/root/etc/config/chinadns create mode 100644 package/cnsztl/luci-app-chinadns/files/root/etc/init.d/chinadns create mode 100755 package/cnsztl/luci-app-chinadns/files/root/etc/uci-defaults/luci-chinadns create mode 100644 package/cnsztl/luci-app-chinadns/tools/po2lmo/Makefile create mode 100644 package/cnsztl/luci-app-chinadns/tools/po2lmo/src/po2lmo.c create mode 100644 package/cnsztl/luci-app-chinadns/tools/po2lmo/src/template_lmo.c create mode 100644 package/cnsztl/luci-app-chinadns/tools/po2lmo/src/template_lmo.h create mode 100644 package/jsda/openwrt-chinadns/Makefile diff --git a/package/cnsztl/ChinaDNS/Makefile b/package/cnsztl/ChinaDNS/Makefile new file mode 100644 index 0000000000..13ee8b14d1 --- /dev/null +++ b/package/cnsztl/ChinaDNS/Makefile @@ -0,0 +1,53 @@ +# +# Copyright (C) 2014-2018 OpenWrt-dist +# Copyright (C) 2014-2018 Jian Chang +# +# This is free software, licensed under the GNU General Public License v3. +# See /LICENSE for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=ChinaDNS +PKG_VERSION:=2.0.1 +PKG_RELEASE:=1 + +PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz +PKG_SOURCE_URL:=https://github.com/lrinQVQ/openwrt-chinadns/releases/download/v$(PKG_VERSION) +PKG_MD5SUM:=5c7aa6a89a969a1dd6469b4808b0759b + +PKG_LICENSE:=GPLv3 +PKG_LICENSE_FILES:=LICENSE +PKG_MAINTAINER:=Jian Chang + +PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)/$(PKG_NAME)-$(PKG_VERSION) + +PKG_INSTALL:=1 +PKG_FIXUP:=autoreconf +PKG_USE_MIPS16:=0 +PKG_BUILD_PARALLEL:=1 + +include $(INCLUDE_DIR)/package.mk + +define Package/ChinaDNS + SECTION:=net + CATEGORY:=Network + TITLE:=Protect yourself against DNS poisoning in China. + URL:=https://github.com/shadowsocks/ChinaDNS +endef + +define Package/ChinaDNS/description +Protect yourself against DNS poisoning in China. +endef + +define Package/ChinaDNS/conffiles +/etc/chinadns_chnroute.txt +endef + +define Package/ChinaDNS/install + $(INSTALL_CONF) $(PKG_BUILD_DIR)/chnroute.txt $(1)/etc/chinadns_chnroute.txt + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/src/chinadns $(1)/usr/sbin +endef + +$(eval $(call BuildPackage,ChinaDNS)) diff --git a/package/cnsztl/luci-app-chinadns/Makefile b/package/cnsztl/luci-app-chinadns/Makefile new file mode 100644 index 0000000000..d9258b5a3c --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/Makefile @@ -0,0 +1,83 @@ +# +# Copyright (C) 2017 Ian Li +# +# This is free software, licensed under the GNU General Public License v3. +# See /LICENSE for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=luci-app-chinadns +PKG_VERSION:=2.0.0 +PKG_RELEASE:=beta + +PKG_LICENSE:=GPLv3 +PKG_LICENSE_FILES:=LICENSE +PKG_MAINTAINER:=Ian Li + +PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME) + +include $(INCLUDE_DIR)/package.mk + +define Package/luci-app-chinadns + SECTION:=luci + CATEGORY:=LuCI + SUBMENU:=3. Applications + TITLE:=LuCI Support for ChinaDNS + PKGARCH:=all + DEPENDS:=+ChinaDNS +endef + +define Package/luci-app-chinadns/description + LuCI Support for ChinaDNS. +endef + +define Build/Prepare + $(foreach po,$(wildcard ${CURDIR}/files/luci/i18n/*.po), \ + po2lmo $(po) $(PKG_BUILD_DIR)/$(patsubst %.po,%.lmo,$(notdir $(po)));) +endef + +define Build/Configure +endef + +define Build/Compile +endef + +define Package/luci-app-chinadns/postinst +#!/bin/sh +if [ -z "$${IPKG_INSTROOT}" ]; then + if [ -f /etc/uci-defaults/luci-chinadns ]; then + ( . /etc/uci-defaults/luci-chinadns ) && \ + rm -f /etc/uci-defaults/luci-chinadns + fi + rm -rf /tmp/luci-indexcache +fi +exit 0 +endef + +define Package/luci-app-chinadns/postrm +#!/bin/sh +rm -f /tmp/luci-indexcache +exit 0 +endef + +define Package/luci-app-chinadns/conffiles +/etc/config/chinadns +endef + +define Package/luci-app-chinadns/install + $(INSTALL_DIR) $(1)/usr/lib/lua/luci/i18n + $(INSTALL_DATA) $(PKG_BUILD_DIR)/chinadns.*.lmo $(1)/usr/lib/lua/luci/i18n/ + $(INSTALL_DIR) $(1)/usr/lib/lua/luci/controller + $(INSTALL_DATA) ./files/luci/controller/chinadns.lua $(1)/usr/lib/lua/luci/controller/chinadns.lua + $(INSTALL_DIR) $(1)/usr/lib/lua/luci/model/cbi/chinadns + $(INSTALL_DATA) ./files/luci/model/cbi/chinadns.lua $(1)/usr/lib/lua/luci/model/cbi/chinadns.lua + $(INSTALL_DIR) $(1)/etc/config + $(INSTALL_DATA) ./files/root/etc/config/chinadns $(1)/etc/config/chinadns + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_BIN) ./files/root/etc/init.d/chinadns $(1)/etc/init.d/chinadns + $(INSTALL_DIR) $(1)/etc/uci-defaults + $(INSTALL_BIN) ./files/root/etc/uci-defaults/luci-chinadns $(1)/etc/uci-defaults/luci-chinadns +endef + +$(eval $(call BuildPackage,luci-app-chinadns)) \ No newline at end of file diff --git a/package/cnsztl/luci-app-chinadns/files/luci/controller/chinadns.lua b/package/cnsztl/luci-app-chinadns/files/luci/controller/chinadns.lua new file mode 100644 index 0000000000..a037ad3a9d --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/files/luci/controller/chinadns.lua @@ -0,0 +1,13 @@ +-- Copyright (C) 2016 OpenWrt-dist +-- Copyright (C) 2016 Jian Chang +-- Licensed to the public under the GNU General Public License v3. + +module("luci.controller.chinadns", package.seeall) + +function index() + if not nixio.fs.access("/etc/config/chinadns") then + return + end + + entry({"admin", "services", "chinadns"}, cbi("chinadns"), _("ChinaDNS"), 70).dependent = true +end diff --git a/package/cnsztl/luci-app-chinadns/files/luci/i18n/chinadns.zh-cn.po b/package/cnsztl/luci-app-chinadns/files/luci/i18n/chinadns.zh-cn.po new file mode 100644 index 0000000000..abbfb3e548 --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/files/luci/i18n/chinadns.zh-cn.po @@ -0,0 +1,35 @@ +msgid "" +msgstr "Content-Type: text/plain; charset=UTF-8\n" + +msgid "ChinaDNS" +msgstr "ChinaDNS" + +msgid "CHNRoute File" +msgstr "国内路由表" + +msgid "Enable" +msgstr "启用" + +msgid "Foreign Subnet" +msgstr "外网子网" + +msgid "Just foreign subnet. The subnet of interface WAN will be used as local subnet." +msgstr "仅填写外网子网地址即可。接口WAN的子网地址将被用作本地子网地址。" + +msgid "General Setting" +msgstr "基本设置" + +msgid "Local Port" +msgstr "本地端口" + +msgid "NOT RUNNING" +msgstr "未运行" + +msgid "RUNNING" +msgstr "运行中" + +msgid "Upstream Server" +msgstr "上游服务器" + +msgid "A DNS server with edns-client-subnet support required." +msgstr "需要具有edns-client-subnet支持的DNS服务器。" diff --git a/package/cnsztl/luci-app-chinadns/files/luci/model/cbi/chinadns.lua b/package/cnsztl/luci-app-chinadns/files/luci/model/cbi/chinadns.lua new file mode 100644 index 0000000000..7ccd036d2a --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/files/luci/model/cbi/chinadns.lua @@ -0,0 +1,48 @@ +-- Copyright (C) 2016 OpenWrt-dist +-- Copyright (C) 2016 Jian Chang +-- Copyright (C) 2017 Ian Li +-- Licensed to the public under the GNU General Public License v3. + +local m, s, o + +if luci.sys.call("pidof chinadns >/dev/null") == 0 then + m = Map("chinadns", translate("ChinaDNS"), "%s - %s" %{translate("ChinaDNS"), translate("RUNNING")}) +else + m = Map("chinadns", translate("ChinaDNS"), "%s - %s" %{translate("ChinaDNS"), translate("NOT RUNNING")}) +end + +s = m:section(TypedSection, "chinadns", translate("General Setting")) +s.anonymous = true + +o = s:option(Flag, "enable", translate("Enable")) +o.rmempty = false + +o = s:option(Value, "port", translate("Local Port")) +o.placeholder = 5353 +o.default = 5353 +o.datatype = "port" +o.rmempty = false + +o = s:option(Value, "chnroute", translate("CHNRoute File")) +o.placeholder = "/etc/chinadns_chnroute.txt" +o.default = "/etc/chinadns_chnroute.txt" +o.datatype = "file" +o.rmempty = false + +o = s:option(Value, "foreign_subnet", + translate("Foreign Subnet"), + translate("Just foreign subnet. The subnet of interface WAN will be used as local subnet.")) +o.placeholder = "45.76.96.0" +o.default = "45.76.96.0" +o.datatype = "ipaddr" +o.rmempty = false + +o = s:option(Value, "server", + translate("Upstream Server"), + translate("A DNS server with edns-client-subnet support required.")) +o.placeholder = "8.8.8.8" +o.default = "8.8.8.8" +o.datatype = "ipaddr" +o.rmempty = false + +return m \ No newline at end of file diff --git a/package/cnsztl/luci-app-chinadns/files/root/etc/config/chinadns b/package/cnsztl/luci-app-chinadns/files/root/etc/config/chinadns new file mode 100644 index 0000000000..8ef7301e85 --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/files/root/etc/config/chinadns @@ -0,0 +1,6 @@ +config chinadns + option chnroute '/etc/chinadns_chnroute.txt' + option enable '0' + option port '5300' + option server '8.8.8.8' + option foreign_subnet '45.76.96.0' diff --git a/package/cnsztl/luci-app-chinadns/files/root/etc/init.d/chinadns b/package/cnsztl/luci-app-chinadns/files/root/etc/init.d/chinadns new file mode 100644 index 0000000000..316ea89c42 --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/files/root/etc/init.d/chinadns @@ -0,0 +1,62 @@ +#!/bin/sh /etc/rc.common +# +# Copyright (C) 2017 Jian Chang , Ian Li +# +# This is free software, licensed under the GNU General Public License v3. +# See /LICENSE for more information. +# + +START=90 + +SERVICE_USE_PID=1 +SERVICE_WRITE_PID=1 +SERVICE_DAEMONIZE=1 + +xappend() { + parms="$parms $1" +} + +append_bool() { + local section="$1" + local option="$2" + local value="$3" + local _loctmp + config_get_bool _loctmp "$section" "$option" + [ "$_loctmp" = 1 ] || return 0 + xappend "$value" +} + +append_parm() { + local section="$1" + local option="$2" + local switch="$3" + local _loctmp + config_get _loctmp "$section" "$option" + [ -z "$_loctmp" ] && return 0 + xappend "$switch $_loctmp" +} + +start_chinadns() { + local enable + local local_subnet=$(echo `ifstatus wan | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.'`0) + local foreign_subnet + config_get_bool enable $1 enable + [ "$enable" = 1 ] || return 0 + config_get foreign_subnet $1 foreign_subnet + append_parm $1 port "-p" + append_parm $1 server "-s" + append_parm $1 chnroute "-c" + xappend "-e $local_subnet,$foreign_subnet" + service_start /usr/sbin/chinadns \ + $parms + return $? +} + +start() { + config_load chinadns + config_foreach start_chinadns chinadns +} + +stop() { + service_stop /usr/bin/chinadns +} diff --git a/package/cnsztl/luci-app-chinadns/files/root/etc/uci-defaults/luci-chinadns b/package/cnsztl/luci-app-chinadns/files/root/etc/uci-defaults/luci-chinadns new file mode 100755 index 0000000000..47e155717c --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/files/root/etc/uci-defaults/luci-chinadns @@ -0,0 +1,8 @@ +#!/bin/sh +uci -q batch <<-EOF >/dev/null + delete ucitrack.@ChinaDNS[-1] + add ucitrack ChinaDNS + set ucitrack.@ChinaDNS[-1].init=ChinaDNS + commit ucitrack +EOF +exit 0 diff --git a/package/cnsztl/luci-app-chinadns/tools/po2lmo/Makefile b/package/cnsztl/luci-app-chinadns/tools/po2lmo/Makefile new file mode 100644 index 0000000000..ad2c133207 --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/tools/po2lmo/Makefile @@ -0,0 +1,12 @@ + +INSTALL = install +PREFIX = /usr/bin + +po2lmo: src/po2lmo.o src/template_lmo.o + $(CC) $(LDFLAGS) -o src/po2lmo src/po2lmo.o src/template_lmo.o + +install: + $(INSTALL) -m 755 src/po2lmo $(PREFIX) + +clean: + $(RM) src/po2lmo src/*.o diff --git a/package/cnsztl/luci-app-chinadns/tools/po2lmo/src/po2lmo.c b/package/cnsztl/luci-app-chinadns/tools/po2lmo/src/po2lmo.c new file mode 100644 index 0000000000..0da792b680 --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/tools/po2lmo/src/po2lmo.c @@ -0,0 +1,247 @@ +/* + * lmo - Lua Machine Objects - PO to LMO conversion tool + * + * Copyright (C) 2009-2012 Jo-Philipp Wich + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "template_lmo.h" + +static void die(const char *msg) +{ + fprintf(stderr, "Error: %s\n", msg); + exit(1); +} + +static void usage(const char *name) +{ + fprintf(stderr, "Usage: %s input.po output.lmo\n", name); + exit(1); +} + +static void print(const void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + if( fwrite(ptr, size, nmemb, stream) == 0 ) + die("Failed to write stdout"); +} + +static int extract_string(const char *src, char *dest, int len) +{ + int pos = 0; + int esc = 0; + int off = -1; + + for( pos = 0; (pos < strlen(src)) && (pos < len); pos++ ) + { + if( (off == -1) && (src[pos] == '"') ) + { + off = pos + 1; + } + else if( off >= 0 ) + { + if( esc == 1 ) + { + switch (src[pos]) + { + case '"': + case '\\': + off++; + break; + } + dest[pos-off] = src[pos]; + esc = 0; + } + else if( src[pos] == '\\' ) + { + dest[pos-off] = src[pos]; + esc = 1; + } + else if( src[pos] != '"' ) + { + dest[pos-off] = src[pos]; + } + else + { + dest[pos-off] = '\0'; + break; + } + } + } + + return (off > -1) ? strlen(dest) : -1; +} + +static int cmp_index(const void *a, const void *b) +{ + uint32_t x = ((const lmo_entry_t *)a)->key_id; + uint32_t y = ((const lmo_entry_t *)b)->key_id; + + if (x < y) + return -1; + else if (x > y) + return 1; + + return 0; +} + +static void print_uint32(uint32_t x, FILE *out) +{ + uint32_t y = htonl(x); + print(&y, sizeof(uint32_t), 1, out); +} + +static void print_index(void *array, int n, FILE *out) +{ + lmo_entry_t *e; + + qsort(array, n, sizeof(*e), cmp_index); + + for (e = array; n > 0; n--, e++) + { + print_uint32(e->key_id, out); + print_uint32(e->val_id, out); + print_uint32(e->offset, out); + print_uint32(e->length, out); + } +} + +int main(int argc, char *argv[]) +{ + char line[4096]; + char key[4096]; + char val[4096]; + char tmp[4096]; + int state = 0; + int offset = 0; + int length = 0; + int n_entries = 0; + void *array = NULL; + lmo_entry_t *entry = NULL; + uint32_t key_id, val_id; + + FILE *in; + FILE *out; + + if( (argc != 3) || ((in = fopen(argv[1], "r")) == NULL) || ((out = fopen(argv[2], "w")) == NULL) ) + usage(argv[0]); + + memset(line, 0, sizeof(key)); + memset(key, 0, sizeof(val)); + memset(val, 0, sizeof(val)); + + while( (NULL != fgets(line, sizeof(line), in)) || (state >= 2 && feof(in)) ) + { + if( state == 0 && strstr(line, "msgid \"") == line ) + { + switch(extract_string(line, key, sizeof(key))) + { + case -1: + die("Syntax error in msgid"); + case 0: + state = 1; + break; + default: + state = 2; + } + } + else if( state == 1 || state == 2 ) + { + if( strstr(line, "msgstr \"") == line || state == 2 ) + { + switch(extract_string(line, val, sizeof(val))) + { + case -1: + state = 4; + break; + default: + state = 3; + } + } + else + { + switch(extract_string(line, tmp, sizeof(tmp))) + { + case -1: + state = 2; + break; + default: + strcat(key, tmp); + } + } + } + else if( state == 3 ) + { + switch(extract_string(line, tmp, sizeof(tmp))) + { + case -1: + state = 4; + break; + default: + strcat(val, tmp); + } + } + + if( state == 4 ) + { + if( strlen(key) > 0 && strlen(val) > 0 ) + { + key_id = sfh_hash(key, strlen(key)); + val_id = sfh_hash(val, strlen(val)); + + if( key_id != val_id ) + { + n_entries++; + array = realloc(array, n_entries * sizeof(lmo_entry_t)); + entry = (lmo_entry_t *)array + n_entries - 1; + + if (!array) + die("Out of memory"); + + entry->key_id = key_id; + entry->val_id = val_id; + entry->offset = offset; + entry->length = strlen(val); + + length = strlen(val) + ((4 - (strlen(val) % 4)) % 4); + + print(val, length, 1, out); + offset += length; + } + } + + state = 0; + memset(key, 0, sizeof(key)); + memset(val, 0, sizeof(val)); + } + + memset(line, 0, sizeof(line)); + } + + print_index(array, n_entries, out); + + if( offset > 0 ) + { + print_uint32(offset, out); + fsync(fileno(out)); + fclose(out); + } + else + { + fclose(out); + unlink(argv[2]); + } + + fclose(in); + return(0); +} diff --git a/package/cnsztl/luci-app-chinadns/tools/po2lmo/src/template_lmo.c b/package/cnsztl/luci-app-chinadns/tools/po2lmo/src/template_lmo.c new file mode 100644 index 0000000000..27205a7228 --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/tools/po2lmo/src/template_lmo.c @@ -0,0 +1,328 @@ +/* + * lmo - Lua Machine Objects - Base functions + * + * Copyright (C) 2009-2010 Jo-Philipp Wich + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "template_lmo.h" + +/* + * Hash function from http://www.azillionmonkeys.com/qed/hash.html + * Copyright (C) 2004-2008 by Paul Hsieh + */ + +uint32_t sfh_hash(const char *data, int len) +{ + uint32_t hash = len, tmp; + int rem; + + if (len <= 0 || data == NULL) return 0; + + rem = len & 3; + len >>= 2; + + /* Main loop */ + for (;len > 0; len--) { + hash += sfh_get16(data); + tmp = (sfh_get16(data+2) << 11) ^ hash; + hash = (hash << 16) ^ tmp; + data += 2*sizeof(uint16_t); + hash += hash >> 11; + } + + /* Handle end cases */ + switch (rem) { + case 3: hash += sfh_get16(data); + hash ^= hash << 16; + hash ^= data[sizeof(uint16_t)] << 18; + hash += hash >> 11; + break; + case 2: hash += sfh_get16(data); + hash ^= hash << 11; + hash += hash >> 17; + break; + case 1: hash += *data; + hash ^= hash << 10; + hash += hash >> 1; + } + + /* Force "avalanching" of final 127 bits */ + hash ^= hash << 3; + hash += hash >> 5; + hash ^= hash << 4; + hash += hash >> 17; + hash ^= hash << 25; + hash += hash >> 6; + + return hash; +} + +uint32_t lmo_canon_hash(const char *str, int len) +{ + char res[4096]; + char *ptr, prev; + int off; + + if (!str || len >= sizeof(res)) + return 0; + + for (prev = ' ', ptr = res, off = 0; off < len; prev = *str, off++, str++) + { + if (isspace(*str)) + { + if (!isspace(prev)) + *ptr++ = ' '; + } + else + { + *ptr++ = *str; + } + } + + if ((ptr > res) && isspace(*(ptr-1))) + ptr--; + + return sfh_hash(res, ptr - res); +} + +lmo_archive_t * lmo_open(const char *file) +{ + int in = -1; + uint32_t idx_offset = 0; + struct stat s; + + lmo_archive_t *ar = NULL; + + if (stat(file, &s) == -1) + goto err; + + if ((in = open(file, O_RDONLY)) == -1) + goto err; + + if ((ar = (lmo_archive_t *)malloc(sizeof(*ar))) != NULL) + { + memset(ar, 0, sizeof(*ar)); + + ar->fd = in; + ar->size = s.st_size; + + fcntl(ar->fd, F_SETFD, fcntl(ar->fd, F_GETFD) | FD_CLOEXEC); + + if ((ar->mmap = mmap(NULL, ar->size, PROT_READ, MAP_SHARED, ar->fd, 0)) == MAP_FAILED) + goto err; + + idx_offset = ntohl(*((const uint32_t *) + (ar->mmap + ar->size - sizeof(uint32_t)))); + + if (idx_offset >= ar->size) + goto err; + + ar->index = (lmo_entry_t *)(ar->mmap + idx_offset); + ar->length = (ar->size - idx_offset - sizeof(uint32_t)) / sizeof(lmo_entry_t); + ar->end = ar->mmap + ar->size; + + return ar; + } + +err: + if (in > -1) + close(in); + + if (ar != NULL) + { + if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED)) + munmap(ar->mmap, ar->size); + + free(ar); + } + + return NULL; +} + +void lmo_close(lmo_archive_t *ar) +{ + if (ar != NULL) + { + if ((ar->mmap != NULL) && (ar->mmap != MAP_FAILED)) + munmap(ar->mmap, ar->size); + + close(ar->fd); + free(ar); + + ar = NULL; + } +} + + +lmo_catalog_t *_lmo_catalogs = NULL; +lmo_catalog_t *_lmo_active_catalog = NULL; + +int lmo_load_catalog(const char *lang, const char *dir) +{ + DIR *dh = NULL; + char pattern[16]; + char path[PATH_MAX]; + struct dirent *de = NULL; + + lmo_archive_t *ar = NULL; + lmo_catalog_t *cat = NULL; + + if (!lmo_change_catalog(lang)) + return 0; + + if (!dir || !(dh = opendir(dir))) + goto err; + + if (!(cat = malloc(sizeof(*cat)))) + goto err; + + memset(cat, 0, sizeof(*cat)); + + snprintf(cat->lang, sizeof(cat->lang), "%s", lang); + snprintf(pattern, sizeof(pattern), "*.%s.lmo", lang); + + while ((de = readdir(dh)) != NULL) + { + if (!fnmatch(pattern, de->d_name, 0)) + { + snprintf(path, sizeof(path), "%s/%s", dir, de->d_name); + ar = lmo_open(path); + + if (ar) + { + ar->next = cat->archives; + cat->archives = ar; + } + } + } + + closedir(dh); + + cat->next = _lmo_catalogs; + _lmo_catalogs = cat; + + if (!_lmo_active_catalog) + _lmo_active_catalog = cat; + + return 0; + +err: + if (dh) closedir(dh); + if (cat) free(cat); + + return -1; +} + +int lmo_change_catalog(const char *lang) +{ + lmo_catalog_t *cat; + + for (cat = _lmo_catalogs; cat; cat = cat->next) + { + if (!strncmp(cat->lang, lang, sizeof(cat->lang))) + { + _lmo_active_catalog = cat; + return 0; + } + } + + return -1; +} + +static lmo_entry_t * lmo_find_entry(lmo_archive_t *ar, uint32_t hash) +{ + unsigned int m, l, r; + uint32_t k; + + l = 0; + r = ar->length - 1; + + while (1) + { + m = l + ((r - l) / 2); + + if (r < l) + break; + + k = ntohl(ar->index[m].key_id); + + if (k == hash) + return &ar->index[m]; + + if (k > hash) + { + if (!m) + break; + + r = m - 1; + } + else + { + l = m + 1; + } + } + + return NULL; +} + +int lmo_translate(const char *key, int keylen, char **out, int *outlen) +{ + uint32_t hash; + lmo_entry_t *e; + lmo_archive_t *ar; + + if (!key || !_lmo_active_catalog) + return -2; + + hash = lmo_canon_hash(key, keylen); + + for (ar = _lmo_active_catalog->archives; ar; ar = ar->next) + { + if ((e = lmo_find_entry(ar, hash)) != NULL) + { + *out = ar->mmap + ntohl(e->offset); + *outlen = ntohl(e->length); + return 0; + } + } + + return -1; +} + +void lmo_close_catalog(const char *lang) +{ + lmo_archive_t *ar, *next; + lmo_catalog_t *cat, *prev; + + for (prev = NULL, cat = _lmo_catalogs; cat; prev = cat, cat = cat->next) + { + if (!strncmp(cat->lang, lang, sizeof(cat->lang))) + { + if (prev) + prev->next = cat->next; + else + _lmo_catalogs = cat->next; + + for (ar = cat->archives; ar; ar = next) + { + next = ar->next; + lmo_close(ar); + } + + free(cat); + break; + } + } +} diff --git a/package/cnsztl/luci-app-chinadns/tools/po2lmo/src/template_lmo.h b/package/cnsztl/luci-app-chinadns/tools/po2lmo/src/template_lmo.h new file mode 100644 index 0000000000..57f59aa56b --- /dev/null +++ b/package/cnsztl/luci-app-chinadns/tools/po2lmo/src/template_lmo.h @@ -0,0 +1,92 @@ +/* + * lmo - Lua Machine Objects - General header + * + * Copyright (C) 2009-2012 Jo-Philipp Wich + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _TEMPLATE_LMO_H_ +#define _TEMPLATE_LMO_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if (defined(__GNUC__) && defined(__i386__)) +#define sfh_get16(d) (*((const uint16_t *) (d))) +#else +#define sfh_get16(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ + +(uint32_t)(((const uint8_t *)(d))[0]) ) +#endif + + +struct lmo_entry { + uint32_t key_id; + uint32_t val_id; + uint32_t offset; + uint32_t length; +} __attribute__((packed)); + +typedef struct lmo_entry lmo_entry_t; + + +struct lmo_archive { + int fd; + int length; + uint32_t size; + lmo_entry_t *index; + char *mmap; + char *end; + struct lmo_archive *next; +}; + +typedef struct lmo_archive lmo_archive_t; + + +struct lmo_catalog { + char lang[6]; + struct lmo_archive *archives; + struct lmo_catalog *next; +}; + +typedef struct lmo_catalog lmo_catalog_t; + + +uint32_t sfh_hash(const char *data, int len); +uint32_t lmo_canon_hash(const char *data, int len); + +lmo_archive_t * lmo_open(const char *file); +void lmo_close(lmo_archive_t *ar); + + +extern lmo_catalog_t *_lmo_catalogs; +extern lmo_catalog_t *_lmo_active_catalog; + +int lmo_load_catalog(const char *lang, const char *dir); +int lmo_change_catalog(const char *lang); +int lmo_translate(const char *key, int keylen, char **out, int *outlen); +void lmo_close_catalog(const char *lang); + +#endif diff --git a/package/jsda/luci-app-ssr-plus-jsda/Makefile b/package/jsda/luci-app-ssr-plus-jsda/Makefile index 10f6f1e8ca..42119cb3b9 100644 --- a/package/jsda/luci-app-ssr-plus-jsda/Makefile +++ b/package/jsda/luci-app-ssr-plus-jsda/Makefile @@ -46,6 +46,10 @@ config PACKAGE_$(PKG_NAME)_INCLUDE_Shadowsocks_Socks bool "Include Shadowsocks Socks and Tunnel" default n +config PACKAGE_$(PKG_NAME)_INCLUDE_ChinaDNS + bool "ChinaDNS" + default n + config PACKAGE_$(PKG_NAME)_INCLUDE_dnscrypt_proxy bool "dnscrypt-proxy-full" default n @@ -73,6 +77,7 @@ define Package/luci-app-ssr-plus-jsda +PACKAGE_$(PKG_NAME)_INCLUDE_Shadowsocks_Server:shadowsocks-libev-ss-server \ +PACKAGE_$(PKG_NAME)_INCLUDE_ShadowsocksR_Socks:shadowsocksr-libev-ssr-local \ +PACKAGE_$(PKG_NAME)_INCLUDE_Shadowsocks_Socks:shadowsocks-libev-ss-local \ + +PACKAGE_$(PKG_NAME)_INCLUDE_ChinaDNS:openwrt_chinadns +PACKAGE_$(PKG_NAME)_INCLUDE_dnscrypt_proxy:dnscrypt-proxy-full \ +PACKAGE_$(PKG_NAME)_INCLUDE_dnsforwarder:openwrt-dnsforwarder \ +PACKAGE_$(PKG_NAME)_INCLUDE_haproxy:haproxy diff --git a/package/jsda/openwrt-chinadns/Makefile b/package/jsda/openwrt-chinadns/Makefile new file mode 100644 index 0000000000..6740a05110 --- /dev/null +++ b/package/jsda/openwrt-chinadns/Makefile @@ -0,0 +1,48 @@ +# +# Copyright (C) 2014-2018 OpenWrt-dist +# Copyright (C) 2014-2018 Jian Chang +# +# This is free software, licensed under the GNU General Public License v3. +# See /LICENSE for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=ChinaDNS +PKG_VERSION:=2.0.1 +PKG_RELEASE:=1 + +PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz +PKG_SOURCE_URL:=https://github.com/lrinQVQ/openwrt-chinadns/releases/download/v$(PKG_VERSION) +PKG_MD5SUM:=5c7aa6a89a969a1dd6469b4808b0759b + +PKG_LICENSE:=GPLv3 +PKG_LICENSE_FILES:=LICENSE +PKG_MAINTAINER:=Jian Chang + +PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)/$(PKG_NAME)-$(PKG_VERSION) + +PKG_INSTALL:=1 +PKG_FIXUP:=autoreconf +PKG_USE_MIPS16:=0 +PKG_BUILD_PARALLEL:=1 + +include $(INCLUDE_DIR)/package.mk + +define Package/ChinaDNS + SECTION:=net + CATEGORY:=Network + TITLE:=Protect yourself against DNS poisoning in China. + URL:=https://github.com/shadowsocks/ChinaDNS +endef + +define Package/ChinaDNS/description +Protect yourself against DNS poisoning in China. +endef + +define Package/ChinaDNS/install + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/src/chinadns $(1)/usr/bin +endef + +$(eval $(call BuildPackage,ChinaDNS))