180 lines
5.8 KiB
C#
180 lines
5.8 KiB
C#
using LitJson;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
public class LicenseManager : MonoBehaviour
|
|
{
|
|
private Dictionary<string, string> Data = new();
|
|
private LicenseValidationResponse LicenseRes = new LicenseValidationResponse();
|
|
private string Hostid = "";
|
|
|
|
public void Init()
|
|
{
|
|
Hostid = GetPhysicalAddress();
|
|
Datas.Hostid = Hostid;
|
|
}
|
|
public LicenceData GetLicenceData(string projectid, string userName, string licensecode, string hostid, int duration)
|
|
{
|
|
Data.Clear();
|
|
LicenceData licence = new LicenceData();
|
|
licence.project_id = projectid;
|
|
licence.user_name = userName;
|
|
licence.license_code = licensecode;
|
|
licence.host_id = hostid;
|
|
licence.duration = duration.ToString();
|
|
if (!Data.ContainsKey("project_id"))
|
|
{
|
|
Data.Add("project_id", licence.project_id);
|
|
}
|
|
if (!Data.ContainsKey("user_name"))
|
|
{
|
|
Data.Add("user_name", licence.user_name);
|
|
}
|
|
if (!Data.ContainsKey("license_code"))
|
|
{
|
|
Data.Add("license_code", licence.license_code);
|
|
}
|
|
if (!Data.ContainsKey("host_id"))
|
|
{
|
|
Data.Add("host_id", licence.host_id);
|
|
}
|
|
if (!Data.ContainsKey("duration"))
|
|
{
|
|
Data.Add("duration", licence.duration);
|
|
}
|
|
licence.sign = GenerateSignature(Datas.secretKey, Data);
|
|
return licence;
|
|
}
|
|
/// <summary>
|
|
/// 请求
|
|
/// </summary>
|
|
/// <param name="licence"></param>
|
|
/// <param name="callBack"></param>
|
|
public void LicensePost(Action<bool> callBack = null)
|
|
{
|
|
LicenceData licence = GetLicenceData(Datas.Project_id, Datas.UserName, Datas.Licensecode, Datas.Hostid, Datas.Duration);
|
|
string json = JsonMapper.ToJson(licence);
|
|
HttpRestful.Instance.Post("https://locallicense.zxkedu.com/api/license/validate/", json, (m, n) =>
|
|
{
|
|
LicenseRes = JsonMapper.ToObject<LicenseValidationResponse>(n.text);
|
|
if (LicenseRes.status == "success")
|
|
{
|
|
EncryptedFile();
|
|
}
|
|
callBack.Invoke(m);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成密匙
|
|
/// </summary>
|
|
/// <param name="secretKey"></param>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public string GenerateSignature(string secretKey, Dictionary<string, string> data)
|
|
{
|
|
// 添加当前时间戳
|
|
//string timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
|
|
//获取当前时间戳并四舍五入到最近的分钟
|
|
long timestamp = (long)(DateTimeOffset.UtcNow.ToUnixTimeSeconds() / 60) * 60;
|
|
//long timestamp = (long)(GetTimestamp() / 60) * 60;
|
|
if (!data.ContainsKey("timestamp"))
|
|
{
|
|
data.Add("timestamp", timestamp.ToString());
|
|
}
|
|
else
|
|
{
|
|
data["timestamp"] = timestamp.ToString();
|
|
}
|
|
// 对键值对进行排序
|
|
var sortedData = data.OrderBy(kvp => kvp.Key);
|
|
Debug.Log("sortedData对键值对进行排序:" + sortedData);
|
|
|
|
// 构建查询字符串
|
|
string queryString = string.Join("&", sortedData.Select(kvp => $"{kvp.Key}={kvp.Value}"));
|
|
Debug.Log("queryString构建查询字符串:" + queryString);
|
|
// 使用HMAC-SHA256生成签名
|
|
using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey)))
|
|
{
|
|
byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(queryString));
|
|
Debug.Log("生成签名:" + BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant());
|
|
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
|
}
|
|
}
|
|
|
|
public void StartSetDuration()
|
|
{
|
|
if (gameObject.activeSelf)
|
|
{
|
|
StartCoroutine(SetDuration(60f));
|
|
}
|
|
}
|
|
|
|
IEnumerator SetDuration(float times)
|
|
{
|
|
while (true)
|
|
{
|
|
yield return new WaitForSeconds(times);
|
|
Datas.Duration++;
|
|
LicensePost();
|
|
Datas.Duration = 0;
|
|
}
|
|
}
|
|
public string GetMachineGuid()
|
|
{
|
|
if (string.IsNullOrEmpty(Hostid))
|
|
{
|
|
NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
|
|
foreach (NetworkInterface ni in nis)
|
|
{
|
|
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
|
|
ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
|
|
{
|
|
Hostid = ni.GetPhysicalAddress().ToString();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return Hostid;
|
|
}
|
|
|
|
string GetPhysicalAddress()
|
|
{
|
|
//string physicalAddress = "";
|
|
//NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
|
|
//foreach (NetworkInterface ni in nis)
|
|
//{
|
|
// if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet && ni.OperationalStatus == OperationalStatus.Up)
|
|
// {
|
|
// physicalAddress = ni.GetPhysicalAddress().ToString();
|
|
// break;
|
|
// }
|
|
//}
|
|
return SystemInfo.deviceUniqueIdentifier;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 本地存储Hostid
|
|
/// </summary>
|
|
public void EncryptedFile()
|
|
{
|
|
string dataToEncrypt = Datas.UserName + "|" + Datas.Licensecode + "|";
|
|
EncryptFileCreator.EncryptAndSaveData(dataToEncrypt, "encryptedData.txt");
|
|
}
|
|
/// <summary>
|
|
/// 读取Hostid
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string ReadDecryptedFile()
|
|
{
|
|
string decryptedData = DecryptFileReader.ReadAndDecryptData("encryptedData.txt");
|
|
return decryptedData;
|
|
}
|
|
}
|