client-config.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. -- Copyright (C) 2017 yushi studio <[email protected]> github.com/ywb94
  2. -- Licensed to the public under the GNU General Public License v3.
  3. require "nixio.fs"
  4. require "luci.sys"
  5. require "luci.http"
  6. local m, s, o, kcp_enable
  7. local shadowsocksr = "shadowsocksr"
  8. local sid = arg[1]
  9. local uuid = luci.sys.exec("cat /proc/sys/kernel/random/uuid")
  10. local function isKcptun(file)
  11. if not nixio.fs.access(file, "rwx", "rx", "rx") then
  12. nixio.fs.chmod(file, 755)
  13. end
  14. local str = luci.sys.exec(file .. " -v | awk '{printf $1}'")
  15. return (str:lower() == "kcptun")
  16. end
  17. local server_table = {}
  18. local encrypt_methods = {
  19. "none",
  20. "table",
  21. "rc4",
  22. "rc4-md5-6",
  23. "rc4-md5",
  24. "aes-128-cfb",
  25. "aes-192-cfb",
  26. "aes-256-cfb",
  27. "aes-128-ctr",
  28. "aes-192-ctr",
  29. "aes-256-ctr",
  30. "bf-cfb",
  31. "camellia-128-cfb",
  32. "camellia-192-cfb",
  33. "camellia-256-cfb",
  34. "cast5-cfb",
  35. "des-cfb",
  36. "idea-cfb",
  37. "rc2-cfb",
  38. "seed-cfb",
  39. "salsa20",
  40. "chacha20",
  41. "chacha20-ietf",
  42. }
  43. local encrypt_methods_ss = {
  44. -- aead
  45. "aes-128-gcm",
  46. "aes-192-gcm",
  47. "aes-256-gcm",
  48. "chacha20-ietf-poly1305",
  49. "xchacha20-ietf-poly1305",
  50. -- stream
  51. "table",
  52. "rc4",
  53. "rc4-md5",
  54. "aes-128-cfb",
  55. "aes-192-cfb",
  56. "aes-256-cfb",
  57. "aes-128-ctr",
  58. "aes-192-ctr",
  59. "aes-256-ctr",
  60. "bf-cfb",
  61. "camellia-128-cfb",
  62. "camellia-192-cfb",
  63. "camellia-256-cfb",
  64. "salsa20",
  65. "chacha20",
  66. "chacha20-ietf",
  67. }
  68. local protocol = {
  69. "origin",
  70. "verify_deflate",
  71. "auth_sha1_v4",
  72. "auth_aes128_sha1",
  73. "auth_aes128_md5",
  74. "auth_chain_a",
  75. "auth_chain_b",
  76. "auth_chain_c",
  77. "auth_chain_d",
  78. "auth_chain_e",
  79. "auth_chain_f",
  80. }
  81. obfs = {
  82. "plain",
  83. "http_simple",
  84. "http_post",
  85. "random_head",
  86. "tls1.2_ticket_auth",
  87. }
  88. local securitys = {
  89. "auto",
  90. "none",
  91. "aes-128-gcm",
  92. "chacha20-poly1305"
  93. }
  94. local flows = {
  95. "xtls-rprx-origin",
  96. "xtls-rprx-origin-udp443",
  97. "xtls-rprx-direct",
  98. "xtls-rprx-direct-udp443",
  99. "xtls-rprx-splice",
  100. "xtls-rprx-splice-udp443"
  101. }
  102. m = Map(shadowsocksr, translate("Edit ShadowSocksR Server"))
  103. m.redirect = luci.dispatcher.build_url("admin/services/shadowsocksr/servers")
  104. if m.uci:get(shadowsocksr, sid) ~= "servers" then
  105. luci.http.redirect(m.redirect)
  106. return
  107. end
  108. -- [[ Servers Setting ]]--
  109. s = m:section(NamedSection, sid, "servers")
  110. s.anonymous = true
  111. s.addremove = false
  112. o = s:option(DummyValue, "ssr_url", "SS/SSR/V2RAY/TROJAN URL")
  113. o.rawhtml = true
  114. o.template = "shadowsocksr/ssrurl"
  115. o.value = sid
  116. o = s:option(ListValue, "type", translate("Server Node Type"))
  117. o:value("ssr", translate("ShadowsocksR"))
  118. if nixio.fs.access("/usr/bin/ss-redir") then
  119. o:value("ss", translate("Shadowsocks New Version"))
  120. end
  121. if nixio.fs.access("/usr/bin/xray") or nixio.fs.access("/usr/bin/xray/xray") or nixio.fs.access("/usr/bin/v2ray/v2ray") or nixio.fs.access("/usr/bin/v2ray") then
  122. o:value("vmess", translate("Vmess"))
  123. o:value("vless", translate("VLESS"))
  124. end
  125. if nixio.fs.access("/usr/sbin/trojan") then
  126. o:value("trojan", translate("Trojan"))
  127. end
  128. if nixio.fs.access("/usr/bin/naive") then
  129. o:value("naiveproxy", translate("NaiveProxy"))
  130. end
  131. if nixio.fs.access("/usr/sbin/redsocks2") then
  132. o:value("socks5", translate("Socks5"))
  133. o:value("tun", translate("Network Tunnel"))
  134. end
  135. o.description = translate("Using incorrect encryption mothod may causes service fail to start")
  136. o = s:option(Value, "alias", translate("Alias(optional)"))
  137. o = s:option(ListValue, "iface", translate("Network interface to use"))
  138. for _, e in ipairs(luci.sys.net.devices()) do if e ~= "lo" then o:value(e) end end
  139. o:depends("type", "tun")
  140. o.description = translate("Redirect traffic to this network interface")
  141. o = s:option(Value, "server", translate("Server Address"))
  142. o.datatype = "host"
  143. o.rmempty = false
  144. o:depends("type", "ssr")
  145. o:depends("type", "ss")
  146. o:depends("type", "vmess")
  147. o:depends("type", "vless")
  148. o:depends("type", "trojan")
  149. o:depends("type", "naiveproxy")
  150. o:depends("type", "socks5")
  151. o = s:option(Value, "server_port", translate("Server Port"))
  152. o.datatype = "port"
  153. o.rmempty = false
  154. o:depends("type", "ssr")
  155. o:depends("type", "ss")
  156. o:depends("type", "vmess")
  157. o:depends("type", "vless")
  158. o:depends("type", "trojan")
  159. o:depends("type", "naiveproxy")
  160. o:depends("type", "socks5")
  161. o = s:option(Flag, "auth_enable", translate("Enable Authentication"))
  162. o.rmempty = false
  163. o.default = "0"
  164. o:depends("type", "socks5")
  165. o = s:option(Value, "username", translate("Username"))
  166. o.rmempty = true
  167. o:depends("type", "naiveproxy")
  168. o:depends({type = "socks5", auth_enable = true})
  169. o = s:option(Value, "password", translate("Password"))
  170. o.password = true
  171. o.rmempty = true
  172. o:depends("type", "ssr")
  173. o:depends("type", "ss")
  174. o:depends("type", "trojan")
  175. o:depends("type", "naiveproxy")
  176. o:depends({type = "socks5", auth_enable = true})
  177. o = s:option(ListValue, "encrypt_method", translate("Encrypt Method"))
  178. for _, v in ipairs(encrypt_methods) do o:value(v) end
  179. o.rmempty = true
  180. o:depends("type", "ssr")
  181. o = s:option(ListValue, "encrypt_method_ss", translate("Encrypt Method"))
  182. for _, v in ipairs(encrypt_methods_ss) do o:value(v) end
  183. o.rmempty = true
  184. o:depends("type", "ss")
  185. -- Shadowsocks Plugin
  186. o = s:option(Value, "plugin", translate("Plugin"))
  187. o.rmempty = true
  188. o:depends("type", "ss")
  189. o = s:option(Value, "plugin_opts", translate("Plugin Opts"))
  190. o.rmempty = true
  191. o:depends("type", "ss")
  192. o = s:option(ListValue, "protocol", translate("Protocol"))
  193. for _, v in ipairs(protocol) do o:value(v) end
  194. o.rmempty = true
  195. o:depends("type", "ssr")
  196. o = s:option(Value, "protocol_param", translate("Protocol param(optional)"))
  197. o:depends("type", "ssr")
  198. o = s:option(ListValue, "obfs", translate("Obfs"))
  199. for _, v in ipairs(obfs) do o:value(v) end
  200. o.rmempty = true
  201. o:depends("type", "ssr")
  202. o = s:option(Value, "obfs_param", translate("Obfs param(optional)"))
  203. o:depends("type", "ssr")
  204. -- AlterId
  205. o = s:option(Value, "alter_id", translate("AlterId"))
  206. o.datatype = "port"
  207. o.default = 16
  208. o.rmempty = true
  209. o:depends("type", "vmess")
  210. -- VmessId
  211. o = s:option(Value, "vmess_id", translate("Vmess/VLESS ID (UUID)"))
  212. o.rmempty = true
  213. o.default = uuid
  214. o:depends("type", "vmess")
  215. o:depends("type", "vless")
  216. -- VLESS Encryption
  217. o = s:option(Value, "vless_encryption", translate("VLESS Encryption"))
  218. o.rmempty = true
  219. o.default = "none"
  220. o:depends("type", "vless")
  221. -- 加密方式
  222. o = s:option(ListValue, "security", translate("Encrypt Method"))
  223. for _, v in ipairs(securitys) do o:value(v, v:upper()) end
  224. o.rmempty = true
  225. o:depends("type", "vmess")
  226. -- 传输协议
  227. o = s:option(ListValue, "transport", translate("Transport"))
  228. o:value("tcp", "TCP")
  229. o:value("kcp", "mKCP")
  230. o:value("ws", "WebSocket")
  231. o:value("h2", "HTTP/2")
  232. o:value("quic", "QUIC")
  233. o.rmempty = true
  234. o:depends("type", "vmess")
  235. o:depends("type", "vless")
  236. -- [[ TCP部分 ]]--
  237. -- TCP伪装
  238. o = s:option(ListValue, "tcp_guise", translate("Camouflage Type"))
  239. o:depends("transport", "tcp")
  240. o:value("none", translate("None"))
  241. o:value("http", "HTTP")
  242. o.rmempty = true
  243. -- HTTP域名
  244. o = s:option(Value, "http_host", translate("HTTP Host"))
  245. o:depends("tcp_guise", "http")
  246. o.rmempty = true
  247. -- HTTP路径
  248. o = s:option(Value, "http_path", translate("HTTP Path"))
  249. o:depends("tcp_guise", "http")
  250. o.rmempty = true
  251. -- [[ WS部分 ]]--
  252. -- WS域名
  253. o = s:option(Value, "ws_host", translate("WebSocket Host"))
  254. o:depends({transport = "ws", tls = false})
  255. o.rmempty = true
  256. -- WS路径
  257. o = s:option(Value, "ws_path", translate("WebSocket Path"))
  258. o:depends("transport", "ws")
  259. o.rmempty = true
  260. -- [[ H2部分 ]]--
  261. -- H2域名
  262. o = s:option(Value, "h2_host", translate("HTTP/2 Host"))
  263. o:depends("transport", "h2")
  264. o.rmempty = true
  265. -- H2路径
  266. o = s:option(Value, "h2_path", translate("HTTP/2 Path"))
  267. o:depends("transport", "h2")
  268. o.rmempty = true
  269. -- [[ QUIC部分 ]]--
  270. o = s:option(ListValue, "quic_security", translate("QUIC Security"))
  271. o:depends("transport", "quic")
  272. o:value("none", translate("None"))
  273. o:value("aes-128-gcm", translate("aes-128-gcm"))
  274. o:value("chacha20-poly1305", translate("chacha20-poly1305"))
  275. o.rmempty = true
  276. o = s:option(Value, "quic_key", translate("QUIC Key"))
  277. o:depends("transport", "quic")
  278. o.rmempty = true
  279. o = s:option(ListValue, "quic_guise", translate("Header"))
  280. o:depends("transport", "quic")
  281. o.rmempty = true
  282. o:value("none", translate("None"))
  283. o:value("srtp", translate("VideoCall (SRTP)"))
  284. o:value("utp", translate("BitTorrent (uTP)"))
  285. o:value("wechat-video", translate("WechatVideo"))
  286. o:value("dtls", "DTLS 1.2")
  287. o:value("wireguard", "WireGuard")
  288. -- [[ mKCP部分 ]]--
  289. o = s:option(ListValue, "kcp_guise", translate("Camouflage Type"))
  290. o:depends("transport", "kcp")
  291. o:value("none", translate("None"))
  292. o:value("srtp", translate("VideoCall (SRTP)"))
  293. o:value("utp", translate("BitTorrent (uTP)"))
  294. o:value("wechat-video", translate("WechatVideo"))
  295. o:value("dtls", "DTLS 1.2")
  296. o:value("wireguard", "WireGuard")
  297. o.rmempty = true
  298. o = s:option(Value, "mtu", translate("MTU"))
  299. o.datatype = "uinteger"
  300. o:depends("transport", "kcp")
  301. o.default = 1350
  302. o.rmempty = true
  303. o = s:option(Value, "tti", translate("TTI"))
  304. o.datatype = "uinteger"
  305. o:depends("transport", "kcp")
  306. o.default = 50
  307. o.rmempty = true
  308. o = s:option(Value, "uplink_capacity", translate("Uplink Capacity"))
  309. o.datatype = "uinteger"
  310. o:depends("transport", "kcp")
  311. o.default = 5
  312. o.rmempty = true
  313. o = s:option(Value, "downlink_capacity", translate("Downlink Capacity"))
  314. o.datatype = "uinteger"
  315. o:depends("transport", "kcp")
  316. o.default = 20
  317. o.rmempty = true
  318. o = s:option(Value, "read_buffer_size", translate("Read Buffer Size"))
  319. o.datatype = "uinteger"
  320. o:depends("transport", "kcp")
  321. o.default = 2
  322. o.rmempty = true
  323. o = s:option(Value, "write_buffer_size", translate("Write Buffer Size"))
  324. o.datatype = "uinteger"
  325. o:depends("transport", "kcp")
  326. o.default = 2
  327. o.rmempty = true
  328. o = s:option(Value, "seed", translate("Obfuscate password (optional)"))
  329. o:depends({type = "vless", transport = "kcp"})
  330. o.rmempty = true
  331. o = s:option(Flag, "congestion", translate("Congestion"))
  332. o:depends("transport", "kcp")
  333. o.rmempty = true
  334. -- [[ TLS ]]--
  335. o = s:option(Flag, "tls", translate("TLS"))
  336. o.rmempty = true
  337. o.default = "0"
  338. o:depends("type", "vmess")
  339. o:depends({type = "vless", xtls = false})
  340. o:depends("type", "trojan")
  341. -- XTLS
  342. if nixio.fs.access("/usr/bin/xray") or nixio.fs.access("/usr/bin/xray/xray") then
  343. o = s:option(Flag, "xtls", translate("XTLS"))
  344. o.rmempty = true
  345. o.default = "0"
  346. o:depends({type = "vless", transport = "tcp", tls = false})
  347. end
  348. -- Flow
  349. o = s:option(Value, "vless_flow", translate("Flow"))
  350. for _, v in ipairs(flows) do o:value(v, v) end
  351. o.rmempty = true
  352. o.default = "xtls-rprx-splice"
  353. o:depends("xtls", true)
  354. o = s:option(Value, "tls_host", translate("TLS Host"))
  355. o:depends("type", "trojan")
  356. o:depends("tls", true)
  357. o:depends("xtls", true)
  358. o.rmempty = true
  359. -- [[ allowInsecure ]]--
  360. o = s:option(Flag, "insecure", translate("allowInsecure"))
  361. o.rmempty = false
  362. o:depends("tls", true)
  363. o:depends("xtls", true)
  364. o.description = translate("If true, allowss insecure connection at TLS client, e.g., TLS server uses unverifiable certificates.")
  365. -- [[ Mux ]]--
  366. o = s:option(Flag, "mux", translate("Mux"))
  367. o.rmempty = false
  368. o:depends("type", "vmess")
  369. o:depends({type = "vless", xtls = false})
  370. o = s:option(Value, "concurrency", translate("Concurrency"))
  371. o.datatype = "uinteger"
  372. o.rmempty = true
  373. o.default = "8"
  374. o:depends("mux", "1")
  375. -- [[ Cert ]]--
  376. o = s:option(Flag, "certificate", translate("Self-signed Certificate"))
  377. o.rmempty = true
  378. o.default = "0"
  379. o:depends("type", "trojan")
  380. o:depends("type", "vmess")
  381. o:depends("type", "vless")
  382. o.description = translate("If you have a self-signed certificate,please check the box")
  383. o = s:option(DummyValue, "upload", translate("Upload"))
  384. o.template = "shadowsocksr/certupload"
  385. o:depends("certificate", 1)
  386. cert_dir = "/etc/ssl/private/"
  387. local path
  388. luci.http.setfilehandler(function(meta, chunk, eof)
  389. if not fd then
  390. if (not meta) or (not meta.name) or (not meta.file) then return end
  391. fd = nixio.open(cert_dir .. meta.file, "w")
  392. if not fd then
  393. path = translate("Create upload file error.")
  394. return
  395. end
  396. end
  397. if chunk and fd then fd:write(chunk) end
  398. if eof and fd then
  399. fd:close()
  400. fd = nil
  401. path = '/etc/ssl/private/' .. meta.file .. ''
  402. end
  403. end)
  404. if luci.http.formvalue("upload") then
  405. local f = luci.http.formvalue("ulfile")
  406. if #f <= 0 then path = translate("No specify upload file.") end
  407. end
  408. o = s:option(Value, "certpath", translate("Current Certificate Path"))
  409. o:depends("certificate", 1)
  410. o:value("/etc/ssl/private/")
  411. o.description = translate("Please confirm the current certificate path")
  412. o.default = "/etc/ssl/private/"
  413. o = s:option(Flag, "fast_open", translate("TCP Fast Open"))
  414. o.rmempty = true
  415. o.default = "0"
  416. o:depends("type", "ssr")
  417. o:depends("type", "ss")
  418. o:depends("type", "trojan")
  419. o = s:option(Flag, "switch_enable", translate("Enable Auto Switch"))
  420. o.rmempty = false
  421. o.default = "1"
  422. o = s:option(Value, "local_port", translate("Local Port"))
  423. o.datatype = "port"
  424. o.default = 1234
  425. o.rmempty = false
  426. if nixio.fs.access("/usr/bin/kcptun-client") then
  427. kcp_enable = s:option(Flag, "kcp_enable", translate("KcpTun Enable"), translate("bin:/usr/bin/kcptun-client"))
  428. kcp_enable.rmempty = true
  429. kcp_enable.default = "0"
  430. kcp_enable:depends("type", "ssr")
  431. kcp_enable:depends("type", "ss")
  432. o = s:option(Value, "kcp_port", translate("KcpTun Port"))
  433. o.datatype = "port"
  434. o.default = 4000
  435. function o.validate(self, value, section)
  436. local kcp_file = "/usr/bin/kcptun-client"
  437. local enable = kcp_enable:formvalue(section) or kcp_enable.disabled
  438. if enable == kcp_enable.enabled then
  439. if not nixio.fs.access(kcp_file) then
  440. return nil, translate("Haven't a Kcptun executable file")
  441. elseif not isKcptun(kcp_file) then
  442. return nil, translate("Not a Kcptun executable file")
  443. end
  444. end
  445. return value
  446. end
  447. o:depends("type", "ssr")
  448. o:depends("type", "ss")
  449. o = s:option(Value, "kcp_password", translate("KcpTun Password"))
  450. o.password = true
  451. o:depends("type", "ssr")
  452. o:depends("type", "ss")
  453. o = s:option(Value, "kcp_param", translate("KcpTun Param"))
  454. o.default = "--nocomp"
  455. o:depends("type", "ssr")
  456. o:depends("type", "ss")
  457. end
  458. return m