Clean code

This commit is contained in:
2dust 2019-10-18 14:49:42 +08:00
parent 9224ea9819
commit d5c69c7838
21 changed files with 55 additions and 121 deletions

View file

@ -3,7 +3,7 @@ using System.Net;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
namespace v2rayN.HttpProxyHandler namespace v2rayN.Base
{ {
public class HttpWebServer public class HttpWebServer
{ {

View file

@ -1,12 +1,10 @@
using System; using System;
using System.Collections;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text;
using System.Threading; using System.Threading;
namespace v2rayN.HttpProxyHandler namespace v2rayN.Base
{ {
public class HttpWebServerB public class HttpWebServerB
{ {

View file

@ -1,7 +1,7 @@
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
namespace v2rayN.Forms namespace v2rayN.Base
{ {
class ListViewFlickerFree : ListView class ListViewFlickerFree : ListView
{ {

View file

@ -2,7 +2,7 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
namespace v2rayN namespace v2rayN.Base
{ {
static class StringEx static class StringEx
{ {

View file

@ -1,7 +1,7 @@
using System; using System;
using System.Net; using System.Net;
namespace v2rayN.HttpProxyHandler namespace v2rayN.Base
{ {
class WebClientEx : WebClient class WebClientEx : WebClient
{ {

View file

@ -31,7 +31,7 @@
this.components = new System.ComponentModel.Container(); this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.lvServers = new v2rayN.Forms.ListViewFlickerFree(); this.lvServers = new v2rayN.Base.ListViewFlickerFree();
this.cmsLv = new System.Windows.Forms.ContextMenuStrip(this.components); this.cmsLv = new System.Windows.Forms.ContextMenuStrip(this.components);
this.menuAddVmessServer = new System.Windows.Forms.ToolStripMenuItem(); this.menuAddVmessServer = new System.Windows.Forms.ToolStripMenuItem();
this.menuAddShadowsocksServer = new System.Windows.Forms.ToolStripMenuItem(); this.menuAddShadowsocksServer = new System.Windows.Forms.ToolStripMenuItem();
@ -784,7 +784,7 @@
private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TextBox txtMsgBox; private System.Windows.Forms.TextBox txtMsgBox;
private v2rayN.Forms.ListViewFlickerFree lvServers; private v2rayN.Base.ListViewFlickerFree lvServers;
private System.Windows.Forms.NotifyIcon notifyMain; private System.Windows.Forms.NotifyIcon notifyMain;
private System.Windows.Forms.ContextMenuStrip cmsMain; private System.Windows.Forms.ContextMenuStrip cmsMain;
private System.Windows.Forms.ToolStripMenuItem menuExit; private System.Windows.Forms.ToolStripMenuItem menuExit;

View file

@ -8,6 +8,7 @@ using System.Windows.Forms;
using v2rayN.Handler; using v2rayN.Handler;
using v2rayN.HttpProxyHandler; using v2rayN.HttpProxyHandler;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Base;
namespace v2rayN.Forms namespace v2rayN.Forms
{ {
@ -15,7 +16,7 @@ namespace v2rayN.Forms
{ {
private V2rayHandler v2rayHandler; private V2rayHandler v2rayHandler;
private PACListHandle pacListHandle; private PACListHandle pacListHandle;
private V2rayUpdateHandle v2rayUpdateHandle; private DownloadHandle downloadHandle;
private List<int> lvSelecteds = new List<int>(); private List<int> lvSelecteds = new List<int>();
private StatisticsHandler statistics = null; private StatisticsHandler statistics = null;
@ -1279,10 +1280,10 @@ namespace v2rayN.Forms
private void tsbCheckUpdateCore_Click(object sender, EventArgs e) private void tsbCheckUpdateCore_Click(object sender, EventArgs e)
{ {
if (v2rayUpdateHandle == null) if (downloadHandle == null)
{ {
v2rayUpdateHandle = new V2rayUpdateHandle(); downloadHandle = new DownloadHandle();
v2rayUpdateHandle.AbsoluteCompleted += (sender2, args) => downloadHandle.AbsoluteCompleted += (sender2, args) =>
{ {
if (args.Success) if (args.Success)
{ {
@ -1298,7 +1299,7 @@ namespace v2rayN.Forms
} }
else else
{ {
v2rayUpdateHandle.DownloadFileAsync(config, url, false); downloadHandle.DownloadFileAsync(config, url, false);
} }
})); }));
} }
@ -1307,7 +1308,7 @@ namespace v2rayN.Forms
AppendText(false, args.Msg); AppendText(false, args.Msg);
} }
}; };
v2rayUpdateHandle.UpdateCompleted += (sender2, args) => downloadHandle.UpdateCompleted += (sender2, args) =>
{ {
if (args.Success) if (args.Success)
{ {
@ -1318,7 +1319,7 @@ namespace v2rayN.Forms
{ {
CloseV2ray(); CloseV2ray();
string fileName = v2rayUpdateHandle.DownloadFileName; string fileName = downloadHandle.DownloadFileName;
fileName = Utils.GetPath(fileName); fileName = Utils.GetPath(fileName);
using (ZipArchive archive = ZipFile.OpenRead(fileName)) using (ZipArchive archive = ZipFile.OpenRead(fileName))
{ {
@ -1346,14 +1347,14 @@ namespace v2rayN.Forms
AppendText(false, args.Msg); AppendText(false, args.Msg);
} }
}; };
v2rayUpdateHandle.Error += (sender2, args) => downloadHandle.Error += (sender2, args) =>
{ {
AppendText(true, args.GetException().Message); AppendText(true, args.GetException().Message);
}; };
} }
AppendText(false, UIRes.I18N("MsgStartUpdatingV2rayCore")); AppendText(false, UIRes.I18N("MsgStartUpdatingV2rayCore"));
v2rayUpdateHandle.AbsoluteV2rayCore(config); downloadHandle.AbsoluteV2rayCore(config);
} }
private void tsbCheckUpdatePACList_Click(object sender, EventArgs e) private void tsbCheckUpdatePACList_Click(object sender, EventArgs e)
@ -1473,8 +1474,8 @@ namespace v2rayN.Forms
continue; continue;
} }
V2rayUpdateHandle v2rayUpdateHandle3 = new V2rayUpdateHandle(); DownloadHandle downloadHandle3 = new DownloadHandle();
v2rayUpdateHandle3.UpdateCompleted += (sender2, args) => downloadHandle3.UpdateCompleted += (sender2, args) =>
{ {
if (args.Success) if (args.Success)
{ {
@ -1503,12 +1504,12 @@ namespace v2rayN.Forms
AppendText(false, args.Msg); AppendText(false, args.Msg);
} }
}; };
v2rayUpdateHandle3.Error += (sender2, args) => downloadHandle3.Error += (sender2, args) =>
{ {
AppendText(true, args.GetException().Message); AppendText(true, args.GetException().Message);
}; };
v2rayUpdateHandle3.WebDownloadString(url); downloadHandle3.WebDownloadString(url);
AppendText(false, $"{hashCode}{UIRes.I18N("MsgStartGettingSubscriptions")}"); AppendText(false, $"{hashCode}{UIRes.I18N("MsgStartGettingSubscriptions")}");
} }

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows.Forms; using System.Windows.Forms;
using v2rayN.Handler; using v2rayN.Handler;
using v2rayN.Base;
namespace v2rayN.Forms namespace v2rayN.Forms
{ {
@ -381,8 +382,8 @@ namespace v2rayN.Forms
for (int k = 0; k < lstUrl.Count; k++) for (int k = 0; k < lstUrl.Count; k++)
{ {
var txt = lstTxt[k]; var txt = lstTxt[k];
V2rayUpdateHandle v2rayUpdateHandle3 = new V2rayUpdateHandle(); DownloadHandle downloadHandle = new DownloadHandle();
v2rayUpdateHandle3.UpdateCompleted += (sender2, args) => downloadHandle.UpdateCompleted += (sender2, args) =>
{ {
if (args.Success) if (args.Success)
{ {
@ -398,12 +399,12 @@ namespace v2rayN.Forms
AppendText(false, args.Msg); AppendText(false, args.Msg);
} }
}; };
v2rayUpdateHandle3.Error += (sender2, args) => downloadHandle.Error += (sender2, args) =>
{ {
AppendText(true, args.GetException().Message); AppendText(true, args.GetException().Message);
}; };
v2rayUpdateHandle3.WebDownloadString(lstUrl[k]); downloadHandle.WebDownloadString(lstUrl[k]);
} }
} }
void AppendText(bool notify, string text) void AppendText(bool notify, string text)

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Windows.Forms; using System.Windows.Forms;
using v2rayN.Base;
using v2rayN.Mode; using v2rayN.Mode;
namespace v2rayN.Forms namespace v2rayN.Forms

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Net;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Base;
namespace v2rayN.Handler namespace v2rayN.Handler
{ {

View file

@ -1,20 +1,15 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Text; using v2rayN.Base;
using Newtonsoft.Json;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Properties;
using v2rayN.HttpProxyHandler;
using System.Diagnostics;
namespace v2rayN.Handler namespace v2rayN.Handler
{ {
/// <summary> /// <summary>
///Update V2ray Core ///Download
/// </summary> /// </summary>
class V2rayUpdateHandle class DownloadHandle
{ {
public event EventHandler<ResultEventArgs> AbsoluteCompleted; public event EventHandler<ResultEventArgs> AbsoluteCompleted;

View file

@ -11,7 +11,7 @@ namespace v2rayN.Handler
{ {
class SpeedtestHandler class SpeedtestHandler
{ {
private V2rayUpdateHandle v2rayUpdateHandle2; private DownloadHandle downloadHandle2;
private Config _config; private Config _config;
private V2rayHandler _v2rayHandler; private V2rayHandler _v2rayHandler;
private List<int> _selecteds; private List<int> _selecteds;
@ -186,10 +186,10 @@ namespace v2rayN.Handler
string url = Global.SpeedTestUrl; string url = Global.SpeedTestUrl;
testCounter = 0; testCounter = 0;
if (v2rayUpdateHandle2 == null) if (downloadHandle2 == null)
{ {
v2rayUpdateHandle2 = new V2rayUpdateHandle(); downloadHandle2 = new DownloadHandle();
v2rayUpdateHandle2.UpdateCompleted += (sender2, args) => downloadHandle2.UpdateCompleted += (sender2, args) =>
{ {
if (args.Success) if (args.Success)
{ {
@ -204,7 +204,7 @@ namespace v2rayN.Handler
_updateFunc(ItemIndex, args.Msg); _updateFunc(ItemIndex, args.Msg);
} }
}; };
v2rayUpdateHandle2.Error += (sender2, args) => downloadHandle2.Error += (sender2, args) =>
{ {
_updateFunc(ItemIndex, args.GetException().Message); _updateFunc(ItemIndex, args.GetException().Message);
if (ServerSpeedTestSub(testCounter, url) != 0) if (ServerSpeedTestSub(testCounter, url) != 0)
@ -232,7 +232,7 @@ namespace v2rayN.Handler
testCounter++; testCounter++;
v2rayUpdateHandle2.DownloadFileAsync(_config, url,true); downloadHandle2.DownloadFileAsync(_config, url,true);
return 0; return 0;
} }

View file

@ -1,10 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using v2rayN.Mode;
using System.Net;
using System.Text;
using System.Linq; using System.Linq;
using System.Net;
using v2rayN.Base;
using v2rayN.Mode;
namespace v2rayN.Handler namespace v2rayN.Handler
{ {

View file

@ -10,21 +10,6 @@ namespace v2rayN.HttpProxyHandler
/// </summary> /// </summary>
class HttpProxyHandle class HttpProxyHandle
{ {
private static string GetTimestamp(DateTime value)
{
return value.ToString("MMddHHmmssfff");
}
public static void ReSetPACProxy(Config config)
{
if (config.listenerType == 2)
{
//SysProxyHandle.SetIEProxy(false, false, null, null);
//PACServerHandle.Stop();
}
Update(config, false);
}
public static bool Update(Config config, bool forceDisable) public static bool Update(Config config, bool forceDisable)
{ {
int type = config.listenerType; int type = config.listenerType;
@ -46,7 +31,6 @@ namespace v2rayN.HttpProxyHandler
if (type == 1) if (type == 1)
{ {
PACServerHandle.Stop(); PACServerHandle.Stop();
PACFileWatcherHandle.StopWatch();
SysProxyHandle.SetIEProxy(true, true, $"{Global.Loopback}:{port}", null); SysProxyHandle.SetIEProxy(true, true, $"{Global.Loopback}:{port}", null);
} }
else if (type == 2) else if (type == 2)
@ -55,12 +39,10 @@ namespace v2rayN.HttpProxyHandler
SysProxyHandle.SetIEProxy(true, false, null, pacUrl); SysProxyHandle.SetIEProxy(true, false, null, pacUrl);
PACServerHandle.Stop(); PACServerHandle.Stop();
PACServerHandle.Init(config); PACServerHandle.Init(config);
PACFileWatcherHandle.StartWatch(config);
} }
else if (type == 3) else if (type == 3)
{ {
PACServerHandle.Stop(); PACServerHandle.Stop();
PACFileWatcherHandle.StopWatch();
SysProxyHandle.SetIEProxy(false, false, null, null); SysProxyHandle.SetIEProxy(false, false, null, null);
} }
else if (type == 4) else if (type == 4)
@ -69,14 +51,12 @@ namespace v2rayN.HttpProxyHandler
SysProxyHandle.SetIEProxy(false, false, null, null); SysProxyHandle.SetIEProxy(false, false, null, null);
PACServerHandle.Stop(); PACServerHandle.Stop();
PACServerHandle.Init(config); PACServerHandle.Init(config);
PACFileWatcherHandle.StartWatch(config);
} }
} }
else else
{ {
SysProxyHandle.SetIEProxy(false, false, null, null); SysProxyHandle.SetIEProxy(false, false, null, null);
PACServerHandle.Stop(); PACServerHandle.Stop();
PACFileWatcherHandle.StopWatch();
} }
} }
catch (Exception ex) catch (Exception ex)
@ -168,9 +148,7 @@ namespace v2rayN.HttpProxyHandler
public static string GetPacUrl() public static string GetPacUrl()
{ {
string pacUrl = $"http://{Global.Loopback}:{Global.pacPort}/pac/?t={GetTimestamp(DateTime.Now)}"; string pacUrl = $"http://{Global.Loopback}:{Global.pacPort}/pac/?t={ DateTime.Now.ToString("HHmmss")}";
return pacUrl; return pacUrl;
} }
} }

View file

@ -1,45 +0,0 @@
using System.IO;
using System.Windows.Forms;
using v2rayN.Mode;
namespace v2rayN.HttpProxyHandler
{
/// <summary>
/// 提供PAC功能支持
/// </summary>
class PACFileWatcherHandle
{
private static FileSystemWatcher fileSystemWatcher;
private static long fileSize;
public static void StartWatch(Config config)
{
if (fileSystemWatcher == null)
{
fileSystemWatcher = new FileSystemWatcher(Utils.StartupPath());
fileSystemWatcher.Filter = "pac.txt";
fileSystemWatcher.NotifyFilter = NotifyFilters.Size;
fileSystemWatcher.Changed += (sender, args) =>
{
var fileInfo = new FileInfo(args.FullPath);
if (fileSize != fileInfo.Length)
{
fileSize = fileInfo.Length;
HttpProxyHandle.ReSetPACProxy(config);
}
};
}
fileSystemWatcher.EnableRaisingEvents = true;
}
public static void StopWatch()
{
if (fileSystemWatcher != null)
{
fileSystemWatcher.EnableRaisingEvents = false;
}
}
}
}

View file

@ -4,6 +4,7 @@ using System.IO;
using System.Net; using System.Net;
using System.Text; using System.Text;
using Newtonsoft.Json; using Newtonsoft.Json;
using v2rayN.Base;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Properties; using v2rayN.Properties;

View file

@ -5,6 +5,7 @@ using System.Text;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Properties; using v2rayN.Properties;
using v2rayN.Tool; using v2rayN.Tool;
using v2rayN.Base;
namespace v2rayN.HttpProxyHandler namespace v2rayN.HttpProxyHandler
{ {

View file

@ -1,8 +1,9 @@
using System; using Newtonsoft.Json;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Newtonsoft.Json; using v2rayN.Base;
using v2rayN.Mode; using v2rayN.Mode;
using v2rayN.Properties; using v2rayN.Properties;
using v2rayN.Tool; using v2rayN.Tool;

View file

@ -1,6 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using v2rayN.Base;
namespace v2rayN.Mode namespace v2rayN.Mode
{ {

View file

@ -18,6 +18,7 @@ using ZXing;
using ZXing.Common; using ZXing.Common;
using ZXing.QrCode; using ZXing.QrCode;
using System.Security.Principal; using System.Security.Principal;
using v2rayN.Base;
namespace v2rayN namespace v2rayN
{ {

View file

@ -114,6 +114,7 @@
<HintPath>LIB\System.Memory.dll</HintPath> <HintPath>LIB\System.Memory.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="System.Messaging" />
<Reference Include="System.Net" /> <Reference Include="System.Net" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
@ -140,7 +141,7 @@
<Compile Include="Forms\AddServer4Form.Designer.cs"> <Compile Include="Forms\AddServer4Form.Designer.cs">
<DependentUpon>AddServer4Form.cs</DependentUpon> <DependentUpon>AddServer4Form.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Forms\ListViewFlickerFree.cs"> <Compile Include="Base\ListViewFlickerFree.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>
<Compile Include="Forms\MainForm.cs"> <Compile Include="Forms\MainForm.cs">
@ -181,17 +182,16 @@
</Compile> </Compile>
<Compile Include="Handler\SpeedtestHandler.cs" /> <Compile Include="Handler\SpeedtestHandler.cs" />
<Compile Include="Handler\StatisticsHandler.cs" /> <Compile Include="Handler\StatisticsHandler.cs" />
<Compile Include="Handler\V2rayUpdateHandle.cs" /> <Compile Include="Handler\DownloadHandle.cs" />
<Compile Include="HttpProxyHandler\HttpWebServer.cs" /> <Compile Include="Base\HttpWebServer.cs" />
<Compile Include="HttpProxyHandler\HttpWebServerB.cs" /> <Compile Include="Base\HttpWebServerB.cs" />
<Compile Include="HttpProxyHandler\PACFileWatcherHandle.cs" />
<Compile Include="HttpProxyHandler\PrivoxyHandler.cs" /> <Compile Include="HttpProxyHandler\PrivoxyHandler.cs" />
<Compile Include="HttpProxyHandler\PACListHandle.cs" /> <Compile Include="HttpProxyHandler\PACListHandle.cs" />
<Compile Include="HttpProxyHandler\PACServerHandle.cs" /> <Compile Include="HttpProxyHandler\PACServerHandle.cs" />
<Compile Include="HttpProxyHandler\ProxySetting.cs" /> <Compile Include="HttpProxyHandler\ProxySetting.cs" />
<Compile Include="HttpProxyHandler\SysProxyHandle.cs" /> <Compile Include="HttpProxyHandler\SysProxyHandle.cs" />
<Compile Include="HttpProxyHandler\HttpProxyHandle.cs" /> <Compile Include="HttpProxyHandler\HttpProxyHandle.cs" />
<Compile Include="HttpProxyHandler\WebClientEx.cs"> <Compile Include="Base\WebClientEx.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>
<Compile Include="Mode\EMove.cs" /> <Compile Include="Mode\EMove.cs" />
@ -208,7 +208,7 @@
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<DependentUpon>ResUI.resx</DependentUpon> <DependentUpon>ResUI.resx</DependentUpon>
</Compile> </Compile>
<Compile Include="StringEx.cs"> <Compile Include="Base\StringEx.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Forms\AddServerForm.cs"> <Compile Include="Forms\AddServerForm.cs">