gen_config.lua 8.8 KB

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