gen_config.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 nil,
  32. pluginOpts = (server.v2ray_protocol == "shadowsocks") and server.plugin_opts or nil,
  33. servers = {
  34. {
  35. address = server.server,
  36. port = tonumber(server.server_port),
  37. password = server.password,
  38. method = (server.v2ray_protocol == "shadowsocks") and server.encrypt_method_ss or nil,
  39. uot = (server.v2ray_protocol == "shadowsocks") and (server.uot == '1') or nil,
  40. ivCheck = (server.v2ray_protocol == "shadowsocks") and (server.ivCheck == '1') or nil,
  41. flow = (server.v2ray_protocol == "trojan") and (server.xtls == '1') and (server.vless_flow and server.vless_flow or "xtls-rprx-splice") or nil
  42. }
  43. }
  44. }
  45. if (server.v2ray_protocol == "shadowsocks") and (server.mux ~= "1") and (not (outbound_settings.plugin or server.transport ~= "tcp" or server.tls or server.xtls)) then
  46. server.v2ray_protocol = "shadowsocks_sing"
  47. outbound_settings = outbound_settings.servers[1]
  48. elseif (server.v2ray_protocol == "trojan") and (server.tls and server.mux ~= "1") and (not (server.transport ~= "tcp" or server.xtls)) then
  49. server.v2ray_protocol = "trojan_sing"
  50. outbound_settings = outbound_settings.servers[1]
  51. outbound_settings.serverName = server.tls_host
  52. outbound_settings.insecure = (server.insecure == "1") and true or false
  53. end
  54. end
  55. function socks_http()
  56. outbound_settings = {
  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. address = server.server,
  74. port = tonumber(server.server_port),
  75. localAddresses = server.local_addresses,
  76. privateKey = server.private_key,
  77. peerPublicKey = server.peer_pubkey,
  78. preSharedKey = server.preshared_key or nil,
  79. mtu = tonumber(server.mtu) or 1500
  80. }
  81. end
  82. local outbound = {}
  83. function outbound:new(o)
  84. o = o or {}
  85. setmetatable(o, self)
  86. self.__index = self
  87. return o
  88. end
  89. function outbound:handleIndex(index)
  90. local switch = {
  91. vmess = function()
  92. vmess_vless()
  93. end,
  94. vless = function()
  95. vmess_vless()
  96. end,
  97. trojan = function()
  98. trojan_shadowsocks()
  99. end,
  100. shadowsocks = function()
  101. trojan_shadowsocks()
  102. end,
  103. socks = function()
  104. socks_http()
  105. end,
  106. http = function()
  107. socks_http()
  108. end,
  109. wireguard = function()
  110. wireguard()
  111. end
  112. }
  113. if switch[index] then
  114. switch[index]()
  115. end
  116. end
  117. local settings = outbound:new()
  118. settings:handleIndex(server.v2ray_protocol)
  119. local Xray = {
  120. log = {
  121. -- error = "/var/ssrplus.log",
  122. loglevel = "warning"
  123. },
  124. -- 传入连接
  125. inbound = (local_port ~= "0") and {
  126. -- listening
  127. port = tonumber(local_port),
  128. protocol = "dokodemo-door",
  129. settings = {network = proto, followRedirect = true},
  130. sniffing = {enabled = true, destOverride = {"http", "tls"}}
  131. } or nil,
  132. -- 开启 socks 代理
  133. inboundDetour = (proto:find("tcp") and socks_port ~= "0") and {
  134. {
  135. -- socks
  136. protocol = "socks",
  137. port = tonumber(socks_port),
  138. settings = {auth = "noauth", udp = true}
  139. }
  140. } or nil,
  141. -- 传出连接
  142. outbound = {
  143. protocol = server.v2ray_protocol,
  144. settings = outbound_settings,
  145. -- 底层传输配置
  146. streamSettings = (server.v2ray_protocol and server.v2ray_protocol:sub(-#"_sing") ~= "_sing") and {
  147. network = server.transport or "tcp",
  148. security = (server.xtls == '1') and "xtls" or (server.tls == '1') and "tls" or nil,
  149. tlsSettings = (server.tls == '1' and (server.insecure == "1" or server.tls_host or server.fingerprint)) and {
  150. -- tls
  151. fingerprint = server.fingerprint,
  152. allowInsecure = (server.insecure == "1") and true or nil,
  153. serverName = server.tls_host
  154. } or nil,
  155. xtlsSettings = (server.xtls == '1' and (server.insecure == "1" or server.tls_host)) and {
  156. -- xtls
  157. allowInsecure = (server.insecure == "1") and true or nil,
  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. mtu = tonumber(server.mtu),
  173. tti = tonumber(server.tti),
  174. uplinkCapacity = tonumber(server.uplink_capacity),
  175. downlinkCapacity = tonumber(server.downlink_capacity),
  176. congestion = (server.congestion == "1") and true or false,
  177. readBufferSize = tonumber(server.read_buffer_size),
  178. writeBufferSize = tonumber(server.write_buffer_size),
  179. header = {type = server.kcp_guise},
  180. seed = server.seed or nil
  181. } or nil,
  182. wsSettings = (server.transport == "ws") and (server.ws_path or server.ws_host or server.tls_host) and {
  183. -- ws
  184. headers = (server.ws_host or server.tls_host) and {
  185. -- headers
  186. Host = server.ws_host or server.tls_host
  187. } or nil,
  188. path = server.ws_path,
  189. maxEarlyData = tonumber(server.ws_ed) or nil,
  190. earlyDataHeaderName = server.ws_ed_header or nil
  191. } or nil,
  192. httpSettings = (server.transport == "h2") and {
  193. -- h2
  194. path = server.h2_path or "",
  195. host = {server.h2_host} or nil,
  196. read_idle_timeout = tonumber(server.read_idle_timeout) or nil,
  197. health_check_timeout = tonumber(server.health_check_timeout) or nil
  198. } or nil,
  199. quicSettings = (server.transport == "quic") and {
  200. -- quic
  201. security = server.quic_security,
  202. key = server.quic_key,
  203. header = {type = server.quic_guise}
  204. } or nil,
  205. grpcSettings = (server.transport == "grpc") and {
  206. -- grpc
  207. serviceName = server.serviceName or "",
  208. mode = (server.grpc_mode ~= "gun") and server.grpc_mode or nil,
  209. idle_timeout = tonumber(server.idle_timeout) or nil,
  210. health_check_timeout = tonumber(server.health_check_timeout) or nil,
  211. permit_without_stream = (server.permit_without_stream == "1") and true or nil,
  212. initial_windows_size = tonumber(server.initial_windows_size) or nil
  213. } or nil
  214. } or nil,
  215. mux = (server.mux == "1" and server.xtls ~= "1" and server.transport ~= "grpc") and {
  216. -- mux
  217. enabled = true,
  218. concurrency = tonumber(server.concurrency),
  219. packetEncoding = (server.v2ray_protocol == "vmess" or server.v2ray_protocol == "vless") and server.packet_encoding or nil
  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 = {"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 .. ":" .. server.server_port,
  277. protocol = server.hysteria_protocol,
  278. up_mbps = tonumber(server.uplink_capacity),
  279. down_mbps = tonumber(server.downlink_capacity),
  280. socks5 = (proto:find("tcp") and tonumber(socks_port) and tonumber(socks_port) ~= "0") and {
  281. listen = "0.0.0.0:" .. tonumber(socks_port),
  282. timeout = 300,
  283. disable_udp = false
  284. } or nil,
  285. redirect_tcp = (proto:find("tcp") and local_port ~= "0") and {
  286. listen = "0.0.0.0:" .. tonumber(local_port),
  287. timeout = 300
  288. } or nil,
  289. tproxy_udp = (proto:find("udp") and local_port ~= "0") and {
  290. listen = "0.0.0.0:" .. tonumber(local_port),
  291. timeout = 60
  292. } or nil,
  293. obfs = server.seed,
  294. auth = (server.auth_type == "1") and server.auth_payload or nil,
  295. auth_str = (server.auth_type == "2") and server.auth_payload or nil,
  296. alpn = server.quic_tls_alpn,
  297. server_name = server.tls_host,
  298. insecure = (server.insecure == "1") and true or false,
  299. ca = (server.certificate) and server.certpath or nil,
  300. recv_window_conn = tonumber(server.recv_window_conn),
  301. recv_window = tonumber(server.recv_window),
  302. disable_mtu_discovery = (server.disable_mtu_discovery == "1") and true or false
  303. }
  304. local config = {}
  305. function config:new(o)
  306. o = o or {}
  307. setmetatable(o, self)
  308. self.__index = self
  309. return o
  310. end
  311. function config:handleIndex(index)
  312. local switch = {
  313. ss = function()
  314. ss.protocol = socks_port
  315. if server.plugin and server.plugin ~= "none" then
  316. ss.plugin = server.plugin
  317. ss.plugin_opts = server.plugin_opts or nil
  318. end
  319. print(json.stringify(ss, 1))
  320. end,
  321. ssr = function()
  322. ss.protocol = server.protocol
  323. ss.protocol_param = server.protocol_param
  324. ss.method = server.encrypt_method
  325. ss.obfs = server.obfs
  326. ss.obfs_param = server.obfs_param
  327. print(json.stringify(ss, 1))
  328. end,
  329. v2ray = function()
  330. print(json.stringify(Xray, 1))
  331. end,
  332. trojan = function()
  333. print(json.stringify(trojan, 1))
  334. end,
  335. naiveproxy = function()
  336. print(json.stringify(naiveproxy, 1))
  337. end,
  338. hysteria = function()
  339. print(json.stringify(hysteria, 1))
  340. end
  341. }
  342. if switch[index] then
  343. switch[index]()
  344. end
  345. end
  346. local f = config:new()
  347. f:handleIndex(server.type)