添加SHA256检测,去除对XP的支持

This commit is contained in:
2019-08-31 22:04:15 +08:00
parent 14faab4894
commit 8a392690fa
4 changed files with 59 additions and 59 deletions

View File

@ -3,9 +3,11 @@ using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.Win32;
internal static class Win32API
{
@ -18,6 +20,7 @@ namespace setup
{
class Program
{
static double systemVersion = Convert.ToDouble(Environment.OSVersion.Version.Major + "." + Environment.OSVersion.Version.Minor);
public static bool IsAdministrator()
{
WindowsIdentity current = WindowsIdentity.GetCurrent();
@ -48,7 +51,7 @@ namespace setup
bool is64 = IsWin64(cur);
string result = "";
string arch = is64 ? "amd64" : "386";
string url = "https://server-0.sercretcore.cn/api/download?arch=" + arch + "&platform=windows";
string url = "http://server-0.sercretcore.cn/api/download?arch=" + arch + "&platform=windows";
Stream stream;
try
{
@ -166,6 +169,7 @@ namespace setup
{
FileStream fstream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
WebRequest wRequest = WebRequest.Create(url);
try
{
WebResponse wResponse = wRequest.GetResponse();
@ -263,55 +267,27 @@ namespace setup
}
}
public static string GetChecksum(string file)
{
using (FileStream stream = File.OpenRead(file))
{
var sha = new SHA256Managed();
byte[] checksum = sha.ComputeHash(stream);
return BitConverter.ToString(checksum).Replace("-", string.Empty).ToLower();
}
}
static void Main(string[] args)
{
if (systemVersion < 6.1)
{
Console.WriteLine("Minimum system support for Windows 7 or Windows Server 2008 R2.");
Console.ReadLine();
Environment.Exit(0);
}
if (IsAdministrator())
{
int systemVersion = Environment.OSVersion.Version.Major;
if (systemVersion < 6)
{
// ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
// ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
// print initial status
Console.WriteLine("Runtime: " + System.Diagnostics.FileVersionInfo.GetVersionInfo(typeof(int).Assembly.Location).ProductVersion);
Console.WriteLine("Enabled protocols: " + ServicePointManager.SecurityProtocol);
Console.WriteLine("Available protocols: ");
Boolean platformSupportsTls12 = false;
foreach (SecurityProtocolType protocol in Enum.GetValues(typeof(SecurityProtocolType)))
{
Console.WriteLine(protocol.GetHashCode());
if (protocol.GetHashCode() == 3072)
{
platformSupportsTls12 = true;
}
}
Console.WriteLine("Is Tls12 enabled: " + ServicePointManager.SecurityProtocol.HasFlag((SecurityProtocolType)3072));
// enable Tls12, if possible
if (!ServicePointManager.SecurityProtocol.HasFlag((SecurityProtocolType)3072))
{
if (platformSupportsTls12)
{
Console.WriteLine("Platform supports Tls12, but it is not enabled. Enabling it now.");
ServicePointManager.SecurityProtocol |= (SecurityProtocolType)3072;
}
else
{
Console.WriteLine("Platform does not supports Tls12.");
}
}
// disable ssl3
if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Ssl3))
{
Console.WriteLine("Ssl3SSL3 is enabled. Disabling it now.");
// disable SSL3. Has no negative impact if SSL3 is already disabled. The enclosing "if" if just for illustration.
System.Net.ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Ssl3;
}
Console.WriteLine("Enabled protocols: " + ServicePointManager.SecurityProtocol);
}
Console.Write(@"
DC-Agent
@ -375,6 +351,7 @@ SOFTWARE.");
string patternStatus = "\"status\":.";
string patternDownloadURL = "\"downloadURL\":\".*?[^\\\\]\",";
string patternSha256 = "\"sha256\":\".*?[^\\\\]\",";
string status = Regex.Matches(res, patternStatus)[0].Value.Replace("\"status\":", "");
if (status != "1")
@ -386,13 +363,26 @@ SOFTWARE.");
}
string downloadURL = Regex.Matches(res, patternDownloadURL)[0].Value.Replace("\"downloadURL\":\"", "").Replace("\",", "");
string fileSha256 = Regex.Matches(res, patternSha256)[0].Value.Replace("\"sha256\":\"", "").Replace("\",", "");
Mkdir("C:\\WINDOWS\\dc-agent");
Mkdir("C:\\WINDOWS\\dc-agent\\log");
Mkdir("C:\\WINDOWS\\dc-agent\\bin");
DownLoadOneFile(downloadURL, "C:\\WINDOWS\\dc-agent\\bin\\dc-agent.exe");
string checkSha256 = GetChecksum("C:\\WINDOWS\\dc-agent\\bin\\dc-agent.exe");
if (checkSha256 != fileSha256)
{
Console.WriteLine(checkSha256);
Console.WriteLine(fileSha256);
Console.WriteLine("Error: File sha256 check failed.");
Console.ReadLine();
Environment.Exit(0);
return;
}
Console.WriteLine(ExecuteOutCmd("C:\\WINDOWS\\dc-agent\\bin\\dc-agent.exe", "install"));
Console.WriteLine(ExecuteOutCmd("C:\\WINDOWS\\dc-agent\\bin\\dc-agent.exe", "start"));