gen_config.lua 12 KB

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