gen_config.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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') 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 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. tcpSettings = (server.transport == "tcp" and server.tcp_guise == "http") and {
  167. -- tcp
  168. header = {
  169. type = server.tcp_guise,
  170. request = {
  171. -- request
  172. path = {server.http_path} or {"/"},
  173. headers = {Host = {server.http_host} or {}}
  174. }
  175. }
  176. } or nil,
  177. kcpSettings = (server.transport == "kcp") and {
  178. mtu = tonumber(server.mtu),
  179. tti = tonumber(server.tti),
  180. uplinkCapacity = tonumber(server.uplink_capacity),
  181. downlinkCapacity = tonumber(server.downlink_capacity),
  182. congestion = (server.congestion == "1") and true or false,
  183. readBufferSize = tonumber(server.read_buffer_size),
  184. writeBufferSize = tonumber(server.write_buffer_size),
  185. header = {type = server.kcp_guise},
  186. seed = server.seed or nil
  187. } or nil,
  188. wsSettings = (server.transport == "ws") and (server.ws_path or server.ws_host or server.tls_host) and {
  189. -- ws
  190. headers = (server.ws_host or server.tls_host) and {
  191. -- headers
  192. Host = server.ws_host or server.tls_host
  193. } or nil,
  194. path = server.ws_path,
  195. maxEarlyData = tonumber(server.ws_ed) or nil,
  196. earlyDataHeaderName = server.ws_ed_header or nil
  197. } or nil,
  198. httpSettings = (server.transport == "h2") and {
  199. -- h2
  200. path = server.h2_path or "",
  201. host = {server.h2_host} or nil,
  202. read_idle_timeout = tonumber(server.read_idle_timeout) or nil,
  203. health_check_timeout = tonumber(server.health_check_timeout) or nil
  204. } or nil,
  205. quicSettings = (server.transport == "quic") and {
  206. -- quic
  207. security = server.quic_security,
  208. key = server.quic_key,
  209. header = {type = server.quic_guise}
  210. } or nil,
  211. grpcSettings = (server.transport == "grpc") and {
  212. -- grpc
  213. serviceName = server.serviceName or "",
  214. mode = (server.grpc_mode ~= "gun") and server.grpc_mode or nil,
  215. multiMode = (server.grpc_mode == "multi") and true or false,
  216. idle_timeout = tonumber(server.idle_timeout) or nil,
  217. health_check_timeout = tonumber(server.health_check_timeout) or nil,
  218. permit_without_stream = (server.permit_without_stream == "1") and true or nil,
  219. initial_windows_size = tonumber(server.initial_windows_size) or nil
  220. } or nil
  221. },
  222. mux = (server.mux == "1" and server.transport ~= "grpc") and {
  223. -- mux
  224. enabled = true,
  225. concurrency = tonumber(server.concurrency),
  226. packetEncoding = (server.v2ray_protocol == "vmess" or server.v2ray_protocol == "vless") and server.packet_encoding or nil
  227. } or nil
  228. } or nil
  229. }
  230. 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"
  231. local cipher13 = "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384"
  232. local trojan = {
  233. log_level = 3,
  234. run_type = (proto == "nat" or proto == "tcp") and "nat" or "client",
  235. local_addr = "0.0.0.0",
  236. local_port = tonumber(local_port),
  237. remote_addr = server.server,
  238. remote_port = tonumber(server.server_port),
  239. udp_timeout = 60,
  240. -- 传入连接
  241. password = {server.password},
  242. -- 传出连接
  243. ssl = {
  244. verify = (server.insecure == "0") and true or false,
  245. verify_hostname = (server.tls == "1") and true or false,
  246. cert = (server.certificate) and server.certpath or nil,
  247. cipher = cipher,
  248. cipher_tls13 = cipher13,
  249. sni = server.tls_host,
  250. alpn = server.tls_alpn or {"h2", "http/1.1"},
  251. curve = "",
  252. reuse_session = true,
  253. session_ticket = (server.tls_sessionTicket == "1") and true or false
  254. },
  255. udp_timeout = 60,
  256. tcp = {
  257. -- tcp
  258. no_delay = true,
  259. keep_alive = true,
  260. reuse_port = true,
  261. fast_open = (server.fast_open == "1") and true or false,
  262. fast_open_qlen = 20
  263. }
  264. }
  265. local naiveproxy = {
  266. proxy = (server.username and server.password and server.server and server.server_port) and "https://" .. server.username .. ":" .. server.password .. "@" .. server.server .. ":" .. server.server_port,
  267. listen = (proto == "redir") and "redir" .. "://0.0.0.0:" .. tonumber(local_port) or "socks" .. "://0.0.0.0:" .. tonumber(local_port),
  268. ["insecure-concurrency"] = tonumber(server.concurrency) or 1
  269. }
  270. local ss = {
  271. server = (server.kcp_enable == "1") and "127.0.0.1" or server.server,
  272. server_port = tonumber(server.server_port),
  273. local_address = "0.0.0.0",
  274. local_port = tonumber(local_port),
  275. mode = (proto == "tcp,udp") and "tcp_and_udp" or proto .. "_only",
  276. password = server.password,
  277. method = server.encrypt_method_ss,
  278. timeout = tonumber(server.timeout),
  279. fast_open = (server.fast_open == "1") and true or false,
  280. reuse_port = true
  281. }
  282. local hysteria = {
  283. server = server.server .. ":" .. server.server_port,
  284. protocol = server.hysteria_protocol,
  285. up_mbps = tonumber(server.uplink_capacity),
  286. down_mbps = tonumber(server.downlink_capacity),
  287. socks5 = (proto:find("tcp") and tonumber(socks_port) and tonumber(socks_port) ~= 0) and {
  288. listen = "0.0.0.0:" .. tonumber(socks_port),
  289. timeout = 300,
  290. disable_udp = false
  291. } or nil,
  292. redirect_tcp = (proto:find("tcp") and local_port ~= "0") and {
  293. listen = "0.0.0.0:" .. tonumber(local_port),
  294. timeout = 300
  295. } or nil,
  296. tproxy_udp = (proto:find("udp") and local_port ~= "0") and {
  297. listen = "0.0.0.0:" .. tonumber(local_port),
  298. timeout = 60
  299. } or nil,
  300. obfs = server.seed,
  301. auth = (server.auth_type == "1") and server.auth_payload or nil,
  302. auth_str = (server.auth_type == "2") and server.auth_payload or nil,
  303. alpn = server.quic_tls_alpn,
  304. server_name = server.tls_host,
  305. insecure = (server.insecure == "1") and true or false,
  306. ca = (server.certificate) and server.certpath or nil,
  307. recv_window_conn = tonumber(server.recv_window_conn),
  308. recv_window = tonumber(server.recv_window),
  309. disable_mtu_discovery = (server.disable_mtu_discovery == "1") and true or false,
  310. fast_open = (server.fast_open == "1") and true or false
  311. }
  312. local tuic = {
  313. relay = {
  314. server = server.server,
  315. port = tonumber(server.server_port),
  316. token = server.password,
  317. certificates = server.certificate and { server.certpath } or nil,
  318. udp_relay_mode = server.udp_relay_mode,
  319. congestion_controller = server.congestion_controller,
  320. heartbeat_interval = tonumber(server.heartbeat_interval),
  321. alpn = server.tls_alpn,
  322. disable_sni = (server.disable_sni == "1"),
  323. reduce_rtt = (server.reduce_rtt == "1"),
  324. max_udp_relay_packet_size = tonumber(server.max_udp_relay_packet_size)
  325. },
  326. ["local"] = {
  327. port = tonumber(local_port),
  328. ip = "0.0.0.0"
  329. }
  330. }
  331. local config = {}
  332. function config:new(o)
  333. o = o or {}
  334. setmetatable(o, self)
  335. self.__index = self
  336. return o
  337. end
  338. function config:handleIndex(index)
  339. local switch = {
  340. ss = function()
  341. ss.protocol = socks_port
  342. if server.plugin and server.plugin ~= "none" then
  343. ss.plugin = server.plugin
  344. ss.plugin_opts = server.plugin_opts or nil
  345. end
  346. print(json.stringify(ss, 1))
  347. end,
  348. ssr = function()
  349. ss.protocol = server.protocol
  350. ss.protocol_param = server.protocol_param
  351. ss.method = server.encrypt_method
  352. ss.obfs = server.obfs
  353. ss.obfs_param = server.obfs_param
  354. print(json.stringify(ss, 1))
  355. end,
  356. v2ray = function()
  357. print(json.stringify(Xray, 1))
  358. end,
  359. trojan = function()
  360. print(json.stringify(trojan, 1))
  361. end,
  362. naiveproxy = function()
  363. print(json.stringify(naiveproxy, 1))
  364. end,
  365. hysteria = function()
  366. print(json.stringify(hysteria, 1))
  367. end,
  368. tuic = function()
  369. print(json.stringify(tuic, 1))
  370. end
  371. }
  372. if switch[index] then
  373. switch[index]()
  374. end
  375. end
  376. local f = config:new()
  377. f:handleIndex(server.type)