gen_config.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. #!/usr/bin/lua
  2. local ucursor = require "luci.model.uci".cursor()
  3. local json = require "luci.jsonc"
  4. local server_section = arg[1]
  5. local proto = arg[2]
  6. local local_port = arg[3] or "0"
  7. local socks_port = arg[4] or "0"
  8. local chain = arg[5] or "0"
  9. local chain_local_port = string.split(chain, "/")[2] or "0"
  10. local server = ucursor:get_all("shadowsocksr", server_section)
  11. local outbound_settings = nil
  12. function vmess_vless()
  13. outbound_settings = {
  14. vnext = {
  15. {
  16. address = server.server,
  17. port = tonumber(server.server_port),
  18. users = {
  19. {
  20. id = server.vmess_id,
  21. alterId = (server.v2ray_protocol == "vmess" or not server.v2ray_protocol) and tonumber(server.alter_id) or nil,
  22. security = (server.v2ray_protocol == "vmess" or not server.v2ray_protocol) and server.security or nil,
  23. encryption = (server.v2ray_protocol == "vless") and server.vless_encryption or nil,
  24. flow = ((server.tls == '1') or (server.reality == '1')) and server.tls_flow or nil
  25. }
  26. }
  27. }
  28. }
  29. }
  30. end
  31. function trojan_shadowsocks()
  32. outbound_settings = {
  33. servers = {
  34. {
  35. address = server.server,
  36. port = tonumber(server.server_port),
  37. password = server.password,
  38. method = ((server.v2ray_protocol == "shadowsocks") and server.encrypt_method_ss) or nil,
  39. uot = (server.v2ray_protocol == "shadowsocks") and (server.uot == '1') or nil,
  40. ivCheck = (server.v2ray_protocol == "shadowsocks") and (server.ivCheck == '1') or nil,
  41. }
  42. }
  43. }
  44. end
  45. function socks_http()
  46. outbound_settings = {
  47. version = server.socks_ver or nil,
  48. servers = {
  49. {
  50. address = server.server,
  51. port = tonumber(server.server_port),
  52. users = (server.auth_enable == "1") and {
  53. {
  54. user = server.username,
  55. pass = server.password
  56. }
  57. } or nil
  58. }
  59. }
  60. }
  61. end
  62. function wireguard()
  63. outbound_settings = {
  64. secretKey = server.private_key,
  65. address = server.local_addresses,
  66. peers = {
  67. {
  68. publicKey = server.peer_pubkey,
  69. preSharedKey = server.preshared_key,
  70. endpoint = server.server .. ":" .. server.server_port
  71. }
  72. },
  73. mtu = tonumber(server.mtu)
  74. }
  75. end
  76. local outbound = {}
  77. function outbound:new(o)
  78. o = o or {}
  79. setmetatable(o, self)
  80. self.__index = self
  81. return o
  82. end
  83. function outbound:handleIndex(index)
  84. local switch = {
  85. vmess = function()
  86. vmess_vless()
  87. end,
  88. vless = function()
  89. vmess_vless()
  90. end,
  91. trojan = function()
  92. trojan_shadowsocks()
  93. end,
  94. shadowsocks = function()
  95. trojan_shadowsocks()
  96. end,
  97. socks = function()
  98. socks_http()
  99. end,
  100. http = function()
  101. socks_http()
  102. end,
  103. wireguard = function()
  104. wireguard()
  105. end
  106. }
  107. if switch[index] then
  108. switch[index]()
  109. end
  110. end
  111. local settings = outbound:new()
  112. settings:handleIndex(server.v2ray_protocol)
  113. local Xray = {
  114. log = {
  115. -- error = "/var/ssrplus.log",
  116. loglevel = "warning"
  117. },
  118. -- 传入连接
  119. inbound = (local_port ~= "0") and {
  120. -- listening
  121. port = tonumber(local_port),
  122. protocol = "dokodemo-door",
  123. settings = {network = proto, followRedirect = true},
  124. sniffing = {
  125. enabled = true,
  126. destOverride = {"http", "tls", "quic"},
  127. domainsExcluded = {
  128. "courier.push.apple.com",
  129. "rbsxbxp-mim.vivox.com",
  130. "rbsxbxp.www.vivox.com",
  131. "rbsxbxp-ws.vivox.com",
  132. "rbspsxp.www.vivox.com",
  133. "rbspsxp-mim.vivox.com",
  134. "rbspsxp-ws.vivox.com",
  135. "rbswxp.www.vivox.com",
  136. "rbswxp-mim.vivox.com",
  137. "disp-rbspsp-5-1.vivox.com",
  138. "disp-rbsxbp-5-1.vivox.com",
  139. "proxy.rbsxbp.vivox.com",
  140. "proxy.rbspsp.vivox.com",
  141. "proxy.rbswp.vivox.com",
  142. "rbswp.vivox.com",
  143. "rbsxbp.vivox.com",
  144. "rbspsp.vivox.com",
  145. "rbspsp.www.vivox.com",
  146. "rbswp.www.vivox.com",
  147. "rbsxbp.www.vivox.com",
  148. "rbsxbxp.vivox.com",
  149. "rbspsxp.vivox.com",
  150. "rbswxp.vivox.com",
  151. "Mijia Cloud",
  152. "dlg.io.mi.com"
  153. }
  154. }
  155. } or nil,
  156. -- 开启 socks 代理
  157. inboundDetour = (proto:find("tcp") and socks_port ~= "0") and {
  158. {
  159. -- socks
  160. protocol = "socks",
  161. port = tonumber(socks_port),
  162. settings = {auth = "noauth", udp = true}
  163. }
  164. } or nil,
  165. -- 传出连接
  166. outbound = {
  167. protocol = server.v2ray_protocol,
  168. settings = outbound_settings,
  169. -- 底层传输配置
  170. streamSettings = {
  171. network = server.transport or "tcp",
  172. security = (server.tls == '1') and "tls" or (server.reality == '1') and "reality" or nil,
  173. tlsSettings = (server.tls == '1') and {
  174. -- tls
  175. alpn = server.tls_alpn,
  176. fingerprint = server.fingerprint,
  177. allowInsecure = (server.insecure == "1"),
  178. serverName = server.tls_host,
  179. certificates = server.certificate and {
  180. usage = "verify",
  181. certificateFile = server.certpath
  182. } or nil,
  183. } or nil,
  184. realitySettings = (server.reality == '1') and {
  185. publicKey = server.reality_publickey,
  186. shortId = server.reality_shortid,
  187. spiderX = server.reality_spiderx,
  188. fingerprint = server.fingerprint,
  189. serverName = server.tls_host
  190. } or nil,
  191. tcpSettings = (server.transport == "tcp" and server.tcp_guise == "http") and {
  192. -- tcp
  193. header = {
  194. type = server.tcp_guise,
  195. request = {
  196. -- request
  197. path = {server.http_path} or {"/"},
  198. headers = {Host = {server.http_host} or {}}
  199. }
  200. }
  201. } or nil,
  202. kcpSettings = (server.transport == "kcp") and {
  203. -- kcp
  204. mtu = tonumber(server.mtu),
  205. tti = tonumber(server.tti),
  206. uplinkCapacity = tonumber(server.uplink_capacity),
  207. downlinkCapacity = tonumber(server.downlink_capacity),
  208. congestion = (server.congestion == "1") and true or false,
  209. readBufferSize = tonumber(server.read_buffer_size),
  210. writeBufferSize = tonumber(server.write_buffer_size),
  211. header = {type = server.kcp_guise},
  212. seed = server.seed or nil
  213. } or nil,
  214. wsSettings = (server.transport == "ws") and (server.ws_path or server.ws_host or server.tls_host) and {
  215. -- ws
  216. headers = (server.ws_host or server.tls_host) and {
  217. -- headers
  218. Host = server.ws_host or server.tls_host
  219. } or nil,
  220. path = server.ws_path,
  221. maxEarlyData = tonumber(server.ws_ed) or nil,
  222. earlyDataHeaderName = server.ws_ed_header or nil
  223. } or nil,
  224. httpupgradeSettings = (server.transport == "httpupgrade") and {
  225. -- httpupgrade
  226. host = (server.httpupgrade_host or server.tls_host) or nil,
  227. path = server.httpupgrade_path or ""
  228. } or nil,
  229. httpSettings = (server.transport == "h2") and {
  230. -- h2
  231. path = server.h2_path or "",
  232. host = {server.h2_host} or nil,
  233. read_idle_timeout = tonumber(server.read_idle_timeout) or nil,
  234. health_check_timeout = tonumber(server.health_check_timeout) or nil
  235. } or nil,
  236. quicSettings = (server.transport == "quic") and {
  237. -- quic
  238. security = server.quic_security,
  239. key = server.quic_key,
  240. header = {type = server.quic_guise}
  241. } or nil,
  242. grpcSettings = (server.transport == "grpc") and {
  243. -- grpc
  244. serviceName = server.serviceName or "",
  245. multiMode = (server.grpc_mode == "multi") and true or false,
  246. idle_timeout = tonumber(server.idle_timeout) or nil,
  247. health_check_timeout = tonumber(server.health_check_timeout) or nil,
  248. permit_without_stream = (server.permit_without_stream == "1") and true or nil,
  249. initial_windows_size = tonumber(server.initial_windows_size) or nil
  250. } or nil,
  251. sockopt = {
  252. tcpMptcp = (server.mptcp == "1") and true or false, -- MPTCP
  253. tcpNoDelay = (server.mptcp == "1") and true or false, -- MPTCP
  254. tcpcongestion = server.custom_tcpcongestion -- 连接服务器节点的 TCP 拥塞控制算法
  255. }
  256. },
  257. mux = {
  258. -- mux
  259. enabled = (server.mux == "1") and true or false, -- Mux
  260. concurrency = tonumber(server.concurrency), -- TCP 最大并发连接数
  261. xudpConcurrency = tonumber(server.xudpConcurrency), -- UDP 最大并发连接数
  262. xudpProxyUDP443 = server.xudpProxyUDP443 -- 对被代理的 UDP/443 流量处理方式
  263. }
  264. }
  265. }
  266. local cipher = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:AES128-SHA:AES256-SHA:DES-CBC3-SHA"
  267. local cipher13 = "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384"
  268. local trojan = {
  269. log_level = 3,
  270. run_type = (proto == "nat" or proto == "tcp") and "nat" or "client",
  271. local_addr = "0.0.0.0",
  272. local_port = tonumber(local_port),
  273. remote_addr = server.server,
  274. remote_port = tonumber(server.server_port),
  275. udp_timeout = 60,
  276. -- 传入连接
  277. password = {server.password},
  278. -- 传出连接
  279. ssl = {
  280. verify = (server.insecure == "0") and true or false,
  281. verify_hostname = (server.tls == "1") and true or false,
  282. cert = (server.certificate) and server.certpath or nil,
  283. cipher = cipher,
  284. cipher_tls13 = cipher13,
  285. sni = server.tls_host,
  286. alpn = server.tls_alpn or {"h2", "http/1.1"},
  287. curve = "",
  288. reuse_session = true,
  289. session_ticket = (server.tls_sessionTicket == "1") and true or false
  290. },
  291. udp_timeout = 60,
  292. tcp = {
  293. -- tcp
  294. no_delay = true,
  295. keep_alive = true,
  296. reuse_port = true,
  297. fast_open = (server.fast_open == "1") and true or false,
  298. fast_open_qlen = 20
  299. }
  300. }
  301. local naiveproxy = {
  302. proxy = (server.username and server.password and server.server and server.server_port) and "https://" .. server.username .. ":" .. server.password .. "@" .. server.server .. ":" .. server.server_port,
  303. listen = (proto == "redir") and "redir" .. "://0.0.0.0:" .. tonumber(local_port) or "socks" .. "://0.0.0.0:" .. tonumber(local_port),
  304. ["insecure-concurrency"] = tonumber(server.concurrency) or 1
  305. }
  306. local ss = {
  307. server = (server.kcp_enable == "1") and "127.0.0.1" or server.server,
  308. server_port = tonumber(server.server_port),
  309. local_address = "0.0.0.0",
  310. local_port = tonumber(local_port),
  311. mode = (proto == "tcp,udp") and "tcp_and_udp" or proto .. "_only",
  312. password = server.password,
  313. method = server.encrypt_method_ss,
  314. timeout = tonumber(server.timeout),
  315. fast_open = (server.fast_open == "1") and true or false,
  316. reuse_port = true
  317. }
  318. local hysteria = {
  319. server = (server.server_port and (server.port_range and (server.server .. ":" .. server.server_port .. "," .. server.port_range) or server.server .. ":" .. server.server_port) or (server.port_range and server.server .. ":" .. server.port_range or server.server .. ":443")),
  320. bandwidth = {
  321. up = tonumber(server.uplink_capacity) and tonumber(server.uplink_capacity) .. " mbps" or nil,
  322. down = tonumber(server.downlink_capacity) and tonumber(server.downlink_capacity) .. " mbps" or nil
  323. },
  324. socks5 = (proto:find("tcp") and tonumber(socks_port) and tonumber(socks_port) ~= 0) and {
  325. listen = "0.0.0.0:" .. tonumber(socks_port),
  326. disable_udp = false
  327. } or nil,
  328. transport = (server.transport_protocol) and {
  329. type = (server.transport_protocol) or udp,
  330. udp = (server.port_range and (server.hopinterval) and {
  331. hopInterval = (server.port_range and (tonumber(server.hopinterval) .. "s") or nil)
  332. } or nil)
  333. } or nil,
  334. --[[
  335. tcpTProxy = (proto:find("tcp") and local_port ~= "0") and {
  336. listen = "0.0.0.0:" .. tonumber(local_port)
  337. } or nil,
  338. ]]
  339. tcpRedirect = (proto:find("tcp") and local_port ~= "0") and {
  340. listen = "0.0.0.0:" .. tonumber(local_port)
  341. } or nil,
  342. udpTProxy = (proto:find("udp") and local_port ~= "0") and {
  343. listen = "0.0.0.0:" .. tonumber(local_port)
  344. } or nil,
  345. obfs = (server.flag_obfs == "1") and {
  346. type = server.obfs_type,
  347. salamander = { password = server.salamander }
  348. } or nil,
  349. quic = (server.flag_quicparam == "1" ) and {
  350. initStreamReceiveWindow = (server.initstreamreceivewindow and server.initstreamreceivewindow or nil),
  351. maxStreamReceiveWindow = (server.maxstreamseceivewindow and server.maxstreamseceivewindow or nil),
  352. initConnReceiveWindow = (server.initconnreceivewindow and server.initconnreceivewindow or nil),
  353. maxConnReceiveWindow = (server.maxconnreceivewindow and server.maxconnreceivewindow or nil),
  354. maxIdleTimeout = (tonumber(server.maxidletimeout) and tonumber(server.maxidletimeout) .. "s" or nil),
  355. keepAlivePeriod = (tonumber(server.keepaliveperiod) and tonumber(server.keepaliveperiod) .. "s" or nil),
  356. disable_mtu_discovery = (server.disablepathmtudiscovery == "1") and true or false
  357. } or nil,
  358. auth = server.hy2_auth,
  359. tls = (server.tls_host) and {
  360. sni = server.tls_host,
  361. --alpn = server.tls_alpn or nil,
  362. insecure = (server.insecure == "1") and true or false,
  363. pinSHA256 = (server.insecure == "1") and server.pinsha256 or nil
  364. } or {
  365. sni = server.server,
  366. insecure = (server.insecure == "1") and true or false
  367. },
  368. fast_open = (server.fast_open == "1") and true or false,
  369. lazy = (server.lazy_mode == "1") and true or false
  370. }
  371. local shadowtls = {
  372. client = {
  373. server_addr = server.server_port and server.server .. ":" .. server.server_port or nil,
  374. listen = "127.0.0.1:" .. tonumber(local_port),
  375. tls_names = server.shadowtls_sni,
  376. password = server.password
  377. },
  378. v3 = (server.shadowtls_protocol == "v3") and true or false,
  379. disable_nodelay = (server.disable_nodelay == "1") and true or false,
  380. fastopen = (server.fastopen == "1") and true or false,
  381. strict = (server.strict == "1") and true or false
  382. }
  383. local chain_sslocal = {
  384. locals = local_port ~= "0" and {
  385. {
  386. local_address = "0.0.0.0",
  387. local_port = (chain_local_port == "0" and tonumber(server.local_port) or tonumber(chain_local_port)),
  388. mode = (proto:find("tcp,udp") and "tcp_and_udp") or proto .. "_only",
  389. protocol = "redir",
  390. tcp_redir = "redirect",
  391. --tcp_redir = "tproxy",
  392. udp_redir = "tproxy"
  393. },
  394. socks_port ~= "0" and {
  395. protocol = "socks",
  396. local_address = "0.0.0.0",
  397. local_port = tonumber(socks_port)
  398. } or nil
  399. } or {{
  400. protocol = "socks",
  401. local_address = "0.0.0.0",
  402. ocal_port = tonumber(socks_port)
  403. }},
  404. servers = {
  405. {
  406. server = "127.0.0.1",
  407. server_port = (tonumber(local_port) == 0 and tonumber(chain_local_port) or tonumber(local_port)),
  408. method = server.sslocal_method,
  409. password = server.sslocal_password
  410. }
  411. }
  412. }
  413. local chain_vmess = {
  414. inbounds = (local_port ~= "0") and {
  415. {
  416. port = (chain_local_port == "0" and tonumber(server.local_port) or tonumber(chain_local_port)),
  417. protocol = "dokodemo-door",
  418. settings = {
  419. network = proto,
  420. followRedirect = true
  421. },
  422. streamSettings = {
  423. sockopt = {tproxy = "redirect"}
  424. },
  425. sniffing = {
  426. enable = true,
  427. destOverride = {"http","tls"}
  428. }
  429. },
  430. (proto:find("tcp") and socks_port ~= "0") and {
  431. protocol = "socks",
  432. port = tonumber(socks_port)
  433. } or nil
  434. } or { protocol = "socks",port = tonumber(socks_port) },
  435. outbound = {
  436. protocol = "vmess",
  437. settings = {
  438. vnext = {{
  439. address = "127.0.0.1",
  440. port = (tonumber(local_port) == 0 and tonumber(chain_local_port) or tonumber(local_port)),
  441. users = {{
  442. id = (server.vmess_uuid),
  443. security = server.vmess_method,
  444. level = 0
  445. }}
  446. }}
  447. }
  448. }
  449. }
  450. local tuic = {
  451. relay = {
  452. server = server.server_port and server.server .. ":" .. server.server_port,
  453. ip = server.tuic_ip,
  454. uuid = server.tuic_uuid,
  455. password = server.tuic_passwd,
  456. certificates = server.certificate and { server.certpath } or nil,
  457. udp_relay_mode = server.udp_relay_mode,
  458. congestion_control = server.congestion_control,
  459. heartbeat = server.heartbeat and server.heartbeat .. "s" or nil,
  460. timeout = server.timeout and server.timeout .. "s" or nil,
  461. gc_interval = server.gc_interval and server.gc_interval .. "s" or nil,
  462. gc_lifetime = server.gc_lifetime and server.gc_lifetime .. "s" or nil,
  463. alpn = server.tls_alpn,
  464. disable_sni = (server.disable_sni == "1") and true or false,
  465. zero_rtt_handshake = (server.zero_rtt_handshake == "1") and true or false,
  466. send_window = tonumber(server.send_window),
  467. receive_window = tonumber(server.receive_window)
  468. },
  469. ["local"] = {
  470. server = tonumber(socks_port) and (server.tuic_dual_stack == "1" and "[::1]:" or "127.0.0.1:") .. (socks_port == "0" and local_port or tonumber(socks_port)),
  471. dual_stack = (server.tuic_dual_stack == "1") and true or false,
  472. max_packet_size = tonumber(server.tuic_max_package_size)
  473. }
  474. }
  475. local config = {}
  476. function config:new(o)
  477. o = o or {}
  478. setmetatable(o, self)
  479. self.__index = self
  480. return o
  481. end
  482. function config:handleIndex(index)
  483. local switch = {
  484. ss = function()
  485. ss.protocol = socks_port
  486. if server.plugin and server.plugin ~= "none" then
  487. ss.plugin = server.plugin
  488. ss.plugin_opts = server.plugin_opts or nil
  489. end
  490. print(json.stringify(ss, 1))
  491. end,
  492. ssr = function()
  493. ss.protocol = server.protocol
  494. ss.protocol_param = server.protocol_param
  495. ss.method = server.encrypt_method
  496. ss.obfs = server.obfs
  497. ss.obfs_param = server.obfs_param
  498. print(json.stringify(ss, 1))
  499. end,
  500. v2ray = function()
  501. print(json.stringify(Xray, 1))
  502. end,
  503. trojan = function()
  504. print(json.stringify(trojan, 1))
  505. end,
  506. naiveproxy = function()
  507. print(json.stringify(naiveproxy, 1))
  508. end,
  509. hysteria = function()
  510. print(json.stringify(hysteria, 1))
  511. end,
  512. shadowtls = function()
  513. local chain_switch = {
  514. sslocal = function()
  515. if (chain:find("chain")) then
  516. print(json.stringify(chain_sslocal, 1))
  517. else
  518. print(json.stringify(shadowtls, 1))
  519. end
  520. end,
  521. vmess = function()
  522. if (chain:find("chain")) then
  523. print(json.stringify(chain_vmess, 1))
  524. else
  525. print(json.stringify(shadowtls, 1))
  526. end
  527. end
  528. }
  529. local ChainType = server.chain_type
  530. if chain_switch[ChainType] then
  531. chain_switch[ChainType]()
  532. end
  533. end,
  534. tuic = function()
  535. print(json.stringify(tuic, 1))
  536. end
  537. }
  538. if switch[index] then
  539. switch[index]()
  540. end
  541. end
  542. local f = config:new()
  543. f:handleIndex(server.type)