From 283c7a0a3bd62ecddf5b15428fc4df893a2f1e1f Mon Sep 17 00:00:00 2001 From: zxlhhyccc Date: Sun, 24 Nov 2024 22:25:08 +0800 Subject: [PATCH] luci-app-ssr-plus: Add `apple domains data` update settings and replace 'apple DNS' support. **** Fix `Netflix IP` update code. Signed-off-by: Zxl hhyccc --- .../model/cbi/shadowsocksr/advanced.lua | 11 ++++ .../luasrc/model/cbi/shadowsocksr/status.lua | 11 ++++ luci-app-ssr-plus/po/zh_Hans/ssr-plus.po | 12 +++++ .../root/etc/init.d/shadowsocksr | 8 +++ .../root/usr/share/shadowsocksr/update.lua | 54 ++++++++++++++++++- 5 files changed, 95 insertions(+), 1 deletion(-) diff --git a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/advanced.lua b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/advanced.lua index 5fc774ff14c..0dc68d3992e 100644 --- a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/advanced.lua +++ b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/advanced.lua @@ -113,6 +113,17 @@ o = s:option(Flag, "apple_optimization", translate("Apple domains optimization") o.rmempty = false o.default = "1" +o = s:option(Value, "apple_url", translate("Apple Domains Update url")) +o:value("https://fastly.jsdelivr.net/gh/felixonmars/dnsmasq-china-list/apple.china.conf", translate("felixonmars/dnsmasq-china-list")) +o.default = "https://fastly.jsdelivr.net/gh/felixonmars/dnsmasq-china-list/apple.china.conf" +o:depends("apple_optimization", "1") + +o = s:option(Value, "apple_dns", translate("Apple Domains DNS"), translate("If empty, Not change Apple domains parsing DNS")) +o.rmempty = true +o.default = "" +o.datatype = "ip4addr" +o:depends("apple_optimization", "1") + o = s:option(Flag, "adblock", translate("Enable adblock")) o.rmempty = false diff --git a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/status.lua b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/status.lua index a095d44e908..2a5c5e20567 100644 --- a/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/status.lua +++ b/luci-app-ssr-plus/luasrc/model/cbi/shadowsocksr/status.lua @@ -48,6 +48,10 @@ if nixio.fs.access("/etc/ssrplus/china_ssr.txt") then ip_count = tonumber(luci.sys.exec("cat /etc/ssrplus/china_ssr.txt | wc -l")) end +if nixio.fs.access("/etc/ssrplus/applechina.conf") then + apple_count = tonumber(luci.sys.exec("cat /etc/ssrplus/applechina.conf | wc -l")) +end + if nixio.fs.access("/etc/ssrplus/netflixip.list") then nfip_count = tonumber(luci.sys.exec("cat /etc/ssrplus/netflixip.list | wc -l")) end @@ -169,6 +173,13 @@ s.rawhtml = true s.template = "shadowsocksr/refresh" s.value = ip_count .. " " .. translate("Records") +if uci:get_first("shadowsocksr", 'global', 'apple_optimization', '0') ~= '0' then + s = m:field(DummyValue, "apple_data", translate("Apple Domains Data")) + s.rawhtml = true + s.template = "shadowsocksr/refresh" + s.value = apple_count .. " " .. translate("Records") +end + if uci:get_first("shadowsocksr", 'global', 'netflix_enable', '0') ~= '0' then s = m:field(DummyValue, "nfip_data", translate("Netflix IP Data")) s.rawhtml = true diff --git a/luci-app-ssr-plus/po/zh_Hans/ssr-plus.po b/luci-app-ssr-plus/po/zh_Hans/ssr-plus.po index 18f19a7ebc0..8785fc8c08b 100644 --- a/luci-app-ssr-plus/po/zh_Hans/ssr-plus.po +++ b/luci-app-ssr-plus/po/zh_Hans/ssr-plus.po @@ -410,6 +410,9 @@ msgstr "【中国大陆 IP 段】数据库" msgid "Netflix IP Data" msgstr "【Netflix IP 段】数据库" +msgid "Apple Domains Data" +msgstr "【Apple 域名】数据库" + msgid "Advertising Data" msgstr "【广告屏蔽】数据库" @@ -470,6 +473,15 @@ msgstr "启用广告屏蔽" msgid "adblock_url" msgstr "广告屏蔽更新 URL" +msgid "Apple Domains Update url" +msgstr "Apple 域名更新 URL" + +msgid "Apple Domains DNS" +msgstr "Apple 域名 DNS" + +msgid "If empty, Not change Apple domains parsing DNS" +msgstr "如果为空,则不更改 Apple 域名解析 DNS" + msgid "gfwlist Update url" msgstr "GFW 列表更新 URL" diff --git a/luci-app-ssr-plus/root/etc/init.d/shadowsocksr b/luci-app-ssr-plus/root/etc/init.d/shadowsocksr index 740bdce3016..8edf65b4f1d 100755 --- a/luci-app-ssr-plus/root/etc/init.d/shadowsocksr +++ b/luci-app-ssr-plus/root/etc/init.d/shadowsocksr @@ -258,6 +258,14 @@ start_dns() { fi if [ "$(uci_get_by_type global apple_optimization 1)" == "1" ]; then + local new_appledns="$(uci_get_by_type global apple_dns)" + if [ -n "$new_appledns" ]; then + sed -i 's/[[:space:]]//g' /etc/ssrplus/applechina.conf #去除所有空白字符 + local old_appledns=$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' /etc/ssrplus/applechina.conf | sort -u) + if [ -n "$old_appledns" ] && [ "$old_appledns" != "$new_appledns" ]; then + sed -i "s,$(printf '%s' "$old_appledns"),$(printf '%s' "$new_appledns"),g" /etc/ssrplus/applechina.conf + fi + fi echolog "Apple 域名中国大陆 CDN 的 优化规则正在加载。" cp -f /etc/ssrplus/applechina.conf $TMP_DNSMASQ_PATH/ echolog "Apple 域名中国大陆 CDN 的 优化规则加载完毕。" diff --git a/luci-app-ssr-plus/root/usr/share/shadowsocksr/update.lua b/luci-app-ssr-plus/root/usr/share/shadowsocksr/update.lua index d786864b363..be93bfdf0f8 100755 --- a/luci-app-ssr-plus/root/usr/share/shadowsocksr/update.lua +++ b/luci-app-ssr-plus/root/usr/share/shadowsocksr/update.lua @@ -59,6 +59,7 @@ local excluded_domain = { local mydnsip = '127.0.0.1' local mydnsport = '5335' local ipsetname = 'gfwlist' +local new_appledns = uci:get_first("shadowsocksr", "global", "apple_dns") local bc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -- base64decoding local function base64_dec(data) @@ -112,6 +113,29 @@ local function generate_gfwlist(type) os.remove("/tmp/ssr-update.tmp") end +-- 更换 Apple dns +local function generate_apple(type) + local domains, domains_map = {}, {} + local out = io.open("/tmp/ssr-update." .. type, "w") + for line in io.lines("/tmp/ssr-update.tmp") do + if not (string.find(line, comment_pattern)) then + local start, finish, match = string.find(line, domain_pattern) + if start and not domains_map[match] then + domains_map[match] = true + match = string.gsub(match, "%s", "") --从域名中去除所有空白字符 + table.insert(domains, match) + end + end + end + for _, domain in ipairs(domains) do + if new_appledns and new_appledns ~= "" then + out:write(string.format("address=/%s/%s\n", domain, new_appledns)) + end + end + out:close() + os.remove("/tmp/ssr-update.tmp") +end + -- adblock转码至dnsmasq格式 local function generate_adblock(type) local domains, domains_map = {}, {} @@ -159,6 +183,22 @@ local function update(url, file, type, file2) generate_gfwlist(type) Num = 2 end + if type == "apple_data" then + local apple = io.open("/tmp/ssr-update." .. type, "r") + local decode = apple:read("*a") + if decode:find("address=") then + apple:close() + else + apple:close() + -- 写回applechina + apple = io.open("/tmp/ssr-update.tmp", "w") + apple:write(decode) + apple:close() + if new_appledns and new_appledns ~= "" then + generate_apple(type) + end + end + end if type == "ad_data" then local adblock = io.open("/tmp/ssr-update." .. type, "r") local decode = adblock:read("*a") @@ -217,12 +257,16 @@ if args then update(uci:get_first("shadowsocksr", "global", "chnroute_url"), "/etc/ssrplus/china_ssr.txt", args, TMP_PATH .. "/china_ssr.txt") os.exit(0) end + if args == "apple_data" then + update(uci:get_first("shadowsocksr", "global", "apple_url"), "/etc/ssrplus/applechina.conf", args, TMP_DNSMASQ_PATH .. "/applechina.conf") + os.exit(0) + end if args == "ad_data" then update(uci:get_first("shadowsocksr", "global", "adblock_url"), "/etc/ssrplus/ad.conf", args, TMP_DNSMASQ_PATH .. "/ad.conf") os.exit(0) end if args == "nfip_data" then - update(uci:get_first("shadowsocksr", "global", "nfip_url"), "/etc/ssrplus/netflixip.list", args) + update(uci:get_first("shadowsocksr", "global", "nfip_url"), "/etc/ssrplus/netflixip.list", args, TMP_DNSMASQ_PATH .. "/netflixip.list") os.exit(0) end else @@ -230,10 +274,18 @@ else update(uci:get_first("shadowsocksr", "global", "gfwlist_url"), "/etc/ssrplus/gfw_list.conf", "gfw_data", TMP_DNSMASQ_PATH .. "/gfw_list.conf") log("正在更新【国内IP段】数据库") update(uci:get_first("shadowsocksr", "global", "chnroute_url"), "/etc/ssrplus/china_ssr.txt", "ip_data", TMP_PATH .. "/china_ssr.txt") + if uci:get_first("shadowsocksr", "global", "apple_optimization", "0") == "1" then + log("正在更新【Apple域名】数据库") + update(uci:get_first("shadowsocksr", "global", "apple_url"), "/etc/ssrplus/applechina.conf", "apple_data", TMP_DNSMASQ_PATH .. "/applechina.conf") + end if uci:get_first("shadowsocksr", "global", "adblock", "0") == "1" then log("正在更新【广告屏蔽】数据库") update(uci:get_first("shadowsocksr", "global", "adblock_url"), "/etc/ssrplus/ad.conf", "ad_data", TMP_DNSMASQ_PATH .. "/ad.conf") end + if uci:get_first("shadowsocksr", "global", "netflix_enable", "0") == "1" then + log("正在更新【Netflix IP段】数据库") + update(uci:get_first("shadowsocksr", "global", "nfip_url"), "/etc/ssrplus/netflixip.list", "nfip_data", TMP_DNSMASQ_PATH .. "/netflixip.list") + end -- log("正在更新【Netflix IP段】数据库") -- update(uci:get_first("shadowsocksr", "global", "nfip_url"), "/etc/ssrplus/netflixip.list", "nfip_data") end