gen_config.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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] or "tcp"
  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 socks_server = ucursor:get_all("shadowsocksr", "@socks5_proxy[0]") or {}
  12. local xray_fragment = ucursor:get_all("shadowsocksr", "@global_xray_fragment[0]") or {}
  13. local xray_noise = ucursor:get_all("shadowsocksr", "@xray_noise_packets[0]") or {}
  14. local outbound_settings = nil
  15. local node_id = server_section
  16. local remarks = server.alias or ""
  17. function vmess_vless()
  18. outbound_settings = {
  19. vnext = {
  20. {
  21. address = server.server,
  22. port = tonumber(server.server_port),
  23. users = {
  24. {
  25. id = server.vmess_id,
  26. alterId = (server.v2ray_protocol == "vmess" or not server.v2ray_protocol) and tonumber(server.alter_id) or nil,
  27. security = (server.v2ray_protocol == "vmess" or not server.v2ray_protocol) and server.security or nil,
  28. encryption = (server.v2ray_protocol == "vless") and server.vless_encryption or "none",
  29. flow = (((server.xtls == '1') or (server.tls == '1') or (server.reality == '1')) and (((server.tls_flow ~= "none") and server.tls_flow) or ((server.xhttp_tls_flow ~= "none") and server.xhttp_tls_flow))) or nil
  30. }
  31. }
  32. }
  33. }
  34. }
  35. end
  36. function trojan_shadowsocks()
  37. outbound_settings = {
  38. servers = {
  39. {
  40. address = server.server,
  41. port = tonumber(server.server_port),
  42. password = server.password,
  43. method = ((server.v2ray_protocol == "shadowsocks") and server.encrypt_method_ss) or nil,
  44. uot = (server.v2ray_protocol == "shadowsocks") and (server.uot == '1') or nil,
  45. ivCheck = (server.v2ray_protocol == "shadowsocks") and (server.ivCheck == '1') or nil,
  46. }
  47. }
  48. }
  49. end
  50. function socks_http()
  51. outbound_settings = {
  52. version = server.socks_ver or nil,
  53. servers = {
  54. {
  55. address = server.server,
  56. port = tonumber(server.server_port),
  57. users = (server.auth_enable == "1") and {
  58. {
  59. user = server.username,
  60. pass = server.password
  61. }
  62. } or nil
  63. }
  64. }
  65. }
  66. end
  67. function wireguard()
  68. outbound_settings = {
  69. secretKey = server.private_key,
  70. address = server.local_addresses,
  71. peers = {
  72. {
  73. publicKey = server.peer_pubkey,
  74. preSharedKey = server.preshared_key,
  75. endpoint = server.server .. ":" .. server.server_port,
  76. keepAlive = tonumber(server.keepaliveperiod),
  77. allowedIPs = (server.allowedips) or nil,
  78. }
  79. },
  80. noKernelTun = (server.kernelmode == "1") and true or false,
  81. reserved = {server.reserved} or nil,
  82. mtu = tonumber(server.mtu)
  83. }
  84. end
  85. local outbound = {}
  86. function outbound:new(o)
  87. o = o or {}
  88. setmetatable(o, self)
  89. self.__index = self
  90. return o
  91. end
  92. function outbound:handleIndex(index)
  93. local switch = {
  94. vmess = function()
  95. vmess_vless()
  96. end,
  97. vless = function()
  98. vmess_vless()
  99. end,
  100. trojan = function()
  101. trojan_shadowsocks()
  102. end,
  103. shadowsocks = function()
  104. trojan_shadowsocks()
  105. end,
  106. socks = function()
  107. socks_http()
  108. end,
  109. http = function()
  110. socks_http()
  111. end,
  112. wireguard = function()
  113. wireguard()
  114. end
  115. }
  116. if switch[index] then
  117. switch[index]()
  118. end
  119. end
  120. local settings = outbound:new()
  121. settings:handleIndex(server.v2ray_protocol)
  122. local Xray = {
  123. log = {
  124. -- error = "/var/ssrplus.log",
  125. loglevel = "warning"
  126. },
  127. -- 初始化 inbounds 表
  128. inbounds = {},
  129. -- 初始化 outbounds 表
  130. outbounds = {},
  131. }
  132. -- 传入连接
  133. -- 添加 dokodemo-door 配置,如果 local_port 不为 0
  134. if local_port ~= "0" then
  135. table.insert(Xray.inbounds, {
  136. -- listening
  137. port = tonumber(local_port),
  138. protocol = "dokodemo-door",
  139. settings = {network = proto, followRedirect = true},
  140. sniffing = {
  141. enabled = true,
  142. destOverride = {"http", "tls", "quic"},
  143. metadataOnly = false,
  144. domainsExcluded = {
  145. "courier.push.apple.com",
  146. "rbsxbxp-mim.vivox.com",
  147. "rbsxbxp.www.vivox.com",
  148. "rbsxbxp-ws.vivox.com",
  149. "rbspsxp.www.vivox.com",
  150. "rbspsxp-mim.vivox.com",
  151. "rbspsxp-ws.vivox.com",
  152. "rbswxp.www.vivox.com",
  153. "rbswxp-mim.vivox.com",
  154. "disp-rbspsp-5-1.vivox.com",
  155. "disp-rbsxbp-5-1.vivox.com",
  156. "proxy.rbsxbp.vivox.com",
  157. "proxy.rbspsp.vivox.com",
  158. "proxy.rbswp.vivox.com",
  159. "rbswp.vivox.com",
  160. "rbsxbp.vivox.com",
  161. "rbspsp.vivox.com",
  162. "rbspsp.www.vivox.com",
  163. "rbswp.www.vivox.com",
  164. "rbsxbp.www.vivox.com",
  165. "rbsxbxp.vivox.com",
  166. "rbspsxp.vivox.com",
  167. "rbswxp.vivox.com",
  168. "Mijia Cloud",
  169. "dlg.io.mi.com"
  170. }
  171. }
  172. })
  173. end
  174. -- 开启 socks 代理
  175. -- 检查是否启用 socks 代理
  176. if proto and proto:find("tcp") and socks_port ~= "0" then
  177. table.insert(Xray.inbounds, {
  178. -- socks
  179. protocol = "socks",
  180. port = tonumber(socks_port),
  181. settings = {
  182. auth = socks_server.socks5_auth or "noauth",
  183. udp = true,
  184. mixed = ((socks_server.socks5_mixed == '1') and true or false) or (socks_server.server == 'same') and nil,
  185. accounts = (socks_server.server ~= "same" and (socks_server.socks5_auth and socks_server.socks5_auth ~= "noauth")) and {
  186. {
  187. user = socks_server.socks5_user,
  188. pass = socks_server.socks5_pass
  189. }
  190. } or nil
  191. } or nil
  192. })
  193. end
  194. -- 传出连接
  195. Xray.outbounds = {
  196. {
  197. protocol = server.v2ray_protocol,
  198. settings = outbound_settings,
  199. -- 底层传输配置
  200. streamSettings = (server.v2ray_protocol ~= "wireguard") and {
  201. network = server.transport or "raw",
  202. security = (server.xtls == '1') and "xtls" or (server.tls == '1') and "tls" or (server.reality == '1') and "reality" or nil,
  203. tlsSettings = (server.tls == '1') and {
  204. -- tls
  205. alpn = (server.tls_alpn and server.tls_alpn ~= "") and (function()
  206. local alpn = {}
  207. string.gsub(server.tls_alpn, '[^,]+', function(w)
  208. table.insert(alpn, w)
  209. end)
  210. if #alpn > 0 then
  211. return alpn
  212. else
  213. return nil
  214. end
  215. end)() or nil,
  216. fingerprint = server.fingerprint,
  217. allowInsecure = (server.insecure == "1" or server.insecure == true or server.insecure == "true"),
  218. serverName = server.tls_host,
  219. certificates = server.certificate and {
  220. usage = "verify",
  221. certificateFile = server.certpath
  222. } or nil,
  223. echConfigList = (server.enable_ech == "1") and server.ech_config or nil,
  224. echForceQuery = (server.enable_ech == "1") and (server.ech_ForceQuery or "none") or nil
  225. } or nil,
  226. xtlsSettings = (server.xtls == '1') and server.tls_host and {
  227. -- xtls
  228. allowInsecure = (server.insecure == "1") and true or nil,
  229. serverName = server.tls_host,
  230. minVersion = "1.3"
  231. } or nil,
  232. realitySettings = (server.reality == '1') and {
  233. publicKey = server.reality_publickey,
  234. shortId = server.reality_shortid or "",
  235. spiderX = server.reality_spiderx or "",
  236. fingerprint = server.fingerprint,
  237. mldsa65Verify = (server.enable_mldsa65verify == '1') and server.reality_mldsa65verify or nil,
  238. serverName = server.tls_host
  239. } or nil,
  240. rawSettings = (server.transport == "raw" or server.transport == "tcp") and {
  241. -- tcp
  242. header = {
  243. type = server.tcp_guise,
  244. request = (server.tcp_guise == "http") and {
  245. -- request
  246. path = {server.http_path} or {"/"},
  247. headers = {Host = {server.http_host} or {}}
  248. } or nil
  249. }
  250. } or nil,
  251. kcpSettings = (server.transport == "kcp") and {
  252. -- kcp
  253. mtu = tonumber(server.mtu),
  254. tti = tonumber(server.tti),
  255. uplinkCapacity = tonumber(server.uplink_capacity),
  256. downlinkCapacity = tonumber(server.downlink_capacity),
  257. congestion = (server.congestion == "1") and true or false,
  258. readBufferSize = tonumber(server.read_buffer_size),
  259. writeBufferSize = tonumber(server.write_buffer_size),
  260. header = {type = server.kcp_guise},
  261. seed = server.seed or nil
  262. } or nil,
  263. wsSettings = (server.transport == "ws") and (server.ws_path or server.ws_host or server.tls_host) and {
  264. -- ws
  265. host = server.ws_host or server.tls_host or nil,
  266. path = server.ws_path or "/",
  267. maxEarlyData = tonumber(server.ws_ed) or nil,
  268. earlyDataHeaderName = server.ws_ed_header or nil
  269. } or nil,
  270. httpupgradeSettings = (server.transport == "httpupgrade") and {
  271. -- httpupgrade
  272. host = (server.httpupgrade_host or server.tls_host) or nil,
  273. path = server.httpupgrade_path or ""
  274. } or nil,
  275. xhttpSettings = (server.transport == "xhttp" or server.transport == "splithttp") and {
  276. -- xhttp
  277. mode = server.xhttp_mode or "auto",
  278. host = (server.xhttp_host or server.tls_host) or nil,
  279. path = server.xhttp_path or "/",
  280. extra = (server.enable_xhttp_extra == "1" and server.xhttp_extra) and (function()
  281. local success, parsed = pcall(json.parse, server.xhttp_extra)
  282. if success then
  283. return parsed.extra or parsed
  284. else
  285. return nil
  286. end
  287. end)() or nil
  288. } or nil,
  289. httpSettings = (server.transport == "h2") and {
  290. -- h2
  291. path = server.h2_path or "",
  292. host = {server.h2_host} or nil,
  293. read_idle_timeout = tonumber(server.read_idle_timeout) or nil,
  294. health_check_timeout = tonumber(server.health_check_timeout) or nil
  295. } or nil,
  296. quicSettings = (server.transport == "quic") and {
  297. -- quic
  298. security = server.quic_security,
  299. key = server.quic_key,
  300. header = {type = server.quic_guise}
  301. } or nil,
  302. grpcSettings = (server.transport == "grpc") and {
  303. -- grpc
  304. serviceName = server.serviceName or "",
  305. multiMode = (server.grpc_mode == "multi") and true or false,
  306. idle_timeout = tonumber(server.idle_timeout) or nil,
  307. health_check_timeout = tonumber(server.health_check_timeout) or nil,
  308. permit_without_stream = (server.permit_without_stream == "1") and true or nil,
  309. initial_windows_size = tonumber(server.initial_windows_size) or nil
  310. } or nil,
  311. sockopt = {
  312. mark = 250,
  313. tcpFastOpen = ((server.transport == "xhttp" and server.tcpfastopen == "1") and true or false) or (server.transport ~= "xhttp") and nil, -- XHTTP Tcp Fast Open
  314. tcpMptcp = (server.mptcp == "1") and true or nil, -- MPTCP
  315. Penetrate = (server.mptcp == "1") and true or nil, -- Penetrate MPTCP
  316. tcpcongestion = server.custom_tcpcongestion, -- 连接服务器节点的 TCP 拥塞控制算法
  317. dialerProxy = (xray_fragment.fragment == "1" or xray_fragment.noise == "1") and
  318. ((remarks ~= nil and remarks ~= "") and (node_id .. "." .. remarks) or node_id) or nil
  319. }
  320. } or nil,
  321. mux = (server.v2ray_protocol ~= "wireguard") and {
  322. -- mux
  323. enabled = (server.mux == "1"), -- Mux
  324. concurrency = (server.mux == "1" and (tonumber(server.concurrency) or -1)) or nil, -- TCP 最大并发
  325. xudpConcurrency = (server.mux == "1" and (tonumber(server.xudpConcurrency) or 16)) or nil, -- UDP 最大并发
  326. xudpProxyUDP443 = (server.mux == "1" and (server.xudpProxyUDP443 or "reject")) or nil -- 对被代理的 UDP/443 流量处理方式
  327. } or nil
  328. }
  329. }
  330. -- 添加带有 fragment 设置的 dialerproxy 配置
  331. if xray_fragment.fragment ~= "0" or (xray_fragment.noise ~= "0" and xray_noise.enabled ~= "0") then
  332. table.insert(Xray.outbounds, {
  333. protocol = "freedom",
  334. tag = (remarks ~= nil and remarks ~= "") and (node_id .. "." .. remarks) or node_id,
  335. settings = {
  336. domainStrategy = (xray_fragment.noise == "1" and xray_noise.enabled == "1") and xray_noise.domainStrategy,
  337. fragment = (xray_fragment.fragment == "1") and {
  338. packets = (xray_fragment.fragment_packets ~= "") and xray_fragment.fragment_packets or nil,
  339. length = (xray_fragment.fragment_length ~= "") and xray_fragment.fragment_length or nil,
  340. interval = (xray_fragment.fragment_interval ~= "") and xray_fragment.fragment_interval or nil,
  341. maxSplit = (xray_fragment.fragment_maxsplit ~= "") and xray_fragment.fragment_maxsplit or nil
  342. } or nil,
  343. noises = (xray_fragment.noise == "1" and xray_noise.enabled == "1") and {
  344. {
  345. type = xray_noise.type,
  346. packet = xray_noise.packet,
  347. delay = xray_noise.delay:find("-") and xray_noise.delay or tonumber(xray_noise.delay),
  348. applyTo = xray_noise.applyto
  349. }
  350. } or nil
  351. },
  352. streamSettings = {
  353. sockopt = {
  354. mark = 250,
  355. tcpFastOpen = ((server.transport == "xhttp" and server.tcpfastopen == "1") and true or false) or (server.transport ~= "xhttp") and nil, -- XHTTP Tcp Fast Open
  356. tcpMptcp = (server.mptcp == "1") and true or nil, -- MPTCP
  357. Penetrate = (server.mptcp == "1") and true or nil, -- Penetrate MPTCP
  358. tcpcongestion = server.custom_tcpcongestion -- 连接服务器节点的 TCP 拥塞控制算法
  359. }
  360. }
  361. })
  362. end
  363. 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"
  364. local cipher13 = "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384"
  365. local trojan = {
  366. log_level = 3,
  367. run_type = (proto == "nat" or proto == "tcp") and "nat" or "client",
  368. local_addr = "0.0.0.0",
  369. local_port = tonumber(local_port),
  370. remote_addr = server.server,
  371. remote_port = tonumber(server.server_port),
  372. udp_timeout = 60,
  373. -- 传入连接
  374. password = {server.password},
  375. -- 传出连接
  376. ssl = {
  377. verify = (server.insecure == "0") and true or false,
  378. verify_hostname = (server.tls == "1") and true or false,
  379. cert = (server.certificate) and server.certpath or nil,
  380. cipher = cipher,
  381. cipher_tls13 = cipher13,
  382. sni = server.tls_host,
  383. alpn = (server.tls == "1") and (function()
  384. local alpn = {}
  385. if server.tls_alpn and server.tls_alpn ~= "" then
  386. string.gsub(server.tls_alpn, '[^,]+', function(w)
  387. table.insert(alpn, w)
  388. end)
  389. end
  390. if #alpn > 0 then
  391. return alpn
  392. else
  393. return nil
  394. end
  395. end)() or {"h2", "http/1.1"},
  396. curve = "",
  397. reuse_session = true,
  398. session_ticket = (server.tls_sessionTicket == "1") and true or false
  399. },
  400. tcp = {
  401. -- tcp
  402. no_delay = true,
  403. keep_alive = true,
  404. reuse_port = true,
  405. fast_open = (server.fast_open == "1") and true or false,
  406. fast_open_qlen = 20
  407. }
  408. }
  409. local naiveproxy = {
  410. proxy = (server.username and server.password and server.server and server.server_port) and "https://" .. server.username .. ":" .. server.password .. "@" .. server.server .. ":" .. server.server_port,
  411. listen = (proto == "redir") and "redir" .. "://0.0.0.0:" .. tonumber(local_port) or "socks" .. "://0.0.0.0:" .. tonumber(local_port),
  412. ["insecure-concurrency"] = tonumber(server.concurrency) or 1
  413. }
  414. local ss = {
  415. server = (server.kcp_enable == "1") and "127.0.0.1" or server.server,
  416. server_port = tonumber(server.server_port),
  417. local_address = "0.0.0.0",
  418. local_port = tonumber(local_port),
  419. mode = (proto == "tcp,udp") and "tcp_and_udp" or (proto .. "_only"),
  420. password = server.password,
  421. method = server.encrypt_method_ss,
  422. timeout = tonumber(server.timeout),
  423. fast_open = (server.fast_open == "1") and true or false,
  424. reuse_port = true
  425. }
  426. local hysteria2 = {
  427. server = (
  428. server.server_port and
  429. (
  430. server.port_range and
  431. (server.server .. ":" .. server.server_port .. "," .. string.gsub(server.port_range, ":", "-"))
  432. or
  433. (server.server .. ":" .. server.server_port)
  434. )
  435. or
  436. (
  437. server.port_range and
  438. server.server .. ":" .. string.gsub(server.port_range, ":", "-")
  439. or
  440. server.server .. ":443"
  441. )
  442. ),
  443. bandwidth = (server.uplink_capacity or server.downlink_capacity) and {
  444. up = tonumber(server.uplink_capacity) and tonumber(server.uplink_capacity) .. " mbps" or nil,
  445. down = tonumber(server.downlink_capacity) and tonumber(server.downlink_capacity) .. " mbps" or nil
  446. } or nil,
  447. socks5 = (proto:find("tcp") and tonumber(socks_port) and tonumber(socks_port) ~= 0) and {
  448. listen = "0.0.0.0:" .. tonumber(socks_port),
  449. disableUDP = false
  450. } or nil,
  451. transport = server.transport_protocol and {
  452. type = server.transport_protocol or "udp",
  453. udp = (server.port_range and (server.hopinterval) and {
  454. hopInterval = (server.port_range and (tonumber(server.hopinterval) .. "s") or nil)
  455. } or nil)
  456. } or nil,
  457. --[[
  458. tcpTProxy = (proto:find("tcp") and local_port ~= "0") and {
  459. listen = "0.0.0.0:" .. tonumber(local_port)
  460. } or nil,
  461. ]]--
  462. tcpRedirect = (proto:find("tcp") and local_port ~= "0") and {
  463. listen = "0.0.0.0:" .. tonumber(local_port)
  464. } or nil,
  465. udpTProxy = (proto:find("udp") and local_port ~= "0") and {
  466. listen = "0.0.0.0:" .. tonumber(local_port)
  467. } or nil,
  468. obfs = (server.flag_obfs == "1") and {
  469. type = server.obfs_type,
  470. salamander = { password = server.salamander }
  471. } or nil,
  472. quic = (server.flag_quicparam == "1" ) and {
  473. initStreamReceiveWindow = (server.initstreamreceivewindow and server.initstreamreceivewindow or nil),
  474. maxStreamReceiveWindow = (server.maxstreamreceivewindow and server.maxstreamreceivewindow or nil),
  475. initConnReceiveWindow = (server.initconnreceivewindow and server.initconnreceivewindow or nil),
  476. maxConnReceiveWindow = (server.maxconnreceivewindow and server.maxconnreceivewindow or nil),
  477. maxIdleTimeout = (tonumber(server.maxidletimeout) and tonumber(server.maxidletimeout) .. "s" or nil),
  478. keepAlivePeriod = (tonumber(server.keepaliveperiod) and tonumber(server.keepaliveperiod) .. "s" or nil),
  479. disablePathMTUDiscovery = (server.disablepathmtudiscovery == "1") and true or false
  480. } or nil,
  481. auth = server.hy2_auth,
  482. tls = (server.tls_host and server.tls_host ~= "") and {
  483. sni = server.tls_host,
  484. alpn = (server.tls_alpn and server.tls_alpn ~= "") and (function()
  485. local alpn = {}
  486. string.gsub(server.tls_alpn, '[^,]+', function(w)
  487. table.insert(alpn, w)
  488. end)
  489. if #alpn > 0 then
  490. return alpn
  491. else
  492. return nil
  493. end
  494. end)() or nil,
  495. --sni = server.tls_host or (server.tls_host and server.tls_alpn) or nil,
  496. insecure = (server.insecure == "1") and true or false,
  497. pinSHA256 = server.pinsha256 or nil
  498. } or {
  499. sni = server.server,
  500. alpn = (server.type == "hysteria2") and (function()
  501. local alpn = {}
  502. if server.tls_alpn and server.tls_alpn ~= "" then
  503. string.gsub(server.tls_alpn, '[^,]+', function(w)
  504. table.insert(alpn, w)
  505. end)
  506. end
  507. if #alpn > 0 then
  508. return alpn
  509. else
  510. return nil
  511. end
  512. end)() or nil,
  513. insecure = (server.insecure == "1") and true or false,
  514. pinSHA256 = server.pinsha256 or nil
  515. },
  516. fast_open = (server.fast_open == "1") and true or false,
  517. lazy = (server.lazy_mode == "1") and true or false
  518. }
  519. local shadowtls = {
  520. client = {
  521. server_addr = server.server_port and server.server .. ":" .. server.server_port or nil,
  522. listen = "127.0.0.1:" .. tonumber(local_port),
  523. tls_names = server.shadowtls_sni,
  524. password = server.password
  525. },
  526. v3 = (server.shadowtls_protocol == "v3") and true or false,
  527. disable_nodelay = (server.disable_nodelay == "1") and true or false,
  528. fastopen = (server.fastopen == "1") and true or false,
  529. strict = (server.strict == "1") and true or false
  530. }
  531. local chain_sslocal = {
  532. locals = local_port ~= "0" and {
  533. {
  534. local_address = "0.0.0.0",
  535. local_port = (chain_local_port == "0" and tonumber(server.local_port) or tonumber(chain_local_port)),
  536. mode = (proto:find("tcp,udp") and "tcp_and_udp") or proto .. "_only",
  537. protocol = "redir",
  538. tcp_redir = "redirect",
  539. --tcp_redir = "tproxy",
  540. udp_redir = "tproxy"
  541. },
  542. socks_port ~= "0" and {
  543. protocol = "socks",
  544. local_address = "0.0.0.0",
  545. local_port = tonumber(socks_port)
  546. } or nil
  547. } or {{
  548. protocol = "socks",
  549. local_address = "0.0.0.0",
  550. local_port = tonumber(socks_port)
  551. }},
  552. servers = {
  553. {
  554. server = "127.0.0.1",
  555. server_port = (tonumber(local_port) == 0 and tonumber(chain_local_port) or tonumber(local_port)),
  556. method = server.sslocal_method,
  557. password = server.sslocal_password
  558. }
  559. }
  560. }
  561. local chain_vmess = {
  562. inbounds = (local_port ~= "0") and {
  563. {
  564. port = (chain_local_port == "0" and tonumber(server.local_port) or tonumber(chain_local_port)),
  565. protocol = "dokodemo-door",
  566. settings = {
  567. network = proto,
  568. followRedirect = true
  569. },
  570. streamSettings = {
  571. sockopt = {tproxy = "redirect"}
  572. },
  573. sniffing = {
  574. enable = true,
  575. destOverride = {"http","tls"}
  576. }
  577. },
  578. (proto:find("tcp") and socks_port ~= "0") and {
  579. protocol = "socks",
  580. port = tonumber(socks_port)
  581. } or nil
  582. } or { protocol = "socks",port = tonumber(socks_port) },
  583. outbound = {
  584. protocol = "vmess",
  585. settings = {
  586. vnext = {{
  587. address = "127.0.0.1",
  588. port = (tonumber(local_port) == 0 and tonumber(chain_local_port) or tonumber(local_port)),
  589. users = {{
  590. id = (server.vmess_uuid),
  591. security = server.vmess_method,
  592. level = 0
  593. }}
  594. }}
  595. }
  596. }
  597. }
  598. local tuic = {
  599. relay = {
  600. server = server.server_port and server.server .. ":" .. server.server_port,
  601. ip = server.tuic_ip,
  602. uuid = server.tuic_uuid,
  603. password = server.tuic_passwd,
  604. certificates = server.certificate and { server.certpath } or nil,
  605. udp_relay_mode = server.udp_relay_mode,
  606. congestion_control = server.congestion_control,
  607. heartbeat = server.heartbeat and server.heartbeat .. "s" or nil,
  608. timeout = server.timeout and server.timeout .. "s" or nil,
  609. gc_interval = server.gc_interval and server.gc_interval .. "s" or nil,
  610. gc_lifetime = server.gc_lifetime and server.gc_lifetime .. "s" or nil,
  611. alpn = (server.tuic_alpn and server.tuic_alpn ~= "") and (function()
  612. local alpn = {}
  613. string.gsub(server.tuic_alpn, '[^,]+', function(w)
  614. table.insert(alpn, w)
  615. end)
  616. if #alpn > 0 then
  617. return alpn
  618. else
  619. return nil
  620. end
  621. end)() or nil,
  622. disable_sni = (server.disable_sni == "1") and true or false,
  623. zero_rtt_handshake = (server.zero_rtt_handshake == "1") and true or false,
  624. send_window = tonumber(server.send_window),
  625. receive_window = tonumber(server.receive_window)
  626. },
  627. ["local"] = {
  628. server = tonumber(socks_port) and "[::]:" .. (socks_port == "0" and local_port or tonumber(socks_port)),
  629. dual_stack = (server.tuic_dual_stack == "1") and true or nil,
  630. max_packet_size = tonumber(server.tuic_max_package_size)
  631. }
  632. }
  633. local config = {}
  634. function config:new(o)
  635. o = o or {}
  636. setmetatable(o, self)
  637. self.__index = self
  638. return o
  639. end
  640. function config:handleIndex(index)
  641. local switch = {
  642. ss = function()
  643. ss.protocol = socks_port
  644. if server.enable_plugin == "1" and server.plugin and server.plugin ~= "none" then
  645. if server.plugin == "custom" then
  646. ss.plugin = server.custom_plugin
  647. else
  648. ss.plugin = server.plugin
  649. end
  650. ss.plugin_opts = server.plugin_opts or nil
  651. end
  652. print(json.stringify(ss, 1))
  653. end,
  654. ssr = function()
  655. ss.protocol = server.protocol
  656. ss.protocol_param = server.protocol_param
  657. ss.method = server.encrypt_method
  658. ss.obfs = server.obfs
  659. ss.obfs_param = server.obfs_param
  660. print(json.stringify(ss, 1))
  661. end,
  662. v2ray = function()
  663. print(json.stringify(Xray, 1))
  664. end,
  665. trojan = function()
  666. print(json.stringify(trojan, 1))
  667. end,
  668. naiveproxy = function()
  669. print(json.stringify(naiveproxy, 1))
  670. end,
  671. hysteria2 = function()
  672. print(json.stringify(hysteria2, 1))
  673. end,
  674. shadowtls = function()
  675. local chain_switch = {
  676. sslocal = function()
  677. if (chain:find("chain")) then
  678. print(json.stringify(chain_sslocal, 1))
  679. else
  680. print(json.stringify(shadowtls, 1))
  681. end
  682. end,
  683. vmess = function()
  684. if (chain:find("chain")) then
  685. print(json.stringify(chain_vmess, 1))
  686. else
  687. print(json.stringify(shadowtls, 1))
  688. end
  689. end
  690. }
  691. local ChainType = server.chain_type
  692. if chain_switch[ChainType] then
  693. chain_switch[ChainType]()
  694. end
  695. end,
  696. tuic = function()
  697. print(json.stringify(tuic, 1))
  698. end
  699. }
  700. if switch[index] then
  701. switch[index]()
  702. end
  703. end
  704. local f = config:new()
  705. f:handleIndex(server.type)