From 1865621a6ae63a594ba54878619893ca6ad665ce Mon Sep 17 00:00:00 2001 From: YFdyh000 Date: Sun, 15 Mar 2020 18:22:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E9=80=9F=E4=B8=8D=E5=86=8D=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E7=8E=B0=E6=9C=89=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 代码整理和简化 --- v2rayN/v2rayN/Forms/MainForm.cs | 4 +- v2rayN/v2rayN/Handler/DownloadHandle.cs | 2 +- v2rayN/v2rayN/Handler/SpeedtestHandler.cs | 77 ++++------ v2rayN/v2rayN/Handler/V2rayConfigHandler.cs | 15 +- v2rayN/v2rayN/Handler/V2rayHandler.cs | 155 +++++++++++++++----- 5 files changed, 157 insertions(+), 96 deletions(-) diff --git a/v2rayN/v2rayN/Forms/MainForm.cs b/v2rayN/v2rayN/Forms/MainForm.cs index 259b771f..af1c8fdd 100644 --- a/v2rayN/v2rayN/Forms/MainForm.cs +++ b/v2rayN/v2rayN/Forms/MainForm.cs @@ -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); } })); } diff --git a/v2rayN/v2rayN/Handler/DownloadHandle.cs b/v2rayN/v2rayN/Handler/DownloadHandle.cs index a1ecde86..bf99a25f 100644 --- a/v2rayN/v2rayN/Handler/DownloadHandle.cs +++ b/v2rayN/v2rayN/Handler/DownloadHandle.cs @@ -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 { diff --git a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs index cbfc165a..88dedf27 100644 --- a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs +++ b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs @@ -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) { diff --git a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs index 2f04b955..9ea93933 100644 --- a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs @@ -1411,7 +1411,7 @@ namespace v2rayN.Handler #region Gen speedtest config - public static int GenerateClientSpeedtestConfig(Config config, List selecteds, string fileName, out string msg) + public static string GenerateClientSpeedtestConfigString(Config config, List 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(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 diff --git a/v2rayN/v2rayN/Handler/V2rayHandler.cs b/v2rayN/v2rayN/Handler/V2rayHandler.cs index 7769d2c8..0ccdb651 100644 --- a/v2rayN/v2rayN/Handler/V2rayHandler.cs +++ b/v2rayN/v2rayN/Handler/V2rayHandler.cs @@ -56,23 +56,25 @@ namespace v2rayN.Handler } /// - /// 载入V2ray + /// 新建进程,载入V2ray配置文件字符串 + /// 返回新进程pid。 /// - public void LoadV2ray(Config config, List _selecteds) + public int LoadV2rayConfigString(Config config, List _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; } /// @@ -140,6 +142,43 @@ namespace v2rayN.Handler Utils.SaveLog(ex.Message, ex); } } + /// + /// V2ray停止 + /// + 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; + } /// /// 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); } } + /// + /// V2ray启动,新建进程,传入配置字符串 + /// + 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; + } + } /// /// 消息委托