gen_config.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 server = ucursor:get_all("shadowsocksr", server_section)
  9. local outbound_settings = nil
  10. function vmess_vless()
  11. outbound_settings = {
  12. vnext = {
  13. {
  14. address = server.server,
  15. port = tonumber(server.server_port),
  16. users = {
  17. {
  18. id = server.vmess_id,
  19. security = (server.v2ray_protocol == "vmess" or not server.v2ray_protocol) and server.security or nil,
  20. encryption = (server.v2ray_protocol == "vless") and server.vless_encryption or nil,
  21. flow = (server.xtls == '1') and (server.vless_flow or "xtls-rprx-splice") or (server.tls == '1') and server.tls_flow or nil
  22. }
  23. }
  24. }
  25. },
  26. packetEncoding = server.packet_encoding or nil
  27. }
  28. end
  29. function trojan_shadowsocks()
  30. outbound_settings = {
  31. plugin = ((server.v2ray_protocol == "shadowsocks") and server.plugin ~= "none" and server.plugin) or (server.v2ray_protocol == "shadowsocksr" and "shadowsocksr") or nil,
  32. pluginOpts = (server.v2ray_protocol == "shadowsocks") and server.plugin_opts or nil,
  33. pluginArgs = (server.v2ray_protocol == "shadowsocksr") and {
  34. "--protocol=" .. server.protocol,
  35. "--protocol-param=" .. (server.protocol_param or ""),
  36. "--obfs=" .. server.obfs,
  37. "--obfs-param=" .. (server.obfs_param or "")
  38. } or nil,
  39. servers = {
  40. {
  41. address = server.server,
  42. port = tonumber(server.server_port),
  43. password = server.password,
  44. method = ((server.v2ray_protocol == "shadowsocks") and server.encrypt_method_ss) or ((server.v2ray_protocol == "shadowsocksr") and server.encrypt_method) or nil,
  45. uot = (server.v2ray_protocol == "shadowsocks") and (server.uot == '1') or nil,
  46. ivCheck = (server.v2ray_protocol == "shadowsocks") and (server.ivCheck == '1') or nil,
  47. flow = (server.v2ray_protocol == "trojan") and (server.xtls == '1') and (server.vless_flow or "xtls-rprx-splice") or nil
  48. }
  49. }
  50. }
  51. if server.v2ray_protocol == "shadowsocksr" then
  52. server.v2ray_protocol = "shadowsocks"
  53. end
  54. end
  55. function socks_http()
  56. outbound_settings = {
  57. version = server.socks_ver or nil,
  58. servers = {
  59. {
  60. address = server.server,
  61. port = tonumber(server.server_port),
  62. users = (server.auth_enable == "1") and {
  63. {
  64. user = server.username,
  65. pass = server.password
  66. }
  67. } or nil
  68. }
  69. }
  70. }
  71. end
  72. function wireguard()
  73. outbound_settings = {
  74. secretKey = server.private_key,
  75. address = server.local_addresses,
  76. peers = {
  77. {
  78. publicKey = server.peer_pubkey,
  79. preSharedKey = server.preshared_key,
  80. endpoint = server.server .. ":" .. server.server_port
  81. }
  82. },
  83. mtu = tonumber(server.mtu)
  84. }
  85. end
  86. local outbound = {}
  87. function outbound:new(o)
  88. o = o or {}
  89. setmetatable(o, self)
  90. self.__index = self
  91. return o
  92. end
  93. function outbound:handleIndex(index)
  94. local switch = {
  95. vmess = function()
  96. vmess_vless()
  97. end,
  98. vless = function()
  99. vmess_vless()
  100. end,
  101. trojan = function()
  102. trojan_shadowsocks()
  103. end,
  104. shadowsocks = function()
  105. trojan_shadowsocks()
  106. end,
  107. shadowsocksr = function()
  108. trojan_shadowsocks()
  109. end,
  110. socks = function()
  111. socks_http()
  112. end,
  113. http = function()
  114. socks_http()
  115. end,
  116. wireguard = function()
  117. wireguard()
  118. end
  119. }
  120. if switch[index] then
  121. switch[index]()
  122. end
  123. end
  124. local settings = outbound:new()
  125. settings:handleIndex(server.v2ray_protocol)
  126. local Xray = {
  127. log = {
  128. -- error = "/var/ssrplus.log",
  129. loglevel = "warning"
  130. },
  131. -- 传入连接
  132. inbound = (local_port ~= "0") and {
  133. -- listening
  134. port = tonumber(local_port),
  135. protocol = "dokodemo-door",
  136. settings = {network = proto, followRedirect = true},
  137. sniffing = {enabled = true, destOverride = {"http", "tls"}}
  138. } or nil,
  139. -- 开启 socks 代理
  140. inboundDetour = (proto:find("tcp") and socks_port ~= "0") and {
  141. {
  142. -- socks
  143. protocol = "socks",
  144. port = tonumber(socks_port),
  145. settings = {auth = "noauth", udp = true}
  146. }
  147. } or nil,
  148. -- 传出连接
  149. outbound = {
  150. protocol = server.v2ray_protocol,
  151. settings = outbound_settings,
  152. -- 底层传输配置
  153. streamSettings = {
  154. network = server.transport or "tcp",
  155. security = (server.xtls == '1') and "xtls" or (server.tls == '1') and "tls" or nil,
  156. tlsSettings = (server.tls == '1' and (server.insecure == "1" or server.tls_host or server.fingerprint)) and {
  157. -- tls
  158. alpn = server.tls_alpn,
  159. fingerprint = server.fingerprint,
  160. allowInsecure = (server.insecure == "1") and true or nil,
  161. serverName = server.tls_host,
  162. certificates = server.certificate and {
  163. usage = "verify",
  164. certificateFile = server.certpath
  165. } or nil
  166. } or nil,
  167. xtlsSettings = (server.xtls == '1' and (server.insecure == "1" or server.tls_host or server.fingerprint)) and {
  168. -- xtls
  169. alpn = server.tls_alpn,
  170. fingerprint = server.fingerprint,
  171. allowInsecure = (server.insecure == "1") and true or nil,
  172. serverName = server.tls_host,
  173. minVersion = "1.3",
  174. certificates = server.certificate and {
  175. usage = "verify",
  176. certificateFile = server.certpath
  177. } or nil
  178. } or nil,
  179. tcpSettings = (server.transport == "tcp" and server.tcp_guise == "http") and {
  180. -- tcp
  181. header = {
  182. type = server.tcp_guise,
  183. request = {
  184. -- request
  185. path = {server.http_path} or {"/"},
  186. headers = {Host = {server.http_host} or {}}
  187. }
  188. }
  189. } or nil,
  190. kcpSettings = (server.transport == "kcp") and {
  191. mtu = tonumber(server.mtu),
  192. tti = tonumber(server.tti),
  193. uplinkCapacity = tonumber(server.uplink_capacity),
  194. downlinkCapacity = tonumber(server.downlink_capacity),
  195. congestion = (server.congestion == "1") and true or false,
  196. readBufferSize = tonumber(server.read_buffer_size),
  197. writeBufferSize = tonumber(server.write_buffer_size),
  198. header = {type = server.kcp_guise},
  199. seed = server.seed or nil
  200. } or nil,
  201. wsSettings = (server.transport == "ws") and (server.ws_path or server.ws_host or server.tls_host) and {
  202. -- ws
  203. headers = (server.ws_host or server.tls_host) and {
  204. -- headers
  205. Host = server.ws_host or server.tls_host
  206. } or nil,
  207. path = server.ws_path,
  208. maxEarlyData = tonumber(server.ws_ed) or nil,
  209. earlyDataHeaderName = server.ws_ed_header or nil
  210. } or nil,
  211. httpSettings = (server.transport == "h2") and {
  212. -- h2
  213. path = server.h2_path or "",
  214. host = {server.h2_host} or nil,
  215. read_idle_timeout = tonumber(server.read_idle_timeout) or nil,
  216. health_check_timeout = tonumber(server.health_check_timeout) or nil
  217. } or nil,
  218. quicSettings = (server.transport == "quic") and {
  219. -- quic
  220. security = server.quic_security,
  221. key = server.quic_key,
  222. header = {type = server.quic_guise}
  223. } or nil,
  224. grpcSettings = (server.transport == "grpc") and {
  225. -- grpc
  226. serviceName = server.serviceName or "",
  227. mode = (server.grpc_mode ~= "gun") and server.grpc_mode or nil,
  228. multiMode = (server.grpc_mode == "multi") and true or false,
  229. idle_timeout = tonumber(server.idle_timeout) or nil,
  230. health_check_timeout = tonumber(server.health_check_timeout) or nil,
  231. permit_without_stream = (server.permit_without_stream == "1") and true or nil,
  232. initial_windows_size = tonumber(server.initial_windows_size) or nil
  233. } or nil
  234. },
  235. mux = (server.mux == "1" and server.xtls ~= "1" and server.transport ~= "grpc") and {
  236. -- mux
  237. enabled = true,
  238. concurrency = tonumber(server.concurrency),
  239. packetEncoding = (server.v2ray_protocol == "vmess" or server.v2ray_protocol == "vless") and server.packet_encoding or nil
  240. } or nil
  241. } or nil
  242. }
  243. 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"
  244. local cipher13 = "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384"
  245. local trojan = {
  246. log_level = 3,
  247. run_type = (proto == "nat" or proto == "tcp") and "nat" or "client",
  248. local_addr = "0.0.0.0",
  249. local_port = tonumber(local_port),
  250. remote_addr = server.server,
  251. remote_port = tonumber(server.server_port),
  252. udp_timeout = 60,
  253. -- 传入连接
  254. password = {server.password},
  255. -- 传出连接
  256. ssl = {
  257. verify = (server.insecure == "0") and true or false,
  258. verify_hostname = (server.tls == "1") and true or false,
  259. cert = (server.certificate) and server.certpath or nil,
  260. cipher = cipher,
  261. cipher_tls13 = cipher13,
  262. sni = server.tls_host,
  263. alpn = server.tls_alpn or {"h2", "http/1.1"},
  264. curve = "",
  265. reuse_session = true,
  266. session_ticket = (server.tls_sessionTicket == "1") and true or false
  267. },
  268. udp_timeout = 60,
  269. tcp = {
  270. -- tcp
  271. no_delay = true,
  272. keep_alive = true,
  273. reuse_port = true,
  274. fast_open = (server.fast_open == "1") and true or false,
  275. fast_open_qlen = 20
  276. }
  277. }
  278. local naiveproxy = {
  279. proxy = (server.username and server.password and server.server and server.server_port) and "https://" .. server.username .. ":" .. server.password .. "@" .. server.server .. ":" .. server.server_port,
  280. listen = (proto == "redir") and "redir" .. "://0.0.0.0:" .. tonumber(local_port) or "socks" .. "://0.0.0.0:" .. tonumber(local_port),
  281. ["insecure-concurrency"] = tonumber(server.concurrency) or 1
  282. }
  283. local ss = {
  284. server = (server.kcp_enable == "1") and "127.0.0.1" or server.server,
  285. server_port = tonumber(server.server_port),
  286. local_address = "0.0.0.0",
  287. local_port = tonumber(local_port),
  288. mode = (proto == "tcp,udp") and "tcp_and_udp" or proto .. "_only",
  289. password = server.password,
  290. method = server.encrypt_method_ss,
  291. timeout = tonumber(server.timeout),
  292. fast_open = (server.fast_open == "1") and true or false,
  293. reuse_port = true
  294. }
  295. local hysteria = {
  296. server = server.server .. ":" .. server.server_port,
  297. protocol = server.hysteria_protocol,
  298. up_mbps = tonumber(server.uplink_capacity),
  299. down_mbps = tonumber(server.downlink_capacity),
  300. socks5 = (proto:find("tcp") and tonumber(socks_port) and tonumber(socks_port) ~= 0) and {
  301. listen = "0.0.0.0:" .. tonumber(socks_port),
  302. timeout = 300,
  303. disable_udp = false
  304. } or nil,
  305. redirect_tcp = (proto:find("tcp") and local_port ~= "0") and {
  306. listen = "0.0.0.0:" .. tonumber(local_port),
  307. timeout = 300
  308. } or nil,
  309. tproxy_udp = (proto:find("udp") and local_port ~= "0") and {
  310. listen = "0.0.0.0:" .. tonumber(local_port),
  311. timeout = 60
  312. } or nil,
  313. obfs = server.seed,
  314. auth = (server.auth_type == "1") and server.auth_payload or nil,
  315. auth_str = (server.auth_type == "2") and server.auth_payload or nil,
  316. alpn = server.quic_tls_alpn,
  317. server_name = server.tls_host,
  318. insecure = (server.insecure == "1") and true or false,
  319. ca = (server.certificate) and server.certpath or nil,
  320. recv_window_conn = tonumber(server.recv_window_conn),
  321. recv_window = tonumber(server.recv_window),
  322. disable_mtu_discovery = (server.disable_mtu_discovery == "1") and true or false,
  323. fast_open = (server.fast_open == "1") and true or false
  324. }
  325. local tuic = {
  326. relay = {
  327. server = server.server,
  328. port = tonumber(server.server_port),
  329. token = server.password,
  330. certificates = server.certificate and { server.certpath } or nil,
  331. udp_relay_mode = server.udp_relay_mode,
  332. congestion_controller = server.congestion_controller,
  333. heartbeat_interval = tonumber(server.heartbeat_interval),
  334. alpn = server.tls_alpn,
  335. disable_sni = (server.disable_sni == "1"),
  336. reduce_rtt = (server.reduce_rtt == "1"),
  337. max_udp_relay_packet_size = tonumber(server.max_udp_relay_packet_size)
  338. },
  339. ["local"] = {
  340. port = tonumber(local_port),
  341. ip = "0.0.0.0"
  342. }
  343. }
  344. local config = {}
  345. function config:new(o)
  346. o = o or {}
  347. setmetatable(o, self)
  348. self.__index = self
  349. return o
  350. end
  351. function config:handleIndex(index)
  352. local switch = {
  353. ss = function()
  354. ss.protocol = socks_port
  355. if server.plugin and server.plugin ~= "none" then
  356. ss.plugin = server.plugin
  357. ss.plugin_opts = server.plugin_opts or nil
  358. end
  359. print(json.stringify(ss, 1))
  360. end,
  361. ssr = function()
  362. ss.protocol = server.protocol
  363. ss.protocol_param = server.protocol_param
  364. ss.method = server.encrypt_method
  365. ss.obfs = server.obfs
  366. ss.obfs_param = server.obfs_param
  367. print(json.stringify(ss, 1))
  368. end,
  369. v2ray = function()
  370. print(json.stringify(Xray, 1))
  371. end,
  372. trojan = function()
  373. print(json.stringify(trojan, 1))
  374. end,
  375. naiveproxy = function()
  376. print(json.stringify(naiveproxy, 1))
  377. end,
  378. hysteria = function()
  379. print(json.stringify(hysteria, 1))
  380. end,
  381. tuic = function()
  382. print(json.stringify(tuic, 1))
  383. end
  384. }
  385. if switch[index] then
  386. switch[index]()
  387. end
  388. end
  389. local f = config:new()
  390. f:handleIndex(server.type)