Skip to content

Commit

Permalink
Merge pull request netchx#90 from chsbuffer/master
Browse files Browse the repository at this point in the history
Fix killing Netch ChildProcess
  • Loading branch information
BingLingGroup authored Sep 1, 2019
2 parents f9c07d8 + 89dc161 commit c61da58
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 56 deletions.
31 changes: 31 additions & 0 deletions Netch/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ public bool Start(Objects.Server server, Objects.Mode mode)
result = true;
break;
case "Shadowsocks":
KillProcess("Shadowsocks");
result = pSSController.Start(server, mode);
break;
case "ShadowsocksR":
KillProcess("ShadowsocksR");
result = pSSRController.Start(server, mode);
break;
case "VMess":
KillProcess("v2ray");
result = pVMessController.Start(server, mode);
break;
default:
Expand Down Expand Up @@ -130,5 +133,33 @@ public void Stop()
pHTTPController.Stop();
pTUNTAPController.Stop();
}

public void KillProcess(String name) {
Process[] processes = Process.GetProcessesByName(name);
foreach (Process p in processes)
{
if (IsChildProcess(p, name))
{
p.Kill();
}
}
}

private static bool IsChildProcess(Process process,string name)
{
bool result;
try
{
string FileName = (Directory.GetCurrentDirectory() + "\\bin\\" + name + ".exe").ToLower();
string procFileName = process.MainModule.FileName.ToLower();
result = FileName.Equals(procFileName, StringComparison.Ordinal);
}
catch (Exception e)
{
Utils.Logging.Info(e.Message);
result = false;
}
return result;
}
}
}
12 changes: 0 additions & 12 deletions Netch/Controllers/SSController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ public class SSController
/// <returns>是否启动成功</returns>
public bool Start(Objects.Server server, Objects.Mode mode)
{
foreach (var proc in Process.GetProcessesByName("Shadowsocks"))
{
try
{
proc.Kill();
}
catch (Exception)
{
// 跳过
}
}

if (!File.Exists("bin\\Shadowsocks.exe"))
{
return false;
Expand Down
12 changes: 0 additions & 12 deletions Netch/Controllers/SSRController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ public class SSRController
/// <returns>是否启动成功</returns>
public bool Start(Objects.Server server, Objects.Mode mode)
{
foreach (var proc in Process.GetProcessesByName("ShadowsocksR"))
{
try
{
proc.Kill();
}
catch (Exception)
{
// 跳过
}
}

if (!File.Exists("bin\\ShadowsocksR.exe"))
{
return false;
Expand Down
12 changes: 0 additions & 12 deletions Netch/Controllers/VMessController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ public class VMessController
/// <returns>是否启动成功</returns>
public bool Start(Objects.Server server, Objects.Mode mode)
{
foreach (var proc in Process.GetProcessesByName("v2ray"))
{
try
{
proc.Kill();
}
catch (Exception)
{
// 跳过
}
}

if (!File.Exists("bin\\v2ray.exe") || !File.Exists("bin\\v2ctl.exe"))
{
return false;
Expand Down
30 changes: 15 additions & 15 deletions Netch/Forms/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Netch/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MainForm()
InitializeComponent();

CheckForIllegalCrossThreadCalls = false;
ToolStrip.Renderer = new Override.ToolStripProfessionalRender();
//MenuStrip.Renderer = new Override.ToolStripProfessionalRender();
}

public void TestServer()
Expand Down Expand Up @@ -657,7 +657,7 @@ private void ControlButton_Click(object sender, EventArgs e)
return;
}

ToolStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false;
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false;
ControlButton.Text = "...";
StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting")}";
State = Objects.State.Starting;
Expand All @@ -683,7 +683,7 @@ private void ControlButton_Click(object sender, EventArgs e)
}
else
{
ToolStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
ControlButton.Text = Utils.i18N.Translate("Start");
StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Start failed")}";
State = Objects.State.Stopped;
Expand Down Expand Up @@ -714,7 +714,7 @@ private void ControlButton_Click(object sender, EventArgs e)
UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = false;
}

ToolStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
ControlButton.Text = Utils.i18N.Translate("Start");
StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Stopped")}";
State = Objects.State.Stopped;
Expand Down
2 changes: 1 addition & 1 deletion Netch/Forms/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="ToolStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<metadata name="MenuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
Expand Down

0 comments on commit c61da58

Please sign in to comment.