gen_config.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. alterId = (server.v2ray_protocol == "vmess" or not server.v2ray_protocol) and tonumber(server.alter_id) or nil,
  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. }
  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_v2ray_ss or nil,
  36. flow = (server.v2ray_protocol == "trojan") and (server.xtls == '1') and (server.vless_flow and server.vless_flow or "xtls-rprx-splice") or nil
  37. }
  38. }
  39. }
  40. end
  41. function socks_http()
  42. outbound_settings = {
  43. --
  44. servers = {
  45. {
  46. --
  47. address = server.server,
  48. port = tonumber(server.server_port),
  49. users = (server.auth_enable == "1") and {
  50. {
  51. --
  52. user = server.username,
  53. pass = server.password
  54. }
  55. } or nil
  56. }
  57. }
  58. }
  59. end
  60. local outbound = {}
  61. function outbound:new(o)
  62. o = o or {}
  63. setmetatable(o, self)
  64. self.__index = self
  65. return o
  66. end
  67. function outbound:handleIndex(index)
  68. local switch = {
  69. vmess = function()
  70. vmess_vless()
  71. end,
  72. vless = function()
  73. vmess_vless()
  74. end,
  75. trojan = function()
  76. trojan_shadowsocks()
  77. end,
  78. shadowsocks = function()
  79. trojan_shadowsocks()
  80. end,
  81. socks = function()
  82. socks_http()
  83. end,
  84. http = function()
  85. socks_http()
  86. end
  87. }
  88. if switch[index] then
  89. switch[index]()
  90. end
  91. end
  92. local settings = outbound:new()
  93. settings:handleIndex(server.v2ray_protocol)
  94. local Xray = {
  95. log = {
  96. -- error = "/var/ssrplus.log",
  97. loglevel = "warning"
  98. },
  99. -- 传入连接
  100. inbound = (local_port ~= "0") and {
  101. -- listening
  102. port = tonumber(local_port),
  103. protocol = "dokodemo-door",
  104. settings = {network = proto, followRedirect = true},
  105. sniffing = {enabled = true, destOverride = {"http", "tls"}}
  106. } or nil,
  107. -- 开启 socks 代理
  108. inboundDetour = (proto:find("tcp") and socks_port ~= "0") and {
  109. {
  110. -- socks
  111. protocol = "socks",
  112. port = tonumber(socks_port),
  113. settings = {auth = "noauth", udp = true}
  114. }
  115. } or nil,
  116. -- 传出连接
  117. outbound = {
  118. protocol = server.v2ray_protocol or "vmess",
  119. settings = outbound_settings,
  120. -- 底层传输配置
  121. streamSettings = {
  122. network = server.transport or "tcp",
  123. security = (server.xtls == '1') and "xtls" or (server.tls == '1') and "tls" or nil,
  124. tlsSettings = (server.tls == '1' and (server.insecure == "1" or server.tls_host)) and {
  125. -- tls
  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. } 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. },
  176. mux = (server.mux == "1" and server.xtls ~= "1") and {
  177. -- mux
  178. enabled = true,
  179. concurrency = tonumber(server.concurrency)
  180. } or nil
  181. } or nil
  182. }
  183. local cipher =
  184. "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"
  185. local cipher13 = "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384"
  186. local trojan = {
  187. log_level = 3,
  188. run_type = (proto == "nat" or proto == "tcp") and "nat" or "client",
  189. local_addr = "0.0.0.0",
  190. local_port = tonumber(local_port),
  191. remote_addr = server.server,
  192. remote_port = tonumber(server.server_port),
  193. udp_timeout = 60,
  194. -- 传入连接
  195. password = {server.password},
  196. -- 传出连接
  197. ssl = {
  198. verify = (server.insecure == "0") and true or false,
  199. verify_hostname = (server.tls == "1") and true or false,
  200. cert = "",
  201. cipher = cipher,
  202. cipher_tls13 = cipher13,
  203. sni = server.tls_host,
  204. alpn = {"h2", "http/1.1"},
  205. curve = "",
  206. reuse_session = true,
  207. session_ticket = (server.tls_sessionTicket == "1") and true or false
  208. },
  209. udp_timeout = 60,
  210. mux = (server.mux == "1") and {
  211. -- mux
  212. enabled = true,
  213. concurrency = tonumber(server.concurrency),
  214. idle_timeout = 60
  215. } or nil,
  216. tcp = {
  217. -- tcp
  218. no_delay = true,
  219. keep_alive = true,
  220. reuse_port = true,
  221. fast_open = (server.fast_open == "1") and true or false,
  222. fast_open_qlen = 20
  223. }
  224. }
  225. local naiveproxy = {
  226. proxy = (server.username and server.password and server.server and server.server_port) and "https://" .. server.username .. ":" .. server.password .. "@" .. server.server .. ":" .. server.server_port,
  227. listen = (proto == "redir") and "redir" .. "://0.0.0.0:" .. tonumber(local_port) or "socks" .. "://0.0.0.0:" .. tonumber(local_port),
  228. concurrency = (socks_port ~= "0") and tonumber(socks_port) or "1"
  229. }
  230. local ss = {
  231. server = (server.kcp_enable == "1") and "127.0.0.1" or server.server,
  232. server_port = tonumber(server.server_port),
  233. local_address = "0.0.0.0",
  234. local_port = tonumber(local_port),
  235. password = server.password,
  236. method = server.encrypt_method_ss,
  237. timeout = tonumber(server.timeout),
  238. fast_open = (server.fast_open == "1") and true or false,
  239. reuse_port = true
  240. }
  241. local config = {}
  242. function config:new(o)
  243. o = o or {}
  244. setmetatable(o, self)
  245. self.__index = self
  246. return o
  247. end
  248. function config:handleIndex(index)
  249. local switch = {
  250. ss = function()
  251. if server.plugin and server.plugin ~= "none" then
  252. ss.plugin = server.plugin
  253. ss.plugin_opts = server.plugin_opts or nil
  254. end
  255. print(json.stringify(ss, 1))
  256. end,
  257. ssr = function()
  258. ss.protocol = server.protocol
  259. ss.protocol_param = server.protocol_param
  260. ss.method = server.encrypt_method
  261. ss.obfs = server.obfs
  262. ss.obfs_param = server.obfs_param
  263. print(json.stringify(ss, 1))
  264. end,
  265. v2ray = function()
  266. print(json.stringify(Xray, 1))
  267. end,
  268. trojan = function()
  269. print(json.stringify(trojan, 1))
  270. end,
  271. trojan_go = function()
  272. trojan.ssl.cipher = server.fingerprint == nil and cipher or (server.fingerprint == "disable" and cipher13 .. ":" .. cipher or "")
  273. trojan.ssl.cipher_tls13 = server.fingerprint == nil and cipher13 or nil
  274. trojan.ssl.fingerprint = (server.fingerprint ~= nil and server.fingerprint ~= "disable") and server.fingerprint or ""
  275. trojan.ssl.alpn = server.trojan_transport == 'ws' and {} or {"h2", "http/1.1"}
  276. if server.tls ~= "1" and server.trojan_transport == "original" then
  277. --
  278. trojan.ssl = nil
  279. trojan.transport_plugin = server.trojan_transport == "original" and {
  280. enabled = server.plugin_type ~= nil,
  281. type = server.plugin_type or "plaintext",
  282. command = server.plugin_type ~= "plaintext" and server.plugin_cmd or nil,
  283. option = server.plugin_type ~= "plaintext" and server.plugin_option or nil,
  284. arg = server.plugin_type ~= "plaintext" and {server.plugin_arg} or nil,
  285. env = {}
  286. } or nil
  287. end
  288. trojan.websocket = server.trojan_transport and server.trojan_transport:find('ws') and {
  289. --
  290. enabled = true,
  291. path = server.ws_path or "/",
  292. host = server.ws_host or (server.tls_host or server.server)
  293. } or nil
  294. trojan.shadowsocks = (server.ss_aead == "1") and {
  295. --
  296. enabled = true,
  297. method = server.ss_aead_method or "aead_aes_128_gcm",
  298. password = server.ss_aead_pwd or ""
  299. } or nil
  300. print(json.stringify(trojan, 1))
  301. end,
  302. naiveproxy = function()
  303. print(json.stringify(naiveproxy, 1))
  304. end
  305. }
  306. if switch[index] then
  307. switch[index]()
  308. end
  309. end
  310. local f = config:new()
  311. f:handleIndex(server.type)