advanced.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. local m, s, o
  2. local uci = require "luci.model.uci".cursor()
  3. -- 获取 LAN IP 地址
  4. function lanip()
  5. local lan_ip
  6. lan_ip = luci.sys.exec("uci -q get network.lan.ipaddr 2>/dev/null | awk -F '/' '{print $1}' | tr -d '\n'")
  7. if not lan_ip or lan_ip == "" then
  8. lan_ip = luci.sys.exec("ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w 'inet' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -1 | tr -d '\n'")
  9. end
  10. if not lan_ip or lan_ip == "" then
  11. lan_ip = luci.sys.exec("ip addr show | grep -w 'inet' | grep 'global' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1 | tr -d '\n'")
  12. end
  13. return lan_ip
  14. end
  15. local lan_ip = lanip()
  16. local server_table = {}
  17. local type_table = {}
  18. local function is_finded(e)
  19. return luci.sys.exec(string.format('type -t -p "%s" 2>/dev/null', e)) ~= ""
  20. end
  21. uci:foreach("shadowsocksr", "servers", function(s)
  22. if s.alias then
  23. server_table[s[".name"]] = "[%s]:%s" % {string.upper(s.v2ray_protocol or s.type), s.alias}
  24. elseif s.server and s.server_port then
  25. server_table[s[".name"]] = "[%s]:%s:%s" % {string.upper(s.v2ray_protocol or s.type), s.server, s.server_port}
  26. end
  27. if s.type then
  28. type_table[s[".name"]] = s.type
  29. end
  30. end)
  31. local key_table = {}
  32. for key, _ in pairs(server_table) do
  33. table.insert(key_table, key)
  34. end
  35. table.sort(key_table)
  36. m = Map("shadowsocksr")
  37. -- [[ global ]]--
  38. s = m:section(TypedSection, "global", translate("Server failsafe auto swith and custom update settings"))
  39. s.anonymous = true
  40. -- o = s:option(Flag, "monitor_enable", translate("Enable Process Deamon"))
  41. -- o.rmempty = false
  42. -- o.default = "1"
  43. o = s:option(Flag, "enable_switch", translate("Enable Auto Switch"))
  44. o.rmempty = false
  45. o.default = "1"
  46. o = s:option(Value, "switch_time", translate("Switch check cycly(second)"))
  47. o.datatype = "uinteger"
  48. o:depends("enable_switch", "1")
  49. o.default = 667
  50. o = s:option(Value, "switch_timeout", translate("Check timout(second)"))
  51. o.datatype = "uinteger"
  52. o:depends("enable_switch", "1")
  53. o.default = 5
  54. o = s:option(Value, "switch_try_count", translate("Check Try Count"))
  55. o.datatype = "uinteger"
  56. o:depends("enable_switch", "1")
  57. o.default = 3
  58. o = s:option(Value, "gfwlist_url", translate("gfwlist Update url"))
  59. o:value("https://fastly.jsdelivr.net/gh/YW5vbnltb3Vz/domain-list-community@release/gfwlist.txt", translate("v2fly/domain-list-community"))
  60. o:value("https://fastly.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/gfw.txt", translate("Loyalsoldier/v2ray-rules-dat"))
  61. o:value("https://fastly.jsdelivr.net/gh/Loukky/gfwlist-by-loukky/gfwlist.txt", translate("Loukky/gfwlist-by-loukky"))
  62. o:value("https://fastly.jsdelivr.net/gh/gfwlist/gfwlist/gfwlist.txt", translate("gfwlist/gfwlist"))
  63. o.default = "https://fastly.jsdelivr.net/gh/YW5vbnltb3Vz/domain-list-community@release/gfwlist.txt"
  64. o = s:option(Value, "chnroute_url", translate("Chnroute Update url"))
  65. o:value("https://ispip.clang.cn/all_cn.txt", translate("Clang.CN"))
  66. o:value("https://ispip.clang.cn/all_cn_cidr.txt", translate("Clang.CN.CIDR"))
  67. o:value("https://fastly.jsdelivr.net/gh/gaoyifan/china-operator-ip@ip-lists/china.txt", translate("china-operator-ip"))
  68. o.default = "https://ispip.clang.cn/all_cn.txt"
  69. o = s:option(Flag, "netflix_enable", translate("Enable Netflix Mode"))
  70. o.description = translate("When disabled shunt mode, will same time stopped shunt service.")
  71. o.rmempty = false
  72. o = s:option(Value, "nfip_url", translate("nfip_url"))
  73. o:value("https://fastly.jsdelivr.net/gh/QiuSimons/Netflix_IP/NF_only.txt", translate("Netflix IP Only"))
  74. o:value("https://fastly.jsdelivr.net/gh/QiuSimons/Netflix_IP/getflix.txt", translate("Netflix and AWS"))
  75. o.default = "https://fastly.jsdelivr.net/gh/QiuSimons/Netflix_IP/NF_only.txt"
  76. o.description = translate("Customize Netflix IP Url")
  77. o:depends("netflix_enable", "1")
  78. o = s:option(ListValue, "shunt_dns_mode", translate("DNS Query Mode For Shunt Mode"))
  79. if is_finded("dns2socks") then
  80. o:value("1", translate("Use DNS2SOCKS query and cache"))
  81. end
  82. if is_finded("dns2socks-rust") then
  83. o:value("2", translate("Use DNS2SOCKS-RUST query and cache"))
  84. end
  85. if is_finded("mosdns") then
  86. o:value("3", translate("Use MosDNS query"))
  87. end
  88. if is_finded("dnsproxy") then
  89. o:value("4", translate("Use DNSPROXY query and cache"))
  90. end
  91. o:depends("netflix_enable", "1")
  92. o.default = 1
  93. o = s:option(Value, "shunt_dnsserver", translate("Anti-pollution DNS Server For Shunt Mode"))
  94. o:value("8.8.4.4:53", translate("Google Public DNS (8.8.4.4)"))
  95. o:value("8.8.8.8:53", translate("Google Public DNS (8.8.8.8)"))
  96. o:value("208.67.222.222:53", translate("OpenDNS (208.67.222.222)"))
  97. o:value("208.67.220.220:53", translate("OpenDNS (208.67.220.220)"))
  98. o:value("209.244.0.3:53", translate("Level 3 Public DNS (209.244.0.3)"))
  99. o:value("209.244.0.4:53", translate("Level 3 Public DNS (209.244.0.4)"))
  100. o:value("4.2.2.1:53", translate("Level 3 Public DNS (4.2.2.1)"))
  101. o:value("4.2.2.2:53", translate("Level 3 Public DNS (4.2.2.2)"))
  102. o:value("4.2.2.3:53", translate("Level 3 Public DNS (4.2.2.3)"))
  103. o:value("4.2.2.4:53", translate("Level 3 Public DNS (4.2.2.4)"))
  104. o:value("1.1.1.1:53", translate("Cloudflare DNS (1.1.1.1)"))
  105. o:depends("shunt_dns_mode", "1")
  106. o:depends("shunt_dns_mode", "2")
  107. o.description = translate("Custom DNS Server format as IP:PORT (default: 8.8.4.4:53)")
  108. o.datatype = "ip4addrport"
  109. o = s:option(ListValue, "shunt_mosdns_dnsserver", translate("Anti-pollution DNS Server"))
  110. o:value("tcp://8.8.4.4:53,tcp://8.8.8.8:53", translate("Google Public DNS"))
  111. o:value("tcp://208.67.222.222:53,tcp://208.67.220.220:53", translate("OpenDNS"))
  112. o:value("tcp://209.244.0.3:53,tcp://209.244.0.4:53", translate("Level 3 Public DNS-1 (209.244.0.3-4)"))
  113. o:value("tcp://4.2.2.1:53,tcp://4.2.2.2:53", translate("Level 3 Public DNS-2 (4.2.2.1-2)"))
  114. o:value("tcp://4.2.2.3:53,tcp://4.2.2.4:53", translate("Level 3 Public DNS-3 (4.2.2.3-4)"))
  115. o:value("tcp://1.1.1.1:53,tcp://1.0.0.1:53", translate("Cloudflare DNS"))
  116. o:depends("shunt_dns_mode", "3")
  117. o.description = translate("Custom DNS Server for MosDNS")
  118. o = s:option(Flag, "shunt_mosdns_ipv6", translate("Disable IPv6 In MosDNS Query Mode (Shunt Mode)"))
  119. o:depends("shunt_dns_mode", "3")
  120. o.rmempty = false
  121. o.default = "0"
  122. if is_finded("dnsproxy") then
  123. o = s:option(ListValue, "shunt_parse_method", translate("Select DNS parse Mode"))
  124. o.description = translate(
  125. "<ul>" ..
  126. "<li>" .. translate("When use DNS list file, please ensure list file exists and is formatted correctly.") .. "</li>" ..
  127. "<li>" .. translate("Tips: Dnsproxy DNS Parse List Path:") ..
  128. " <a href='http://" .. lan_ip .. "/cgi-bin/luci/admin/services/shadowsocksr/control' target='_blank'>" ..
  129. translate("Click here to view or manage the DNS list file") .. "</a>" .. "</li>" ..
  130. "</ul>"
  131. )
  132. o:value("single_dns", translate("Set Single DNS"))
  133. o:value("parse_file", translate("Use DNS List File"))
  134. o:depends("shunt_dns_mode", "4")
  135. o.rmempty = true
  136. o.default = "single_dns"
  137. o = s:option(Value, "dnsproxy_shunt_forward", translate("Anti-pollution DNS Server"))
  138. o:value("sdns://AgUAAAAAAAAABzguOC40LjQgsKKKE4EwvtIbNjGjagI2607EdKSVHowYZtyvD9iPrkkHOC44LjQuNAovZG5zLXF1ZXJ5", translate("Google DNSCrypt SDNS"))
  139. o:value("sdns://AgcAAAAAAAAADzE4NS4yMjIuMjIyLjIyMiAOp5Svj-oV-Fz-65-8H2VKHLKJ0egmfEgrdPeAQlUFFA8xODUuMjIyLjIyMi4yMjIKL2Rucy1xdWVyeQ", translate("dns.sb DNSCrypt SDNS"))
  140. o:value("sdns://AgMAAAAAAAAADTE0OS4xMTIuMTEyLjkgsBkgdEu7dsmrBT4B4Ht-BQ5HPSD3n3vqQ1-v5DydJC8SZG5zOS5xdWFkOS5uZXQ6NDQzCi9kbnMtcXVlcnk", translate("Quad9 DNSCrypt SDNS"))
  141. o:value("sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", translate("AdGuard DNSCrypt SDNS"))
  142. o:value("sdns://AgcAAAAAAAAABzEuMC4wLjGgENk8mGSlIfMGXMOlIlCcKvq7AVgcrZxtjon911-ep0cg63Ul-I8NlFj4GplQGb_TTLiczclX57DvMV8Q-JdjgRgSZG5zLmNsb3VkZmxhcmUuY29tCi9kbnMtcXVlcnk", translate("Cloudflare DNSCrypt SDNS"))
  143. o:value("sdns://AgcAAAAAAAAADjEwNC4xNi4yNDkuMjQ5ABJjbG91ZGZsYXJlLWRucy5jb20KL2Rucy1xdWVyeQ", translate("cloudflare-dns.com DNSCrypt SDNS"))
  144. o:depends("shunt_parse_method", "single_dns")
  145. o.description = translate("Custom DNS Server (support: IP:Port or tls://IP:Port or https://IP/dns-query and other format).")
  146. o = s:option(ListValue, "shunt_upstreams_logic_mode", translate("Defines the upstreams logic mode"))
  147. o.description = translate(
  148. "<ul>" ..
  149. "<li>" .. translate("Defines the upstreams logic mode, possible values: load_balance, parallel, fastest_addr (default: load_balance).") .. "</li>" .. "<li>" .. translate("When two or more DNS servers are deployed, enable this function.") .. "</li>" ..
  150. "</ul>"
  151. )
  152. o:value("load_balance", translate("load_balance"))
  153. o:value("parallel", translate("parallel"))
  154. o:value("fastest_addr", translate("fastest_addr"))
  155. o:depends("shunt_parse_method", "parse_file")
  156. o.rmempty = true
  157. o.default = "load_balance"
  158. o = s:option(Flag, "shunt_dnsproxy_ipv6", translate("Disable IPv6 query mode"))
  159. o.description = translate("When disabled, all AAAA requests are not resolved.")
  160. o:depends("shunt_parse_method", "single_dns")
  161. o:depends("shunt_parse_method", "parse_file")
  162. o.rmempty = false
  163. o.default = "1"
  164. end
  165. o = s:option(Flag, "apple_optimization", translate("Apple domains optimization"), translate("For Apple domains equipped with Chinese mainland CDN, always responsive to Chinese CDN IP addresses"))
  166. o.rmempty = false
  167. o.default = "1"
  168. o = s:option(Value, "apple_url", translate("Apple Domains Update url"))
  169. o:value("https://fastly.jsdelivr.net/gh/felixonmars/dnsmasq-china-list/apple.china.conf", translate("felixonmars/dnsmasq-china-list"))
  170. o.default = "https://fastly.jsdelivr.net/gh/felixonmars/dnsmasq-china-list/apple.china.conf"
  171. o:depends("apple_optimization", "1")
  172. o = s:option(Value, "apple_dns", translate("Apple Domains DNS"), translate("If empty, Not change Apple domains parsing DNS (Default is empty)"))
  173. o.rmempty = true
  174. o.default = ""
  175. o.datatype = "ip4addr"
  176. o:depends("apple_optimization", "1")
  177. o = s:option(Flag, "adblock", translate("Enable adblock"))
  178. o.rmempty = false
  179. o = s:option(Value, "adblock_url", translate("adblock_url"))
  180. o:value("https://raw.githubusercontent.com/neodevpro/neodevhost/master/lite_dnsmasq.conf", translate("NEO DEV HOST Lite"))
  181. o:value("https://raw.githubusercontent.com/neodevpro/neodevhost/master/dnsmasq.conf", translate("NEO DEV HOST Full"))
  182. o:value("https://anti-ad.net/anti-ad-for-dnsmasq.conf", translate("anti-AD"))
  183. o.default = "https://raw.githubusercontent.com/neodevpro/neodevhost/master/lite_dnsmasq.conf"
  184. o:depends("adblock", "1")
  185. o.description = translate("Support AdGuardHome and DNSMASQ format list")
  186. o = s:option(Button, "Reset", translate("Reset to defaults"))
  187. o.inputstyle = "reload"
  188. o.write = function()
  189. luci.sys.call("/etc/init.d/shadowsocksr reset")
  190. luci.http.redirect(luci.dispatcher.build_url("admin", "services", "shadowsocksr", "servers"))
  191. end
  192. -- [[ SOCKS5 Proxy ]]--
  193. s = m:section(TypedSection, "socks5_proxy", translate("Global SOCKS5 Proxy Server"))
  194. s.anonymous = true
  195. -- Enable/Disable Option
  196. o = s:option(Flag, "enabled", translate("Enable"))
  197. o.default = 0
  198. o.rmempty = false
  199. -- Server Selection
  200. o = s:option(ListValue, "server", translate("Server"))
  201. o:value("same", translate("Same as Global Server"))
  202. for _, key in pairs(key_table) do
  203. o:value(key, server_table[key])
  204. end
  205. o.default = "same"
  206. o.rmempty = false
  207. -- Dynamic value handling based on enabled/disabled state
  208. o.cfgvalue = function(self, section)
  209. local enabled = m:get(section, "enabled")
  210. if enabled == "0" then
  211. return m:get(section, "old_server")
  212. end
  213. return Value.cfgvalue(self, section)-- Default to `same` when enabled
  214. end
  215. o.write = function(self, section, value)
  216. local enabled = m:get(section, "enabled")
  217. if enabled == "0" then
  218. local old_server = Value.cfgvalue(self, section)
  219. if old_server ~= "nil" then
  220. m:set(section, "old_server", old_server)
  221. end
  222. m:set(section, "server", "nil")
  223. else
  224. m:del(section, "old_server")
  225. -- Write the value normally when enabled
  226. Value.write(self, section, value)
  227. end
  228. end
  229. -- Socks Auth
  230. if is_finded("xray") then
  231. o = s:option(ListValue, "socks5_auth", translate("Socks5 Auth Mode"), translate("Socks protocol auth methods, default:noauth."))
  232. o.default = "noauth"
  233. o:value("noauth", "NOAUTH")
  234. o:value("password", "PASSWORD")
  235. o.rmempty = true
  236. for key, server_type in pairs(type_table) do
  237. if server_type == "v2ray" then
  238. -- 如果服务器类型是 v2ray,则设置依赖项显示
  239. o:depends("server", key)
  240. end
  241. end
  242. o:depends({server = "same", disable = true})
  243. -- Socks User
  244. o = s:option(Value, "socks5_user", translate("Socks5 User"), translate("Only when Socks5 Auth Mode is password valid, Mandatory."))
  245. o.rmempty = true
  246. o:depends("socks5_auth", "password")
  247. -- Socks Password
  248. o = s:option(Value, "socks5_pass", translate("Socks5 Password"), translate("Only when Socks5 Auth Mode is password valid, Not mandatory."))
  249. o.password = true
  250. o.rmempty = true
  251. o:depends("socks5_auth", "password")
  252. -- Socks Mixed
  253. o = s:option(Flag, "socks5_mixed", translate("Enabled Mixed"), translate("Mixed as an alias of socks, default:Enabled."))
  254. o.default = "1"
  255. o.rmempty = false
  256. for key, server_type in pairs(type_table) do
  257. if server_type == "v2ray" then
  258. -- 如果服务器类型是 v2ray,则设置依赖项显示
  259. o:depends("server", key)
  260. end
  261. end
  262. o:depends({server = "same", disable = true})
  263. end
  264. -- Local Port
  265. o = s:option(Value, "local_port", translate("Local Port"))
  266. o.datatype = "port"
  267. o.default = 1080
  268. o.rmempty = false
  269. -- [[ fragmen Settings ]]--
  270. if is_finded("xray") then
  271. s = m:section(TypedSection, "global_xray_fragment", translate("Xray Fragment Settings"))
  272. s.anonymous = true
  273. o = s:option(Flag, "fragment", translate("Fragment"), translate("TCP fragments, which can deceive the censorship system in some cases, such as bypassing SNI blacklists."))
  274. o.default = 0
  275. o = s:option(ListValue, "fragment_packets", translate("Fragment Packets"), translate("\"1-3\" is for segmentation at TCP layer, applying to the beginning 1 to 3 data writes by the client. \"tlshello\" is for TLS client hello packet fragmentation."))
  276. o.default = "tlshello"
  277. o:value("tlshello", "tlshello")
  278. o:value("1-1", "1-1")
  279. o:value("1-2", "1-2")
  280. o:value("1-3", "1-3")
  281. o:value("1-5", "1-5")
  282. o:depends("fragment", true)
  283. o = s:option(Value, "fragment_length", translate("Fragment Length"), translate("Fragmented packet length (byte)"))
  284. o.default = "100-200"
  285. o:depends("fragment", true)
  286. o = s:option(Value, "fragment_interval", translate("Fragment Interval"), translate("Fragmentation interval (ms)"))
  287. o.default = "10-20"
  288. o:depends("fragment", true)
  289. o = s:option(Flag, "noise", translate("Noise"), translate("UDP noise, Under some circumstances it can bypass some UDP based protocol restrictions."))
  290. o.default = 0
  291. s = m:section(TypedSection, "xray_noise_packets", translate("Xray Noise Packets"))
  292. s.description = translate(
  293. "<font style='color:red'>" .. translate("To send noise packets, select \"Noise\" in Xray Settings.") .. "</font>" ..
  294. "<br/><font><b>" .. translate("For specific usage, see:") .. "</b></font>" ..
  295. "<a href='https://xtls.github.io/config/outbounds/freedom.html' target='_blank'>" ..
  296. "<font style='color:green'><b>" .. translate("Click to the page") .. "</b></font></a>")
  297. s.template = "cbi/tblsection"
  298. s.sortable = true
  299. s.anonymous = true
  300. s.addremove = true
  301. s.remove = function(self, section)
  302. for k, v in pairs(self.children) do
  303. v.rmempty = true
  304. v.validate = nil
  305. end
  306. TypedSection.remove(self, section)
  307. end
  308. o = s:option(Flag, "enabled", translate("Enable"))
  309. o.default = 1
  310. o.rmempty = false
  311. o = s:option(ListValue, "type", translate("Type"))
  312. o.default = "base64"
  313. o:value("rand", "rand")
  314. o:value("str", "str")
  315. o:value("hex", "hex")
  316. o:value("base64", "base64")
  317. o = s:option(Value, "domainStrategy", translate("Domain Strategy"))
  318. o.default = "UseIP"
  319. o:value("AsIs", "AsIs")
  320. o:value("UseIP", "UseIP")
  321. o:value("UseIPv4", "UseIPv4")
  322. o:value("ForceIP", "ForceIP")
  323. o:value("ForceIPv4", "ForceIPv4")
  324. o.rmempty = false
  325. o = s:option(Value, "packet", translate("Packet"))
  326. o.datatype = "minlength(1)"
  327. o.rmempty = false
  328. o = s:option(Value, "delay", translate("Delay (ms)"))
  329. o.datatype = "or(uinteger,portrange)"
  330. o.rmempty = false
  331. end
  332. return m