gen_config.lua 16 KB

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