using System;
|
using System.IO;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Drawing;
|
using System.Linq;
|
using System.Text;
|
using System.Windows.Forms;
|
using System.Diagnostics;
|
using System.Runtime.InteropServices;
|
using System.Threading;
|
using System.Management;
|
using System.Management.Instrumentation;
|
using System.Security.Cryptography;
|
using System.Security;
|
using System.Collections;
|
using CefSharp;
|
using CefSharp.WinForms;
|
using System.Net;
|
|
namespace TestWebview
|
{
|
class utils
|
{
|
|
public static string user_code = string.Empty;
|
public utils()
|
{
|
}
|
|
public static string get_user_code()
|
{
|
if (string.IsNullOrEmpty(user_code))
|
{
|
user_code = GetHash("CPU >> " + cpuId() + "\nBIOS >> " + biosId() + "\nBASE >> " + baseId() + videoId() + "\nMAC >> " + macId());
|
}
|
return user_code;
|
}
|
|
|
public static bool download_latest_config_file(string url, string filename)
|
{
|
try
|
{
|
|
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
|
httpWebRequest.Method = "GET";
|
using (WebResponse response = httpWebRequest.GetResponse())
|
{
|
Stream stream = response.GetResponseStream();
|
StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);
|
File.WriteAllText(filename, streamReader.ReadToEnd());
|
}
|
return true;
|
|
}
|
catch (Exception e)
|
{
|
return false;
|
}
|
|
}
|
|
|
public static bool check_url_avaliable(string url)
|
{
|
try
|
{
|
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
|
myRequest.Method = "HEAD"; //设置提交方式可以为"get","head"等
|
myRequest.Timeout = 10000; //设置网页响应时间长度
|
myRequest.AllowAutoRedirect = true;//是否允许自动重定向
|
myRequest.UseDefaultCredentials = true;
|
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
|
return (myResponse.StatusCode == HttpStatusCode.OK);//返回响应的状态
|
|
}
|
catch (Exception e)
|
{
|
return false;
|
|
}
|
|
}
|
|
private static string GetHash(string s)
|
{
|
MD5 sec = new MD5CryptoServiceProvider();
|
ASCIIEncoding enc = new ASCIIEncoding();
|
byte[] bt = enc.GetBytes(s);
|
return GetHexString(sec.ComputeHash(bt));
|
}
|
private static string GetHexString(byte[] bt)
|
{
|
string s = string.Empty;
|
for (int i = 0; i < bt.Length; i++)
|
{
|
byte b = bt[i];
|
int n, n1, n2;
|
n = (int)b;
|
n1 = n & 15;
|
n2 = (n >> 4) & 15;
|
if (n2 > 9)
|
s += ((char)(n2 - 10 + (int)'A')).ToString();
|
else
|
s += n2.ToString();
|
if (n1 > 9)
|
s += ((char)(n1 - 10 + (int)'A')).ToString();
|
else
|
s += n1.ToString();
|
if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
|
}
|
return s;
|
}
|
#region Original Device ID Getting Code
|
//Return a hardware identifier
|
private static string identifier
|
(string wmiClass, string wmiProperty, string wmiMustBeTrue)
|
{
|
string result = "";
|
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
|
System.Management.ManagementObjectCollection moc = mc.GetInstances();
|
foreach (System.Management.ManagementObject mo in moc)
|
{
|
if (mo[wmiMustBeTrue].ToString() == "True")
|
{
|
//Only get the first one
|
if (result == "")
|
{
|
try
|
{
|
result = mo[wmiProperty].ToString();
|
break;
|
}
|
catch
|
{
|
}
|
}
|
}
|
}
|
return result;
|
}
|
//Return a hardware identifier
|
private static string identifier(string wmiClass, string wmiProperty)
|
{
|
string result = "";
|
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
|
System.Management.ManagementObjectCollection moc = mc.GetInstances();
|
foreach (System.Management.ManagementObject mo in moc)
|
{
|
//Only get the first one
|
if (result == "")
|
{
|
try
|
{
|
result = mo[wmiProperty].ToString();
|
break;
|
}
|
catch
|
{
|
}
|
}
|
}
|
return result;
|
}
|
private static string cpuId()
|
{
|
//Uses first CPU identifier available in order of preference
|
//Don't get all identifiers, as it is very time consuming
|
string retVal = identifier("Win32_Processor", "UniqueId");
|
if (retVal == "") //If no UniqueID, use ProcessorID
|
{
|
retVal = identifier("Win32_Processor", "ProcessorId");
|
if (retVal == "") //If no ProcessorId, use Name
|
{
|
retVal = identifier("Win32_Processor", "Name");
|
if (retVal == "") //If no Name, use Manufacturer
|
{
|
retVal = identifier("Win32_Processor", "Manufacturer");
|
}
|
//Add clock speed for extra security
|
retVal += identifier("Win32_Processor", "MaxClockSpeed");
|
}
|
}
|
return retVal;
|
}
|
//BIOS Identifier
|
private static string biosId()
|
{
|
return identifier("Win32_BIOS", "Manufacturer")
|
+ identifier("Win32_BIOS", "SMBIOSBIOSVersion")
|
+ identifier("Win32_BIOS", "IdentificationCode")
|
+ identifier("Win32_BIOS", "SerialNumber")
|
+ identifier("Win32_BIOS", "ReleaseDate")
|
+ identifier("Win32_BIOS", "Version");
|
}
|
//Main physical hard drive ID
|
private static string diskId()
|
{
|
return identifier("Win32_DiskDrive", "Model")
|
+ identifier("Win32_DiskDrive", "Manufacturer")
|
+ identifier("Win32_DiskDrive", "Signature")
|
+ identifier("Win32_DiskDrive", "TotalHeads");
|
}
|
//Motherboard ID
|
private static string baseId()
|
{
|
return identifier("Win32_BaseBoard", "Model")
|
+ identifier("Win32_BaseBoard", "Manufacturer")
|
+ identifier("Win32_BaseBoard", "Name")
|
+ identifier("Win32_BaseBoard", "SerialNumber");
|
}
|
//Primary video controller ID
|
private static string videoId()
|
{
|
return identifier("Win32_VideoController", "DriverVersion")
|
+ identifier("Win32_VideoController", "Name");
|
}
|
//First enabled network card ID
|
private static string macId()
|
{
|
return identifier("Win32_NetworkAdapterConfiguration",
|
"MACAddress", "IPEnabled");
|
}
|
|
|
#endregion
|
|
|
}
|
}
|