Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/c#/GeneralUpdate.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
app.MapGet("/versions/{clientType}/{clientVersion}/{clientAppKey}", (int clientType, string clientVersion, string clientAppKey, IUpdateService updateService) =>
{
var versions = new List<VersionDTO>();
var md5 = "9bf414990a67e74f11752d03f49b15d8";//���ɺõĸ��°��ļ���MD5�룬��Ϊ���ظ��ͻ��˵�ʱ����Ҫͬ�������֤�Ƿ����
var hash = "9bf414990a67e74f11752d03f49b15d8";//���ɺõĸ��°��ļ���MD5�룬��Ϊ���ظ��ͻ��˵�ʱ����Ҫͬ�������֤�Ƿ����
var pubTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
string version = null;
if (clientType == AppType.ClientApp)
Expand All @@ -53,7 +53,7 @@
}
var url = $"http://192.168.50.203/update.zip";//���°������ص�ַ
var name = "update";
versions.Add(new VersionDTO(md5, pubTime, version, url, name));
versions.Add(new VersionDTO(hash, pubTime, version, url, name));
return updateService.Update(clientType, clientVersion, version, clientAppKey, GetAppSecretKey(), false, versions);
});

Expand All @@ -69,7 +69,7 @@
int.TryParse(contextReq.Form["clientType"], out int clientType);
var version = contextReq.Form["clientType"].ToString();
var clientAppKey = contextReq.Form["clientAppKey"].ToString();
var md5 = contextReq.Form["md5"].ToString();
var hash = contextReq.Form["hash"].ToString();

if (!request.HasFormContentType) throw new Exception("ContentType was not included in the request !");
var form = await request.ReadFormAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<None Remove="GeneralUpdate.ClientCore.1.1.1.nupkg" />
<None Remove="GeneralUpdate.ClientCore.nuspec" />
<None Remove="GeneralUpdate.ico" />
<None Remove="HashAlgorithms\.gitkeep" />
<None Remove="nuget.exe" />
<None Remove="Pipelines\Attributes\.gitkeep" />
<None Remove="Pipelines\Context\.gitkeep" />
Expand Down Expand Up @@ -113,13 +114,17 @@
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomArgs\UnZipExceptionArgs.cs" Link="Exceptions\CustomArgs\UnZipExceptionArgs.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomArgs\UpdateExceptionArgs.cs" Link="Exceptions\CustomArgs\UpdateExceptionArgs.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomException\GeneralUpdateException.cs" Link="Exceptions\CustomException\GeneralUpdateException.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\HashAlgorithmBase.cs" Link="HashAlgorithms\HashAlgorithmBase.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\Md5HashAlgorithm.cs" Link="HashAlgorithms\Md5HashAlgorithm.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\Sha1HashAlgorithm.cs" Link="HashAlgorithms\Sha1HashAlgorithm.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\Sha256HashAlgorithm.cs" Link="HashAlgorithms\Sha256HashAlgorithm.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Attributes\DynamicallyAccessedMembersAttribute.cs" Link="Pipelines\Attributes\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Attributes\DynamicallyAccessedMemberTypes.cs" Link="Pipelines\Attributes\DynamicallyAccessedMemberTypes.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Context\BaseContext.cs" Link="Pipelines\Context\BaseContext.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\MiddlewareResolver\ActivatorMiddlewareResolver.cs" Link="Pipelines\MiddlewareResolver\ActivatorMiddlewareResolver.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Middleware\ConfigMiddleware.cs" Link="Pipelines\Middleware\ConfigMiddleware.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Middleware\HashMiddleware.cs" Link="Pipelines\Middleware\HashMiddleware.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Middleware\IMiddleware.cs" Link="Pipelines\Middleware\IMiddleware.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Middleware\MD5Middleware.cs" Link="Pipelines\Middleware\MD5Middleware.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Middleware\MiddlewareExtensions.cs" Link="Pipelines\Middleware\MiddlewareExtensions.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Middleware\MiddlewareStack.cs" Link="Pipelines\Middleware\MiddlewareStack.cs" />
<Compile Include="..\GeneralUpdate.Core\Pipelines\Middleware\PatchMiddleware.cs" Link="Pipelines\Middleware\PatchMiddleware.cs" />
Expand Down Expand Up @@ -209,6 +214,7 @@
<Folder Include="Events\MultiEventArgs\" />
<Folder Include="Exceptions\CustomArgs\" />
<Folder Include="Exceptions\CustomException\" />
<Folder Include="HashAlgorithms\" />
<Folder Include="Pipelines\MiddlewareResolver\" />
<Folder Include="Pipelines\Middleware\" />
<Folder Include="Pipelines\Context\" />
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static List<VersionInfo> ToDataObjects(List<VersionConfigDO> versionDTO)

