gen_config.lua 12 KB

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