gen_config.lua 8.6 KB

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