EndPointCore.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Spire.Pdf.Exporting.XPS.Schema;
  10. using System.Xml.Linq;
  11. using System.Net;
  12. using TencentCloud.Mrs.V20200910.Models;
  13. using static molilian.core.TkPoolCore;
  14. using Org.BouncyCastle.Tls;
  15. using MySql.Data.MySqlClient;
  16. using System.Data;
  17. namespace molilian.core
  18. {
  19. public partial class EndPointCore
  20. {
  21. private static readonly object _lockObj = new();
  22. private static IEnumerable<EndPointDTO> _cached;
  23. public static string CurrentEndPoint { get; set; }
  24. static EndPointCore()
  25. {
  26. CurrentEndPoint = Environment.GetEnvironmentVariable("EndPoint");
  27. }
  28. public static IDbConnection GetDbConnection(string connString)
  29. {
  30. var conn = new MySqlConnection(connString);
  31. return conn;
  32. }
  33. public static EndPointDTO GetOne(string node)
  34. {
  35. var list = List();
  36. if (!list.Any()) return null;
  37. var item = list.Where(s => s.name == node).FirstOrDefault();
  38. return item;
  39. }
  40. public static EndPointDTO GetCouponAdmin()
  41. {
  42. var list = List();
  43. if (!list.Any()) return null;
  44. var item = list.Where(s => s.is_coupon_api && !s.is_public_api).FirstOrDefault();
  45. #if DEBUG
  46. item.db_server = "Server=rm-2ze74506m3gfsqe7mco.rwlb.rds.aliyuncs.com; Port=3306; Database=coupon; Uid=coupon; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60;";
  47. #endif
  48. /*
  49. Server=rm-2zey1jqxnoqy9mcc0zo.rwlb.rds.aliyuncs.com; Port=3306; Database=taoke; Uid=taoke; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60
  50. Server=rm-2zey1jqxnoqy9mcc0zo.rwlb.rds.aliyuncs.com; Port=3306; Database=taoke; Uid=taoke; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60
  51. Server=rm-2zey1jqxnoqy9mcc0zo.rwlb.rds.aliyuncs.com; Port=3306; Database=taoke; Uid=taoke; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60
  52. Server=rm-2ze74506m3gfsqe7mco.rwlb.rds.aliyuncs.com; Port=3306; Database=coupon; Uid=coupon; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60;
  53. */
  54. return item;
  55. }
  56. public static EndPointDTO GetParseAdmin()
  57. {
  58. var list = List();
  59. if (!list.Any()) return null;
  60. var item = list.Where(s => !s.is_coupon_api && !s.is_public_api).FirstOrDefault();
  61. #if DEBUG
  62. item.db_server = "Server=rm-2zey1jqxnoqy9mcc0uo.rwlb.rds.aliyuncs.com; Port=3306; Database=taoke; Uid=taoke; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60;";
  63. #endif
  64. return item;
  65. }
  66. public static IEnumerable<EndPointDTO> List(bool force = false)
  67. {
  68. if (!force && _cached != null) return _cached;
  69. string cache_key = $"cache:end_point";
  70. var list = RedisHelper.Get<IEnumerable<EndPointDTO>>(cache_key);
  71. if (force || list == null)
  72. {
  73. lock (_lockObj)
  74. {
  75. list = new DBContext.Table("end_point")
  76. .Where("status=@status", new { status = 1 })
  77. .Select<EndPointDTO>();
  78. if (list == null) return default;
  79. RedisHelper.Set(cache_key, list, 30 * 86400);
  80. }
  81. }
  82. _cached = list;
  83. return list;
  84. }
  85. public static void Refresh()
  86. {
  87. _ = List(true);
  88. }
  89. public static List<T> ProcessEndPointNodes<T>(Func<EndPointDTO, T> nodeAction, bool force = false)
  90. {
  91. var resultList = new List<T>();
  92. var list = List(force);
  93. foreach (var node in list)
  94. {
  95. var result = nodeAction(node);
  96. if (result != null) resultList.Add(result);
  97. }
  98. return resultList;
  99. }
  100. public static async Task<List<T>> ProcessEndPointNodesTaskAsync<T>(Func<EndPointDTO, Task<T>> nodeAction, bool force = false)
  101. {
  102. var resultList = new List<T>();
  103. var list = List(force);
  104. foreach (var node in list)
  105. {
  106. var result = await nodeAction(node);
  107. if (result != null) resultList.Add(result);
  108. }
  109. return resultList;
  110. }
  111. public static async Task ProcessEndPointNodesAsync(Func<EndPointDTO, Task> nodeAction, bool force = false)
  112. {
  113. var list = List(force);
  114. var tasks = new List<Task>();
  115. foreach (var node in list)
  116. {
  117. tasks.Add(nodeAction(node));
  118. }
  119. await Task.WhenAll(tasks);
  120. }
  121. public static async Task NotifyReload(bool onlyAccount = false, CancellationToken cancellationToken = default)
  122. {
  123. await ProcessEndPointNodesAsync(async node =>
  124. {
  125. try
  126. {
  127. string url = $"{node.api_server}Task_70160bd632/reload";
  128. if (onlyAccount) url = $"{node.api_server}Task_70160bd632/reloadAccount";
  129. await new WebClientUtility().RequestAsync(url, "GET", cancellationToken);
  130. }
  131. catch (Exception ex)
  132. {
  133. _ = new LoggerLibrary("NotifyReload", "error")
  134. .Info(node.Convert2Json())
  135. .Info(ex.Message, ex.StackTrace)
  136. .SaveAsync();
  137. NotifyCore.Notify(new NifyMessage
  138. {
  139. message = $"【NotifyReload异常】{node.description}\n{ex.Message}\n{ex.StackTrace}",
  140. priority = NifyMessagePriority.high,
  141. tags = ["red_circle"]
  142. });
  143. }
  144. });
  145. }
  146. public static List<(string, string)> NotifyCheckDeepUrl(int accountid, CancellationToken cancellationToken = default)
  147. {
  148. var result = ProcessEndPointNodes<(string, string)>(node =>
  149. {
  150. try
  151. {
  152. if (!node.is_public_api) return (node.name, "break");
  153. if (!node.status) return (node.name, "账号下线");
  154. string url = $"{node.api_server}Task_70160bd632/CheckDeepUrl?accountid={accountid}";
  155. var result = new WebClientUtility().Request(url, "GET");
  156. string body = result.Body();
  157. var root = body.Convert2JsonElement();
  158. var message = root.Read<string>("message", string.Empty);
  159. return (node.name, message);
  160. }
  161. catch (Exception ex)
  162. {
  163. _ = new LoggerLibrary("CheckDeepUrl", "error")
  164. .Info(node.Convert2Json(), $"accountid:\t{accountid}")
  165. .Info(ex.Message, ex.StackTrace)
  166. .SaveAsync();
  167. NotifyCore.Notify(new NifyMessage
  168. {
  169. message = $"【CheckDeepUrl异常】{node.description}\n{ex.Message}\n{ex.StackTrace}",
  170. priority = NifyMessagePriority.high,
  171. tags = ["red_circle"]
  172. });
  173. return (node.name, ex.Message);
  174. }
  175. });
  176. return result;
  177. }
  178. }
  179. }