1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| -- 修改版xray的位置
local xray = "./3rd/core/xray.exe"
-- 只打包测速结果小于5000毫秒的服务器
local latency = 5000
-- API端口
local apiPort = 12345
-- 把多个服务器打包成配置包
local function PackServers(latency)
local uids = {}
local servs = std.Server:GetAllServers()
foreach coreServ in servs do
local wserv = coreServ:Wrap()
local index = wserv:GetIndex()
if index ~= 1 then
local r = wserv:GetSpeedTestResult()
if r < latency then
table.insert(uids, wserv:GetUid())
end
end
end
return std.Server:PackServersToString(uids)
end
-- 辅助函数,执行API命令
local function ExecApiCmd(port, cmd, arg, stdin)
local timeout = 10 * 1000
local args = 'api ' .. cmd .. ' --server=127.0.0.1:' .. tostring(port)
if not string.isempty(arg) then
args = args .. " " .. arg
end
print(xray, args)
local r = std.Sys:RunAndGetResult(xray, args, nil, stdin, timeout, nil, nil)
-- for debugging
print(r)
end
local function Main()
-- 调用API清除所有outbounds
ExecApiCmd(apiPort, "rmo", '"*"', nil)
-- 生成配置包
local config = PackServers(latency)
-- 调用API把配置包添加到服务器中
ExecApiCmd(apiPort, "ado", nil, config)
-- 把配置包存入第一个服务器
local wserv = std.Server:GetWrappedServerByIndex(1)
wserv:SetConfigQuiet(config)
end
Main()
|