gen_config.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 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. end
  46. function socks_http()
  47. outbound_settings = {
  48. servers = {
  49. {
  50. address = server.server,
  51. port = tonumber(server.server_port),
  52. users = (server.auth_enable == "1") and {
  53. {
  54. user = server.username,
  55. pass = server.password
  56. }
  57. } or nil
  58. }
  59. }
  60. }
  61. end
  62. local outbound = {}
  63. function outbound:new(o)
  64. o = o or {}
  65. setmetatable(o, self)
  66. self.__index = self
  67. return o
  68. end
  69. function outbound:handleIndex(index)
  70. local switch = {
  71. vmess = function()
  72. vmess_vless()
  73. end,
  74. vless = function()
  75. vmess_vless()
  76. end,
  77. trojan = function()
  78. trojan_shadowsocks()
  79. end,
  80. shadowsocks = function()
  81. trojan_shadowsocks()
  82. end,
  83. socks = function()
  84. socks_http()
  85. end,
  86. http = function()
  87. socks_http()
  88. end
  89. }
  90. if switch[index] then
  91. switch[index]()
  92. end
  93. end
  94. local settings = outbound:new()
  95. settings:handleIndex(server.v2ray_protocol)
  96. local Xray = {
  97. log = {
  98. -- error = "/var/ssrplus.log",
  99. loglevel = "warning"
  100. },
  101. -- 传入连接
  102. inbound = (local_port ~= "0") and {
  103. -- listening
  104. port = tonumber(local_port),
  105. protocol = "dokodemo-door",
  106. settings = {network = proto, followRedirect = true},
  107. sniffing = {enabled = true, destOverride = {"http", "tls"}}
  108. } or nil,
  109. -- 开启 socks 代理
  110. inboundDetour = (proto:find("tcp") and socks_port ~= "0") and {
  111. {
  112. -- socks
  113. protocol = "socks",
  114. port = tonumber(socks_port),
  115. settings = {auth = "noauth", udp = true}
  116. }
  117. } or nil,
  118. -- 传出连接
  119. outbound = {
  120. protocol = server.v2ray_protocol,
  121. settings = outbound_settings,
  122. -- 底层传输配置
  123. streamSettings = {
  124. network = server.transport or "tcp",
  125. security = (server.xtls == '1') and "xtls" or (server.tls == '1') and "tls" or nil,
  126. tlsSettings = (server.tls == '1' and (server.insecure == "1" or server.tls_host or server.fingerprint)) and {
  127. -- tls
  128. fingerprint = server.fingerprint,
  129. allowInsecure = (server.insecure == "1") and true or nil,
  130. serverName = server.tls_host
  131. } or nil,
  132. xtlsSettings = (server.xtls == '1' and (server.insecure == "1" or server.tls_host)) and {
  133. -- xtls
  134. allowInsecure = (server.insecure == "1") and true or nil,
  135. serverName = server.tls_host
  136. } or nil,
  137. tcpSettings = (server.transport == "tcp" and server.tcp_guise == "http") and {
  138. -- tcp
  139. header = {
  140. type = server.tcp_guise,
  141. request = {
  142. -- request
  143. path = {server.http_path} or {"/"},
  144. headers = {Host = {server.http_host} or {}}
  145. }
  146. }
  147. } or nil,
  148. kcpSettings = (server.transport == "kcp") and {
  149. mtu = tonumber(server.mtu),
  150. tti = tonumber(server.tti),
  151. uplinkCapacity = tonumber(server.uplink_capacity),
  152. downlinkCapacity = tonumber(server.downlink_capacity),
  153. congestion = (server.congestion == "1") and true or false,
  154. readBufferSize = tonumber(server.read_buffer_size),
  155. writeBufferSize = tonumber(server.write_buffer_size),
  156. header = {type = server.kcp_guise},
  157. seed = server.seed or nil
  158. } or nil,
  159. wsSettings = (server.transport == "ws") and (server.ws_path or server.ws_host or server.tls_host) and {
  160. -- ws
  161. path = server.ws_path,
  162. headers = (server.ws_host or server.tls_host) and {
  163. -- headers
  164. Host = server.ws_host or server.tls_host
  165. } or nil
  166. } or nil,
  167. httpSettings = (server.transport == "h2") and {
  168. -- h2
  169. path = server.h2_path or "",
  170. host = {server.h2_host} or nil,
  171. read_idle_timeout = tonumber(server.read_idle_timeout) or nil,
  172. health_check_timeout = tonumber(server.health_check_timeout) or nil
  173. } or nil,
  174. quicSettings = (server.transport == "quic") and {
  175. -- quic
  176. security = server.quic_security,
  177. key = server.quic_key,
  178. header = {type = server.quic_guise}
  179. } or nil,
  180. grpcSettings = (server.transport == "grpc") and {
  181. -- grpc
  182. serviceName = server.serviceName or "",
  183. multiMode = (server.mux == "1") and true or false,
  184. idle_timeout = tonumber(server.idle_timeout) or nil,
  185. health_check_timeout = tonumber(server.health_check_timeout) or nil,
  186. permit_without_stream = (server.permit_without_stream == "1") and true or nil,
  187. initial_windows_size = tonumber(server.initial_windows_size) or nil
  188. } or nil
  189. },
  190. mux = (server.mux == "1" and server.xtls ~= "1" and server.transport ~= "grpc") and {
  191. -- mux
  192. enabled = true,
  193. concurrency = tonumber(server.concurrency),
  194. packetEncoding = (server.v2ray_protocol == "vmess" or server.v2ray_protocol == "vless") and server.packet_encoding or nil
  195. } or nil
  196. } or nil
  197. }
  198. 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"
  199. local cipher13 = "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384"
  200. local trojan = {
  201. log_level = 3,
  202. run_type = (proto == "nat" or proto == "tcp") and "nat" or "client",
  203. local_addr = "0.0.0.0",
  204. local_port = tonumber(local_port),
  205. remote_addr = server.server,
  206. remote_port = tonumber(server.server_port),
  207. udp_timeout = 60,
  208. -- 传入连接
  209. password = {server.password},
  210. -- 传出连接
  211. ssl = {
  212. verify = (server.insecure == "0") and true or false,
  213. verify_hostname = (server.tls == "1") and true or false,
  214. cert = (server.certificate) and server.certpath or nil,
  215. cipher = cipher,
  216. cipher_tls13 = cipher13,
  217. sni = server.tls_host,
  218. alpn = {"h2", "http/1.1"},
  219. curve = "",
  220. reuse_session = true,
  221. session_ticket = (server.tls_sessionTicket == "1") and true or false
  222. },
  223. udp_timeout = 60,
  224. tcp = {
  225. -- tcp
  226. no_delay = true,
  227. keep_alive = true,
  228. reuse_port = true,
  229. fast_open = (server.fast_open == "1") and true or false,
  230. fast_open_qlen = 20
  231. }
  232. }
  233. local naiveproxy = {
  234. proxy = (server.username and server.password and server.server and server.server_port) and "https://" .. server.username .. ":" .. server.password .. "@" .. server.server .. ":" .. server.server_port,
  235. listen = (proto == "redir") and "redir" .. "://0.0.0.0:" .. tonumber(local_port) or "socks" .. "://0.0.0.0:" .. tonumber(local_port),
  236. ["insecure-concurrency"] = tonumber(server.concurrency) or 1
  237. }
  238. local ss = {
  239. server = (server.kcp_enable == "1") and "127.0.0.1" or server.server,
  240. server_port = tonumber(server.server_port),
  241. local_address = "0.0.0.0",
  242. local_port = tonumber(local_port),
  243. mode = (proto == "tcp,udp") and "tcp_and_udp" or proto .. "_only",
  244. password = server.password,
  245. method = server.encrypt_method_ss,
  246. timeout = tonumber(server.timeout),
  247. fast_open = (server.fast_open == "1") and true or false,
  248. reuse_port = true
  249. }
  250. local config = {}
  251. function config:new(o)
  252. o = o or {}
  253. setmetatable(o, self)
  254. self.__index = self
  255. return o
  256. end
  257. function config:handleIndex(index)
  258. local switch = {
  259. ss = function()
  260. ss.protocol = socks_port
  261. if server.plugin and server.plugin ~= "none" then
  262. ss.plugin = server.plugin
  263. ss.plugin_opts = server.plugin_opts or nil
  264. end
  265. print(json.stringify(ss, 1))
  266. end,
  267. ssr = function()
  268. ss.protocol = server.protocol
  269. ss.protocol_param = server.protocol_param
  270. ss.method = server.encrypt_method
  271. ss.obfs = server.obfs
  272. ss.obfs_param = server.obfs_param
  273. print(json.stringify(ss, 1))
  274. end,
  275. v2ray = function()
  276. print(json.stringify(Xray, 1))
  277. end,
  278. trojan = function()
  279. print(json.stringify(trojan, 1))
  280. end,
  281. naiveproxy = function()
  282. print(json.stringify(naiveproxy, 1))
  283. end
  284. }
  285. if switch[index] then
  286. switch[index]()
  287. end
  288. end
  289. local f = config:new()
  290. f:handleIndex(server.type)