update.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/lua
  2. ------------------------------------------------
  3. -- This file is part of the luci-app-ssr-plus update.lua
  4. -- By Mattraks
  5. ------------------------------------------------
  6. require "luci.sys"
  7. require "luci.model.uci"
  8. local icount = 0
  9. local args = arg[1]
  10. local uci = luci.model.uci.cursor()
  11. -- 以下设置更新数据库至 DNSMASQ 路径
  12. -- 获取 DEFAULT_DNSMASQ_CFGID
  13. local DEFAULT_DNSMASQ_CFGID = uci:get_first("dhcp", "dnsmasq", ".name")
  14. -- 查找包含 conf-dir 选项的 dnsmasq.conf 文件路径
  15. local DNSMASQ_CONF_PATH = string.format("grep -l '^conf-dir=' /tmp/etc/dnsmasq.conf.%s*", DEFAULT_DNSMASQ_CFGID):gsub("%s+", "") -- 去除空白字符
  16. -- 获取 DNSMASQ_CONF_DIR
  17. local DNSMASQ_CONF_DIR = string.format("grep '^conf-dir=' %s | cut -d'=' -f2 | head -n 1", DNSMASQ_CONF_PATH):gsub("%s+", "") -- 去除空白字符
  18. -- 设置 TMP_DNSMASQ_PATH 路径
  19. local TMP_DNSMASQ_PATH = DNSMASQ_CONF_DIR .. "/dnsmasq-ssrplus.d"
  20. local TMP_PATH = "/var/etc/ssrplus"
  21. -- match comments/title/whitelist/ip address/excluded_domain
  22. local comment_pattern = "^[!\\[@]+"
  23. local ip_pattern = "^%d+%.%d+%.%d+%.%d+"
  24. local domain_pattern = "([%w%-%_]+%.[%w%.%-%_]+)[%/%*]*"
  25. local excluded_domain = {
  26. "apple.com", "sina.cn", "sina.com.cn", "baidu.com", "byr.cn", "jlike.com",
  27. "weibo.com", "zhongsou.com", "youdao.com", "sogou.com", "so.com", "soso.com",
  28. "aliyun.com", "taobao.com", "jd.com", "qq.com"
  29. }
  30. -- gfwlist parameter
  31. local mydnsip = '127.0.0.1'
  32. local mydnsport = '5335'
  33. local ipsetname = 'gfwlist'
  34. local bc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  35. -- base64decoding
  36. local function base64_dec(data)
  37. data = string.gsub(data, '[^' .. bc .. '=]', '')
  38. return (data:gsub('.', function(x)
  39. if (x == '=') then
  40. return ''
  41. end
  42. local r, f = '', (bc:find(x) - 1)
  43. for i = 6, 1, -1 do
  44. r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and '1' or '0')
  45. end
  46. return r;
  47. end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
  48. if (#x ~= 8) then
  49. return ''
  50. end
  51. local c = 0
  52. for i = 1, 8 do
  53. c = c + (x:sub(i, i) == '1' and 2 ^ (8 - i) or 0)
  54. end
  55. return string.char(c)
  56. end))
  57. end
  58. -- check if domain is excluded
  59. local function check_excluded_domain(value)
  60. for _, domain in ipairs(excluded_domain) do
  61. if value:find(domain) then
  62. return true
  63. end
  64. end
  65. end
  66. -- gfwlist转码至dnsmasq格式
  67. local function generate_gfwlist(type)
  68. local domains, domains_map = {}, {}
  69. local out = io.open("/tmp/ssr-update." .. type, "w")
  70. for line in io.lines("/tmp/ssr-update.tmp") do
  71. if not (string.find(line, comment_pattern) or string.find(line, ip_pattern) or check_excluded_domain(line)) then
  72. local start, finish, match = string.find(line, domain_pattern)
  73. if start and not domains_map[match] then
  74. domains_map[match] = true
  75. table.insert(domains, match)
  76. end
  77. end
  78. end
  79. for _, domain in ipairs(domains) do
  80. out:write(string.format("server=/%s/%s#%s\n", domain, mydnsip, mydnsport))
  81. out:write(string.format("ipset=/%s/%s\n", domain, ipsetname))
  82. end
  83. out:close()
  84. os.remove("/tmp/ssr-update.tmp")
  85. end
  86. -- adblock转码至dnsmasq格式
  87. local function generate_adblock(type)
  88. local domains, domains_map = {}, {}
  89. local out = io.open("/tmp/ssr-update." .. type, "w")
  90. for line in io.lines("/tmp/ssr-update.tmp") do
  91. if not (string.find(line, comment_pattern)) then
  92. local start, finish, match = string.find(line, domain_pattern)
  93. if start and not domains_map[match] then
  94. domains_map[match] = true
  95. table.insert(domains, match)
  96. end
  97. end
  98. end
  99. for _, domain in ipairs(domains) do
  100. out:write(string.format("address=/%s/\n", domain))
  101. end
  102. out:close()
  103. os.remove("/tmp/ssr-update.tmp")
  104. end
  105. local log = function(...)
  106. if args then
  107. print("{ret=" .. table.concat({...}, ",retcount=") .. "}")
  108. else
  109. print(os.date("%Y-%m-%d %H:%M:%S ") .. table.concat({...}, " "))
  110. end
  111. end
  112. local function update(url, file, type, file2)
  113. local Num = 1
  114. local refresh_cmd = "wget --no-check-certificate -q -O /tmp/ssr-update." .. type .. " " .. url
  115. local sret = luci.sys.call(refresh_cmd)
  116. if sret == 0 then
  117. if type == "gfw_data" then
  118. local gfwlist = io.open("/tmp/ssr-update." .. type, "r")
  119. local decode = gfwlist:read("*a")
  120. if not decode:find("google") then
  121. decode = base64_dec(decode)
  122. end
  123. gfwlist:close()
  124. -- 写回gfwlist
  125. gfwlist = io.open("/tmp/ssr-update.tmp", "w")
  126. gfwlist:write(decode)
  127. gfwlist:close()
  128. generate_gfwlist(type)
  129. Num = 2
  130. end
  131. if type == "ad_data" then
  132. local adblock = io.open("/tmp/ssr-update." .. type, "r")
  133. local decode = adblock:read("*a")
  134. if decode:find("address=") then
  135. adblock:close()
  136. else
  137. adblock:close()
  138. -- 写回adblock
  139. adblock = io.open("/tmp/ssr-update.tmp", "w")
  140. adblock:write(decode)
  141. adblock:close()
  142. generate_adblock(type)
  143. end
  144. end
  145. local new_md5 = luci.sys.exec("echo -n $([ -f '/tmp/ssr-update." .. type .. "' ] && md5sum /tmp/ssr-update." .. type .. " | awk '{print $1}')")
  146. local old_md5 = luci.sys.exec("echo -n $([ -f '" .. file .. "' ] && md5sum " .. file .. " | awk '{print $1}')")
  147. if new_md5 == old_md5 then
  148. if args then
  149. log(1)
  150. else
  151. log("你已经是最新数据,无需更新!")
  152. end
  153. else
  154. icount = luci.sys.exec("cat /tmp/ssr-update." .. type .. " | wc -l")
  155. luci.sys.exec("cp -f /tmp/ssr-update." .. type .. " " .. file)
  156. if file2 then
  157. luci.sys.exec("cp -f /tmp/ssr-update." .. type .. " " .. file2)
  158. end
  159. if type == "gfw_data" or type == "ad_data" then
  160. luci.sys.call("/usr/share/shadowsocksr/gfw2ipset.sh")
  161. else
  162. luci.sys.call("/usr/share/shadowsocksr/chinaipset.sh " .. TMP_PATH .. "/china_ssr.txt")
  163. end
  164. if args then
  165. log(0, tonumber(icount) / Num)
  166. else
  167. log("更新成功! 新的总记录数:" .. tostring(tonumber(icount) / Num))
  168. end
  169. end
  170. else
  171. if args then
  172. log(-1)
  173. else
  174. log("更新失败!")
  175. end
  176. end
  177. os.remove("/tmp/ssr-update." .. type)
  178. end
  179. if args then
  180. if args == "gfw_data" then
  181. update(uci:get_first("shadowsocksr", "global", "gfwlist_url"), "/etc/ssrplus/gfw_list.conf", args, TMP_DNSMASQ_PATH .. "/gfw_list.conf")
  182. os.exit(0)
  183. end
  184. if args == "ip_data" then
  185. update(uci:get_first("shadowsocksr", "global", "chnroute_url"), "/etc/ssrplus/china_ssr.txt", args, TMP_PATH .. "/china_ssr.txt")
  186. os.exit(0)
  187. end
  188. if args == "ad_data" then
  189. update(uci:get_first("shadowsocksr", "global", "adblock_url"), "/etc/ssrplus/ad.conf", args, TMP_DNSMASQ_PATH .. "/ad.conf")
  190. os.exit(0)
  191. end
  192. if args == "nfip_data" then
  193. update(uci:get_first("shadowsocksr", "global", "nfip_url"), "/etc/ssrplus/netflixip.list", args)
  194. os.exit(0)
  195. end
  196. else
  197. log("正在更新【GFW列表】数据库")
  198. update(uci:get_first("shadowsocksr", "global", "gfwlist_url"), "/etc/ssrplus/gfw_list.conf", "gfw_data", TMP_DNSMASQ_PATH .. "/gfw_list.conf")
  199. log("正在更新【国内IP段】数据库")
  200. update(uci:get_first("shadowsocksr", "global", "chnroute_url"), "/etc/ssrplus/china_ssr.txt", "ip_data", TMP_PATH .. "/china_ssr.txt")
  201. if uci:get_first("shadowsocksr", "global", "adblock", "0") == "1" then
  202. log("正在更新【广告屏蔽】数据库")
  203. update(uci:get_first("shadowsocksr", "global", "adblock_url"), "/etc/ssrplus/ad.conf", "ad_data", TMP_DNSMASQ_PATH .. "/ad.conf")
  204. end
  205. -- log("正在更新【Netflix IP段】数据库")
  206. -- update(uci:get_first("shadowsocksr", "global", "nfip_url"), "/etc/ssrplus/netflixip.list", "nfip_data")
  207. end