Skip to content

Commit

Permalink
Merge pull request #318 from chsbuffer/dev
Browse files Browse the repository at this point in the history
多项改进,Bug修复
  • Loading branch information
chsbuffer authored Jul 19, 2020
2 parents a319833 + 54a263a commit e10c994
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 307 deletions.
2 changes: 1 addition & 1 deletion Netch/Controllers/EncryptedProxy/VMessController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class VMessController : EncryptedProxy
{
public VMessController()
{
Name = "v2ray";
Name = "V2Ray";
MainFile = "v2ray.exe";
StartedKeywords("started");
StoppedKeywords("config file not readable", "failed to");
Expand Down
17 changes: 13 additions & 4 deletions Netch/Controllers/MainController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Netch.Forms;
using Netch.Models;
using Netch.Utils;

namespace Netch.Controllers
{
public class MainController
{
/// <summary>
/// 记录当前使用的端口
/// <see cref="MainForm.LocalPortText"/>
/// </summary>
public static readonly List<int> UsingPorts = new List<int>();

public EncryptedProxy pEncryptedProxyController;

public ModeController pModeController;
Expand Down Expand Up @@ -60,19 +68,19 @@ public bool Start(Server server, Mode mode)
// 检查端口是否被占用
if (PortHelper.PortInUse(Global.Settings.Socks5LocalPort))
{
MessageBoxX.Show("Socks5" + i18N.Translate("port is in use."));
MessageBoxX.Show(i18N.Translate("The {0} port is in use.", "Socks5"));
return false;
}

if (PortHelper.PortInUse(Global.Settings.HTTPLocalPort))
{
MessageBoxX.Show("HTTP" + i18N.Translate("port is in use."));
MessageBoxX.Show(i18N.Translate("The {0} port is in use.", "HTTP"));
return false;
}

if (PortHelper.PortInUse(Global.Settings.RedirectorTCPPort, PortType.TCP))
{
MessageBoxX.Show("RedirectorTCP"+i18N.Translate("port is in use."));
MessageBoxX.Show(i18N.Translate("The {0} port is in use.", "Redirector TCP"));
return false;
}

Expand Down Expand Up @@ -138,7 +146,8 @@ public bool Start(Server server, Mode mode)
/// </summary>
public void Stop()
{
pEncryptedProxyController?.Stop();
Task.Run(() => pEncryptedProxyController?.Stop());
Task.Run(() => UsingPorts.Clear());
pModeController?.Stop();
}

Expand Down
15 changes: 8 additions & 7 deletions Netch/Controllers/Mode/NFController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public NFController()

public override bool Start(Server server, Mode mode)
{
Logging.Info("内置驱动版本" + DriverVersion(BinDriver));
Logging.Info("系统驱动版本" + DriverVersion(SystemDriver));
Logging.Info("内置驱动版本: " + DriverVersion(BinDriver));
if (DriverVersion(SystemDriver) != DriverVersion(BinDriver))
{
if (File.Exists(SystemDriver))
{
Logging.Info("系统驱动版本: " + DriverVersion(SystemDriver));
Logging.Info("更新驱动");
UninstallDriver();
}
Expand Down Expand Up @@ -108,13 +108,14 @@ public override bool Start(Server server, Mode mode)

//备份并替换系统DNS
_sysDns = DNS.getSystemDns();
string[] dns = { "1.1.1.1", "8.8.8.8" };
DNS.SetDNS(dns);

return true; }
string[] dns = {"1.1.1.1", "8.8.8.8"};
DNS.SetDNS(dns);

return true;
}
}

Logging.Error(Name + "启动超时");
Logging.Error(Name + " 启动超时");
Stop();
if (!RestartService()) return false;
}
Expand Down
42 changes: 19 additions & 23 deletions Netch/Forms/MainForm.Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void ControlFun()

if (ModeComboBox.SelectedIndex == -1)
{
MessageBoxX.Show(i18N.Translate("Please select an mode first"));
MessageBoxX.Show(i18N.Translate("Please select a mode first"));
return;
}

Expand All @@ -51,11 +51,9 @@ private void ControlFun()
Task.Run(() =>
{
UpdateStatus(State.Started,
i18N.Translate(StateExtension.GetStatusString(State.Started)) + PortText(server.Type, mode.Type));

i18N.Translate(StateExtension.GetStatusString(State.Started)) + LocalPortText(server.Type, mode.Type));
Bandwidth.NetTraffic(server, mode, _mainController);
});

// 如果勾选启动后最小化
if (Global.Settings.MinimizeWhenStarted)
{
Expand Down Expand Up @@ -110,50 +108,48 @@ private void ControlFun()
{
// 停止
UpdateStatus(State.Stopping);
Task.Run(() =>
{
_mainController.Stop();
UpdateStatus(State.Stopped);

TestServer();
});
_mainController.Stop();
UpdateStatus(State.Stopped);
Task.Run(TestServer);
}
}

private string PortText(string serverType, int modeType)
private static string LocalPortText(string serverType, int modeType)
{
var text = new StringBuilder(" (");
text.Append(Global.Settings.LocalAddress == "0.0.0.0"
? i18N.Translate("Allow other Devices to connect") + " "
: "");
if (Global.Settings.LocalAddress == "0.0.0.0")
text.Append(i18N.Translate("Allow other Devices to connect") + " ");
if (serverType == "Socks5")
{
// 不可控Socks5
if (modeType == 3 || modeType == 5)
{
// 可控HTTP
text.Append(
$"HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
MainController.UsingPorts.Add(Global.Settings.HTTPLocalPort);
text.Append($"HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
else
{
// 不可控HTTP
return "";
return string.Empty;
}
}
else
{
// 可控Socks5
text.Append(
$"Socks5 {i18N.Translate("Local Port", ": ")}{Global.Settings.Socks5LocalPort}");
MainController.UsingPorts.Add(Global.Settings.Socks5LocalPort);
text.Append($"Socks5 {i18N.Translate("Local Port", ": ")}{Global.Settings.Socks5LocalPort}");
if (modeType == 3 || modeType == 5)
{
//有HTTP
text.Append(
$" | HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
// 有HTTP
MainController.UsingPorts.Add(Global.Settings.HTTPLocalPort);
text.Append($" | HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
}

if (modeType == 0)
MainController.UsingPorts.Add(Global.Settings.RedirectorTCPPort);

text.Append(")");
return text.ToString();
}
Expand Down
33 changes: 0 additions & 33 deletions Netch/Forms/MainForm.MenuStrip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,39 +230,6 @@ private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sende

#region 选项

private void RestartServiceToolStripMenuItem_Click(object sender, EventArgs e)
{
Enabled = false;
StatusText(i18N.Translate("Restarting service"));

Task.Run(() =>
{
try
{
var service = new ServiceController("netfilter2");
if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
else if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
}
catch (Exception)
{
NFAPI.nf_registerDriver("netfilter2");
}

MessageBoxX.Show(i18N.Translate("Service has been restarted"), owner: this);
Enabled = true;
});
}

private void UninstallServiceToolStripMenuItem_Click(object sender, EventArgs e)
{
Enabled = false;
Expand Down
73 changes: 36 additions & 37 deletions Netch/Forms/MainForm.Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void ProfileButton_Click(object sender, EventArgs e)
}
else if (ModeComboBox.SelectedIndex == -1)
{
MessageBoxX.Show(i18N.Translate("Please select an mode first"));
MessageBoxX.Show(i18N.Translate("Please select a mode first"));
}
else if (ProfileNameText.Text == "")
{
Expand All @@ -159,54 +159,53 @@ private void ProfileButton_Click(object sender, EventArgs e)
SaveProfile(index);
ProfileButtons[index].Text = ProfileNameText.Text;
}
return;
}
else if (ModifierKeys == Keys.Shift)

if (Global.Settings.Profiles[index].IsDummy)
{
if (MessageBoxX.Show(i18N.Translate("Remove this Profile?"), confirm: true) == DialogResult.OK)
{
RemoveProfile(index);
ProfileButtons[index].Text = i18N.Translate("None");
MessageBoxX.Show(i18N.Translate("Profile Removed!"));
}
MessageBoxX.Show(i18N.Translate("No saved profile here. Save a profile first by Ctrl+Click on the button"));
return;
}
else

if (ModifierKeys == Keys.Shift)
{
if (Global.Settings.Profiles[index].IsDummy)
{
MessageBoxX.Show(i18N.Translate("No saved profile here. Save a profile first by Ctrl+Click on the button"));
return;
}
if (MessageBoxX.Show(i18N.Translate("Remove this Profile?"), confirm: true) != DialogResult.OK) return;
RemoveProfile(index);
ProfileButtons[index].Text = i18N.Translate("None");
MessageBoxX.Show(i18N.Translate("Profile Removed!"));
return;
}

try
{
ProfileNameText.Text = LoadProfile(index);
try
{
ProfileNameText.Text = LoadProfile(index);

// start the profile
ControlFun();
if (State == State.Stopping || State == State.Stopped)
{
Task.Run(() =>
{
while (State != State.Stopped)
{
Thread.Sleep(250);
}

ControlButton.PerformClick();
});
}
}
catch (Exception ee)
// start the profile
ControlFun();
if (State == State.Stopping || State == State.Stopped)
{
Task.Run(() =>
{
Logging.Info(ee.ToString());
ProfileButtons[index].Text = i18N.Translate("Error");
Thread.Sleep(1200);
ProfileButtons[index].Text = i18N.Translate("None");
while (State != State.Stopped)
{
Thread.Sleep(250);
}

ControlFun();
});
}
}
catch (Exception ee)
{
Task.Run(() =>
{
Logging.Info(ee.ToString());
ProfileButtons[index].Text = i18N.Translate("Error");
Thread.Sleep(1200);
ProfileButtons[index].Text = i18N.Translate("None");
});
}
}
}
}
4 changes: 2 additions & 2 deletions Netch/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private void EditModePictureBox_Click(object sender, EventArgs e)
}
else
{
MessageBoxX.Show(i18N.Translate("Please select an mode first"));
MessageBoxX.Show(i18N.Translate("Please select a mode first"));
}
}

Expand All @@ -336,7 +336,7 @@ private void DeleteModePictureBox_Click(object sender, EventArgs e)
}
else
{
MessageBoxX.Show(i18N.Translate("Please select an mode first"));
MessageBoxX.Show(i18N.Translate("Please select a mode first"));
}
}

Expand Down
Loading

0 comments on commit e10c994

Please sign in to comment.