gen_config.lua 12 KB

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