using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collections.Generic; using System.Linq; using System.Text; using dodohold.core; using Spire.Pdf.Exporting.XPS.Schema; using System.Xml.Linq; using System.Net; using TencentCloud.Mrs.V20200910.Models; using static molilian.core.TkPoolCore; using Org.BouncyCastle.Tls; namespace molilian.core { public partial class EndPointCore { private static readonly object _lockObj = new(); private static IEnumerable _cached; public static EndPointDTO GetOne(string node) { var list = List(); if (!list.Any()) return null; var item = list.Where(s => s.name == node).FirstOrDefault(); return item; } public static IEnumerable List(bool force = false) { if (!force && _cached != null) return _cached; string cache_key = $"cache:end_point"; var list = RedisHelper.Get>(cache_key); if (force || list == null) { lock (_lockObj) { list = new DBContext.Table("end_point") .Where("status=@status", new { status = 1 }) .Select(); if (list == null) return default; RedisHelper.Set(cache_key, list, 30 * 86400); } } _cached = list; return list; } public static void Refresh() { _ = List(true); } public static List ProcessEndPointNodes(Func nodeAction, bool force = false) { var resultList = new List(); var list = List(force); foreach (var node in list) { var result = nodeAction(node); if (result != null) resultList.Add(result); } return resultList; } public static async Task ProcessEndPointNodesAsync(Func nodeAction, bool force = false) { var list = List(force); var tasks = new List(); foreach (var node in list) { tasks.Add(nodeAction(node)); } await Task.WhenAll(tasks); } public static async Task NotifyReload(bool onlyAccount = false, CancellationToken cancellationToken = default) { await ProcessEndPointNodesAsync(async node => { try { string url = $"{node.api_server}Task_70160bd632/reload"; if (onlyAccount) url = $"{node.api_server}Task_70160bd632/reloadAccount"; await new WebClientUtility().RequestAsync(url, "GET", cancellationToken); } catch (Exception ex) { _ = new LoggerLibrary("NotifyReload", "error") .Info(node.Convert2Json()) .Info(ex.Message, ex.StackTrace) .SaveAsync(); NotifyCore.Notify(new NifyMessage { message = $"【NotifyReload异常】{node.description}\n{ex.Message}\n{ex.StackTrace}", priority = NifyMessagePriority.high, tags = ["red_circle"] }); } }); } } }