Clean code

pull/188/head
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.Threading;
namespace v2rayN.HttpProxyHandler
namespace v2rayN.Base
{
public class HttpWebServer
{

View File

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

View File

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

View File

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

View File

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

View File

@ -31,7 +31,7 @@
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
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.menuAddVmessServer = 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 groupBox2;
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.ContextMenuStrip cmsMain;
private System.Windows.Forms.ToolStripMenuItem menuExit;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,21 +10,6 @@ namespace v2rayN.HttpProxyHandler
/// </summary>
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)
{
int type = config.listenerType;
@ -46,7 +31,6 @@ namespace v2rayN.HttpProxyHandler
if (type == 1)
{
PACServerHandle.Stop();
PACFileWatcherHandle.StopWatch();
SysProxyHandle.SetIEProxy(true, true, $"{Global.Loopback}:{port}", null);
}
else if (type == 2)
@ -55,12 +39,10 @@ namespace v2rayN.HttpProxyHandler
SysProxyHandle.SetIEProxy(true, false, null, pacUrl);
PACServerHandle.Stop();
PACServerHandle.Init(config);
PACFileWatcherHandle.StartWatch(config);
}
else if (type == 3)
{
PACServerHandle.Stop();
PACFileWatcherHandle.StopWatch();
SysProxyHandle.SetIEProxy(false, false, null, null);
}
else if (type == 4)
@ -69,14 +51,12 @@ namespace v2rayN.HttpProxyHandler
SysProxyHandle.SetIEProxy(false, false, null, null);
PACServerHandle.Stop();
PACServerHandle.Init(config);
PACFileWatcherHandle.StartWatch(config);
}
}
else
{
SysProxyHandle.SetIEProxy(false, false, null, null);
PACServerHandle.Stop();
PACFileWatcherHandle.StopWatch();
}
}
catch (Exception ex)
@ -168,9 +148,7 @@ namespace v2rayN.HttpProxyHandler
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;
}
}

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.Text;
using Newtonsoft.Json;
using v2rayN.Base;
using v2rayN.Mode;
using v2rayN.Properties;

View File

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

View File

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

View File

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

View File

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

View File

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