update.lua 6.4 KB

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