Skip to content

Commit

Permalink
feat: prepare chinadns-ng v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
1715173329 authored Apr 2, 2024
1 parent 2580ef7 commit 0d2f0d8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
36 changes: 23 additions & 13 deletions htdocs/luci-static/resources/view/homeproxy/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ return view.extend({
o.rmempty = false;

o = s.taboption('routing', form.Value, 'dns_server', _('DNS server'),
_('You can only have one server set. It MUST support TCP query.'));
_('It MUST support TCP query.'));
o.value('wan', _('Use DNS server from WAN'));
o.value('1.1.1.1', _('CloudFlare Public DNS (1.1.1.1)'));
o.value('208.67.222.222', _('Cisco Public DNS (208.67.222.222)'));
Expand All @@ -182,27 +182,37 @@ return view.extend({
}

if (features.hp_has_chinadns_ng) {
o = s.taboption('routing', form.Value, 'china_dns_server', _('China DNS server'),
_('You can only have two servers set at maximum.'));
o.value('', _('Disable'));
o = s.taboption('routing', form.DynamicList, 'china_dns_server', _('China DNS server'));
o.value('wan', _('Use DNS server from WAN'));
o.value('wan_114', _('Use DNS server from WAN + 114DNS'));
o.value('223.5.5.5', _('Aliyun Public DNS (223.5.5.5)'));
o.value('210.2.4.8', _('CNNIC Public DNS (210.2.4.8)'));
o.value('119.29.29.29', _('Tencent Public DNS (119.29.29.29)'));
o.value('114.114.114.114', _('Xinfeng Public DNS (114.114.114.114)'));
o.depends('routing_mode', 'bypass_mainland_china');
o.validate = function(section_id, value) {
if (section_id && value && !['wan', 'wan_114'].includes(value)) {
var dns_servers = value.split(',');
var ipv6_support = this.map.lookupOption('ipv6_support', section_id)[0].formvalue(section_id);
o.validate = function(section_id) {
if (section_id) {
var value = this.map.lookupOption('china_dns_server', section_id)[0].formvalue(section_id);
if (value.length < 1)
return true;

if (dns_servers.length > 2)
if (!features.hp_has_chinadns_ng_v2 && value.length > 2)
return _('You can only have two servers set at maximum.');

for (var i of dns_servers)
if (!stubValidator.apply((ipv6_support === '1') ? 'ipaddr' : 'ip4addr', i))
return _('Expecting: %s').format(_('valid IP address'));
for (var dns of value) {
var ipv6_support = this.map.lookupOption('ipv6_support', section_id)[0].formvalue(section_id);
if (dns === 'wan') {
continue;
} else {
var err = _('Expecting: %s').format(_('valid address#port'));
dns = dns.split('#');
if (dns.length > 2)
return err;
if (!stubValidator.apply((ipv6_support === '1') ? 'ipaddr' : 'ip4addr', dns[0]))
return err;
if (dns[1] && !stubValidator.apply('port', dns[1]))
return err;
}
}
}

return true;
Expand Down
11 changes: 7 additions & 4 deletions root/etc/init.d/homeproxy
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,8 @@ start_service() {
# chinadns-ng
if [ -n "$china_dns_server" ]; then
local wandns="$(ifstatus wan | jsonfilter -e '@["dns-server"][0]' || echo "119.29.29.29")"
case "$china_dns_server" in
"wan") china_dns_server="$wandns" ;;
"wan_114") china_dns_server="$wandns,114.114.114.114" ;;
esac
china_dns_server="${china_dns_server/wan/$wandns}"
china_dns_server="${china_dns_server// /,}"

for i in $(seq 1 "$(grep -c "processor" "/proc/cpuinfo")"); do
procd_open_instance "chinadns-ng-$i"
Expand All @@ -199,6 +197,11 @@ start_service() {
[ "$ipv6_support" -eq "1" ] || procd_append_param command --no-ipv6=tC
procd_append_param command --reuse-port

if chinadns-ng --version | grep -q "target:"; then
procd_append_param command --cache 10000
procd_append_param command --cache-stale 3600
fi

if [ -x "/sbin/ujail" ]; then
procd_add_jail "chinadns-ng" log
procd_add_jail_mount "$HP_DIR/resources/china_list.txt"
Expand Down
15 changes: 15 additions & 0 deletions root/etc/uci-defaults/luci-homeproxy-migration
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

china_dns_server="$(uci -q get "homeproxy.config.china_dns_server")"
if [ "$china_dns_server" = "wan_114" ]; then
uci -q delete homeproxy.config.china_dns_server
uci -q add_list "homeproxy.config.china_dns_server"="wan"
uci -q add_list "homeproxy.config.china_dns_server"="114.114.114.114"
elif echo "$china_dns_server" | grep -q ","; then
uci -q delete homeproxy.config.china_dns_server
for dns in ${china_dns_server//,/ }; do
uci -q add_list "homeproxy.config.china_dns_server"="$dns"
done
fi

exit 0
2 changes: 2 additions & 0 deletions root/usr/share/rpcd/ucode/luci.homeproxy
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ const methods = {
}

features.hp_has_chinadns_ng = access('/usr/bin/chinadns-ng');
if (features.hp_has_chinadns_ng)
features.hp_has_chinadns_ng_v2 = (system('/usr/bin/chinadns-ng --version | grep -q "target:"') === 0);
features.hp_has_ip_full = access('/usr/libexec/ip-full');
features.hp_has_tcp_brutal = hasKernelModule('brutal.ko');
features.hp_has_tproxy = hasKernelModule('nft_tproxy.ko') || access('/etc/modules.d/nft-tproxy');
Expand Down

0 comments on commit 0d2f0d8

Please sign in to comment.