diff --git a/Netch/Controllers/HTTPController.cs b/Netch/Controllers/HTTPController.cs
index bccd135a..b2de86b8 100644
--- a/Netch/Controllers/HTTPController.cs
+++ b/Netch/Controllers/HTTPController.cs
@@ -2,12 +2,13 @@ using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
-using Microsoft.Win32;
-using Netch.Models;
-using Netch.Utils;
using WindowsProxy;
+using Microsoft.Win32;
+using Netch.Forms;
+using Netch.Models;
using Netch.Servers.Socks5;
using Netch.Servers.Trojan;
+using Netch.Utils;
using Netch.Utils.HttpProxyHandler;
namespace Netch.Controllers
@@ -16,7 +17,7 @@ namespace Netch.Controllers
{
public const string IEProxyExceptions = "localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*";
- public PrivoxyController pPrivoxyController = new PrivoxyController();
+ public PrivoxyController pPrivoxyController = new();
private string prevBypass, prevHTTP, prevPAC;
private bool prevEnabled;
@@ -35,13 +36,11 @@ namespace Netch.Controllers
try
{
if (pPrivoxyController.Start(MainController.Server, mode))
- {
Global.Job.AddProcess(pPrivoxyController.Instance);
- }
if (mode.Type == 3)
{
- if ((MainController.Server is Socks5 or Trojan) && mode.BypassChina)
+ if (MainController.Server is Socks5 or Trojan && mode.BypassChina)
{
//启动PAC服务器
PACServerHandle.InitPACServer("127.0.0.1");
@@ -68,35 +67,6 @@ namespace Netch.Controllers
return true;
}
- private void RecordPrevious()
- {
- try
- {
- var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
- if (registry == null)
- throw new Exception();
-
- prevPAC = registry.GetValue("AutoConfigURL")?.ToString() ?? "";
- prevHTTP = registry.GetValue("ProxyServer")?.ToString() ?? "";
- prevBypass = registry.GetValue("ProxyOverride")?.ToString() ?? "";
- prevEnabled = registry.GetValue("ProxyEnable")?.Equals(1) ?? false; // HTTP Proxy Enabled
-
- if (prevHTTP == $"127.0.0.1:{Global.Settings.HTTPLocalPort}")
- {
- prevEnabled = false;
- prevHTTP = "";
- }
-
- if (prevPAC != "")
- prevEnabled = true;
- }
- catch
- {
- prevEnabled = false;
- prevPAC = prevHTTP = prevBypass = "";
- }
- }
-
///
/// 停止
///
@@ -138,5 +108,34 @@ namespace Netch.Controllers
};
Task.WaitAll(tasks);
}
+
+ private void RecordPrevious()
+ {
+ try
+ {
+ var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
+ if (registry == null)
+ throw new Exception();
+
+ prevPAC = registry.GetValue("AutoConfigURL")?.ToString() ?? "";
+ prevHTTP = registry.GetValue("ProxyServer")?.ToString() ?? "";
+ prevBypass = registry.GetValue("ProxyOverride")?.ToString() ?? "";
+ prevEnabled = registry.GetValue("ProxyEnable")?.Equals(1) ?? false; // HTTP Proxy Enabled
+
+ if (prevHTTP == $"127.0.0.1:{Global.Settings.HTTPLocalPort}")
+ {
+ prevEnabled = false;
+ prevHTTP = "";
+ }
+
+ if (prevPAC != "")
+ prevEnabled = true;
+ }
+ catch
+ {
+ prevEnabled = false;
+ prevPAC = prevHTTP = prevBypass = "";
+ }
+ }
}
}
\ No newline at end of file
diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs
index d0329e49..533fb891 100644
--- a/Netch/Controllers/MainController.cs
+++ b/Netch/Controllers/MainController.cs
@@ -3,6 +3,7 @@ using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
+using Netch.Forms;
using Netch.Models;
using Netch.Servers.Socks5;
using Netch.Utils;
diff --git a/Netch/Controllers/NFController.cs b/Netch/Controllers/NFController.cs
index b2f57009..fcd78b01 100644
--- a/Netch/Controllers/NFController.cs
+++ b/Netch/Controllers/NFController.cs
@@ -5,6 +5,7 @@ using System.Linq;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Threading.Tasks;
+using Netch.Forms;
using Netch.Models;
using Netch.Servers.Shadowsocks;
using Netch.Servers.Socks5;
@@ -15,14 +16,12 @@ namespace Netch.Controllers
{
public class NFController : IModeController
{
- private static readonly ServiceController NFService = new ServiceController("netfilter2");
+ private static readonly ServiceController NFService = new("netfilter2");
private static readonly string BinDriver = string.Empty;
private static readonly string SystemDriver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys";
private static string _sysDns;
- public string Name { get; } = "Redirector";
-
static NFController()
{
string fileName;
@@ -47,6 +46,8 @@ namespace Netch.Controllers
BinDriver = "bin\\" + fileName;
}
+ public string Name { get; } = "Redirector";
+
public bool Start(in Mode mode)
{
if (!CheckDriver())
@@ -105,8 +106,19 @@ namespace Netch.Controllers
return aio_init();
}
+ public void Stop()
+ {
+ Task.Run(() =>
+ {
+ if (Global.Settings.ModifySystemDNS)
+ //恢复系统DNS
+ DNS.OutboundDNS = _sysDns;
+ });
+
+ aio_free();
+ }
+
///
- ///
///
///
///
@@ -119,7 +131,6 @@ namespace Netch.Controllers
}
///
- ///
///
///
///
@@ -161,9 +172,7 @@ namespace Netch.Controllers
}
if (!File.Exists(SystemDriver))
- {
return InstallDriver();
- }
var updateFlag = false;
@@ -178,18 +187,14 @@ namespace Netch.Controllers
{
// Installed greater than Bin
if (systemResult.Major != binResult.Major)
- {
// API breaking changes
updateFlag = true;
- }
}
}
else
{
if (!systemFileVersion.Equals(binFileVersion))
- {
updateFlag = true;
- }
}
if (!updateFlag) return true;
@@ -267,18 +272,6 @@ namespace Netch.Controllers
aio_dial((int) NameList.TYPE_ADDNAME, @"NTT\.exe");
}
- public void Stop()
- {
- Task.Run(() =>
- {
- if (Global.Settings.ModifySystemDNS)
- //恢复系统DNS
- DNS.OutboundDNS = _sysDns;
- });
-
- aio_free();
- }
-
#region NativeMethods
private const int UdpNameListOffset = (int) NameList.TYPE_UDPTYPE - (int) NameList.TYPE_TCPTYPE;
@@ -299,7 +292,7 @@ namespace Netch.Controllers
private static extern ulong aio_getDL();
- public enum NameList : int
+ public enum NameList
{
TYPE_FILTERLOOPBACK,
TYPE_FILTERTCP,
diff --git a/Netch/Controllers/UpdateChecker.cs b/Netch/Controllers/UpdateChecker.cs
index 758c9114..ae5d7de2 100644
--- a/Netch/Controllers/UpdateChecker.cs
+++ b/Netch/Controllers/UpdateChecker.cs
@@ -1,13 +1,17 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
using System.Net;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
using Netch.Models.GitHubRelease;
using Netch.Utils;
using Newtonsoft.Json;
namespace Netch.Controllers
{
- public class UpdateChecker
+ public static class UpdateChecker
{
public const string Owner = @"NetchX";
public const string Repo = @"Netch";
@@ -15,20 +19,20 @@ namespace Netch.Controllers
public const string Name = @"Netch";
public const string Copyright = @"Copyright © 2019 - 2020";
- public const string AssemblyVersion = @"1.7.1";
+ public const string AssemblyVersion = @"1.7.0";
private const string Suffix = @"";
public static readonly string Version = $"{AssemblyVersion}{(string.IsNullOrEmpty(Suffix) ? "" : $"-{Suffix}")}";
- public string LatestVersionNumber;
- public string LatestVersionUrl;
- public Release LatestRelease;
+ public static string LatestVersionNumber;
+ public static string LatestVersionUrl;
+ public static Release LatestRelease;
- public event EventHandler NewVersionFound;
- public event EventHandler NewVersionFoundFailed;
- public event EventHandler NewVersionNotFound;
+ public static event EventHandler NewVersionFound;
+ public static event EventHandler NewVersionFoundFailed;
+ public static event EventHandler NewVersionNotFound;
- public async void Check(bool isPreRelease)
+ public static async void Check(bool isPreRelease)
{
try
{
@@ -45,12 +49,12 @@ namespace Netch.Controllers
if (VersionUtil.CompareVersion(LatestRelease.tag_name, Version) > 0)
{
Logging.Info("发现新版本");
- NewVersionFound?.Invoke(this, new EventArgs());
+ NewVersionFound?.Invoke(null, new EventArgs());
}
else
{
Logging.Info("目前是最新版本");
- NewVersionNotFound?.Invoke(this, new EventArgs());
+ NewVersionNotFound?.Invoke(null, new EventArgs());
}
}
catch (Exception e)
@@ -58,12 +62,65 @@ namespace Netch.Controllers
if (e is WebException)
Logging.Warning($"获取新版本失败: {e.Message}");
else
- {
Logging.Warning(e.ToString());
+
+ NewVersionFoundFailed?.Invoke(null, new EventArgs());
+ }
+ }
+
+ public static async Task UpdateNetch()
+ {
+ var latestVersionDownloadUrl = LatestRelease.assets[0].browser_download_url;
+ var tagPage = await WebUtil.DownloadStringAsync(WebUtil.CreateRequest(LatestVersionUrl));
+ var match = Regex.Match(tagPage, @"
(?.*) | ", RegexOptions.Singleline);
+
+ // TODO Replace with regex get basename and sha256
+ var fileName = Path.GetFileName(new Uri(latestVersionDownloadUrl).LocalPath);
+ fileName = fileName.Insert(fileName.LastIndexOf('.'), LatestVersionNumber);
+ var fileFullPath = Path.Combine(Global.NetchDir, "data", fileName);
+
+ var sha256 = match.Groups["sha256"].Value;
+
+ if (File.Exists(fileFullPath))
+ {
+ if (Utils.Utils.SHA256CheckSum(fileFullPath) == sha256)
+ {
+ RunUpdater();
+ return;
}
- NewVersionFoundFailed?.Invoke(this, new EventArgs());
+ File.Delete(fileFullPath);
+ }
+
+
+ try
+ {
+ // TODO Replace "New Version Found" to Progress bar
+ await WebUtil.DownloadFileAsync(WebUtil.CreateRequest(latestVersionDownloadUrl), fileFullPath);
+ }
+ catch (Exception e)
+ {
+ throw new Exception(i18N.Translate("Download Update Failed", ": ") + e.Message);
+ }
+
+ if (Utils.Utils.SHA256CheckSum(fileFullPath) != sha256)
+ throw new Exception(i18N.Translate("The downloaded file has the wrong hash"));
+
+ RunUpdater();
+
+ void RunUpdater()
+ {
+ // if debugging process stopped, debugger will kill child processes!!!!
+ // 调试进程结束,调试器将会杀死子进程
+ // uncomment if(!Debugger.isAttach) block in NetchUpdater Project's main() method and attach to NetchUpdater process to debug
+ // 在 NetchUpdater 项目的 main() 方法中取消注释 if(!Debugger.isAttach)块,并附加到 NetchUpdater 进程进行调试
+ Process.Start(new ProcessStartInfo
+ {
+ FileName = Path.Combine(Global.NetchDir, "NetchUpdater.exe"),
+ Arguments =
+ $"{Global.Settings.UDPSocketPort} \"{fileFullPath}\" \"{Global.NetchDir}\""
+ });
}
}
}
-}
+}
\ No newline at end of file
diff --git a/Netch/Forms/MainForm.MenuStrip.cs b/Netch/Forms/MainForm.MenuStrip.cs
index 5bd0e235..93599f55 100644
--- a/Netch/Forms/MainForm.MenuStrip.cs
+++ b/Netch/Forms/MainForm.MenuStrip.cs
@@ -175,18 +175,18 @@ namespace Netch.Forms
{
void OnNewVersionNotFound(object o, EventArgs args)
{
- _updater.NewVersionNotFound -= OnNewVersionNotFound;
+ UpdateChecker.NewVersionNotFound -= OnNewVersionNotFound;
NotifyTip(i18N.Translate("Already latest version"));
}
void OnNewVersionFoundFailed(object o, EventArgs args)
{
- _updater.NewVersionFoundFailed -= OnNewVersionFoundFailed;
+ UpdateChecker.NewVersionFoundFailed -= OnNewVersionFoundFailed;
NotifyTip(i18N.Translate("New version found failed"), info: false);
}
- _updater.NewVersionNotFound += OnNewVersionNotFound;
- _updater.NewVersionFoundFailed += OnNewVersionFoundFailed;
+ UpdateChecker.NewVersionNotFound += OnNewVersionNotFound;
+ UpdateChecker.NewVersionFoundFailed += OnNewVersionFoundFailed;
CheckUpdate();
});
}
diff --git a/Netch/Forms/MainForm.Misc.cs b/Netch/Forms/MainForm.Misc.cs
index 6ee45df1..b89f4092 100644
--- a/Netch/Forms/MainForm.Misc.cs
+++ b/Netch/Forms/MainForm.Misc.cs
@@ -1,45 +1,28 @@
using System;
-using System.ComponentModel;
-using System.Diagnostics;
-using System.IO;
using System.Linq;
-using System.Text.RegularExpressions;
using System.Windows.Forms;
using Netch.Controllers;
using Netch.Utils;
namespace Netch.Forms
{
- ///
- /// this class is used to disable Designer
- ///
- ///
- /// 此类用于禁用设计器
- ///
- [DesignerCategory("")]
- public partial class Dummy
- {
- }
-
partial class MainForm
{
- private readonly UpdateChecker _updater = new UpdateChecker();
-
private void CheckUpdate()
{
- _updater.NewVersionFound += (o, args) =>
+ UpdateChecker.NewVersionFound += (_, _) =>
{
- NotifyTip($"{i18N.Translate(@"New version available", ": ")}{_updater.LatestVersionNumber}");
+ NotifyTip($"{i18N.Translate(@"New version available", ": ")}{UpdateChecker.LatestVersionNumber}");
NewVersionLabel.Visible = true;
};
- _updater.Check(Global.Settings.CheckBetaUpdate);
+ UpdateChecker.Check(Global.Settings.CheckBetaUpdate);
}
private async void NewVersionLabel_Click(object sender, EventArgs e)
{
- if (!_updater.LatestRelease.assets.Any())
+ if (!UpdateChecker.LatestRelease.assets.Any())
{
- Utils.Utils.Open(_updater.LatestVersionUrl);
+ Utils.Utils.Open(UpdateChecker.LatestVersionUrl);
return;
}
@@ -47,59 +30,13 @@ namespace Netch.Forms
return;
NotifyTip(i18N.Translate("Start downloading new version"));
- var latestVersionDownloadUrl = _updater.LatestRelease.assets[0].browser_download_url;
- var tagPage = await WebUtil.DownloadStringAsync(WebUtil.CreateRequest(_updater.LatestVersionUrl));
- var match = Regex.Match(tagPage, @"(?.*) | ", RegexOptions.Singleline);
-
- // TODO Replace with regex get basename and sha256
- var fileName = Path.GetFileName(new Uri(latestVersionDownloadUrl).LocalPath);
- fileName = fileName.Insert(fileName.LastIndexOf('.'), _updater.LatestVersionNumber);
- var fileFullPath = Path.Combine(Global.NetchDir, "data", fileName);
-
- var sha256 = match.Groups["sha256"].Value;
-
try
{
- if (File.Exists(fileFullPath))
- {
- if (Utils.Utils.SHA256CheckSum(fileFullPath) == sha256)
- {
- RunUpdater();
- return;
- }
-
- File.Delete(fileFullPath);
- }
-
- // TODO Replace "New Version Found" to Progress bar
- await WebUtil.DownloadFileAsync(WebUtil.CreateRequest(latestVersionDownloadUrl), fileFullPath);
-
- if (Utils.Utils.SHA256CheckSum(fileFullPath) != sha256)
- {
- MessageBoxX.Show("The downloaded file has the wrong hash");
- return;
- }
-
- RunUpdater();
+ await UpdateChecker.UpdateNetch();
}
catch (Exception exception)
{
- NotifyTip($"{i18N.Translate("Download update failed")}\n{exception.Message}");
- Logging.Error($"下载更新失败 {exception}");
- }
-
- void RunUpdater()
- {
- // if debugging process stopped, debugger will kill child processes!!!!
- // 调试进程结束,调试器将会杀死子进程
- // uncomment if(!Debugger.isAttach) block in NetchUpdater Project's main() method and attach to NetchUpdater process to debug
- // 在 NetchUpdater 项目的 main() 方法中取消注释 if(!Debugger.isAttach)块,并附加到 NetchUpdater 进程进行调试
- Process.Start(new ProcessStartInfo
- {
- FileName = Path.Combine(Global.NetchDir, "NetchUpdater.exe"),
- Arguments =
- $"{Global.Settings.UDPSocketPort} \"{fileFullPath}\" \"{Global.NetchDir}\""
- });
+ NotifyTip(exception.Message);
}
}
}
diff --git a/Netch/Utils/MessageBoxX.cs b/Netch/Forms/MessageBoxX.cs
similarity index 83%
rename from Netch/Utils/MessageBoxX.cs
rename to Netch/Forms/MessageBoxX.cs
index f66967d0..2cb31e42 100644
--- a/Netch/Utils/MessageBoxX.cs
+++ b/Netch/Forms/MessageBoxX.cs
@@ -1,10 +1,11 @@
using System;
using System.Windows.Forms;
using Netch.Models;
+using Netch.Utils;
-namespace Netch.Utils
+namespace Netch.Forms
{
- static class MessageBoxX
+ public static class MessageBoxX
{
///
///
@@ -34,11 +35,11 @@ namespace Netch.Utils
};
return MessageBox.Show(
- owner: owner,
- text: text,
- caption: i18N.Translate(title),
- buttons: confirm ? MessageBoxButtons.OKCancel : MessageBoxButtons.OK,
- icon: msgIcon);
+ owner,
+ text,
+ i18N.Translate(title),
+ confirm ? MessageBoxButtons.OKCancel : MessageBoxButtons.OK,
+ msgIcon);
}
}
}
\ No newline at end of file