shaoxiadiablo/Assets/AGame/Scripts/Proxy/AntiCheatProxy.cs

214 lines
5.6 KiB
C#
Raw Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2021-05-22
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "AntiCheatProxy" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
using CodeStage.AntiCheat.Detectors;
using F;
using UnityEngine;
using UnityEngine.Networking;
namespace G
{
/// <summary>
/// 反外挂
/// </summary>
public class AntiCheatProxy : GameProxy
{
#region Field
Callback2 _callback;
System.DateTime _lastCheckDateTime;
#endregion
#region Property
/// <summary>
///
/// </summary>
public bool isCheat
{
get { return isCheatMemory || isCheatTime; }
}
/// <summary>
///
/// </summary>
public bool isCheatTime
{
get
{
#if UNITY_EDITOR || DEBUG_MY
return false;
#else
return this.elapseSecond > 300;// || TimeCheatingDetector.Instance.LastResult != TimeCheatingDetector.CheckResult.CheckPassed;
#endif
}
}
/// <summary>
///
/// </summary>
public bool isCheatMemory
{
get;
private set;
}
/// <summary>
///
/// </summary>
public double elapseSecond
{
get
{
return System.Math.Abs((System.DateTime.UtcNow - _lastCheckDateTime).TotalSeconds);
}
set { _lastCheckDateTime = System.DateTime.UtcNow.AddSeconds(value); }
}
#endregion
#region Method
public override void Init()
{
}
private void MemoryDetector_CheatChecked()
{
isCheatMemory = true;
}
public bool SetServerTimestamp(int timestamp)
{
if (Mathf.Abs(Launch.LocalTimestamp - timestamp) < 1800)
{
Debug.Log("时间校验通过");
this.elapseSecond = 0;
return true;
}
return false;
}
/// <summary>
/// 检查在线时间
/// </summary>
/// <param name="callback"></param>
/// <returns>false 回调函数</returns>
public bool CheckCheat(Callback2 callback)
{
if (this.isCheatMemory)
{
callback?.Invoke(ErrorCode.MEMORY_CHEAT, "使用第三方修改工具,会导致封号.");
return false;
}
if (this.isCheatTime)
{
_callback = callback;
//TimeCheatingDetector.Instance.ForceCheck();
#if SDK_WECHAT_WASM
RequestServer();
#else
StartCoroutine(Request());
#endif
return false;
}
callback?.Invoke(0, "");
return true;
}
void RequestServer()
{
if (ServerProxy.Instance && ServerProxy.Instance.isLoginSuccess)
{
ServerProxy.Instance.GetTimestamp((error, message) =>
{
if (!this.isCheatTime)
{
_callback?.Invoke(0, "");
}
else
{
_callback?.Invoke(ErrorCode.TIME_CHEAT, "请检查时间设置");
}
});
}
else
{
_callback?.Invoke(ErrorCode.TIME_OFFLINE, "");
}
}
System.Collections.IEnumerator Request()
{
var webRequest = UnityWebRequest.Get("https://cloud.ohayoo.cn/api/v1/init/time");
webRequest.timeout = 5;
yield return webRequest.SendWebRequest();
if (!webRequest.isNetworkError && !webRequest.isHttpError)
{
var jsonTable = F.Utils.ToJsonTable(webRequest.downloadHandler.data);
if (jsonTable != null)
{
int status = jsonTable.GetInt("status", -1);
if (status == 0)
{
int timestamp = jsonTable.GetInt("server_timestamp");
Launch.SetServerTimestamp(timestamp);
if (SetServerTimestamp(timestamp))
{
_callback?.Invoke(0, "");
}
else
{
_callback?.Invoke(ErrorCode.TIME_CHEAT, "请检查时间设置");
}
}
}
}
}
#endregion
#region Proxy
static AntiCheatProxy _Instance;
public static AntiCheatProxy Instance => _Instance;
private void Awake()
{
_Instance = this;
}
private void Start()
{
ObscuredCheatingDetector.StartDetection(this.MemoryDetector_CheatChecked);
}
float _lastUpdateTime;
public override void UpdatePerSecond()
{
if (Time.realtimeSinceStartup - _lastUpdateTime > 100)
{
_lastUpdateTime = Time.realtimeSinceStartup;
#if SDK_WECHAT_WASM
RequestServer();
#else
StartCoroutine(Request());
#endif
}
}
#endregion
}
}