servers.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. -- Licensed to the public under the GNU General Public License v3.
  2. require "luci.http"
  3. require "luci.sys"
  4. require "luci.dispatcher"
  5. require "luci.model.uci"
  6. local uci = require "luci.model.uci".cursor()
  7. local m, s, o, node
  8. local server_count = 0
  9. -- 确保正确判断程序是否存在
  10. local function is_finded(e)
  11. return luci.sys.exec(string.format('type -t -p "%s" 2>/dev/null', e)) ~= ""
  12. end
  13. local has_ss_rust = is_finded("sslocal") or is_finded("ssserver")
  14. local has_ss_libev = is_finded("ss-redir") or is_finded("ss-local")
  15. local ss_type_list = {}
  16. if has_ss_rust then
  17. table.insert(ss_type_list, { id = "ss-rust", name = translate("ShadowSocks-rust Version") })
  18. end
  19. if has_ss_libev then
  20. table.insert(ss_type_list, { id = "ss-libev", name = translate("ShadowSocks-libev Version") })
  21. end
  22. -- 如果用户没有手动设置,则自动选择
  23. if ss_type == "" then
  24. if has_ss_rust then
  25. ss_type = "ss-rust"
  26. elseif has_ss_libev then
  27. ss_type = "ss-libev"
  28. end
  29. end
  30. uci:foreach("shadowsocksr", "servers", function(s)
  31. server_count = server_count + 1
  32. end)
  33. m = Map("shadowsocksr", translate("Servers subscription and manage"))
  34. -- Server Subscribe
  35. s = m:section(TypedSection, "server_subscribe")
  36. s.anonymous = true
  37. o = s:option(Flag, "auto_update", translate("Auto Update"))
  38. o.rmempty = false
  39. o.description = translate("Auto Update Server subscription, GFW list and CHN route")
  40. o = s:option(ListValue, "auto_update_week_time", translate("Update Time (Every Week)"))
  41. o:value('*', translate("Every Day"))
  42. o:value("1", translate("Every Monday"))
  43. o:value("2", translate("Every Tuesday"))
  44. o:value("3", translate("Every Wednesday"))
  45. o:value("4", translate("Every Thursday"))
  46. o:value("5", translate("Every Friday"))
  47. o:value("6", translate("Every Saturday"))
  48. o:value("0", translate("Every Sunday"))
  49. o.default = "*"
  50. o.rmempty = true
  51. o:depends("auto_update", "1")
  52. o = s:option(ListValue, "auto_update_day_time", translate("Update time (every day)"))
  53. for t = 0, 23 do
  54. o:value(t, t .. ":00")
  55. end
  56. o.default = 2
  57. o.rmempty = true
  58. o:depends("auto_update", "1")
  59. o = s:option(ListValue, "auto_update_min_time", translate("Update Interval (min)"))
  60. for i = 0, 59 do
  61. o:value(i, i .. ":00")
  62. end
  63. o.default = 30
  64. o.rmempty = true
  65. o:depends("auto_update", "1")
  66. -- 确保 ss_type_list 不为空
  67. if #ss_type_list > 0 then
  68. o = s:option(ListValue, "ss_type", string.format("<b><span style='color:red;'>%s</span></b>", translate("ShadowSocks Node Use Version")))
  69. o.description = translate("Selection ShadowSocks Node Use Version.")
  70. for _, v in ipairs(ss_type_list) do
  71. o:value(v.id, v.name) -- 存储 "ss-libev" / "ss-rust",但 UI 显示完整名称
  72. end
  73. o.default = ss_type -- 设置默认值
  74. o.write = function(self, section, value)
  75. -- 更新 Shadowsocks 节点的 has_ss_type
  76. uci:foreach("shadowsocksr", "servers", function(s)
  77. local node_type = uci:get("shadowsocksr", s[".name"], "type") -- 获取节点类型
  78. if node_type == "ss" then -- 仅修改 Shadowsocks 节点
  79. local old_value = uci:get("shadowsocksr", s[".name"], "has_ss_type")
  80. if old_value ~= value then
  81. uci:set("shadowsocksr", s[".name"], "has_ss_type", value)
  82. end
  83. end
  84. end)
  85. -- 更新当前 section 的 ss_type
  86. Value.write(self, section, value)
  87. end
  88. end
  89. o = s:option(DynamicList, "subscribe_url", translate("Subscribe URL"))
  90. o.rmempty = true
  91. o = s:option(Value, "filter_words", translate("Subscribe Filter Words"))
  92. o.rmempty = true
  93. o.description = translate("Filter Words splited by /")
  94. o = s:option(Value, "save_words", translate("Subscribe Save Words"))
  95. o.rmempty = true
  96. o.description = translate("Save Words splited by /")
  97. o = s:option(Button, "update_Sub", translate("Update Subscribe List"))
  98. o.inputstyle = "reload"
  99. o.description = translate("Update subscribe url list first")
  100. o.write = function()
  101. uci:commit("shadowsocksr")
  102. luci.http.redirect(luci.dispatcher.build_url("admin", "services", "shadowsocksr", "servers"))
  103. end
  104. o = s:option(Flag, "allow_insecure", translate("Allow subscribe Insecure nodes By default"))
  105. o.rmempty = false
  106. o.description = translate("Subscribe nodes allows insecure connection as TLS client (insecure)")
  107. o.default = "0"
  108. o = s:option(Flag, "switch", translate("Subscribe Default Auto-Switch"))
  109. o.rmempty = false
  110. o.description = translate("Subscribe new add server default Auto-Switch on")
  111. o.default = "1"
  112. o = s:option(Flag, "proxy", translate("Through proxy update"))
  113. o.rmempty = false
  114. o.description = translate("Through proxy update list, Not Recommended ")
  115. o = s:option(Button, "subscribe", translate("Update All Subscribe Servers"))
  116. o.rawhtml = true
  117. o.template = "shadowsocksr/subscribe"
  118. o = s:option(Button, "delete", translate("Delete All Subscribe Servers"))
  119. o.inputstyle = "reset"
  120. o.description = string.format(translate("Server Count") .. ": %d", server_count)
  121. o.write = function()
  122. uci:delete_all("shadowsocksr", "servers", function(s)
  123. if s.hashkey or s.isSubscribe then
  124. return true
  125. else
  126. return false
  127. end
  128. end)
  129. uci:save("shadowsocksr")
  130. uci:commit("shadowsocksr")
  131. luci.http.redirect(luci.dispatcher.build_url("admin", "services", "shadowsocksr", "delete"))
  132. return
  133. end
  134. -- [[ Servers Manage ]]--
  135. s = m:section(TypedSection, "servers")
  136. s.anonymous = true
  137. s.addremove = true
  138. s.template = "cbi/tblsection"
  139. s.sortable = true
  140. s.extedit = luci.dispatcher.build_url("admin", "services", "shadowsocksr", "servers", "%s")
  141. function s.create(...)
  142. local sid = TypedSection.create(...)
  143. if sid then
  144. luci.http.redirect(s.extedit % sid)
  145. return
  146. end
  147. end
  148. o = s:option(DummyValue, "type", translate("Type"))
  149. function o.cfgvalue(self, section)
  150. return m:get(section, "v2ray_protocol") or Value.cfgvalue(self, section) or translate("None")
  151. end
  152. o = s:option(DummyValue, "alias", translate("Alias"))
  153. function o.cfgvalue(...)
  154. return Value.cfgvalue(...) or translate("None")
  155. end
  156. o = s:option(DummyValue, "server_port", translate("Server Port"))
  157. function o.cfgvalue(...)
  158. return Value.cfgvalue(...) or "N/A"
  159. end
  160. o = s:option(DummyValue, "server_port", translate("Socket Connected"))
  161. o.template = "shadowsocksr/socket"
  162. o.width = "10%"
  163. o.render = function(self, section, scope)
  164. self.transport = s:cfgvalue(section).transport
  165. if self.transport == 'ws' then
  166. self.ws_path = s:cfgvalue(section).ws_path
  167. self.tls = s:cfgvalue(section).tls
  168. end
  169. DummyValue.render(self, section, scope)
  170. end
  171. o = s:option(DummyValue, "server", translate("Ping Latency"))
  172. o.template = "shadowsocksr/ping"
  173. o.width = "10%"
  174. local global_server = uci:get_first('shadowsocksr', 'global', 'global_server')
  175. node = s:option(Button, "apply_node", translate("Apply"))
  176. node.inputstyle = "apply"
  177. node.render = function(self, section, scope)
  178. if section == global_server then
  179. self.title = translate("Reapply")
  180. else
  181. self.title = translate("Apply")
  182. end
  183. Button.render(self, section, scope)
  184. end
  185. node.write = function(self, section)
  186. uci:set("shadowsocksr", '@global[0]', 'global_server', section)
  187. uci:save("shadowsocksr")
  188. uci:commit("shadowsocksr")
  189. luci.http.redirect(luci.dispatcher.build_url("admin", "services", "shadowsocksr", "restart"))
  190. end
  191. o = s:option(Flag, "switch_enable", translate("Auto Switch"))
  192. o.rmempty = false
  193. function o.cfgvalue(...)
  194. return Value.cfgvalue(...) or 1
  195. end
  196. m:append(Template("shadowsocksr/server_list"))
  197. return m