gen_config.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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"}}
  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. show = false,
  155. publicKey = server.reality_publickey,
  156. shortId = server.reality_shortid,
  157. spiderX = server.reality_spiderx,
  158. fingerprint = server.fingerprint,
  159. serverName = server.tls_host
  160. } or nil,
  161. tcpSettings = (server.transport == "tcp" and server.tcp_guise == "http") and {
  162. -- tcp
  163. header = {
  164. type = server.tcp_guise,
  165. request = {
  166. -- request
  167. path = {server.http_path} or {"/"},
  168. headers = {Host = {server.http_host} or {}}
  169. }
  170. }
  171. } or nil,
  172. kcpSettings = (server.transport == "kcp") and {
  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. },
  216. mux = (server.mux == "1" and server.transport ~= "grpc") and {
  217. -- mux
  218. enabled = true,
  219. concurrency = tonumber(server.concurrency)
  220. } or nil
  221. } or nil
  222. }
  223. 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"
  224. local cipher13 = "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384"
  225. local trojan = {
  226. log_level = 3,
  227. run_type = (proto == "nat" or proto == "tcp") and "nat" or "client",
  228. local_addr = "0.0.0.0",
  229. local_port = tonumber(local_port),
  230. remote_addr = server.server,
  231. remote_port = tonumber(server.server_port),
  232. udp_timeout = 60,
  233. -- 传入连接
  234. password = {server.password},
  235. -- 传出连接
  236. ssl = {
  237. verify = (server.insecure == "0") and true or false,
  238. verify_hostname = (server.tls == "1") and true or false,
  239. cert = (server.certificate) and server.certpath or nil,
  240. cipher = cipher,
  241. cipher_tls13 = cipher13,
  242. sni = server.tls_host,
  243. alpn = server.tls_alpn or {"h2", "http/1.1"},
  244. curve = "",
  245. reuse_session = true,
  246. session_ticket = (server.tls_sessionTicket == "1") and true or false
  247. },
  248. udp_timeout = 60,
  249. tcp = {
  250. -- tcp
  251. no_delay = true,
  252. keep_alive = true,
  253. reuse_port = true,
  254. fast_open = (server.fast_open == "1") and true or false,
  255. fast_open_qlen = 20
  256. }
  257. }
  258. local naiveproxy = {
  259. proxy = (server.username and server.password and server.server and server.server_port) and "https://" .. server.username .. ":" .. server.password .. "@" .. server.server .. ":" .. server.server_port,
  260. listen = (proto == "redir") and "redir" .. "://0.0.0.0:" .. tonumber(local_port) or "socks" .. "://0.0.0.0:" .. tonumber(local_port),
  261. ["insecure-concurrency"] = tonumber(server.concurrency) or 1
  262. }
  263. local ss = {
  264. server = (server.kcp_enable == "1") and "127.0.0.1" or server.server,
  265. server_port = tonumber(server.server_port),
  266. local_address = "0.0.0.0",
  267. local_port = tonumber(local_port),
  268. mode = (proto == "tcp,udp") and "tcp_and_udp" or proto .. "_only",
  269. password = server.password,
  270. method = server.encrypt_method_ss,
  271. timeout = tonumber(server.timeout),
  272. fast_open = (server.fast_open == "1") and true or false,
  273. reuse_port = true
  274. }
  275. local hysteria = {
  276. server = server.server_port and (server.server .. ":" .. server.server_port) or (server.server .. ":" .. server.port_range),
  277. bandwidth = {
  278. up = tonumber(server.uplink_capacity) and tonumber(server.uplink_capacity) .. " mbps" or nil,
  279. down = tonumber(server.downlink_capacity) and tonumber(server.downlink_capacity) .. " mbps" or nil
  280. },
  281. socks5 = (proto:find("tcp") and tonumber(socks_port) and tonumber(socks_port) ~= 0) and {
  282. listen = "0.0.0.0:" .. tonumber(socks_port),
  283. disable_udp = false
  284. } or nil,
  285. transport = {
  286. type = server.transport_protocol,
  287. udp = {
  288. hopInterval = tonumber(server.hopinterval) and tonumber(server.hopinterval) .. "s" or nil
  289. }
  290. },
  291. --[[
  292. tcpTProxy = (proto:find("tcp") and local_port ~= "0") and {
  293. listen = "0.0.0.0:" .. tonumber(local_port)
  294. } or nil,
  295. ]]
  296. tcpRedirect = (proto:find("tcp") and local_port ~= "0") and {
  297. listen = "0.0.0.0:" .. tonumber(local_port)
  298. } or nil,
  299. udpTProxy = (proto:find("udp") and local_port ~= "0") and {
  300. listen = "0.0.0.0:" .. tonumber(local_port)
  301. } or nil,
  302. obfs = (server.flag_obfs == "1") and {
  303. type = server.obfs_type,
  304. salamander = { password = server.salamander }
  305. } or nil,
  306. quic = (server.flag_quicparam == "1" ) and {
  307. initStreamReceiveWindow = (server.initstreamreceivewindow and server.initstreamreceivewindow or nil),
  308. maxStreamReceiveWindow = (server.maxstreamseceivewindow and server.maxstreamseceivewindow or nil),
  309. initConnReceiveWindow = (server.initconnreceivewindow and server.initconnreceivewindow or nil),
  310. maxConnReceiveWindow = (server.maxconnreceivewindow and server.maxconnreceivewindow or nil),
  311. maxIdleTimeout = (tonumber(server.maxidletimeout) and tonumber(server.maxidletimeout) .. "s" or nil),
  312. keepAlivePeriod = (tonumber(server.keepaliveperiod) and tonumber(server.keepaliveperiod) .. "s" or nil),
  313. disable_mtu_discovery = (server.disablepathmtudiscovery == "1") and true or false
  314. } or nil,
  315. auth = server.hy2_auth,
  316. tls = (server.tls_host) and {
  317. sni = server.tls_host,
  318. insecure = (server.insecure == "1") and true or false,
  319. pinSHA256 = (server.insecure == "1") and server.pinsha256 or nil
  320. } or {
  321. sni = server.server,
  322. insecure = (server.insecure == "1") and true or false
  323. },
  324. fast_open = (server.fast_open == "1") and true or false,
  325. lazy = (server.lazy_mode == "1") and true or false
  326. }
  327. local shadowtls = {
  328. client = {
  329. server_addr = server.server_port and server.server .. ":" .. server.server_port or nil,
  330. listen = "127.0.0.1:" .. tonumber(local_port),
  331. tls_names = server.shadowtls_sni,
  332. password = server.password
  333. },
  334. v3 = (server.shadowtls_protocol == "v3") and true or false,
  335. disable_nodelay = (server.disable_nodelay == "1") and true or false,
  336. fastopen = (server.fastopen == "1") and true or false,
  337. strict = (server.strict == "1") and true or false
  338. }
  339. local chain_sslocal = {
  340. locals = local_port ~= "0" and {
  341. {
  342. local_address = "0.0.0.0",
  343. local_port = (chain_local_port == "0" and tonumber(server.local_port) or tonumber(chain_local_port)),
  344. mode = (proto:find("tcp,udp") and "tcp_and_udp") or proto .. "_only",
  345. protocol = "redir",
  346. tcp_redir = "redirect",
  347. --tcp_redir = "tproxy",
  348. udp_redir = "tproxy"
  349. },
  350. socks_port ~= "0" and {
  351. protocol = "socks",
  352. local_address = "0.0.0.0",
  353. local_port = tonumber(socks_port)
  354. } or nil
  355. } or {{
  356. protocol = "socks",
  357. local_address = "0.0.0.0",
  358. ocal_port = tonumber(socks_port)
  359. }},
  360. servers = {
  361. {
  362. server = "127.0.0.1",
  363. server_port = (tonumber(local_port) == 0 and tonumber(chain_local_port) or tonumber(local_port)),
  364. method = server.sslocal_method,
  365. password = server.sslocal_password
  366. }
  367. }
  368. }
  369. local chain_vmess = {
  370. inbounds = (local_port ~= "0") and {
  371. {
  372. port = (chain_local_port == "0" and tonumber(server.local_port) or tonumber(chain_local_port)),
  373. protocol = "dokodemo-door",
  374. settings = {
  375. network = proto,
  376. followRedirect = true
  377. },
  378. streamSettings = {
  379. sockopt = {tproxy = "redirect"}
  380. },
  381. sniffing = {
  382. enable = true,
  383. destOverride = {"http","tls"}
  384. }
  385. },
  386. (proto:find("tcp") and socks_port ~= "0") and {
  387. protocol = "socks",
  388. port = tonumber(socks_port)
  389. } or nil
  390. } or { protocol = "socks",port = tonumber(socks_port) },
  391. outbound = {
  392. protocol = "vmess",
  393. settings = {
  394. vnext = {{
  395. address = "127.0.0.1",
  396. port = (tonumber(local_port) == 0 and tonumber(chain_local_port) or tonumber(local_port)),
  397. users = {{
  398. id = (server.vmess_uuid),
  399. security = server.vmess_method,
  400. level = 0
  401. }}
  402. }}
  403. }
  404. }
  405. }
  406. local tuic = {
  407. relay = {
  408. server = server.server_port and server.server .. ":" .. server.server_port,
  409. ip = server.tuic_ip,
  410. uuid = server.tuic_uuid,
  411. password = server.tuic_passwd,
  412. certificates = server.certificate and { server.certpath } or nil,
  413. udp_relay_mode = server.udp_relay_mode,
  414. congestion_control = server.congestion_control,
  415. heartbeat = server.heartbeat and server.heartbeat .. "s" or nil,
  416. timeout = server.timeout and server.timeout .. "s" or nil,
  417. gc_interval = server.gc_interval and server.gc_interval .. "s" or nil,
  418. gc_lifetime = server.gc_lifetime and server.gc_lifetime .. "s" or nil,
  419. alpn = server.tls_alpn,
  420. disable_sni = (server.disable_sni == "1") and true or false,
  421. zero_rtt_handshake = (server.zero_rtt_handshake == "1") and true or false,
  422. send_window = tonumber(server.send_window),
  423. receive_window = tonumber(server.receive_window)
  424. },
  425. ["local"] = {
  426. 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)),
  427. dual_stack = (server.tuic_dual_stack == "1") and true or false,
  428. max_packet_size = tonumber(server.tuic_max_package_size)
  429. }
  430. }
  431. local config = {}
  432. function config:new(o)
  433. o = o or {}
  434. setmetatable(o, self)
  435. self.__index = self
  436. return o
  437. end
  438. function config:handleIndex(index)
  439. local switch = {
  440. ss = function()
  441. ss.protocol = socks_port
  442. if server.plugin and server.plugin ~= "none" then
  443. ss.plugin = server.plugin
  444. ss.plugin_opts = server.plugin_opts or nil
  445. end
  446. print(json.stringify(ss, 1))
  447. end,
  448. ssr = function()
  449. ss.protocol = server.protocol
  450. ss.protocol_param = server.protocol_param
  451. ss.method = server.encrypt_method
  452. ss.obfs = server.obfs
  453. ss.obfs_param = server.obfs_param
  454. print(json.stringify(ss, 1))
  455. end,
  456. v2ray = function()
  457. print(json.stringify(Xray, 1))
  458. end,
  459. trojan = function()
  460. print(json.stringify(trojan, 1))
  461. end,
  462. naiveproxy = function()
  463. print(json.stringify(naiveproxy, 1))
  464. end,
  465. hysteria = function()
  466. print(json.stringify(hysteria, 1))
  467. end,
  468. shadowtls = function()
  469. local chain_switch = {
  470. sslocal = function()
  471. if (chain:find("chain")) then
  472. print(json.stringify(chain_sslocal, 1))
  473. else
  474. print(json.stringify(shadowtls, 1))
  475. end
  476. end,
  477. vmess = function()
  478. if (chain:find("chain")) then
  479. print(json.stringify(chain_vmess, 1))
  480. else
  481. print(json.stringify(shadowtls, 1))
  482. end
  483. end
  484. }
  485. local ChainType = server.chain_type
  486. if chain_switch[ChainType] then
  487. chain_switch[ChainType]()
  488. end
  489. end,
  490. tuic = function()
  491. print(json.stringify(tuic, 1))
  492. end
  493. }
  494. if switch[index] then
  495. switch[index]()
  496. end
  497. end
  498. local f = config:new()
  499. f:handleIndex(server.type)