public static VersionInfo ToDataObject(VersionConfigDO versionDO)
{
return new VersionInfo(versionDO.PubTime, versionDO.Name, versionDO.MD5, versionDO.Version, versionDO.Url);
return new VersionInfo(versionDO.PubTime, versionDO.Name, versionDO.Hash, versionDO.Version, versionDO.Url);
}
}
}
10 changes: 5 additions & 5 deletions src/c#/GeneralUpdate.Core/Domain/DO/VersionConfigDO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class VersionConfigDO
public string Url { get; set; }

/// <summary>
/// MD5 verification code
/// Hash verification code
/// </summary>
public string MD5 { get; set; }
public string Hash { get; set; }

/// <summary>
/// Update the package name.
Expand All @@ -44,17 +44,17 @@ public class VersionConfigDO
/// </summary>
/// <param name="guid"></param>
/// <param name="url"></param>
/// <param name="mD5"></param>
/// <param name="hash"></param>
/// <param name="name"></param>
/// <param name="format"></param>
/// <param name="version"></param>
/// <param name="pubTime"></param>
/// <exception cref="ArgumentNullException"></exception>
public VersionConfigDO(string guid, string url, string mD5, string name, string format, string version, long pubTime)
public VersionConfigDO(string guid, string url, string hash, string name, string format, string version, long pubTime)
{
Guid = guid ?? throw new ArgumentNullException(nameof(guid));
Url = url ?? throw new ArgumentNullException(nameof(url));
MD5 = mD5 ?? throw new ArgumentNullException(nameof(mD5));
Hash = hash ?? throw new ArgumentNullException(nameof(hash));
Name = name ?? throw new ArgumentNullException(nameof(name));
Format = format ?? throw new ArgumentNullException(nameof(format));
Version = version ?? throw new ArgumentNullException(nameof(version));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static List<VersionInfo> ToEntitys(List<VersionDTO> versionDTO)

public static VersionInfo ToEntity(VersionDTO versionDTO)
{
return new VersionInfo(versionDTO.PubTime, versionDTO.Name, versionDTO.MD5, versionDTO.Version, versionDTO.Url);
return new VersionInfo(versionDTO.PubTime, versionDTO.Name, versionDTO.Hash, versionDTO.Version, versionDTO.Url);
}
}
}
6 changes: 3 additions & 3 deletions src/c#/GeneralUpdate.Core/Domain/DTO/VersionDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
{
public class VersionDTO
{
public VersionDTO(string md5, long pubTime, string version, string url, string name)
public VersionDTO(string hash, long pubTime, string version, string url, string name)
{
MD5 = md5;
Hash = hash;
PubTime = pubTime;
Version = version;
Url = url;
Name = name;
}

public string MD5 { get; set; }
public string Hash { get; set; }

public long PubTime { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions src/c#/GeneralUpdate.Core/Domain/Entity/VersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public class VersionInfo : Entity
public VersionInfo()
{ }

public VersionInfo(long pubTime, string name, string mD5, string version, string url)
public VersionInfo(long pubTime, string name, string hash, string version, string url)
{
PubTime = pubTime;
Name = name ?? throw new ArgumentNullException(nameof(name));
MD5 = mD5 ?? throw new ArgumentNullException(nameof(mD5));
Hash = hash ?? throw new ArgumentNullException(nameof(hash));
Version = version ?? throw new ArgumentNullException(nameof(version));
Url = url ?? throw new ArgumentNullException(nameof(Url));
if (!IsURL(Url)) throw new Exception($"Illegal url {nameof(Url)}");
Expand All @@ -30,7 +30,7 @@ public VersionInfo(long pubTime, string name, string mD5, string version, string
/// <summary>
/// Compare and verify with the downloaded update package.
/// </summary>
public string MD5 { get; set; }
public string Hash { get; set; }

/// <summary>
/// The version number.
Expand Down
4 changes: 2 additions & 2 deletions src/c#/GeneralUpdate.Core/Domain/Enum/ProgressType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public enum ProgressType
Patch,

/// <summary>
/// MD5 code
/// Hash code
/// </summary>
MD5
Hash
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static List<VersionInfo> ToDataObjects(List<VersionPO> versionDTO)

public static VersionInfo ToDataObject(VersionPO versionDO)
{
return new VersionInfo(versionDO.PubTime, versionDO.Name, versionDO.MD5, versionDO.Version, versionDO.Url);
return new VersionInfo(versionDO.PubTime, versionDO.Name, versionDO.Hash, versionDO.Version, versionDO.Url);
}
}
}
2 changes: 1 addition & 1 deletion src/c#/GeneralUpdate.Core/Domain/PO/VersionPO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class VersionPO
/// <summary>
/// Compare and verify with the downloaded update package.
/// </summary>
public string MD5 { get; set; }
public string Hash { get; set; }

/// <summary>
/// The version number.
Expand Down
28 changes: 28 additions & 0 deletions src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace GeneralUpdate.Core.HashAlgorithms
{
public abstract class HashAlgorithmBase
{
public string ComputeHash(string fileName)
{
using (var hashAlgorithm = GetHashAlgorithm())
{
using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var dataArray = GetHashAlgorithm().ComputeHash(file);
var stringBuilder = new StringBuilder();
for (int i = 0; i < dataArray.Length; i++)
{
stringBuilder.Append(dataArray[i].ToString("x2"));
}
return stringBuilder.ToString();
}
}
}

protected abstract HashAlgorithm GetHashAlgorithm();
}
}
9 changes: 9 additions & 0 deletions src/c#/GeneralUpdate.Core/HashAlgorithms/Md5HashAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Security.Cryptography;

namespace GeneralUpdate.Core.HashAlgorithms
{
public class Md5HashAlgorithm : HashAlgorithmBase
{
protected override HashAlgorithm GetHashAlgorithm()=> MD5.Create();
}
}
9 changes: 9 additions & 0 deletions src/c#/GeneralUpdate.Core/HashAlgorithms/Sha1HashAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Security.Cryptography;

namespace GeneralUpdate.Core.HashAlgorithms
{
public class Sha1HashAlgorithm : HashAlgorithmBase
{
protected override HashAlgorithm GetHashAlgorithm()=> new SHA1Managed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Security.Cryptography;

namespace GeneralUpdate.Core.HashAlgorithms
{
public class Sha256HashAlgorithm : HashAlgorithmBase
{
protected override HashAlgorithm GetHashAlgorithm()=> SHA256.Create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task InvokeAsync(BaseContext context, MiddlewareStack stack)
{
try
{
EventManager.Instance.Dispatch<Action<object, MultiDownloadProgressChangedEventArgs>>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.MD5, "Update configuration file ..."));
EventManager.Instance.Dispatch<Action<object, MultiDownloadProgressChangedEventArgs>>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.Hash, "Update configuration file ..."));
await ConfigFactory.Instance.Deploy();
var node = stack.Pop();
if (node != null) await node.Next.Invoke(context, stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
using GeneralUpdate.Core.Events;
using GeneralUpdate.Core.Events.CommonArgs;
using GeneralUpdate.Core.Events.MultiEventArgs;
using GeneralUpdate.Core.HashAlgorithms;
using GeneralUpdate.Core.Pipelines.Context;
using GeneralUpdate.Core.Utils;
using System;
using System.Threading.Tasks;

namespace GeneralUpdate.Core.Pipelines.Middleware
{
public class MD5Middleware : IMiddleware
public class HashMiddleware : IMiddleware
{
public async Task InvokeAsync(BaseContext context, MiddlewareStack stack)
{
Exception exception = null;
try
{
EventManager.Instance.Dispatch<Action<object, MultiDownloadProgressChangedEventArgs>>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.MD5, "Verify file MD5 code ..."));
EventManager.Instance.Dispatch<Action<object, MultiDownloadProgressChangedEventArgs>>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.Hash, "Verify file MD5 code ..."));
var version = context.Version;
bool isVerify = VerifyFileMd5(context.ZipfilePath, version.MD5);
if (!isVerify) exception = new Exception($"The update package MD5 code is inconsistent ! version-{version.Version} MD5-{version.MD5} .");
bool isVerify = VerifyFileHash(context.ZipfilePath, version.Hash);
if (!isVerify) exception = new Exception($"The update package hash code is inconsistent ! version-{version.Version} hash-{version.Hash} .");
var node = stack.Pop();
if (node != null) await node.Next.Invoke(context, stack);
}
Expand All @@ -29,10 +29,11 @@ public async Task InvokeAsync(BaseContext context, MiddlewareStack stack)
}
}

private bool VerifyFileMd5(string fileName, string md5)
private bool VerifyFileHash(string fileName, string hash)
{
var packetMD5 = FileUtil.GetFileMD5(fileName);
return md5.ToUpper().Equals(packetMD5.ToUpper());
var hashAlgorithm = new Sha256HashAlgorithm();
var hashSha256 = hashAlgorithm.ComputeHash(fileName);
return string.Equals(hash, hashSha256, StringComparison.OrdinalIgnoreCase);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task InvokeAsync(BaseContext context, MiddlewareStack stack)
EventManager.Instance.Dispatch<Action<object, MultiDownloadProgressChangedEventArgs>>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.Updatefile, "In the unzipped file ..."));
var version = context.Version;
bool isUnzip = UnZip(context);
if (!isUnzip) throw exception = new Exception($"Unzip file failed , Version-{version.Version} MD5-{version.MD5} !");
if (!isUnzip) throw exception = new Exception($"Unzip file failed , Version-{version.Version} MD5-{version.Hash} !");
//await ConfigFactory.Instance.Scan(context.SourcePath, context.TargetPath);
var node = stack.Pop();
if (node != null) await node.Next.Invoke(context, stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override void Execute()
.Build();

var pipelineBuilder = new PipelineBuilder<BaseContext>(context).
UseMiddleware<MD5Middleware>().
UseMiddleware<HashMiddleware>().
UseMiddleware<ZipMiddleware>().
UseMiddleware<PatchMiddleware>();
await pipelineBuilder.Launch();
Expand Down Expand Up @@ -146,7 +146,7 @@ private void WaitForProcessToStart(string applicationPath, TimeSpan timeout, Act
var startTime = DateTime.UtcNow;
while (DateTime.UtcNow - startTime < timeout)
{
Thread.Sleep(2 * 1000);
Thread.Sleep(3 * 1000);
if (!process.HasExited)
{
callbackAction?.Invoke(applicationPath);
Expand Down
23 changes: 0 additions & 23 deletions src/c#/GeneralUpdate.Core/Utils/FileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,11 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace GeneralUpdate.Core.Utils
{
public static class FileUtil
{
public static string GetFileMD5(string fileName)
{
try
{
var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
var md5 = new MD5CryptoServiceProvider();
var retVal = md5.ComputeHash(file);
file.Close();
var sb = new StringBuilder();
for (var i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception)
{
return string.Empty;
}
}

public static void DirectoryCopy(string sourceDirName, string destDirName,
bool copySubDirs, bool isOverWrite, Action<string> action)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class ConfigEntity
public string Name { get; set; }

/// <summary>
/// file md5 code .
/// file hash code .
/// </summary>
public string MD5 { get; set; }
public string Hash { get; set; }

/// <summary>
/// configuation file content.
Expand Down
Loading