Skip to content

Commit

Permalink
- Fix bug deadlock when getting result from CMD
Browse files Browse the repository at this point in the history
- Fix bug cannot clear IPv6 gateway when the gateway is already set
- Add mergedll.bat script to merge exe with dll
  • Loading branch information
chieunhatnang committed Aug 23, 2022
1 parent 5334dcc commit fc937bf
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 28 deletions.
16 changes: 14 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
bin/
obj/
.vs
bin/*
obj/*
packages/*
.gitignore

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
87 changes: 67 additions & 20 deletions LevFastConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial class form_LowEndVietFastVPSConfig : Form
{
#region Final variables
static readonly string APPNAME = "VM QuickConfig";
public readonly string VERSION = "1.6";
public readonly string VERSION = "1.6.1";
static readonly string GITNAME = "VM QuickConfig";
static readonly string GITHOME = "https://github.com/chieunhatnang/VM-QuickConfig";

Expand Down Expand Up @@ -119,6 +119,7 @@ public form_LowEndVietFastVPSConfig(string[] args)
#region Event Processing
private void Form_LowEndVietFastVPSConfig_Load(object sender, EventArgs e)
{

// Check and force change password
if (LEVRegKey.GetValue("ForceChangePassword").ToString() == "1")
{
Expand Down Expand Up @@ -970,24 +971,67 @@ private static int ExecuteCommand(string commnd, bool sync = false, int timeout
/// <param name="noWindow">The <see cref="ProcessStartInfo.CreateNoWindow"/>. Do we prevent the creation of CMD window to run silently ? (silent by default)</param>
/// <returns>True if <paramref name="processName"/> isn't null and process execution succeeded. False if <paramref name="processName"/> is null or empty.
/// Throw an <see cref="Exception"/> if execution failed</returns>
private static string StartProcess(string processName, string args, string verb = null, bool useShell = false, bool redirectErros = true, bool redirectOutput = true, bool noWindow = true)
private static string StartProcess(string processName, string args, string verb = null, bool useShell = false, bool redirectErros = true, bool redirectOutput = true, bool noWindow = true, int timeout = 10000)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = processName;
psi.Arguments = args;
psi.UseShellExecute = useShell;
psi.RedirectStandardOutput = redirectOutput;
psi.RedirectStandardError = redirectErros;
psi.CreateNoWindow = noWindow;
if (verb != null)
psi.Verb = verb;
Process proc = Process.Start(psi);
proc.WaitForExit();
string errors = proc.StandardError.ReadToEnd();
string output = proc.StandardOutput.ReadToEnd();
if (proc.ExitCode != 0)
throw new Exception(processName + " exit code: " + proc.ExitCode.ToString() + " " + (!string.IsNullOrEmpty(errors) ? " " + errors : "") + " " + (!string.IsNullOrEmpty(output) ? " " + output : ""));
return output;
using (Process process = new Process())
{
process.StartInfo.FileName = processName;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = useShell;
process.StartInfo.RedirectStandardOutput = redirectOutput;
process.StartInfo.RedirectStandardError = redirectErros;
process.StartInfo.CreateNoWindow = noWindow;

StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();

using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};

process.Start();

process.BeginOutputReadLine();
process.BeginErrorReadLine();

if (process.WaitForExit(timeout) &&
outputWaitHandle.WaitOne(timeout) &&
errorWaitHandle.WaitOne(timeout))
{
if (process.ExitCode != 0)
throw new Exception(processName + " exit code: " + process.ExitCode.ToString() +
" " + (String.IsNullOrEmpty(error.ToString()) ? " " + error.ToString() : "") + " " +
(String.IsNullOrEmpty(output.ToString()) ? " " + output.ToString() : ""));
return output.ToString();
}
else
{
throw new Exception("Timeout executing!");
}
}
}
}

/// <summary>
Expand Down Expand Up @@ -1043,10 +1087,13 @@ private static void ClearIPv6(NetworkInterface networkInterface)
// get current gateway
args = $"netsh interface ipv6 show route | findstr \"::/0\"";
resultCmd = StartProcess("cmd.exe", "/c " + args, "runas");
var gatewatList = resultCmd.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Split(' ')[38]).Where(x => !x.StartsWith("fe80") && !string.IsNullOrWhiteSpace(x)).ToArray();
var gatewayList = resultCmd.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
.Where(y => Regex.IsMatch(y, @"(?:^|(?<=\s))(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?=\s|$)")).ToArray()[0])
.ToArray();

// delete current gateway
foreach (var gateway in gatewatList)
foreach (var gateway in gatewayList)
{
args = $"route delete ::/0 {gateway}";
StartProcess("cmd.exe", "/c " + args, "runas");
Expand Down
4 changes: 2 additions & 2 deletions LowEndViet.com VPS Tool.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\ILRepack.2.0.18\build\ILRepack.props" Condition="Exists('packages\ILRepack.2.0.18\build\ILRepack.props')" />
<Import Project="packages\ILMerge.3.0.29\build\ILMerge.props" Condition="Exists('packages\ILMerge.3.0.29\build\ILMerge.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -134,7 +134,7 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\ILRepack.2.0.18\build\ILRepack.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\ILRepack.2.0.18\build\ILRepack.props'))" />
<Error Condition="!Exists('packages\ILMerge.3.0.29\build\ILMerge.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\ILMerge.3.0.29\build\ILMerge.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VM QuickConfig")]
[assembly: AssemblyCopyright("Copyright © 2018-2019")]
[assembly: AssemblyCopyright("Copyright © 2018-2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
2 changes: 1 addition & 1 deletion Properties/Resources.Designer.cs

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

2 changes: 1 addition & 1 deletion Properties/Settings.Designer.cs

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

1 change: 1 addition & 0 deletions mergedll.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.\packages\ILMerge.3.0.29\tools\net452\ILMerge.exe /out:".\export\VMQuickConfig.exe" ".\bin\Release\VMQuickConfig.exe" ".\bin\Release\*.dll" /target:winexe /targetplatform:v4,C:\Windows\Microsoft.NET\Framework64\v4.0.30319 /wildcards
2 changes: 1 addition & 1 deletion packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ILRepack" version="2.0.18" targetFramework="net48" />
<package id="ILMerge" version="3.0.29" targetFramework="net48" />
<package id="Microsoft.PowerShell.5.ReferenceAssemblies" version="1.1.0" targetFramework="net48" />
</packages>

0 comments on commit fc937bf

Please sign in to comment.