Merge pull request #567 from yfdyh000/pr5

功能和代码优化
pull/577/head
2dust 2020-03-20 09:23:05 +08:00 committed by GitHub
commit 48218a3dad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 902 additions and 130 deletions

View File

@ -35,25 +35,32 @@ namespace v2rayN.Base
private void StartListen()
{
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
Utils.SaveLog("WebserverB running...");
while (true)
try
{
if (!listener.Pending())
{
Thread.Sleep(100);
continue;
}
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
Utils.SaveLog("WebserverB running...");
TcpClient socket = listener.AcceptTcpClient();
Thread thread = new Thread(new ParameterizedThreadStart(ProcessThread))
while (true)
{
IsBackground = true
};
thread.Start(socket);
Thread.Sleep(1);
if (!listener.Pending())
{
Thread.Sleep(100);
continue;
}
TcpClient socket = listener.AcceptTcpClient();
Thread thread = new Thread(new ParameterizedThreadStart(ProcessThread))
{
IsBackground = true
};
thread.Start(socket);
Thread.Sleep(1);
}
}
catch
{
Utils.SaveLog("WebserverB start fail.");
}
}
private void ProcessThread(object obj)

View File

@ -17,17 +17,7 @@ namespace v2rayN.Base
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request;
if (address.Scheme == "https")
{
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => { return true; };
request = (HttpWebRequest)base.GetWebRequest(address);
//request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = (HttpWebRequest)base.GetWebRequest(address);
}
request = (HttpWebRequest)base.GetWebRequest(address);
request.Timeout = Timeout;
request.ReadWriteTimeout = Timeout;
//request.AllowAutoRedirect = false;

View File

@ -153,7 +153,4 @@
<data name="menuItemImportClipboard.Text" xml:space="preserve">
<value>从剪贴板导入URL</value>
</data>
<data name="menuItemScanScreen.Text" xml:space="preserve">
<value>扫描屏幕上的二维码</value>
</data>
</root>

View File

@ -76,6 +76,7 @@
this.menuAddServers2 = new System.Windows.Forms.ToolStripMenuItem();
this.menuScanScreen2 = new System.Windows.Forms.ToolStripMenuItem();
this.menuCopyPACUrl = new System.Windows.Forms.ToolStripMenuItem();
this.menuUpdateSubscriptions = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.menuExit = new System.Windows.Forms.ToolStripMenuItem();
this.bgwScan = new System.ComponentModel.BackgroundWorker();
@ -103,6 +104,7 @@
this.toolStripSeparator8 = new System.Windows.Forms.ToolStripSeparator();
this.tsbOptionSetting = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
this.tsbTestMe = new System.Windows.Forms.ToolStripButton();
this.tsbReload = new System.Windows.Forms.ToolStripButton();
this.toolStripSeparator7 = new System.Windows.Forms.ToolStripSeparator();
this.tsbCheckUpdate = new System.Windows.Forms.ToolStripDropDownButton();
@ -388,6 +390,7 @@
this.menuAddServers2,
this.menuScanScreen2,
this.menuCopyPACUrl,
this.menuUpdateSubscriptions,
this.toolStripSeparator2,
this.menuExit});
this.cmsMain.Name = "contextMenuStrip1";
@ -473,6 +476,12 @@
resources.ApplyResources(this.menuCopyPACUrl, "menuCopyPACUrl");
this.menuCopyPACUrl.Click += new System.EventHandler(this.menuCopyPACUrl_Click);
//
// menuUpdateSubscriptions
//
this.menuUpdateSubscriptions.Name = "menuUpdateSubscriptions";
resources.ApplyResources(this.menuUpdateSubscriptions, "menuUpdateSubscriptions");
this.menuUpdateSubscriptions.Click += new System.EventHandler(this.menuUpdateSubscriptions_Click);
//
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
@ -606,6 +615,7 @@
this.toolStripSeparator8,
this.tsbOptionSetting,
this.toolStripSeparator5,
this.tsbTestMe,
this.tsbReload,
this.toolStripSeparator7,
this.tsbCheckUpdate,
@ -660,6 +670,13 @@
this.toolStripSeparator5.Name = "toolStripSeparator5";
resources.ApplyResources(this.toolStripSeparator5, "toolStripSeparator5");
//
// tsbTestMe
//
this.tsbTestMe.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.tsbTestMe.Name = "tsbTestMe";
resources.ApplyResources(this.tsbTestMe, "tsbTestMe");
this.tsbTestMe.Click += new System.EventHandler(this.tsbTestMe_Click);
//
// tsbReload
//
resources.ApplyResources(this.tsbReload, "tsbReload");
@ -811,7 +828,7 @@
}
#endregion
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
@ -901,7 +918,9 @@
private System.Windows.Forms.ToolStripMenuItem menuRealPingServer;
private System.Windows.Forms.ToolStripMenuItem menuNotEnabledHttp;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator13;
private System.Windows.Forms.ToolStripMenuItem menuUpdateSubscriptions;
private System.Windows.Forms.ToolStripMenuItem tsbV2rayWebsite;
private System.Windows.Forms.ToolStripButton tsbTestMe;
private System.Windows.Forms.ToolStripMenuItem menuKeepNothing;
private System.Windows.Forms.ToolStripMenuItem menuKeepPACNothing;
}

