NetTimeCheck.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using Random = UnityEngine.Random;
  7. namespace Sdk.Phone
  8. {
  9. public class NetTimeCheck : MonoBehaviour
  10. {
  11. #region 初始化
  12. static NetTimeCheck()
  13. {
  14. var go = new GameObject(nameof(NetTimeCheck), typeof(NetTimeCheck));
  15. DontDestroyOnLoad(go);
  16. }
  17. #endregion
  18. #region NetTime
  19. private const string Url = "https://free.timeanddate.com/ts.php";
  20. private static readonly DateTime Date = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  21. private long startTime;
  22. private void Awake()
  23. {
  24. _serverTime = GetSecTime() - (int) Time.realtimeSinceStartup;
  25. GetServerTime();
  26. }
  27. private void GetServerTime()
  28. {
  29. if (Application.internetReachability == NetworkReachability.NotReachable)
  30. {
  31. Invoke(nameof(GetServerTime), 60f);
  32. return;
  33. }
  34. StartCoroutine(HttpGetHelper(Url, request =>
  35. {
  36. if (request.isHttpError || request.isNetworkError)
  37. {
  38. Log($"服务器时间获取错误:{request.error}");
  39. Invoke(nameof(GetServerTime), 10f);
  40. }
  41. else
  42. {
  43. var text = request.downloadHandler.text;
  44. Log($"Time:{text}");
  45. var str = text.Split('.');
  46. var serverTs = int.Parse(str[0]);
  47. var localTs = GetSecTime();
  48. Log($"服务器时间:{text},本地时间:{localTs}");
  49. ServerTime = (localTs - serverTs < 300f ? localTs : serverTs) - (int) Time.realtimeSinceStartup;
  50. TimeDelayCheck(localTs - serverTs);
  51. }
  52. }));
  53. }
  54. private static void TimeDelayCheck(float inti)
  55. {
  56. var country = System.Globalization.RegionInfo.CurrentRegion.Name;
  57. var dic = new Dictionary<string, string>();
  58. if (inti > 600)
  59. {
  60. dic.Add("country", "dayu600" + country);
  61. }
  62. else if (inti > 300)
  63. {
  64. dic.Add("country", "dayu300" + country);
  65. }
  66. else
  67. {
  68. dic.Add("country", "xiaoyu300" + country);
  69. }
  70. PottingMobile._CustomEvent("TimeDelayCheck",dic);
  71. }
  72. private IEnumerator<UnityWebRequestAsyncOperation> HttpGetHelper(string switcher,
  73. Action<UnityWebRequest> onCompleted, int timeoutSeconds = -1)
  74. {
  75. using (var request = UnityWebRequest.Get(switcher))
  76. {
  77. request.certificateHandler = new BypassCertificate();
  78. if (timeoutSeconds > 0) request.timeout = timeoutSeconds;
  79. yield return request.SendWebRequest();
  80. onCompleted?.Invoke(request);
  81. }
  82. }
  83. private static int _serverTime;
  84. /// <summary>
  85. /// 当前时间戳(服务器) (不提供标准时间, 标准时间根据不同时区有极大的变动, 由CP端自行定义时区转换)
  86. /// </summary>
  87. public static int ServerTime
  88. {
  89. get
  90. {
  91. var sutc = _serverTime + (int) Time.realtimeSinceStartup;
  92. var utc = GetSecTime();
  93. return sutc - utc > 60f ? sutc : utc;
  94. }
  95. private set => _serverTime = value;
  96. }
  97. /// <summary>
  98. /// 当前时间戳(本地)
  99. /// </summary>
  100. /// <returns></returns>
  101. public static int GetSecTime()
  102. {
  103. var ts = DateTime.UtcNow - Date;
  104. return Convert.ToInt32(ts.TotalSeconds);
  105. }
  106. #endregion
  107. #region Hack
  108. //隐藏要素(帧数顺序)
  109. private static string appClsName = "androidx.multidex.MultiDexApplication";
  110. private static int appClsMethods = 1;
  111. private static string signMD5 = "";
  112. public static string apkMD5 = "";
  113. private static int cheatNum;
  114. private static bool noCheatSign;
  115. private void Start()
  116. {
  117. #if UNITY_ANDROID
  118. StartCoroutine(LoopGetWekTime());
  119. StartCoroutine(LoopGetUnityTime());
  120. #endif
  121. }
  122. private static IEnumerator<int> LoopGetWekTime()
  123. {
  124. yield return 0;
  125. yield return 0;
  126. yield return 0;
  127. #if UNITY_ANDROID
  128. int rand = UnityEngine.Random.Range(420, 900);
  129. yield return 0;
  130. while (!noCheatSign)
  131. {
  132. rand--;
  133. yield return 0;
  134. if (rand == 399)
  135. {
  136. yield return 0;
  137. Log("hack : " + cheatNum);
  138. var dic = new Dictionary<string, string>();
  139. dic.Add("cheatNum",cheatNum+"");
  140. PottingMobile._CustomEvent("hackQuit",dic);
  141. yield return 0;
  142. Application.Quit();
  143. yield return 0;
  144. }
  145. yield return 0;
  146. }
  147. #endif
  148. }
  149. private static IEnumerator<int> LoopGetUnityTime()
  150. {
  151. yield return 0;
  152. yield return 0;
  153. yield return 0;
  154. #if UNITY_ANDROID
  155. AndroidJavaClass Player = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  156. AndroidJavaObject Activity = Player.GetStatic<AndroidJavaObject>("currentActivity");
  157. AndroidJavaObject app = Activity.Call<AndroidJavaObject>("getApplication");
  158. AndroidJavaObject PackageManager = Activity.Call<AndroidJavaObject>("getPackageManager");
  159. yield return 0;
  160. {
  161. //application 名称验证
  162. AndroidJavaObject appClass = app.Call<AndroidJavaObject>("getClass");
  163. string appName = appClass.Call<string>("getName");
  164. Log($"appName-{appName}");
  165. var bTrue = (appName == appClsName);
  166. yield return 0; //5
  167. if (!bTrue)
  168. {
  169. var dic = new Dictionary<string, string>();
  170. dic.Add("Application",appName);
  171. PottingMobile._CustomEvent("checkApplication",dic);
  172. noCheatSign = bTrue;
  173. yield return 0;
  174. cheatNum = 1;
  175. yield return 0;
  176. yield break;
  177. }
  178. var appMethods = appClass.Call<AndroidJavaObject[]>("getDeclaredMethods");
  179. Log($"appMethods-num:{appMethods.Length}");
  180. bTrue = appMethods.Length == appClsMethods;
  181. yield return 0; //6
  182. if (!bTrue)
  183. {
  184. var dic = new Dictionary<string, string>();
  185. dic.Add("Methods",appMethods.Length+"");
  186. PottingMobile._CustomEvent("checkApplicationMethods",dic);
  187. noCheatSign = bTrue;
  188. yield return 0;
  189. cheatNum = 2;
  190. yield return 0;
  191. yield break;
  192. }
  193. }
  194. yield return 0;
  195. {
  196. if (!string.IsNullOrEmpty(apkMD5))
  197. {
  198. var apkPath = Activity.Call<string>("getPackageCodePath");
  199. var apkFileMD5 = GetHashFrom(apkPath);
  200. Log($"apkFileMD5-{apkFileMD5}");
  201. var bTrue = apkFileMD5 == apkMD5;
  202. yield return 0; //8
  203. if (!bTrue)
  204. {
  205. noCheatSign = bTrue;
  206. yield return 0;
  207. cheatNum = 2;
  208. yield return 0;
  209. yield break;
  210. }
  211. }
  212. }
  213. yield return 0;
  214. {
  215. //代理类名验证
  216. AndroidJavaObject pmClass = PackageManager.Call<AndroidJavaObject>("getClass");
  217. AndroidJavaObject mPMField = pmClass.Call<AndroidJavaObject>("getDeclaredField", "mPM");
  218. mPMField.Call("setAccessible", true);
  219. AndroidJavaObject mPM = mPMField.Call<AndroidJavaObject>("get", PackageManager);
  220. AndroidJavaObject mPMClass = mPM.Call<AndroidJavaObject>("getClass");
  221. string pmName = mPMClass.Call<string>("getName");
  222. Log($"pmName-{pmName}");
  223. var bTrue = (pmName == "android.content.pm.IPackageManager$Stub$Proxy");
  224. yield return 0; //10
  225. if (!bTrue)
  226. {
  227. var dic = new Dictionary<string, string>();
  228. dic.Add("Methods",pmName);
  229. PottingMobile._CustomEvent("ProxyClass",dic);
  230. noCheatSign = bTrue;
  231. yield return 0;
  232. cheatNum = 3;
  233. yield return 0;
  234. yield break;
  235. }
  236. }
  237. yield return 0;
  238. {
  239. if (!string.IsNullOrEmpty(signMD5))
  240. {
  241. var PackageInfo =
  242. PackageManager.Call<AndroidJavaObject>("getPackageInfo", Application.identifier, 0x00000040);
  243. AndroidJavaObject[] Signatures = PackageInfo.Get<AndroidJavaObject[]>("signatures");
  244. byte[] signBytes = Signatures[0].Call<byte[]>("toByteArray");
  245. string localSign = Hash5(signBytes);
  246. Log($"localSign-{localSign}");
  247. var bTrue = localSign == signMD5;
  248. yield return 0; //12
  249. if (!bTrue)
  250. {
  251. noCheatSign = bTrue;
  252. yield return 0;
  253. cheatNum = 4;
  254. yield return 0;
  255. yield break;
  256. }
  257. }
  258. }
  259. noCheatSign = true;
  260. yield return 0;
  261. if (noCheatSign)
  262. {
  263. //未破解
  264. }
  265. else
  266. {
  267. //破解
  268. }
  269. #endif
  270. }
  271. #endregion
  272. //获取文件的md5hash值
  273. private static string GetHashFrom(string fileName)
  274. {
  275. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
  276. {
  277. byte[] buffur = new byte[fs.Length];
  278. fs.Read(buffur, 0, (int) fs.Length);
  279. return Hash5(buffur);
  280. }
  281. }
  282. /// md5加密,32位大写字母,不包含短横线
  283. private static string Hash5(byte[] input)
  284. {
  285. using (var md5 = System.Security.Cryptography.MD5.Create())
  286. {
  287. var result = md5.ComputeHash(input);
  288. var strResult = BitConverter.ToString(result);
  289. return strResult.Replace("-", "").ToLower();
  290. }
  291. }
  292. private static void Log(object msg)
  293. {
  294. Debug.Log($"Hack-{msg}");
  295. }
  296. public static void Init()
  297. {
  298. }
  299. }
  300. public class BypassCertificate : CertificateHandler
  301. {
  302. protected override bool ValidateCertificate(byte[] certificateData)
  303. {
  304. return true;
  305. }
  306. }
  307. }