gen_config.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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)) 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 = (server.certificate) and server.certpath or nil,
  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. tcp = {
  208. -- tcp
  209. no_delay = true,
  210. keep_alive = true,
  211. reuse_port = true,
  212. fast_open = (server.fast_open == "1") and true or false,
  213. fast_open_qlen = 20
  214. }
  215. }
  216. local naiveproxy = {
  217. proxy = (server.username and server.password and server.server and server.server_port) and "https://" .. server.username .. ":" .. server.password .. "@" .. server.server .. ":" .. server.server_port,
  218. listen = (proto == "redir") and "redir" .. "://0.0.0.0:" .. tonumber(local_port) or "socks" .. "://0.0.0.0:" .. tonumber(local_port),
  219. concurrency = (socks_port ~= "0") and tonumber(socks_port) or "1"
  220. }
  221. local ss = {
  222. server = (server.kcp_enable == "1") and "127.0.0.1" or server.server,
  223. server_port = tonumber(server.server_port),
  224. local_address = "0.0.0.0",
  225. local_port = tonumber(local_port),
  226. mode = (proto == "tcp,udp") and "tcp_and_udp" or proto .. "_only",
  227. password = server.password,
  228. method = server.encrypt_method_ss,
  229. timeout = tonumber(server.timeout),
  230. fast_open = (server.fast_open == "1") and true or false,
  231. reuse_port = true
  232. }
  233. local config = {}
  234. function config:new(o)
  235. o = o or {}
  236. setmetatable(o, self)
  237. self.__index = self
  238. return o
  239. end
  240. function config:handleIndex(index)
  241. local switch = {
  242. ss = function()
  243. ss.protocol = socks_port
  244. if server.plugin and server.plugin ~= "none" then
  245. ss.plugin = server.plugin
  246. ss.plugin_opts = server.plugin_opts or nil
  247. end
  248. print(json.stringify(ss, 1))
  249. end,
  250. ssr = function()
  251. ss.protocol = server.protocol
  252. ss.protocol_param = server.protocol_param
  253. ss.method = server.encrypt_method
  254. ss.obfs = server.obfs
  255. ss.obfs_param = server.obfs_param
  256. print(json.stringify(ss, 1))
  257. end,
  258. v2ray = function()
  259. print(json.stringify(Xray, 1))
  260. end,
  261. trojan = function()
  262. print(json.stringify(trojan, 1))
  263. end,
  264. naiveproxy = function()
  265. print(json.stringify(naiveproxy, 1))
  266. end
  267. }
  268. if switch[index] then
  269. switch[index]()
  270. end
  271. end
  272. local f = config:new()
  273. f:handleIndex(server.type)