using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace LJProxy.Services { public class VersionService:ServiceBase { private static object _updateJsonSyncRoot = new object(); private static object _apkSyncRoot = new object(); public string GetUpdateJson() { string updateJson = string.Empty; string key = "UpdateJson"; lock (_updateJsonSyncRoot) { updateJson = Cache.DefaultCache.Get(key) as string; if (string.IsNullOrEmpty(updateJson)) { string updateJsonPath = @$"{AppDomain.CurrentDomain.BaseDirectory}apk\update.json"; updateJson = System.IO.File.ReadAllText(updateJsonPath, Encoding.UTF8); Cache.DefaultCache.Add(key, updateJson, Cache.CreateCacheItemPolicy(null, DateTime.Now.AddMinutes(30), new List() { updateJsonPath })); } } return updateJson; } public byte[] GetAPK(string apkName) { if (string.IsNullOrEmpty(apkName)) return null; var updateJson = GetUpdateJson(); var updateInfoObj = JObject.Parse(updateJson); string url = updateInfoObj["url"]?.ToString(); if (string.IsNullOrEmpty(url)) { return null; } Regex regx = new Regex(@"GetAppUpdate/(?.*?\.apk)", RegexOptions.IgnoreCase); var matchResult = regx.Match(url); if (matchResult.Success) { var jsonApkName = matchResult.Groups["apkName"]?.Value; if (!apkName.Equals(jsonApkName, StringComparison.OrdinalIgnoreCase)) return null; string key = apkName.ToLower(); byte[] result = null; lock (_apkSyncRoot) { result = Cache.DefaultCache.Get(key) as byte[]; if (result == null) { string apkFilePath = @$"{AppDomain.CurrentDomain.BaseDirectory}apk\{apkName}"; if (!System.IO.File.Exists(apkFilePath)) { return null; } result = System.IO.File.ReadAllBytes(apkFilePath); Cache.DefaultCache.Add(key, result, Cache.CreateCacheItemPolicy(null, DateTime.Now.AddMinutes(30), new List() { apkFilePath })); } } return result; } return null; } } }