测速不再重启现有服务

代码整理和简化
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 else
{ {
downloadHandle.DownloadFileAsync(config, url, null, -1); downloadHandle.DownloadFileAsync(url, null, -1);
} }
})); }));
} }
@ -1263,7 +1263,7 @@ namespace v2rayN.Forms
} }
else else
{ {
downloadHandle.DownloadFileAsync(config, url, null, -1); downloadHandle.DownloadFileAsync(url, null, -1);
} }
})); }));
} }

View File

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

View File

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

View File

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

View File

@ -56,23 +56,25 @@ namespace v2rayN.Handler
} }
/// <summary> /// <summary>
/// 载入V2ray /// 新建进程载入V2ray配置文件字符串
/// 返回新进程pid。
/// </summary> /// </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); ShowMsg(false, msg);
if (V2rayConfigHandler.GenerateClientSpeedtestConfig(config, _selecteds, fileName, out string msg) != 0)
{
ShowMsg(false, msg);
}
else
{
ShowMsg(true, msg);
V2rayRestart();
}
} }
else
{
ShowMsg(true, msg);
pid = V2rayStartNew(configStr);
//V2rayRestart();
// start with -config
}
return pid;
} }
/// <summary> /// <summary>
@ -140,6 +142,43 @@ namespace v2rayN.Handler
Utils.SaveLog(ex.Message, ex); 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> /// <summary>
/// V2ray启动 /// V2ray启动
@ -150,32 +189,21 @@ namespace v2rayN.Handler
try try
{ {
//查找v2ray文件是否存在 string fileName = V2rayFindexe();
string fileName = string.Empty; if (fileName == "") return;
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;
}
Process p = new Process(); Process p = new Process
p.StartInfo.FileName = fileName; {
p.StartInfo.WorkingDirectory = Utils.StartupPath(); StartInfo = new ProcessStartInfo
p.StartInfo.UseShellExecute = false; {
p.StartInfo.RedirectStandardOutput = true; FileName = fileName,
p.StartInfo.CreateNoWindow = true; WorkingDirectory = Utils.StartupPath(),
p.StartInfo.StandardOutputEncoding = Encoding.UTF8; UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8
}
};
p.OutputDataReceived += new DataReceivedEventHandler((sender, e) => p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{ {
if (!String.IsNullOrEmpty(e.Data)) if (!String.IsNullOrEmpty(e.Data))
@ -198,6 +226,57 @@ namespace v2rayN.Handler
ShowMsg(true, msg); 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> /// <summary>
/// 消息委托 /// 消息委托