shadowsocksr.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. -- Copyright (C) 2017 yushi studio <[email protected]>
  2. -- Licensed to the public under the GNU General Public License v3.
  3. module("luci.controller.shadowsocksr", package.seeall)
  4. function index()
  5. if not nixio.fs.access("/etc/config/shadowsocksr") then
  6. return
  7. end
  8. entry({"admin", "services", "shadowsocksr"}, alias("admin", "services", "shadowsocksr", "client"),_("ShadowSocksR Plus+"), 10).dependent = true
  9. entry({"admin", "services", "shadowsocksr", "client"}, cbi("shadowsocksr/client"),_("SSR Client"), 10).leaf = true
  10. entry({"admin", "services", "shadowsocksr", "servers"}, arcombine(cbi("shadowsocksr/servers", {autoapply = true}), cbi("shadowsocksr/client-config")),_("Severs Nodes"), 20).leaf = true
  11. entry({"admin", "services", "shadowsocksr", "control"},cbi("shadowsocksr/control"), _("Access Control"), 30).leaf = true
  12. entry({"admin", "services", "shadowsocksr", "advanced"},cbi("shadowsocksr/advanced"),_("Advanced Settings"), 50).leaf = true
  13. entry({"admin", "services", "shadowsocksr", "server"},arcombine(cbi("shadowsocksr/server"), cbi("shadowsocksr/server-config")),_("SSR Server"), 60).leaf = true
  14. entry({"admin", "services", "shadowsocksr", "status"},form("shadowsocksr/status"),_("Status"), 70).leaf = true
  15. entry({"admin", "services", "shadowsocksr", "check"}, call("check_status"))
  16. entry({"admin", "services", "shadowsocksr", "refresh"}, call("refresh_data"))
  17. entry({"admin", "services", "shadowsocksr", "subscribe"}, call("subscribe"))
  18. entry({"admin", "services", "shadowsocksr", "checkport"}, call("check_port"))
  19. entry({"admin", "services", "shadowsocksr", "log"},form("shadowsocksr/log"),_("Log"), 80).leaf = true
  20. entry({"admin", "services", "shadowsocksr","run"},call("act_status")).leaf = true
  21. entry({"admin", "services", "shadowsocksr", "ping"}, call("act_ping")).leaf = true
  22. end
  23. function subscribe()
  24. luci.sys.call("/usr/bin/lua /usr/share/shadowsocksr/subscribe.lua >> /tmp/ssrplus.log 2>&1")
  25. luci.http.prepare_content("application/json")
  26. luci.http.write_json({ret = 1})
  27. end
  28. function act_status()
  29. local e = {}
  30. e.running = luci.sys.call("busybox ps -w | grep ssr-retcp | grep -v grep >/dev/null") == 0
  31. luci.http.prepare_content("application/json")
  32. luci.http.write_json(e)
  33. end
  34. function act_ping()
  35. local e = {}
  36. local domain = luci.http.formvalue("domain")
  37. local port = luci.http.formvalue("port")
  38. e.index = luci.http.formvalue("index")
  39. local iret = luci.sys.call(" ipset add ss_spec_wan_ac " .. domain .. " 2>/dev/null")
  40. local socket = nixio.socket("inet", "stream")
  41. socket:setopt("socket", "rcvtimeo", 3)
  42. socket:setopt("socket", "sndtimeo", 3)
  43. e.socket = socket:connect(domain, port)
  44. socket:close()
  45. e.ping = luci.sys.exec("ping -c 1 -W 1 %q 2>&1 | grep -o 'time=[0-9]*.[0-9]' | awk -F '=' '{print$2}'" % domain)
  46. if (e.ping == "") then
  47. e.ping = luci.sys.exec(string.format("echo -n $(tcpping -c 1 -i 1 -p %s %s 2>&1 | grep -o 'ttl=[0-9]* time=[0-9]*.[0-9]' | awk -F '=' '{print$3}') 2>/dev/null",port, domain))
  48. end
  49. if (iret == 0) then
  50. luci.sys.call(" ipset del ss_spec_wan_ac " .. domain)
  51. end
  52. luci.http.prepare_content("application/json")
  53. luci.http.write_json(e)
  54. end
  55. function check_status()
  56. local set = "/usr/bin/ssr-check www." .. luci.http.formvalue("set") .. ".com 80 3 1"
  57. sret = luci.sys.call(set)
  58. if sret == 0 then
  59. retstring = "0"
  60. else
  61. retstring = "1"
  62. end
  63. luci.http.prepare_content("application/json")
  64. luci.http.write_json({ret = retstring})
  65. end
  66. function refresh_data()
  67. local set = luci.http.formvalue("set")
  68. local uci = luci.model.uci.cursor()
  69. local icount = 0
  70. local retstring = 0
  71. local function update(url, file, type, file2)
  72. local Num = 1
  73. refresh_cmd = "wget-ssl --no-check-certificate -t 3 -T 10 -O- " .. url .. " > /tmp/ssr-update." .. type
  74. sret = luci.sys.call(refresh_cmd .. " 2>/dev/null")
  75. if sret == 0 then
  76. if type == "gfw_data" then
  77. luci.sys.call("/usr/bin/ssr-gfw " .. type)
  78. Num = 2
  79. end
  80. if type == "ad_data" then
  81. luci.sys.call("/usr/bin/ssr-ad " .. type)
  82. end
  83. local new_md5 = luci.sys.exec("echo -n $([ -f '/tmp/ssr-update." .. type .. "' ] && md5sum /tmp/ssr-update." .. type .. " | awk '{print $1}')")
  84. local old_md5 = luci.sys.exec("echo -n $([ -f '" .. file .. "' ] && md5sum " .. file .. " | awk '{print $1}')")
  85. if new_md5 == old_md5 then
  86. retstring = "0"
  87. else
  88. icount = luci.sys.exec("cat /tmp/ssr-update." .. type .. " | wc -l")
  89. luci.sys.exec("cp -f /tmp/ssr-update." .. type .. " " .. file)
  90. if file2 then luci.sys.exec("cp -f /tmp/ssr-update." .. type .. " " .. file2) end
  91. retstring = tostring(tonumber(icount)/Num)
  92. if type == "gfw_data" or type == "ad_data" then
  93. luci.sys.exec("/usr/share/shadowsocksr/gfw2ipset.sh gfw_data")
  94. else
  95. luci.sys.exec("/etc/init.d/shadowsocksr restart &")
  96. end
  97. end
  98. else
  99. retstring = "-1"
  100. end
  101. luci.sys.exec("rm -f /tmp/ssr-update." .. type)
  102. end
  103. if set == "gfw_data" then
  104. update(uci:get_first("shadowsocksr", "global", "gfwlist_url", "https://cdn.jsdelivr.net/gh/gfwlist/gfwlist/gfwlist.txt"), "/etc/ssr/gfw_list.conf", set, "/tmp/dnsmasq.ssr/gfw_list.conf")
  105. end
  106. if set == "ip_data" then
  107. update(uci:get_first("shadowsocksr", "global", "chnroute_url","https://ispip.clang.cn/all_cn.txt"), "/etc/ssr/china_ssr.txt", set)
  108. end
  109. if set == "ad_data" then
  110. update(uci:get_first("shadowsocksr", "global", "adblock_url","https://easylist-downloads.adblockplus.org/easylistchina+easylist.txt"), "/etc/ssr/ad.conf", set, "/tmp/dnsmasq.ssr/ad.conf")
  111. end
  112. if set == "nfip_data" then
  113. update(uci:get_first("shadowsocksr", "global", "nfip_url","https://raw.githubusercontent.com/QiuSimons/Netflix_IP/master/NF_only.txt"), "/etc/ssr/netflixip.list", set)
  114. end
  115. luci.http.prepare_content("application/json")
  116. luci.http.write_json({ret = retstring,retcount = icount})
  117. end
  118. function check_port()
  119. local set = ""
  120. local retstring = "<br /><br />"
  121. local s
  122. local server_name = ""
  123. local uci = luci.model.uci.cursor()
  124. local iret = 1
  125. uci:foreach("shadowsocksr", "servers", function(s)
  126. if s.alias then
  127. server_name = s.alias
  128. elseif s.server and s.server_port then
  129. server_name = "%s:%s" %{s.server, s.server_port}
  130. end
  131. iret = luci.sys.call("ipset add ss_spec_wan_ac " .. s.server .. " 2>/dev/null")
  132. socket = nixio.socket("inet", "stream")
  133. socket:setopt("socket", "rcvtimeo", 3)
  134. socket:setopt("socket", "sndtimeo", 3)
  135. ret = socket:connect(s.server,s.server_port)
  136. if tostring(ret) == "true" then
  137. socket:close()
  138. retstring = retstring .. "<font color = 'green'>[" .. server_name .. "] OK.</font><br />"
  139. else
  140. retstring = retstring .. "<font color = 'red'>[" .. server_name .. "] Error.</font><br />"
  141. end
  142. if iret == 0 then
  143. luci.sys.call("ipset del ss_spec_wan_ac " .. s.server)
  144. end
  145. end)
  146. luci.http.prepare_content("application/json")
  147. luci.http.write_json({ret = retstring})
  148. end