From b936c194e4800951fdb47e2d491724013c278490 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 22 Aug 2024 15:23:29 +0800 Subject: [PATCH] Refactor v2rayUpgrade --- .../ViewModels/MainWindowViewModel.cs | 2 +- v2rayN/v2rayUpgrade/MainForm.Designer.cs | 107 ------------- v2rayN/v2rayUpgrade/MainForm.cs | 147 ------------------ v2rayN/v2rayUpgrade/MainForm.resx | 120 -------------- v2rayN/v2rayUpgrade/Program.cs | 128 ++++++++++++++- .../Properties/Resources.Designer.cs | 63 -------- v2rayN/v2rayUpgrade/Properties/Resources.resx | 117 -------------- .../Properties/Settings.Designer.cs | 26 ---- .../v2rayUpgrade/Properties/Settings.settings | 7 - v2rayN/v2rayUpgrade/app.manifest | 7 - v2rayN/v2rayUpgrade/v2rayUpgrade.csproj | 13 +- 11 files changed, 129 insertions(+), 608 deletions(-) delete mode 100644 v2rayN/v2rayUpgrade/MainForm.Designer.cs delete mode 100644 v2rayN/v2rayUpgrade/MainForm.cs delete mode 100644 v2rayN/v2rayUpgrade/MainForm.resx delete mode 100644 v2rayN/v2rayUpgrade/Properties/Resources.Designer.cs delete mode 100644 v2rayN/v2rayUpgrade/Properties/Resources.resx delete mode 100644 v2rayN/v2rayUpgrade/Properties/Settings.Designer.cs delete mode 100644 v2rayN/v2rayUpgrade/Properties/Settings.settings delete mode 100644 v2rayN/v2rayUpgrade/app.manifest diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 83774035..6073e9b2 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -734,7 +734,7 @@ namespace ServiceLib.ViewModels { StartInfo = new ProcessStartInfo { - FileName = "v2rayUpgrade.exe", + FileName = "v2rayUpgrade", Arguments = fileName.AppendQuotes(), WorkingDirectory = Utils.StartupPath() } diff --git a/v2rayN/v2rayUpgrade/MainForm.Designer.cs b/v2rayN/v2rayUpgrade/MainForm.Designer.cs deleted file mode 100644 index bdfa4379..00000000 --- a/v2rayN/v2rayUpgrade/MainForm.Designer.cs +++ /dev/null @@ -1,107 +0,0 @@ -namespace v2rayUpgrade -{ - partial class MainForm - { - /// - /// 必需的设计器变量。 - /// - private System.ComponentModel.IContainer components = null; - - /// - /// 清理所有正在使用的资源。 - /// - /// 如果应释放托管资源,为 true;否则为 false。 - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows 窗体设计器生成的代码 - - /// - /// 设计器支持所需的方法 - 不要修改 - /// 使用代码编辑器修改此方法的内容。 - /// - private void InitializeComponent() - { - this.btnClose = new System.Windows.Forms.Button(); - this.btnOK = new System.Windows.Forms.Button(); - this.label1 = new System.Windows.Forms.Label(); - this.label2 = new System.Windows.Forms.Label(); - this.SuspendLayout(); - // - // btnClose - // - this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.btnClose.Font = new System.Drawing.Font("微软雅黑", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.btnClose.ImeMode = System.Windows.Forms.ImeMode.NoControl; - this.btnClose.Location = new System.Drawing.Point(367, 118); - this.btnClose.Name = "btnClose"; - this.btnClose.Size = new System.Drawing.Size(184, 89); - this.btnClose.TabIndex = 1; - this.btnClose.Text = "&Exit(退出)"; - this.btnClose.UseVisualStyleBackColor = true; - this.btnClose.Click += new System.EventHandler(this.btnClose_Click); - // - // btnOK - // - this.btnOK.Font = new System.Drawing.Font("微软雅黑", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.btnOK.ImeMode = System.Windows.Forms.ImeMode.NoControl; - this.btnOK.Location = new System.Drawing.Point(81, 118); - this.btnOK.Name = "btnOK"; - this.btnOK.Size = new System.Drawing.Size(184, 89); - this.btnOK.TabIndex = 0; - this.btnOK.Text = "&Upgrade(升级)"; - this.btnOK.UseVisualStyleBackColor = true; - this.btnOK.Click += new System.EventHandler(this.btnOK_Click); - // - // label1 - // - this.label1.AutoSize = true; - this.label1.Font = new System.Drawing.Font("微软雅黑", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.label1.Location = new System.Drawing.Point(79, 64); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(205, 15); - this.label1.TabIndex = 8; - this.label1.Text = "升级成功后将自动重启v2rayN"; - // - // label2 - // - this.label2.AutoSize = true; - this.label2.Font = new System.Drawing.Font("微软雅黑", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.label2.Location = new System.Drawing.Point(79, 37); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(471, 15); - this.label2.TabIndex = 9; - this.label2.Text = "v2rayN will restart automatically after successful upgrade"; - // - // MainForm - // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(616, 284); - this.Controls.Add(this.label2); - this.Controls.Add(this.label1); - this.Controls.Add(this.btnClose); - this.Controls.Add(this.btnOK); - this.Name = "MainForm"; - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "v2rayUpgrade"; - this.ResumeLayout(false); - this.PerformLayout(); - - } - - #endregion - - private System.Windows.Forms.Button btnClose; - private System.Windows.Forms.Button btnOK; - private System.Windows.Forms.Label label1; - private System.Windows.Forms.Label label2; - } -} - diff --git a/v2rayN/v2rayUpgrade/MainForm.cs b/v2rayN/v2rayUpgrade/MainForm.cs deleted file mode 100644 index 39f8196c..00000000 --- a/v2rayN/v2rayUpgrade/MainForm.cs +++ /dev/null @@ -1,147 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.IO.Compression; -using System.Text; -using System.Windows.Forms; - -namespace v2rayUpgrade -{ - public partial class MainForm : Form - { - private readonly string defaultFilename = "v2ray-windows.zip"; - private string? fileName; - - public MainForm(string[] args) - { - InitializeComponent(); - if (args.Length > 0) - { - fileName = Uri.UnescapeDataString(string.Join(" ", args)); - } - else - { - fileName = defaultFilename; - } - } - - private void ShowWarn(string message) - { - MessageBox.Show(message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning); - } - - private void btnOK_Click(object sender, EventArgs e) - { - try - { - Process[] existing = Process.GetProcessesByName("v2rayN"); - foreach (Process p in existing) - { - string? path = p.MainModule?.FileName; - if (path == GetPath("v2rayN.exe")) - { - p.Kill(); - p.WaitForExit(100); - } - } - } - catch (Exception ex) - { - // Access may be denied without admin right. The user may not be an administrator. - ShowWarn("Failed to close v2rayN(关闭v2rayN失败).\n" + - "Close it manually, or the upgrade may fail.(请手动关闭正在运行的v2rayN,否则可能升级失败。\n\n" + ex.StackTrace); - } - - if (!File.Exists(fileName)) - { - if (File.Exists(defaultFilename)) - { - fileName = defaultFilename; - } - else - { - ShowWarn("Upgrade Failed, File Not Exist(升级失败,文件不存在)."); - return; - } - } - - StringBuilder sb = new(); - try - { - string thisAppOldFile = $"{Application.ExecutablePath}.tmp"; - File.Delete(thisAppOldFile); - string startKey = "v2rayN/"; - - using ZipArchive archive = ZipFile.OpenRead(fileName); - foreach (ZipArchiveEntry entry in archive.Entries) - { - try - { - if (entry.Length == 0) - { - continue; - } - string fullName = entry.FullName; - if (fullName.StartsWith(startKey)) - { - fullName = fullName[startKey.Length..]; - } - if (string.Equals(Application.ExecutablePath, GetPath(fullName), StringComparison.OrdinalIgnoreCase)) - { - File.Move(Application.ExecutablePath, thisAppOldFile); - } - - string entryOutputPath = GetPath(fullName); - Directory.CreateDirectory(Path.GetDirectoryName(entryOutputPath)!); - entry.ExtractToFile(entryOutputPath, true); - } - catch (Exception ex) - { - sb.Append(ex.StackTrace); - } - } - } - catch (Exception ex) - { - ShowWarn("Upgrade Failed(升级失败)." + ex.StackTrace); - return; - } - if (sb.Length > 0) - { - ShowWarn("Upgrade Failed,Hold ctrl + c to copy to clipboard.\n" + - "(升级失败,按住ctrl+c可以复制到剪贴板)." + sb.ToString()); - return; - } - - Process.Start("v2rayN.exe"); - MessageBox.Show("Upgrade successed(升级成功)", "", MessageBoxButtons.OK, MessageBoxIcon.Information); - - Close(); - } - - private void btnClose_Click(object sender, EventArgs e) - { - Close(); - } - - public static string GetExePath() - { - return Application.ExecutablePath; - } - - public static string StartupPath() - { - return Application.StartupPath; - } - - public static string GetPath(string fileName) - { - string startupPath = StartupPath(); - if (string.IsNullOrEmpty(fileName)) - { - return startupPath; - } - return Path.Combine(startupPath, fileName); - } - } -} diff --git a/v2rayN/v2rayUpgrade/MainForm.resx b/v2rayN/v2rayUpgrade/MainForm.resx deleted file mode 100644 index 1af7de15..00000000 --- a/v2rayN/v2rayUpgrade/MainForm.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/v2rayN/v2rayUpgrade/Program.cs b/v2rayN/v2rayUpgrade/Program.cs index bc4303de..906bbde0 100644 --- a/v2rayN/v2rayUpgrade/Program.cs +++ b/v2rayN/v2rayUpgrade/Program.cs @@ -1,21 +1,135 @@ -using System; -using System.Windows.Forms; +using System.Diagnostics; +using System.IO.Compression; +using System.Text; namespace v2rayUpgrade { internal static class Program { + private static readonly string defaultFilename = "v2rayN.zip_temp"; + private static string? fileName; + /// /// 应用程序的主入口点。 /// [STAThread] private static void Main(string[] args) { - Application.EnableVisualStyles(); - Application.SetHighDpiMode(HighDpiMode.SystemAware); - Application.SetHighDpiMode(HighDpiMode.PerMonitorV2); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm(args)); + if (args.Length > 0) + { + fileName = Uri.UnescapeDataString(string.Join(" ", args)); + } + else + { + fileName = defaultFilename; + } + Console.WriteLine(fileName); + Console.WriteLine("In progress, please wait...(正在进行中,请等待)"); + + Thread.Sleep(10000); + + try + { + Process[] existing = Process.GetProcessesByName("v2rayN"); + foreach (Process p in existing) + { + var path = p.MainModule?.FileName ?? ""; + if (path.StartsWith(GetPath("v2rayN"))) + { + p.Kill(); + p.WaitForExit(100); + } + } + } + catch (Exception ex) + { + // Access may be denied without admin right. The user may not be an administrator. + Console.WriteLine("Failed to close v2rayN(关闭v2rayN失败).\n" + + "Close it manually, or the upgrade may fail.(请手动关闭正在运行的v2rayN,否则可能升级失败。\n\n" + ex.StackTrace); + } + + if (!File.Exists(fileName)) + { + if (File.Exists(defaultFilename)) + { + fileName = defaultFilename; + } + else + { + Console.WriteLine("Upgrade Failed, File Not Exist(升级失败,文件不存在)."); + return; + } + } + + StringBuilder sb = new(); + try + { + string thisAppOldFile = $"{GetExePath()}.tmp"; + File.Delete(thisAppOldFile); + string startKey = "v2rayN/"; + + using ZipArchive archive = ZipFile.OpenRead(fileName); + foreach (ZipArchiveEntry entry in archive.Entries) + { + try + { + if (entry.Length == 0) + { + continue; + } + string fullName = entry.FullName; + if (fullName.StartsWith(startKey)) + { + fullName = fullName[startKey.Length..]; + } + if (string.Equals(GetExePath(), GetPath(fullName), StringComparison.OrdinalIgnoreCase)) + { + File.Move(GetExePath(), thisAppOldFile); + } + + string entryOutputPath = GetPath(fullName); + Directory.CreateDirectory(Path.GetDirectoryName(entryOutputPath)!); + entry.ExtractToFile(entryOutputPath, true); + } + catch (Exception ex) + { + sb.Append(ex.StackTrace); + } + } + } + catch (Exception ex) + { + Console.WriteLine("Upgrade Failed(升级失败)." + ex.StackTrace); + return; + } + if (sb.Length > 0) + { + Console.WriteLine("Upgrade Failed,Hold ctrl + c to copy to clipboard.\n" + + "(升级失败,按住ctrl+c可以复制到剪贴板)." + sb.ToString()); + return; + } + + Process.Start("v2rayN"); + } + + public static string GetExePath() + { + return Environment.ProcessPath ?? string.Empty; + } + + public static string StartupPath() + { + return AppDomain.CurrentDomain.BaseDirectory; + } + + public static string GetPath(string fileName) + { + string startupPath = StartupPath(); + if (string.IsNullOrEmpty(fileName)) + { + return startupPath; + } + return Path.Combine(startupPath, fileName); } } } \ No newline at end of file diff --git a/v2rayN/v2rayUpgrade/Properties/Resources.Designer.cs b/v2rayN/v2rayUpgrade/Properties/Resources.Designer.cs deleted file mode 100644 index f766ed10..00000000 --- a/v2rayN/v2rayUpgrade/Properties/Resources.Designer.cs +++ /dev/null @@ -1,63 +0,0 @@ -//------------------------------------------------------------------------------ -// -// 此代码由工具生成。 -// 运行时版本:4.0.30319.42000 -// -// 对此文件的更改可能会导致不正确的行为,并且如果 -// 重新生成代码,这些更改将会丢失。 -// -//------------------------------------------------------------------------------ - -namespace v2rayUpgrade.Properties { - using System; - - - /// - /// 一个强类型的资源类,用于查找本地化的字符串等。 - /// - // 此类是由 StronglyTypedResourceBuilder - // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 - // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen - // (以 /str 作为命令选项),或重新生成 VS 项目。 - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() { - } - - /// - /// 返回此类使用的缓存的 ResourceManager 实例。 - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("v2rayUpgrade.Properties.Resources", typeof(Resources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// 重写当前线程的 CurrentUICulture 属性 - /// 重写当前线程的 CurrentUICulture 属性。 - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - } -} diff --git a/v2rayN/v2rayUpgrade/Properties/Resources.resx b/v2rayN/v2rayUpgrade/Properties/Resources.resx deleted file mode 100644 index af7dbebb..00000000 --- a/v2rayN/v2rayUpgrade/Properties/Resources.resx +++ /dev/null @@ -1,117 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/v2rayN/v2rayUpgrade/Properties/Settings.Designer.cs b/v2rayN/v2rayUpgrade/Properties/Settings.Designer.cs deleted file mode 100644 index ecf1927f..00000000 --- a/v2rayN/v2rayUpgrade/Properties/Settings.Designer.cs +++ /dev/null @@ -1,26 +0,0 @@ -//------------------------------------------------------------------------------ -// -// 此代码由工具生成。 -// 运行时版本:4.0.30319.42000 -// -// 对此文件的更改可能会导致不正确的行为,并且如果 -// 重新生成代码,这些更改将会丢失。 -// -//------------------------------------------------------------------------------ - -namespace v2rayUpgrade.Properties { - - - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.3.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { - - private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default { - get { - return defaultInstance; - } - } - } -} diff --git a/v2rayN/v2rayUpgrade/Properties/Settings.settings b/v2rayN/v2rayUpgrade/Properties/Settings.settings deleted file mode 100644 index 39645652..00000000 --- a/v2rayN/v2rayUpgrade/Properties/Settings.settings +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/v2rayN/v2rayUpgrade/app.manifest b/v2rayN/v2rayUpgrade/app.manifest deleted file mode 100644 index 3081161b..00000000 --- a/v2rayN/v2rayUpgrade/app.manifest +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/v2rayN/v2rayUpgrade/v2rayUpgrade.csproj b/v2rayN/v2rayUpgrade/v2rayUpgrade.csproj index 73349b29..50bb820b 100644 --- a/v2rayN/v2rayUpgrade/v2rayUpgrade.csproj +++ b/v2rayN/v2rayUpgrade/v2rayUpgrade.csproj @@ -1,11 +1,12 @@  + - net8.0-windows - WinExe - true + Exe + net8.0 + enable + enable Copyright © 2019-2024 (GPLv3) - 1.1.0.0 - app.manifest - enable + 1.2.0.0 + \ No newline at end of file