Skip to content

Commit

Permalink
If the update fails during the upgrade, the update will be retried.
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Mar 1, 2025
1 parent aa5e437 commit 9748fbb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 57 deletions.
28 changes: 19 additions & 9 deletions v2rayN/AmazTool/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
namespace AmazTool
namespace AmazTool
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
private static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine(Resx.Resource.Guidelines);
Thread.Sleep(5000);
Utils.WriteLine(Resx.Resource.Guidelines);
Utils.Waiting(5);
return;
}

var argData = Uri.UnescapeDataString(string.Join(" ", args));
if (argData.Equals("rebootas"))
{
Thread.Sleep(1000);
Utils.Waiting(1);
Utils.StartV2RayN();
return;
}

var tryTimes = 0;
UpgradeApp.Init();
while (tryTimes++ < 3)
{
if (!UpgradeApp.Upgrade(argData))
{
continue;
}

UpgradeApp.Upgrade(argData);
Utils.WriteLine(Resx.Resource.Restartv2rayN);
Utils.Waiting(3);
Utils.StartV2RayN();
break;
}
}
}
}
}
105 changes: 62 additions & 43 deletions v2rayN/AmazTool/UpgradeApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,24 @@ namespace AmazTool
{
internal class UpgradeApp
{
public static void Upgrade(string fileName)
public static bool Upgrade(string fileName)
{
Console.WriteLine($"{Resx.Resource.StartUnzipping}\n{fileName}");

Utils.Waiting(5);
Utils.WriteLine($"{Resx.Resource.StartUnzipping}\n{fileName}");

if (!File.Exists(fileName))
{
Console.WriteLine(Resx.Resource.UpgradeFileNotFound);
return;
Utils.WriteLine(Resx.Resource.UpgradeFileNotFound);
return false;
}

Console.WriteLine(Resx.Resource.TryTerminateProcess);
try
{
var existing = Process.GetProcessesByName(Utils.V2rayN);
foreach (var pp in existing)
{
var path = pp.MainModule?.FileName ?? "";
if (path.StartsWith(Utils.GetPath(Utils.V2rayN)))
{
pp?.Kill();
pp?.WaitForExit(1000);
}
}
}
catch (Exception ex)
{
// Access may be denied without admin right. The user may not be an administrator.
Console.WriteLine(Resx.Resource.FailedTerminateProcess + ex.StackTrace);
}
Utils.Waiting(5);

KillV2rayN();

Console.WriteLine(Resx.Resource.StartUnzipping);
Utils.WriteLine(Resx.Resource.StartUnzipping);
StringBuilder sb = new();
try
{
var thisAppOldFile = $"{Utils.GetExePath()}.tmp";
File.Delete(thisAppOldFile);
var splitKey = "/";

using var archive = ZipFile.OpenRead(fileName);
Expand All @@ -56,18 +36,15 @@ public static void Upgrade(string fileName)
continue;
}

Console.WriteLine(entry.FullName);
Utils.WriteLine(entry.FullName);

var lst = entry.FullName.Split(splitKey);
if (lst.Length == 1)
continue;
var fullName = string.Join(splitKey, lst[1..lst.Length]);

if (string.Equals(Utils.GetExePath(), Utils.GetPath(fullName), StringComparison.OrdinalIgnoreCase))
{
File.Move(Utils.GetExePath(), thisAppOldFile);
continue;
}

var fullName = string.Join(splitKey, lst[1..lst.Length]);
var entryOutputPath = Utils.GetPath(fullName);
Directory.CreateDirectory(Path.GetDirectoryName(entryOutputPath)!);
//In the bin folder, if the file already exists, it will be skipped
Expand All @@ -77,29 +54,71 @@ public static void Upgrade(string fileName)
}
entry.ExtractToFile(entryOutputPath, true);

Console.WriteLine(entryOutputPath);
Utils.WriteLine(entryOutputPath);
}
catch (Exception ex)
{
sb.Append(ex.Message);
sb.Append(ex.StackTrace);
}
}
}
catch (Exception ex)
{
Console.WriteLine(Resx.Resource.FailedUpgrade + ex.StackTrace);
//return;
sb.Append(Resx.Resource.FailedUpgrade + ex.StackTrace);
}

if (sb.Length <= 0)
{
return true;
}
if (sb.Length > 0)

Utils.WriteLine(sb.ToString());
Utils.WriteLine(Resx.Resource.FailedUpgrade);
return false;
}

public static bool Init()
{
//Process temporary files generated by the last update
var files = Directory.GetFiles(Utils.GetPath(""), "*.tmp");
foreach (var file in files)
{
Console.WriteLine(Resx.Resource.FailedUpgrade + sb.ToString());
//return;
if (file.Contains(Utils.AmazTool))
{
File.Delete(file);
}
}

Console.WriteLine(Resx.Resource.Restartv2rayN);
Utils.Waiting(2);
var destFileName = $"{Utils.GetExePath()}{Guid.NewGuid().ToString("N")[..8]}.tmp";
File.Move(Utils.GetExePath(), destFileName);

return true;
}

private static bool KillV2rayN()
{
Utils.WriteLine(Resx.Resource.TryTerminateProcess);
try
{
var existing = Process.GetProcessesByName(Utils.V2rayN);
foreach (var pp in existing)
{
var path = pp.MainModule?.FileName ?? "";
if (path.StartsWith(Utils.GetPath(Utils.V2rayN)))
{
pp?.Kill();
pp?.WaitForExit(1000);
}
}
}
catch (Exception ex)
{
// Access may be denied without admin right. The user may not be an administrator.
Utils.WriteLine(Resx.Resource.FailedTerminateProcess + ex.StackTrace);
}

Utils.StartV2RayN();
return true;
}
}
}
16 changes: 11 additions & 5 deletions v2rayN/AmazTool/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;

namespace AmazTool
{
Expand All @@ -14,9 +14,9 @@ public static string StartupPath()
return AppDomain.CurrentDomain.BaseDirectory;
}

public static string GetPath(string fileName)
public static string GetPath(string? fileName)
{
string startupPath = StartupPath();
var startupPath = StartupPath();
if (string.IsNullOrEmpty(fileName))
{
return startupPath;
Expand All @@ -25,6 +25,7 @@ public static string GetPath(string fileName)
}

public static string V2rayN => "v2rayN";
public static string AmazTool => "AmazTool";

public static void StartV2RayN()
{
Expand All @@ -44,9 +45,14 @@ public static void Waiting(int second)
{
for (var i = second; i > 0; i--)
{
Console.WriteLine(i);
Utils.WriteLine(i);
Thread.Sleep(1000);
}
}

public static void WriteLine(object obj)
{
Console.WriteLine(obj);
}
}
}
}

0 comments on commit 9748fbb

Please # to comment.