gen_config.lua 18 KB

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