gen_config.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. 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 or server.fingerprint)) 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. } or nil,
  167. quicSettings = (server.transport == "quic") and {
  168. -- quic
  169. security = server.quic_security,
  170. key = server.quic_key,
  171. header = {type = server.quic_guise}
  172. } or nil
  173. },
  174. mux = (server.mux == "1" and server.xtls ~= "1") and {
  175. -- mux
  176. enabled = true,
  177. concurrency = tonumber(server.concurrency)
  178. } or nil
  179. } or nil
  180. }
  181. 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"
  182. local cipher13 = "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384"
  183. local trojan = {
  184. log_level = 3,
  185. run_type = (proto == "nat" or proto == "tcp") and "nat" or "client",
  186. local_addr = "0.0.0.0",
  187. local_port = tonumber(local_port),
  188. remote_addr = server.server,
  189. remote_port = tonumber(server.server_port),
  190. udp_timeout = 60,
  191. -- 传入连接
  192. password = {server.password},
  193. -- 传出连接
  194. ssl = {
  195. verify = (server.insecure == "0") and true or false,
  196. verify_hostname = (server.tls == "1") and true or false,
  197. cert = "",
  198. cipher = cipher,
  199. cipher_tls13 = cipher13,
  200. sni = server.tls_host,
  201. alpn = {"h2", "http/1.1"},
  202. curve = "",
  203. reuse_session = true,
  204. session_ticket = (server.tls_sessionTicket == "1") and true or false
  205. },
  206. udp_timeout = 60,
  207. mux = (server.mux == "1") and {
  208. -- mux
  209. enabled = true,
  210. concurrency = tonumber(server.concurrency),
  211. idle_timeout = 60
  212. } or nil,
  213. tcp = {
  214. -- tcp
  215. no_delay = true,
  216. keep_alive = true,
  217. reuse_port = true,
  218. fast_open = (server.fast_open == "1") and true or false,
  219. fast_open_qlen = 20
  220. }
  221. }
  222. local naiveproxy = {
  223. proxy = (server.username and server.password and server.server and server.server_port) and "https://" .. server.username .. ":" .. server.password .. "@" .. server.server .. ":" .. server.server_port,
  224. listen = (proto == "redir") and "redir" .. "://0.0.0.0:" .. tonumber(local_port) or "socks" .. "://0.0.0.0:" .. tonumber(local_port),
  225. concurrency = (socks_port ~= "0") and tonumber(socks_port) or "1"
  226. }
  227. local ss = {
  228. server = (server.kcp_enable == "1") and "127.0.0.1" or server.server,
  229. server_port = tonumber(server.server_port),
  230. local_address = "0.0.0.0",
  231. local_port = tonumber(local_port),
  232. password = server.password,
  233. method = server.encrypt_method_ss,
  234. timeout = tonumber(server.timeout),
  235. fast_open = (server.fast_open == "1") and true or false,
  236. reuse_port = true
  237. }
  238. local config = {}
  239. function config:new(o)
  240. o = o or {}
  241. setmetatable(o, self)
  242. self.__index = self
  243. return o
  244. end
  245. function config:handleIndex(index)
  246. local switch = {
  247. ss = function()
  248. if server.plugin and server.plugin ~= "none" then
  249. ss.plugin = server.plugin
  250. ss.plugin_opts = server.plugin_opts or nil
  251. end
  252. print(json.stringify(ss, 1))
  253. end,
  254. ssr = function()
  255. ss.protocol = server.protocol
  256. ss.protocol_param = server.protocol_param
  257. ss.method = server.encrypt_method
  258. ss.obfs = server.obfs
  259. ss.obfs_param = server.obfs_param
  260. print(json.stringify(ss, 1))
  261. end,
  262. v2ray = function()
  263. print(json.stringify(Xray, 1))
  264. end,
  265. trojan = function()
  266. print(json.stringify(trojan, 1))
  267. end,
  268. trojan_go = function()
  269. trojan.ssl.cipher = server.fingerprint == nil and cipher or (server.fingerprint == "disable" and cipher13 .. ":" .. cipher or "")
  270. trojan.ssl.cipher_tls13 = server.fingerprint == nil and cipher13 or nil
  271. trojan.ssl.fingerprint = (server.fingerprint ~= nil and server.fingerprint ~= "disable") and server.fingerprint or ""
  272. trojan.ssl.alpn = server.trojan_transport == 'ws' and {} or {"h2", "http/1.1"}
  273. if server.tls ~= "1" and server.trojan_transport == "original" then
  274. -- tls
  275. trojan.ssl = nil
  276. trojan.transport_plugin = server.trojan_transport == "original" and {
  277. enabled = server.plugin_type ~= nil,
  278. type = server.plugin_type or "plaintext",
  279. command = server.plugin_type ~= "plaintext" and server.plugin_cmd or nil,
  280. option = server.plugin_type ~= "plaintext" and server.plugin_option or nil,
  281. arg = server.plugin_type ~= "plaintext" and {server.plugin_arg} or nil,
  282. env = {}
  283. } or nil
  284. end
  285. trojan.websocket = server.trojan_transport and server.trojan_transport:find('ws') and {
  286. -- ws
  287. enabled = true,
  288. path = server.ws_path or "/",
  289. host = server.ws_host or (server.tls_host or server.server)
  290. } or nil
  291. trojan.shadowsocks = (server.ss_aead == "1") and {
  292. -- ss
  293. enabled = true,
  294. method = server.ss_aead_method or "aead_aes_128_gcm",
  295. password = server.ss_aead_pwd or ""
  296. } or nil
  297. print(json.stringify(trojan, 1))
  298. end,
  299. naiveproxy = function()
  300. print(json.stringify(naiveproxy, 1))
  301. end
  302. }
  303. if switch[index] then
  304. switch[index]()
  305. end
  306. end
  307. local f = config:new()
  308. f:handleIndex(server.type)