client-config.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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("v2ray", translate("V2Ray"))
  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
  139. if e ~= "lo" then o:value(e) end
  140. end
  141. o:depends("type", "tun")
  142. o.description = translate("Redirect traffic to this network interface")
  143. o = s:option(Value, "server", translate("Server Address"))
  144. o.datatype = "host"
  145. o.rmempty = false
  146. o:depends("type", "ssr")
  147. o:depends("type", "ss")
  148. o:depends("type", "v2ray")
  149. o:depends("type", "vless")
  150. o:depends("type", "trojan")
  151. o:depends("type", "naiveproxy")
  152. o:depends("type", "socks5")
  153. o = s:option(Value, "server_port", translate("Server Port"))
  154. o.datatype = "port"
  155. o.rmempty = false
  156. o:depends("type", "ssr")
  157. o:depends("type", "ss")
  158. o:depends("type", "v2ray")
  159. o:depends("type", "vless")
  160. o:depends("type", "trojan")
  161. o:depends("type", "naiveproxy")
  162. o:depends("type", "socks5")
  163. o = s:option(Flag, "auth_enable", translate("Enable Authentication"))
  164. o.rmempty = false
  165. o.default = "0"
  166. o:depends("type", "socks5")
  167. o = s:option(Value, "username", translate("Username"))
  168. o.rmempty = true
  169. o:depends("type", "naiveproxy")
  170. o:depends("type", "socks5")
  171. o = s:option(Value, "password", translate("Password"))
  172. o.password = true
  173. o.rmempty = true
  174. o:depends("type", "ssr")
  175. o:depends("type", "ss")
  176. o:depends("type", "trojan")
  177. o:depends("type", "naiveproxy")
  178. o:depends("type", "socks5")
  179. o = s:option(ListValue, "encrypt_method", translate("Encrypt Method"))
  180. for _, v in ipairs(encrypt_methods) do o:value(v) end
  181. o.rmempty = true
  182. o:depends("type", "ssr")
  183. o = s:option(ListValue, "encrypt_method_ss", translate("Encrypt Method"))
  184. for _, v in ipairs(encrypt_methods_ss) do o:value(v) end
  185. o.rmempty = true
  186. o:depends("type", "ss")
  187. -- Shadowsocks Plugin
  188. o = s:option(Value, "plugin", translate("Plugin"))
  189. o.rmempty = true
  190. o:depends("type", "ss")
  191. o = s:option(Value, "plugin_opts", translate("Plugin Opts"))
  192. o.rmempty = true
  193. o:depends("type", "ss")
  194. o = s:option(ListValue, "protocol", translate("Protocol"))
  195. for _, v in ipairs(protocol) do o:value(v) end
  196. o.rmempty = true
  197. o:depends("type", "ssr")
  198. o = s:option(Value, "protocol_param", translate("Protocol param(optional)"))
  199. o:depends("type", "ssr")
  200. o = s:option(ListValue, "obfs", translate("Obfs"))
  201. for _, v in ipairs(obfs) do o:value(v) end
  202. o.rmempty = true
  203. o:depends("type", "ssr")
  204. o = s:option(Value, "obfs_param", translate("Obfs param(optional)"))
  205. o:depends("type", "ssr")
  206. -- AlterId
  207. o = s:option(Value, "alter_id", translate("AlterId"))
  208. o.datatype = "port"
  209. o.default = 16
  210. o.rmempty = true
  211. o:depends("type", "v2ray")
  212. -- VmessId
  213. o = s:option(Value, "vmess_id", translate("Vmess/VLESS ID (UUID)"))
  214. o.rmempty = true
  215. o.default = uuid
  216. o:depends("type", "v2ray")
  217. o:depends("type", "vless")
  218. -- VLESS Encryption
  219. o = s:option(Value, "vless_encryption", translate("VLESS Encryption"))
  220. o.rmempty = true
  221. o.default = "none"
  222. o:depends("type", "vless")
  223. -- 加密方式
  224. o = s:option(ListValue, "security", translate("Encrypt Method"))
  225. for _, v in ipairs(securitys) do o:value(v, v:upper()) end
  226. o.rmempty = true
  227. o:depends("type", "v2ray")
  228. -- 传输协议
  229. o = s:option(ListValue, "transport", translate("Transport"))
  230. o:value("tcp", "TCP")
  231. o:value("kcp", "mKCP")
  232. o:value("ws", "WebSocket")
  233. o:value("h2", "HTTP/2")
  234. o:value("quic", "QUIC")
  235. o.rmempty = true
  236. o:depends("type", "v2ray")
  237. o:depends("type", "vless")
  238. -- [[ TCP部分 ]]--
  239. -- TCP伪装
  240. o = s:option(ListValue, "tcp_guise", translate("Camouflage Type"))
  241. o:depends("transport", "tcp")
  242. o:value("none", translate("None"))
  243. o:value("http", "HTTP")
  244. o.rmempty = true
  245. -- HTTP域名
  246. o = s:option(Value, "http_host", translate("HTTP Host"))
  247. o:depends("tcp_guise", "http")
  248. o.rmempty = true
  249. -- HTTP路径
  250. o = s:option(Value, "http_path", translate("HTTP Path"))
  251. o:depends("tcp_guise", "http")
  252. o.rmempty = true
  253. -- [[ WS部分 ]]--
  254. -- WS域名
  255. o = s:option(Value, "ws_host", translate("WebSocket Host"))
  256. o:depends("transport", "ws")
  257. o.rmempty = true
  258. -- WS路径
  259. o = s:option(Value, "ws_path", translate("WebSocket Path"))
  260. o:depends("transport", "ws")
  261. o.rmempty = true
  262. -- [[ H2部分 ]]--
  263. -- H2域名
  264. o = s:option(Value, "h2_host", translate("HTTP/2 Host"))
  265. o:depends("transport", "h2")
  266. o.rmempty = true
  267. -- H2路径
  268. o = s:option(Value, "h2_path", translate("HTTP/2 Path"))
  269. o:depends("transport", "h2")
  270. o.rmempty = true
  271. -- [[ QUIC部分 ]]--
  272. o = s:option(ListValue, "quic_security", translate("QUIC Security"))
  273. o:depends("transport", "quic")
  274. o:value("none", translate("None"))
  275. o:value("aes-128-gcm", translate("aes-128-gcm"))
  276. o:value("chacha20-poly1305", translate("chacha20-poly1305"))
  277. o.rmempty = true
  278. o = s:option(Value, "quic_key", translate("QUIC Key"))
  279. o:depends("transport", "quic")
  280. o.rmempty = true
  281. o = s:option(ListValue, "quic_guise", translate("Header"))
  282. o:depends("transport", "quic")
  283. o.rmempty = true
  284. o:value("none", translate("None"))
  285. o:value("srtp", translate("VideoCall (SRTP)"))
  286. o:value("utp", translate("BitTorrent (uTP)"))
  287. o:value("wechat-video", translate("WechatVideo"))
  288. o:value("dtls", "DTLS 1.2")
  289. o:value("wireguard", "WireGuard")
  290. -- [[ mKCP部分 ]]--
  291. o = s:option(ListValue, "kcp_guise", translate("Camouflage Type"))
  292. o:depends("transport", "kcp")
  293. o:value("none", translate("None"))
  294. o:value("srtp", translate("VideoCall (SRTP)"))
  295. o:value("utp", translate("BitTorrent (uTP)"))
  296. o:value("wechat-video", translate("WechatVideo"))
  297. o:value("dtls", "DTLS 1.2")
  298. o:value("wireguard", "WireGuard")
  299. o.rmempty = true
  300. o = s:option(Value, "mtu", translate("MTU"))
  301. o.datatype = "uinteger"
  302. o:depends("transport", "kcp")
  303. o.default = 1350
  304. o.rmempty = true
  305. o = s:option(Value, "tti", translate("TTI"))
  306. o.datatype = "uinteger"
  307. o:depends("transport", "kcp")
  308. o.default = 50
  309. o.rmempty = true
  310. o = s:option(Value, "uplink_capacity", translate("Uplink Capacity"))
  311. o.datatype = "uinteger"
  312. o:depends("transport", "kcp")
  313. o.default = 5
  314. o.rmempty = true
  315. o = s:option(Value, "downlink_capacity", translate("Downlink Capacity"))
  316. o.datatype = "uinteger"
  317. o:depends("transport", "kcp")
  318. o.default = 20
  319. o.rmempty = true
  320. o = s:option(Value, "read_buffer_size", translate("Read Buffer Size"))
  321. o.datatype = "uinteger"
  322. o:depends("transport", "kcp")
  323. o.default = 2
  324. o.rmempty = true
  325. o = s:option(Value, "write_buffer_size", translate("Write Buffer Size"))
  326. o.datatype = "uinteger"
  327. o:depends("transport", "kcp")
  328. o.default = 2
  329. o.rmempty = true
  330. o = s:option(Value, "seed", translate("Obfuscate password (optional)"))
  331. o:depends({type="vless", transport="kcp"})
  332. o.rmempty = true
  333. o = s:option(Flag, "congestion", translate("Congestion"))
  334. o:depends("transport", "kcp")
  335. o.rmempty = true
  336. -- [[ allowInsecure ]]--
  337. o = s:option(Flag, "insecure", translate("allowInsecure"))
  338. o.rmempty = false
  339. o:depends("type", "v2ray")
  340. o:depends("type", "vless")
  341. o:depends("type", "trojan")
  342. o.default = "0"
  343. o.description = translate("If true, allowss insecure connection at TLS client, e.g., TLS server uses unverifiable certificates.")
  344. -- [[ TLS ]]--
  345. o = s:option(Flag, "tls", translate("TLS"))
  346. o.rmempty = true
  347. o.default = "0"
  348. o:depends("type", "v2ray")
  349. o:depends("type", "vless")
  350. o:depends("type", "trojan")
  351. o = s:option(Value, "tls_host", translate("TLS Host"))
  352. --o:depends("type", "trojan")
  353. o:depends("tls", "1")
  354. o.rmempty = true
  355. -- XTLS
  356. if nixio.fs.access("/usr/bin/xray") or nixio.fs.access("/usr/bin/xray/xray") then
  357. o = s:option(Flag, "xtls", translate("XTLS"))
  358. o.rmempty = true
  359. o.default = "0"
  360. o:depends({type="vless", tls=true})
  361. end
  362. -- Flow
  363. o = s:option(Value, "vless_flow", translate("Flow"))
  364. for _, v in ipairs(flows) do o:value(v, v) end
  365. o.rmempty = true
  366. o.default = "xtls-rprx-origin"
  367. o:depends("xtls", "1")
  368. -- [[ Mux ]]--
  369. o = s:option(Flag, "mux", translate("Mux"))
  370. o.rmempty = true
  371. o.default = "0"
  372. o:depends("type", "v2ray")
  373. o:depends({type="vless", xtls=false})
  374. o = s:option(Value, "concurrency", translate("Concurrency"))
  375. o.datatype = "uinteger"
  376. o.rmempty = true
  377. o.default = "8"
  378. o:depends("mux", "1")
  379. -- [[ Cert ]]--
  380. o = s:option(Flag, "certificate", translate("Self-signed Certificate"))
  381. o.rmempty = true
  382. o.default = "0"
  383. o:depends("type", "trojan")
  384. o:depends("type", "v2ray")
  385. o:depends("type", "vless")
  386. o.description = translate("If you have a self-signed certificate,please check the box")
  387. o = s:option(DummyValue, "upload", translate("Upload"))
  388. o.template = "shadowsocksr/certupload"
  389. o:depends("certificate", 1)
  390. cert_dir = "/etc/ssl/private/"
  391. local path
  392. luci.http.setfilehandler(
  393. function(meta, chunk, eof)
  394. if not fd then
  395. if (not meta) or (not meta.name) or (not meta.file) then return end
  396. fd = nixio.open(cert_dir .. meta.file, "w")
  397. if not fd then
  398. path = translate("Create upload file error.")
  399. return
  400. end
  401. end
  402. if chunk and fd then
  403. fd:write(chunk)
  404. end
  405. if eof and fd then
  406. fd:close()
  407. fd = nil
  408. path = '/etc/ssl/private/' .. meta.file .. ''
  409. end
  410. end
  411. )
  412. if luci.http.formvalue("upload") then
  413. local f = luci.http.formvalue("ulfile")
  414. if #f <= 0 then
  415. path = translate("No specify upload file.")
  416. end
  417. end
  418. o = s:option(Value, "certpath", translate("Current Certificate Path"))
  419. o:depends("certificate", 1)
  420. o:value("/etc/ssl/private/")
  421. o.description = translate("Please confirm the current certificate path")
  422. o.default = "/etc/ssl/private/"
  423. o = s:option(Flag, "fast_open", translate("TCP Fast Open"))
  424. o.rmempty = true
  425. o.default = "0"
  426. o:depends("type", "ssr")
  427. o:depends("type", "ss")
  428. o:depends("type", "trojan")
  429. o = s:option(Flag, "switch_enable", translate("Enable Auto Switch"))
  430. o.rmempty = false
  431. o.default = "1"
  432. o = s:option(Value, "local_port", translate("Local Port"))
  433. o.datatype = "port"
  434. o.default = 1234
  435. o.rmempty = false
  436. if nixio.fs.access("/usr/bin/kcptun-client") then
  437. kcp_enable = s:option(Flag, "kcp_enable", translate("KcpTun Enable"), translate("bin:/usr/bin/kcptun-client"))
  438. kcp_enable.rmempty = true
  439. kcp_enable.default = "0"
  440. kcp_enable:depends("type", "ssr")
  441. kcp_enable:depends("type", "ss")
  442. o = s:option(Value, "kcp_port", translate("KcpTun Port"))
  443. o.datatype = "port"
  444. o.default = 4000
  445. function o.validate(self, value, section)
  446. local kcp_file="/usr/bin/kcptun-client"
  447. local enable = kcp_enable:formvalue(section) or kcp_enable.disabled
  448. if enable == kcp_enable.enabled then
  449. if not nixio.fs.access(kcp_file) then
  450. return nil, translate("Haven't a Kcptun executable file")
  451. elseif not isKcptun(kcp_file) then
  452. return nil, translate("Not a Kcptun executable file")
  453. end
  454. end
  455. return value
  456. end
  457. o:depends("type", "ssr")
  458. o:depends("type", "ss")
  459. o = s:option(Value, "kcp_password", translate("KcpTun Password"))
  460. o.password = true
  461. o:depends("type", "ssr")
  462. o:depends("type", "ss")
  463. o = s:option(Value, "kcp_param", translate("KcpTun Param"))
  464. o.default = "--nocomp"
  465. o:depends("type", "ssr")
  466. o:depends("type", "ss")
  467. end
  468. return m