增加时间锁

This commit is contained in:
shenjianxing 2025-04-23 10:01:05 +08:00
parent 4a25c334d1
commit ac5421d899
19 changed files with 653 additions and 2 deletions

View File

@ -1,3 +1,6 @@
using QFramework;
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
@ -31,5 +34,26 @@ public class FixedMainEditor
Menu.SetChecked("Tools/强制Main场景启动", isFixedMain);
return true;
}
[MenuItem("Tools/创建时间锁")]
private static void TimerLock()
{
//创建数据资源文件
//泛型是继承自ScriptableObject的类
TimerLock asset = ScriptableObject.CreateInstance<TimerLock>();
//前一步创建的资源只是存在内存中,现在要把它保存到本地
//通过编辑器API创建一个数据资源文件第二个参数为资源文件在Assets目录下的路径
AssetDatabase.CreateAsset(asset, "Assets/TimerLock.asset");
//保存创建的资源
AssetDatabase.SaveAssets();
//刷新界面
AssetDatabase.Refresh();
}
}
#endif

View File

@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "时间锁", menuName = "创建时间锁/配置文件")]
public class TimerLock : ScriptableObject
{
[Header("软件有效期")]
public string time;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3fa014c2238b5a942b96b9f55a3f9841
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -29,6 +29,34 @@ public class VirtualFPostProcess : IPostprocessBuildWithReport
Debug.LogWarning("未找到数据目录: " + dataFolderPath);
}
DeletAssetBundle(buildOutputPath);
// 生成时间锁
GneratorTimerLock();
}
/// <summary>
/// 生成时间锁文件
/// </summary>
public void GneratorTimerLock()
{
if (File.Exists(Application.dataPath + "/TimerLock.asset"))
{
string path = "Assets/TimerLock.asset";
var asset = AssetDatabase.LoadAssetAtPath<TimerLock>(path);
if (asset != null)
{
if (string.IsNullOrEmpty(asset.time) == false)
{
//第一次获取获取系统时间
DateTime currentDateTime = DateTime.Now;
string RecordData = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");
string strMerge = asset.time + "|" + RecordData;
EncryptFileCreator.EncryptAndSaveData(strMerge, "Timer.txt");
}
}
}
}
/// <summary>
@ -64,7 +92,7 @@ public class VirtualFPostProcess : IPostprocessBuildWithReport
Debug.LogError($"Directory not found: {path}");
}
}
else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows|| EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows64)
else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows || EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows64)
{
string path = Path.Combine(buildOutPutPath, "VirtualFramwork_Data", "StreamingAssets", "AssetBundles");
if (Directory.Exists(path))

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4fec87e21a9630c46b1a6ef7dec72ea6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bb335401f53d3c94490f449b410da1ba
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,88 @@
using System;
using System.IO;
using UnityEngine;
using UnityEngine.Events;
public class DateManager : MonoBehaviour
{
private string EndTimer;//结束时间
private string RecordData;
private void Start()
{
UpdateTime();
}
/// <summary>
/// 更新系统时间
/// </summary>
public void UpdateTime(UnityAction back1 = null, UnityAction back2 = null, UnityAction back3 = null)
{
try
{
string datas = DecryptFileReader.ReadAndDecryptData("Timer.txt");
EndTimer = datas.Split('|')[0];
RecordData = datas.Split('|')[1];
//第一次获取获取系统时间
DateTime currentDateTime = DateTime.Now;
string Data = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");
if (DateTime.TryParse(RecordData, out DateTime recordDateTime) && DateTime.TryParse(Data, out DateTime nowDateTime))
{
if (recordDateTime > nowDateTime)
{
Debug.Log("仿真文件被损坏,请联系管理员进行修复");
back1?.Invoke();
}
else
{
//把上一次存储得系统时间更新到最新
string timer = "Timer.txt";
RecordData = Data;
string strMerge = EndTimer + "|" + RecordData;
EncryptFileCreator.EncryptAndSaveData(strMerge, timer);
back3?.Invoke();
}
}
}
catch (Exception e)
{
Debug.LogError($"数据出错: {e.Message}");
back1?.Invoke();
}
if (JudgeExpire())
{
Debug.Log("请联系管理员进行升级");
back2?.Invoke();
}
}
/// <summary>
/// 判断是否到期
/// </summary>
/// <returns></returns>
public bool JudgeExpire()
{
if (DateTime.TryParse(EndTimer, out DateTime endDataTime) && DateTime.TryParse(RecordData, out DateTime recordDateTime))
{
//结束日期小于目前日期代表到期
if (endDataTime < recordDateTime)
{
return true;
}
}
return false;
}
/// <summary>
/// 给客户打包的时候,需要设置EndTimer,然后运行一次生成文件并注释掉
/// </summary>
public void CreatTimer()
{
//第一次获取获取系统时间
DateTime currentDateTime = DateTime.Now;
RecordData = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss");
string strMerge = EndTimer + "|" + RecordData;
EncryptFileCreator.EncryptAndSaveData(strMerge, "Timer.txt");
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 718cf7f3f1b16f141ab751a37af8cae1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,44 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
public class DecryptFileReader
{
private static byte[] key = Encoding.UTF8.GetBytes("Sixteen byte key"); // 加密密钥,需与加密时一致
private static byte[] iv = Encoding.UTF8.GetBytes("InitializationVe"); // 确保IV长度为16字节
public static string ReadAndDecryptData(string filePath)
{
string fullPath = Path.Combine(Application.streamingAssetsPath, filePath);
if (File.Exists(fullPath) == false)
{
return "";
}
// 读取加密文件
byte[] encryptedData = File.ReadAllBytes(fullPath);
// 创建AES解密器
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// 创建内存流和加密流
using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
byte[] decryptedBytes = new byte[encryptedData.Length];
int decryptedByteCount = csDecrypt.Read(decryptedBytes, 0, decryptedBytes.Length);
// 将解密后的数据转换为字符串
string decryptedData = Encoding.UTF8.GetString(decryptedBytes, 0, decryptedByteCount);
return decryptedData;
}
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 354bc54210a77764ebad2d49b5e927ba
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,56 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
public class EncryptFileCreator
{
private static byte[] key = Encoding.UTF8.GetBytes("Sixteen byte key"); // 加密密钥需16字节
private static byte[] iv = Encoding.UTF8.GetBytes("InitializationVe"); // 确保IV长度为16字节
public static void EncryptAndSaveData(string data,string path)
{
string filePath = Path.Combine(Application.streamingAssetsPath, path);
if (!File.Exists(filePath))
{
try
{
// 创建文件
File.WriteAllText(filePath, "");
Debug.Log("文件已创建");
}
catch (IOException e)
{
Debug.LogError($"创建文件时出错: {e.Message}");
}
}
// 将数据转换为字节数组
byte[] plainText = Encoding.UTF8.GetBytes(data);
// 创建AES加密器
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// 创建内存流和加密流
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(plainText, 0, plainText.Length);
csEncrypt.FlushFinalBlock();
// 获取加密后的数据
byte[] encryptedData = msEncrypt.ToArray();
// 保存加密文件到StreamingAssets文件夹
string fullPath = Path.Combine(Application.streamingAssetsPath, filePath);
File.WriteAllBytes(fullPath, encryptedData);
}
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 22f7660ba2e708d4eb33622fe324499b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,73 @@
using QFramework;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class HttpRestful : MonoSingleton<HttpRestful>
{
public void Get(string url, Action<bool, string> actionResult = null)
{
StartCoroutine(_Get(url, actionResult));
}
private IEnumerator _Get(string url, Action<bool, string> action)
{
using (UnityWebRequest request = UnityWebRequest.Get(url))
{
yield return request.SendWebRequest();
string resstr = "";
if (request.isNetworkError || request.isHttpError)
{
resstr = request.error;
}
else
{
resstr = request.downloadHandler.text;
}
if (action != null)
{
action(request.isHttpError, resstr);
}
}
}
public void Post(string url, string form, Action<bool, DownloadHandler> callBack, string Header = null, string HeaderValue = null, int timeOut = 3)
{
StartCoroutine(_Post(url, form, callBack, Header, HeaderValue, timeOut));
}
public IEnumerator _Post(string url, string form, Action<bool, DownloadHandler> callBack, string Header = null, string HeaderValue = null, int timeOut = 3)
{
//请求链接并将form对象发送到远程服务器
using (UnityWebRequest webRequest = UnityWebRequest.Post(url, form, "application/json"))//;charset=utf-8
{
if (!string.IsNullOrEmpty(Header) && !string.IsNullOrEmpty(HeaderValue))
{
webRequest.SetRequestHeader(Header, HeaderValue);
}
webRequest.timeout = timeOut * 1000;
yield return webRequest.SendWebRequest();
if (webRequest.result!= UnityWebRequest.Result.Success)
{
Debug.Log(webRequest.error);
callBack?.Invoke(false, webRequest.downloadHandler);
}
else
{
callBack?.Invoke(true, webRequest.downloadHandler);
}
//if (webRequest.isHttpError || webRequest.isNetworkError)
//{
// //Debug.LogError("===========");
// callBack?.Invoke(false, null);
//}
//else
//{
// callBack?.Invoke(true, webRequest.downloadHandler);
//}
};
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cca16177d7a134a43a4fb3320d5b8fc7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 32677377141e60248a7a2484825ce1d4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,46 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LicenceData
{
public string project_id;
public string user_name;
public string license_code;
public string host_id;
public string duration; //使用时长
public string sign;
}
public class Datas
{
public static string Project_id = "7";
public static string secretKey = "pZNwkYoMRp7MG_O7aGtmA";
public static string UserName;
public static string Licensecode;
public static string Hostid;
public static int Duration;//持续时间
}
[Serializable]
public class License
{
public int id;
public string user_name;
public string license_code;
public int is_authorized;
public string host_id;
public string authorization_at;
public string authorization_end_at;
public string created_at;
public int login_count;
public int duration;
public string last_login;
}
[Serializable]
public class LicenseValidationResponse
{
public string status;
public string message;
public License license;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8f40d10c2dcbad941ac179b38fde96bd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,180 @@
using Newtonsoft.Json;
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 = JsonConvert.SerializeObject(licence);
HttpRestful.Instance.Post("https://locallicense.zxkedu.com/api/license/validate/", json, (m, n) =>
{
JsonConvert.DeserializeObject(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;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 13a2f840c2d90a44b8473ca7cf9adef1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: