gen_config.lua 22 KB

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