测速不再重启现有服务

代码整理和简化
pull/543/head
YFdyh000 2020-03-15 18:22:11 +08:00
parent d94fcbd8a3
commit 1865621a6a
5 changed files with 157 additions and 96 deletions

View File

@ -1197,7 +1197,7 @@ namespace v2rayN.Forms
}
else
{
downloadHandle.DownloadFileAsync(config, url, null, -1);
downloadHandle.DownloadFileAsync(url, null, -1);
}
}));
}
@ -1263,7 +1263,7 @@ namespace v2rayN.Forms
}
else
{
downloadHandle.DownloadFileAsync(config, url, null, -1);
downloadHandle.DownloadFileAsync(url, null, -1);
}
}));
}

View File

@ -172,7 +172,7 @@ namespace v2rayN.Handler
#region Download
public void DownloadFileAsync(Config config, string url, WebProxy webProxy, int downloadTimeout)
public void DownloadFileAsync(string url, WebProxy webProxy, int downloadTimeout)
{
try
{

View File

@ -100,14 +100,14 @@ namespace v2rayN.Handler
private void RunRealPing()
{
int pid = -1;
try
{
string msg = string.Empty;
Global.reloadV2ray = true;
_v2rayHandler.LoadV2ray(_config, _selecteds);
pid = _v2rayHandler.LoadV2rayConfigString(_config, _selecteds);
Thread.Sleep(5000);
//Thread.Sleep(5000);
int httpPort = _config.GetLocalPort("speedtest");
Task[] tasks = new Task[_selecteds.Count];
int i = -1;
@ -119,7 +119,8 @@ namespace v2rayN.Handler
}
i++;
tasks[i] = Task.Run(() => {
tasks[i] = Task.Run(() =>
{
try
{
WebProxy webProxy = new WebProxy(Global.Loopback, httpPort + itemIndex);
@ -137,28 +138,28 @@ namespace v2rayN.Handler
}
Task.WaitAll(tasks);
Global.reloadV2ray = true;
_v2rayHandler.LoadV2ray(_config);
Thread.Sleep(100);
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
finally
{
if (pid > 0) _v2rayHandler.V2rayStopPid(pid);
}
}
private void RunSpeedTest()
{
int pid = -1;
if (_config.vmess.Count <= 0)
{
return;
}
Global.reloadV2ray = true;
_v2rayHandler.LoadV2ray(_config, _selecteds);
Thread.Sleep(5000);
pid = _v2rayHandler.LoadV2rayConfigString(_config, _selecteds);
string url = _config.speedTestUrl;
testCounter = 0;
@ -167,53 +168,35 @@ namespace v2rayN.Handler
downloadHandle2 = new DownloadHandle();
downloadHandle2.UpdateCompleted += (sender2, args) =>
{
if (args.Success)
{
_updateFunc(ItemIndex, args.Msg);
if (ServerSpeedTestSub(testCounter, url) != 0)
{
return;
}
}
else
{
_updateFunc(ItemIndex, args.Msg);
}
_updateFunc(ItemIndex, args.Msg);
if (args.Success) StartNext();
};
downloadHandle2.Error += (sender2, args) =>
{
_updateFunc(ItemIndex, args.GetException().Message);
if (ServerSpeedTestSub(testCounter, url) != 0)
{
return;
}
StartNext();
};
}
if (ServerSpeedTestSub(testCounter, url) != 0)
StartNext();
void StartNext()
{
return;
if (testCounter >= _selecteds.Count)
{
if (pid > 0) _v2rayHandler.V2rayStopPid(pid);
return;
}
int httpPort = _config.GetLocalPort("speedtest");
int index = _selecteds[testCounter];
testCounter++;
WebProxy webProxy = new WebProxy(Global.Loopback, httpPort + index);
downloadHandle2.DownloadFileAsync(url, webProxy, 20);
}
}
private int ServerSpeedTestSub(int index, string url)
{
if (index >= _selecteds.Count)
{
Global.reloadV2ray = true;
_v2rayHandler.LoadV2ray(_config);
return -1;
}
int httpPort = _config.GetLocalPort("speedtest");
index = _selecteds[index];
testCounter++;
WebProxy webProxy = new WebProxy(Global.Loopback, httpPort + index);
downloadHandle2.DownloadFileAsync(_config, url, webProxy, 20);
return 0;
}
private int GetTcpingTime(string url, int port)
{

View File

@ -1411,7 +1411,7 @@ namespace v2rayN.Handler
#region Gen speedtest config
public static int GenerateClientSpeedtestConfig(Config config, List<int> selecteds, string fileName, out string msg)
public static string GenerateClientSpeedtestConfigString(Config config, List<int> selecteds, out string msg)
{
try
{
@ -1422,7 +1422,7 @@ namespace v2rayN.Handler
)
{
msg = UIRes.I18N("CheckServerSettings");
return -1;
return "";
}
msg = UIRes.I18N("InitialConfiguration");
@ -1433,20 +1433,21 @@ namespace v2rayN.Handler
if (Utils.IsNullOrEmpty(result))
{
msg = UIRes.I18N("FailedGetDefaultConfiguration");
return -1;
return "";
}
V2rayConfig v2rayConfig = Utils.FromJson<V2rayConfig>(result);
if (v2rayConfig == null)
{
msg = UIRes.I18N("FailedGenDefaultConfiguration");
return -1;
return "";
}
log(configCopy, ref v2rayConfig, false);
//routing(config, ref v2rayConfig);
dns(configCopy, ref v2rayConfig);
v2rayConfig.inbounds.RemoveAt(0); // Remove "proxy" service for speedtest, avoiding port conflicts.
int httpPort = configCopy.GetLocalPort("speedtest");
foreach (int index in selecteds)
@ -1482,16 +1483,14 @@ namespace v2rayN.Handler
v2rayConfig.routing.rules.Add(rule);
}
Utils.ToJsonFile(v2rayConfig, fileName);
msg = string.Format(UIRes.I18N("SuccessfulConfiguration"), configCopy.getSummary());
return Utils.ToJson(v2rayConfig);
}
catch
{
msg = UIRes.I18N("FailedGenDefaultConfiguration");
return -1;
return "";
}
return 0;
}
#endregion

View File

@ -56,23 +56,25 @@ namespace v2rayN.Handler
}
/// <summary>
/// 载入V2ray
/// 新建进程载入V2ray配置文件字符串
/// 返回新进程pid。
/// </summary>
public void LoadV2ray(Config config, List<int> _selecteds)
public int LoadV2rayConfigString(Config config, List<int> _selecteds)
{
if (Global.reloadV2ray)
int pid = -1;
string configStr = V2rayConfigHandler.GenerateClientSpeedtestConfigString(config, _selecteds, out string msg);
if (configStr == "")
{
string fileName = Utils.GetPath(v2rayConfigRes);
if (V2rayConfigHandler.GenerateClientSpeedtestConfig(config, _selecteds, fileName, out string msg) != 0)
{
ShowMsg(false, msg);
}
else
{
ShowMsg(true, msg);
V2rayRestart();
}
ShowMsg(false, msg);
}
else
{
ShowMsg(true, msg);
pid = V2rayStartNew(configStr);
//V2rayRestart();
// start with -config
}
return pid;
}
/// <summary>
@ -140,6 +142,43 @@ namespace v2rayN.Handler
Utils.SaveLog(ex.Message, ex);
}
}
/// <summary>
/// V2ray停止
/// </summary>
public void V2rayStopPid(int pid)
{
try
{
Process _p = Process.GetProcessById(pid);
KillProcess(_p);
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
private string V2rayFindexe() {
//查找v2ray文件是否存在
string fileName = string.Empty;
lstV2ray.Reverse();
foreach (string name in lstV2ray)
{
string vName = string.Format("{0}.exe", name);
vName = Utils.GetPath(vName);
if (File.Exists(vName))
{
fileName = vName;
break;
}
}
if (Utils.IsNullOrEmpty(fileName))
{
string msg = string.Format(UIRes.I18N("NotFoundCore"), @"https://github.com/v2ray/v2ray-core/releases");
ShowMsg(true, msg);
}
return fileName;
}
/// <summary>
/// V2ray启动
@ -150,32 +189,21 @@ namespace v2rayN.Handler
try
{
//查找v2ray文件是否存在
string fileName = string.Empty;
foreach (string name in lstV2ray)
{
string vName = string.Format("{0}.exe", name);
vName = Utils.GetPath(vName);
if (File.Exists(vName))
{
fileName = vName;
break;
}
}
if (Utils.IsNullOrEmpty(fileName))
{
string msg = string.Format(UIRes.I18N("NotFoundCore"), @"https://github.com/v2ray/v2ray-core/releases");
ShowMsg(true, msg);
return;
}
string fileName = V2rayFindexe();
if (fileName == "") return;
Process p = new Process();
p.StartInfo.FileName = fileName;
p.StartInfo.WorkingDirectory = Utils.StartupPath();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.StandardOutputEncoding = Encoding.UTF8;
Process p = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
WorkingDirectory = Utils.StartupPath(),
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8
}
};
p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
@ -198,6 +226,57 @@ namespace v2rayN.Handler
ShowMsg(true, msg);
}
}
/// <summary>
/// V2ray启动新建进程传入配置字符串
/// </summary>
private int V2rayStartNew(string configStr)
{
ShowMsg(false, string.Format(UIRes.I18N("StartService"), DateTime.Now.ToString()));
try
{
string fileName = V2rayFindexe();
if (fileName == "") return -1;
Process p = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = "-config stdin:",
WorkingDirectory = Utils.StartupPath(),
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8
}
};
p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
string msg = e.Data + Environment.NewLine;
ShowMsg(false, msg);
}
});
p.Start();
p.BeginOutputReadLine();
p.StandardInput.Write(configStr);
p.StandardInput.Close();
Global.processJob.AddProcess(p.Handle);
return p.Id;
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
string msg = ex.Message;
ShowMsg(true, msg);
return -1;
}
}
/// <summary>
/// 消息委托