View File

@ -9,6 +9,8 @@ using v2rayN.Mode;
using v2rayN.Base;
using v2rayN.Tool;
using System.Diagnostics;
using System.Drawing;
using System.Net;
namespace v2rayN.Forms
{
@ -239,6 +241,16 @@ namespace v2rayN.Forms
item.testResult
});
}
if (k % 2 == 1) // 隔行着色
{
lvItem.BackColor = Color.WhiteSmoke;
}
if (config.index.Equals(k))
{
//lvItem.Checked = true;
lvItem.ForeColor = Color.Blue;
lvItem.Font = new Font(lvItem.Font, FontStyle.Bold);
}
if (lvItem != null) lvServers.Items.Add(lvItem);
}
@ -322,10 +334,12 @@ namespace v2rayN.Forms
toolSslSocksPort.Text = $"{Global.Loopback}:{config.inbound[0].localPort}";
if (config.listenerType != 0)
if (config.listenerType != (int)ListenerType.noHttpProxy)
{
toolSslHttpPort.Text = $"{Global.Loopback}:{Global.httpPort}";
if (config.listenerType % 2 == 0)
if (config.listenerType == ListenerType.GlobalPac ||
config.listenerType == ListenerType.PacOpenAndClear ||
config.listenerType == ListenerType.PacOpenOnly)
{
if (PACServerHandle.IsRunning)
{
@ -640,7 +654,7 @@ namespace v2rayN.Forms
}
private void Speedtest(string actionType)
{
GetLvSelectedIndex();
if (GetLvSelectedIndex() < 0) return;
ClearTestResult();
SpeedtestHandler statistics = new SpeedtestHandler(ref config, ref v2rayHandler, lvSelecteds, actionType, UpdateSpeedtestHandler);
}
@ -871,6 +885,11 @@ namespace v2rayN.Forms
return counter;
}
private void menuUpdateSubscriptions_Click(object sender, EventArgs e)
{
UpdateSubscriptionProcess();
}
#endregion
@ -1116,41 +1135,41 @@ namespace v2rayN.Forms
private void menuNotEnabledHttp_Click(object sender, EventArgs e)
{
SetListenerType(0);
SetListenerType(ListenerType.noHttpProxy);
}
private void menuGlobal_Click(object sender, EventArgs e)
{
SetListenerType(1);
SetListenerType(ListenerType.GlobalHttp);
}
private void menuGlobalPAC_Click(object sender, EventArgs e)
{
SetListenerType(2);
SetListenerType(ListenerType.GlobalPac);
}
private void menuKeep_Click(object sender, EventArgs e)
{
SetListenerType(3);
SetListenerType(ListenerType.HttpOpenAndClear);
}
private void menuKeepPAC_Click(object sender, EventArgs e)
{
SetListenerType(4);
SetListenerType(ListenerType.PacOpenAndClear);
}
private void menuKeepNothing_Click(object sender, EventArgs e)
{
SetListenerType(5);
SetListenerType(ListenerType.HttpOpenOnly);
}
private void menuKeepPACNothing_Click(object sender, EventArgs e)
{
SetListenerType(6);
SetListenerType(ListenerType.PacOpenOnly);
}
private void SetListenerType(int type)
private void SetListenerType(ListenerType type)
{
config.listenerType = type;
ChangePACButtonStatus(type);
}
private void ChangePACButtonStatus(int type)
private void ChangePACButtonStatus(ListenerType type)
{
if (type != 0)
if (type != ListenerType.noHttpProxy)
{
HttpProxyHandle.RestartHttpAgent(config, false);
}
@ -1162,7 +1181,7 @@ namespace v2rayN.Forms
for (int k = 0; k < menuSysAgentMode.DropDownItems.Count; k++)
{
ToolStripMenuItem item = ((ToolStripMenuItem)menuSysAgentMode.DropDownItems[k]);
item.Checked = (type == k);
item.Checked = ((int)type == k);
}
ConfigHandler.SaveConfig(ref config, false);
@ -1174,6 +1193,22 @@ namespace v2rayN.Forms
#region CheckUpdate
private void askToDownload(DownloadHandle downloadHandle, string url)
{
if (UI.ShowYesNo(string.Format(UIRes.I18N("DownloadYesNo"), url)) == DialogResult.Yes)
{
if (httpProxyTest() > 0)
{
int httpPort = config.GetLocalPort(Global.InboundHttp);
WebProxy webProxy = new WebProxy(Global.Loopback, httpPort);
downloadHandle.DownloadFileAsync(url, webProxy, 60);
}
else
{
downloadHandle.DownloadFileAsync(url, null, 60);
}
}
}
private void tsbCheckUpdateN_Click(object sender, EventArgs e)
{
//System.Diagnostics.Process.Start(Global.UpdateUrl);
@ -1190,15 +1225,7 @@ namespace v2rayN.Forms
string url = args.Msg;
this.Invoke((MethodInvoker)(delegate
{
if (UI.ShowYesNo(string.Format(UIRes.I18N("DownloadYesNo"), url)) == DialogResult.No)
{
return;
}
else
{
downloadHandle.DownloadFileAsync(url, null, -1);
}
askToDownload(downloadHandle, url);
}));
}
else
@ -1214,8 +1241,8 @@ namespace v2rayN.Forms
try
{
string fileName = Utils.GetPath(downloadHandle.DownloadFileName);
Process process = Process.Start("v2rayUpgrade.exe", fileName);
string fileName = Utils.GetPath(downloadHandle.DownloadFileName);
Process process = Process.Start("v2rayUpgrade.exe", "\"" + fileName + "\"");
if (process.Id > 0)
{
menuExit_Click(null, null);
@ -1256,15 +1283,7 @@ namespace v2rayN.Forms
string url = args.Msg;
this.Invoke((MethodInvoker)(delegate
{
if (UI.ShowYesNo(string.Format(UIRes.I18N("DownloadYesNo"), url)) == DialogResult.No)
{
return;
}
else
{
downloadHandle.DownloadFileAsync(url, null, -1);
}
askToDownload(downloadHandle, url);
}));
}
else
@ -1415,6 +1434,14 @@ namespace v2rayN.Forms
}
private void tsbSubUpdate_Click(object sender, EventArgs e)
{
UpdateSubscriptionProcess();
}
/// <summary>
/// the subscription update process
/// </summary>
private void UpdateSubscriptionProcess()
{
AppendText(false, UIRes.I18N("MsgUpdateSubscriptionStart"));
@ -1505,5 +1532,16 @@ namespace v2rayN.Forms
{
Process.Start(Global.v2rayWebsiteUrl);
}
private void tsbTestMe_Click(object sender, EventArgs e)
{
string result = httpProxyTest() + "ms";
AppendText(false, string.Format(UIRes.I18N("TestMeOutput"), result));
}
private int httpProxyTest()
{
SpeedtestHandler statistics = new SpeedtestHandler(ref config, ref v2rayHandler, lvSelecteds, "", UpdateSpeedtestHandler);
return statistics.RunAvailabilityCheck();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -363,6 +363,9 @@
<data name="menuTcpingServer.Text" xml:space="preserve">
<value>测试服务器延迟Tcping(多选) (Ctrl+O)</value>
</data>
<data name="menuUpdateSubscriptions.Text" xml:space="preserve">
<value>更新订阅</value>
</data>
<data name="toolSslServerSpeed.Text" xml:space="preserve">
<value>网速显示未启用</value>
</data>
@ -489,6 +492,9 @@
<data name="tsbSubUpdate.Text" xml:space="preserve">
<value>更新订阅</value>
</data>
<data name="tsbTestMe.Text" xml:space="preserve">
<value>测试状态</value>
</data>
<data name="tsbV2rayWebsite.Text" xml:space="preserve">
<value>V2Ray 官网</value>
</data>

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Windows.Forms;
using v2rayN.Handler;
using v2rayN.Base;
using v2rayN.HttpProxyHandler;
namespace v2rayN.Forms
{
@ -67,7 +68,7 @@ namespace v2rayN.Forms
//remoteDNS
txtremoteDNS.Text = config.remoteDNS;
cmblistenerType.SelectedIndex = config.listenerType;
cmblistenerType.SelectedIndex = (int)config.listenerType;
}
/// <summary>
@ -262,7 +263,8 @@ namespace v2rayN.Forms
//remoteDNS
config.remoteDNS = txtremoteDNS.Text.TrimEx();
config.listenerType = cmblistenerType.SelectedIndex;
config.listenerType = (ListenerType)Enum.ToObject(typeof(ListenerType), cmblistenerType.SelectedIndex);
return 0;
}

View File

@ -16,6 +16,7 @@ namespace v2rayN
/// </summary>
public const string SpeedTestUrl = @"http://speedtest-sgp1.digitalocean.com/10mb.test";
public const string SpeedPingTestUrl = @"https://www.google.com/generate_204";
public const string AvailabilityTestUrl = @"https://www.google.com/generate_204";
/// <summary>
/// CustomRoutingListUrl

View File

@ -57,7 +57,7 @@ namespace v2rayN.Handler
private readonly string coreLatestUrl = "https://github.com/v2ray/v2ray-core/releases/latest";
private const string coreUrl = "https://github.com/v2ray/v2ray-core/releases/download/{0}/v2ray-windows-{1}.zip";
public async Task CheckUpdateAsync(string type)
public async void CheckUpdateAsync(string type)
{
Utils.SetSecurityProtocol();
WebRequestHandler webRequestHandler = new WebRequestHandler

View File

@ -32,7 +32,7 @@ namespace v2rayN.Handler
try
{
Color color = ColorTranslator.FromHtml("#3399CC");
int index = config.listenerType;
int index = (int)config.listenerType;
if (index > 0)
{
color = (new Color[] { Color.Red, Color.Purple, Color.DarkGreen, Color.Orange, Color.DarkSlateBlue, Color.RoyalBlue })[index - 1];

View File

@ -150,6 +150,37 @@ namespace v2rayN.Handler
}
}
public int RunAvailabilityCheck() // alias: isLive
{
try
{
int httpPort = _config.GetLocalPort(Global.InboundHttp);
Task<int> t = Task.Run(() =>
{
try
{
WebProxy webProxy = new WebProxy(Global.Loopback, httpPort);
int responseTime = -1;
string status = GetRealPingTime(Global.AvailabilityTestUrl, webProxy, out responseTime);
bool noError = Utils.IsNullOrEmpty(status);
return noError ? responseTime : -1;
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
return -1;
}
});
return t.Result;
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
return -1;
}
}
private void RunSpeedTest()
{
int pid = -1;

View File

@ -3,6 +3,19 @@ using v2rayN.Mode;
namespace v2rayN.HttpProxyHandler
{
/// <summary>
/// 系统代理(http)模式
/// </summary>
public enum ListenerType
{
noHttpProxy = 0,
GlobalHttp = 1,
GlobalPac = 2,
HttpOpenAndClear = 3,
PacOpenAndClear = 4,
HttpOpenOnly = 5,
PacOpenOnly = 6
}
/// <summary>
/// 系统代理(http)总处理
/// 启动privoxy提供http协议
@ -12,29 +25,29 @@ namespace v2rayN.HttpProxyHandler
{
private static bool Update(Config config, bool forceDisable)
{
int type = config.listenerType;
ListenerType type = config.listenerType;
if (forceDisable)
{
type = 0;
type = ListenerType.noHttpProxy;
}
try
{
if (type != 0)
if (type != ListenerType.noHttpProxy)
{
int port = Global.httpPort;
if (port <= 0)
{
return false;
}
if (type == 1)
if (type == ListenerType.GlobalHttp)
{
//PACServerHandle.Stop();
//ProxySetting.SetProxy($"{Global.Loopback}:{port}", Global.IEProxyExceptions, 2);
SysProxyHandle.SetIEProxy(true, true, $"{Global.Loopback}:{port}");
}
else if (type == 2)
else if (type == ListenerType.GlobalPac)
{
string pacUrl = GetPacUrl();
//ProxySetting.SetProxy(pacUrl, "", 4);
@ -42,24 +55,24 @@ namespace v2rayN.HttpProxyHandler
//PACServerHandle.Stop();
PACServerHandle.Init(config);
}
else if (type == 3)
else if (type == ListenerType.HttpOpenAndClear)
{
//PACServerHandle.Stop();
SysProxyHandle.ResetIEProxy();
}
else if (type == 4)
else if (type == ListenerType.PacOpenAndClear)
{
string pacUrl = GetPacUrl();
SysProxyHandle.ResetIEProxy();
//PACServerHandle.Stop();
PACServerHandle.Init(config);
}
else if (type == 5)
else if (type == ListenerType.HttpOpenOnly)
{
//PACServerHandle.Stop();
//SysProxyHandle.ResetIEProxy();
}
else if (type == 6)
else if (type == ListenerType.PacOpenOnly)
{
string pacUrl = GetPacUrl();
//SysProxyHandle.ResetIEProxy();
@ -114,7 +127,7 @@ namespace v2rayN.HttpProxyHandler
{
try
{
if (config.listenerType != 5 && config.listenerType != 6)
if (config.listenerType != ListenerType.HttpOpenOnly && config.listenerType != ListenerType.PacOpenOnly)
{
Update(config, true);
}
@ -138,7 +151,7 @@ namespace v2rayN.HttpProxyHandler
public static void RestartHttpAgent(Config config, bool forced)
{
bool isRestart = false;
if (config.listenerType == 0)
if (config.listenerType == ListenerType.noHttpProxy)
{
// 关闭http proxy时直接返回
return;

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using v2rayN.Base;
using v2rayN.HttpProxyHandler;
namespace v2rayN.Mode
{
@ -107,9 +109,9 @@ namespace v2rayN.Mode
}
/// <summary>
/// 监听状态 0-not 1-http 2-PAC
/// 监听状态
/// </summary>
public int listenerType
public ListenerType listenerType
{
get; set;
}

View File

@ -780,5 +780,14 @@ namespace v2rayN.Resx {
return ResourceManager.GetString("SuccessfullyImportedServerViaScan", resourceCulture);
}
}
/// <summary>
/// 查找类似 The ping of current service: {0} 的本地化字符串。
/// </summary>
internal static string TestMeOutput {
get {
return ResourceManager.GetString("TestMeOutput", resourceCulture);
}
}
}
}

View File

@ -264,12 +264,12 @@
<data name="MsgStartGettingSubscriptions" xml:space="preserve">
<value>Start getting subscriptions</value>
</data>
<data name="MsgStartUpdatingPAC" xml:space="preserve">
<value>Start updating PAC...</value>
</data>
<data name="MsgStartUpdating" xml:space="preserve">
<value>Start updating {0}...</value>
</data>
<data name="MsgStartUpdatingPAC" xml:space="preserve">
<value>Start updating PAC...</value>
</data>
<data name="MsgSubscriptionDecodingFailed" xml:space="preserve">
<value>Subscription content decoding failed (non-BASE64 code)</value>
</data>
@ -358,4 +358,7 @@
<data name="SuccessfullyImportedServerViaScan" xml:space="preserve">
<value>Scan import URL successfully</value>
</data>
<data name="TestMeOutput" xml:space="preserve">
<value>The ping of current service: {0}</value>
</data>
</root>

View File

@ -264,12 +264,12 @@
<data name="MsgStartGettingSubscriptions" xml:space="preserve">
<value>开始获取订阅内容</value>
</data>
<data name="MsgStartUpdatingPAC" xml:space="preserve">
<value>开始更新 PAC...</value>
</data>
<data name="MsgStartUpdating" xml:space="preserve">
<value>开始更新 {0}...</value>
</data>
<data name="MsgStartUpdatingPAC" xml:space="preserve">
<value>开始更新 PAC...</value>
</data>
<data name="MsgSubscriptionDecodingFailed" xml:space="preserve">
<value>订阅内容解码失败(非BASE64码)</value>
</data>
@ -358,4 +358,7 @@
<data name="SuccessfullyImportedServerViaScan" xml:space="preserve">
<value>扫描导入URL成功</value>
</data>
<data name="TestMeOutput" xml:space="preserve">
<value>当前服务的访问延迟: {0}</value>
</data>
</root>

View File

@ -804,21 +804,14 @@ namespace v2rayN
public static string UnGzip(byte[] buf)
{
byte[] buffer = new byte[1024];
int n;
using (MemoryStream sb = new MemoryStream())
MemoryStream sb = new MemoryStream();
using (GZipStream input = new GZipStream(new MemoryStream(buf),
CompressionMode.Decompress,
false))
{
using (GZipStream input = new GZipStream(new MemoryStream(buf),
CompressionMode.Decompress,
false))
{
while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
{
sb.Write(buffer, 0, n);
}
}
return Encoding.UTF8.GetString(sb.ToArray());
input.CopyTo(sb);
}
return Encoding.UTF8.GetString(sb.ToArray());
}
#endregion

View File

@ -41,8 +41,9 @@ namespace v2rayUpgrade
}
catch (Exception ex)
{
showWarn("Failed to close v2rayN(关闭v2rayN失败)." + ex.StackTrace);
return;
// Access may be denied without admin right. The user may not be an administrator.
showWarn("Failed to close v2rayN(关闭v2rayN失败).\n" +
"Close it manually, or the upgrade may fail.(请手动关闭正在运行的v2rayN否则可能升级失败。\n\n" + ex.StackTrace);
}
try