EndPointCore.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 GetRedisServer(EndPointDTO node)
  24. {
  25. #if DEBUG
  26. return node.name switch
  27. {
  28. "bj" => "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook",
  29. "gz" => "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook",
  30. "coupon1" => "123.56.185.166:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon",
  31. _ => string.Empty
  32. };
  33. #else
  34. return node.redis_server;
  35. #endif
  36. }
  37. public static string CurrentEndPoint { get; set; }
  38. static EndPointCore()
  39. {
  40. CurrentEndPoint = Environment.GetEnvironmentVariable("EndPoint");
  41. }
  42. public static IDbConnection GetDbConnection(string connString)
  43. {
  44. var conn = new MySqlConnection(connString);
  45. return conn;
  46. }
  47. public static EndPointDTO GetOne(string node)
  48. {
  49. var list = List();
  50. if (!list.Any()) return null;
  51. var item = list.Where(s => s.name == node).FirstOrDefault();
  52. return item;
  53. }
  54. public static EndPointDTO GetCouponAdmin()
  55. {
  56. var list = List();
  57. if (!list.Any()) return null;
  58. var item = list.Where(s => s.is_coupon_api && !s.is_public_api).FirstOrDefault();
  59. #if DEBUG
  60. // item.db_server = "Server=rm-2ze74506m3gfsqe7mco.rwlb.rds.aliyuncs.com; Port=3306; Database=coupon; Uid=coupon; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60;";
  61. #endif
  62. /*
  63. Server=rm-2zey1jqxnoqy9mcc0zo.rwlb.rds.aliyuncs.com; Port=3306; Database=taoke; Uid=taoke; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60
  64. Server=rm-2zey1jqxnoqy9mcc0zo.rwlb.rds.aliyuncs.com; Port=3306; Database=taoke; Uid=taoke; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60
  65. Server=rm-2zey1jqxnoqy9mcc0zo.rwlb.rds.aliyuncs.com; Port=3306; Database=taoke; Uid=taoke; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60
  66. Server=rm-2ze74506m3gfsqe7mco.rwlb.rds.aliyuncs.com; Port=3306; Database=coupon; Uid=coupon; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60;
  67. */
  68. return item;
  69. }
  70. public static EndPointDTO GetParseAdmin()
  71. {
  72. var list = List();
  73. if (!list.Any()) return null;
  74. var item = list.Where(s => !s.is_coupon_api && !s.is_public_api).FirstOrDefault();
  75. #if DEBUG
  76. item.db_server = "Server=rm-2zey1jqxnoqy9mcc0uo.rwlb.rds.aliyuncs.com; Port=3306; Database=taoke; Uid=taoke; Pwd=67ktWBmw5G4yMs4J;SslMode=None;CharSet=utf8mb4;ConnectionTimeout=60;";
  77. #endif
  78. return item;
  79. }
  80. public static IEnumerable<EndPointDTO> List(bool force = false)
  81. {
  82. if (!force && _cached != null) return _cached;
  83. string cache_key = $"cache:end_point";
  84. var list = RedisHelper.Get<IEnumerable<EndPointDTO>>(cache_key);
  85. if (force || list == null)
  86. {
  87. lock (_lockObj)
  88. {
  89. list = new DBContext.Table("end_point")
  90. .Where("status=@status", new { status = 1 })
  91. .Select<EndPointDTO>();
  92. if (list == null) return default;
  93. RedisHelper.Set(cache_key, list, 30 * 86400);
  94. }
  95. }
  96. _cached = list;
  97. return list;
  98. }
  99. public static void Refresh()
  100. {
  101. _ = List(true);
  102. }
  103. public static List<T> ProcessEndPointNodes<T>(Func<EndPointDTO, T> nodeAction, bool force = false)
  104. {
  105. var resultList = new List<T>();
  106. var list = List(force);
  107. foreach (var node in list)
  108. {
  109. var result = nodeAction(node);
  110. if (result != null) resultList.Add(result);
  111. }
  112. return resultList;
  113. }
  114. public static async Task<List<T>> ProcessEndPointNodesTaskAsync<T>(Func<EndPointDTO, Task<T>> nodeAction, bool force = false)
  115. {
  116. var resultList = new List<T>();
  117. var list = List(force);
  118. var tasks = list.Select(node => nodeAction(node));
  119. var results = await Task.WhenAll(tasks);
  120. return results.Where(result => result != null).ToList();
  121. //foreach (var node in list)
  122. //{
  123. // var result = await nodeAction(node);
  124. // if (result != null) resultList.Add(result);
  125. //}
  126. //return resultList;
  127. }
  128. public static async Task ProcessEndPointNodesAsync(Func<EndPointDTO, Task> nodeAction, bool force = false)
  129. {
  130. var list = List(force);
  131. var tasks = list.Select(node => nodeAction(node));
  132. await Task.WhenAll(tasks);
  133. }
  134. public static async Task NotifyReload(bool onlyAccount = false, CancellationToken cancellationToken = default)
  135. {
  136. await ProcessEndPointNodesAsync(async node =>
  137. {
  138. try
  139. {
  140. string url = $"{node.api_server}Task_70160bd632/reload";
  141. if (onlyAccount) url = $"{node.api_server}Task_70160bd632/reloadAccount";
  142. await new WebClientUtility().RequestAsync(url, "GET", cancellationToken);
  143. }
  144. catch (Exception ex)
  145. {
  146. _ = new LoggerLibrary("NotifyReload", "error")
  147. .Info(node.Convert2Json())
  148. .Info(ex.Message, ex.StackTrace)
  149. .SaveAsync();
  150. NotifyCore.Notify(new NifyMessage
  151. {
  152. message = $"【NotifyReload异常】{node.description}\n{ex.Message}\n{ex.StackTrace}",
  153. priority = NifyMessagePriority.high,
  154. tags = ["red_circle"]
  155. });
  156. }
  157. });
  158. }
  159. public static List<(string, string)> NotifyCheckDeepUrl(int accountid, CancellationToken cancellationToken = default)
  160. {
  161. var result = ProcessEndPointNodes<(string, string)>(node =>
  162. {
  163. try
  164. {
  165. if (!node.is_public_api) return (node.name, "break");
  166. if (!node.status) return (node.name, "账号下线");
  167. string url = $"{node.api_server}Task_70160bd632/CheckDeepUrl?accountid={accountid}";
  168. var result = new WebClientUtility().Request(url, "GET");
  169. string body = result.Body();
  170. var root = body.Convert2JsonElement();
  171. var message = root.Read<string>("message", string.Empty);
  172. return (node.name, message);
  173. }
  174. catch (Exception ex)
  175. {
  176. _ = new LoggerLibrary("CheckDeepUrl", "error")
  177. .Info(node.Convert2Json(), $"accountid:\t{accountid}")
  178. .Info(ex.Message, ex.StackTrace)
  179. .SaveAsync();
  180. NotifyCore.Notify(new NifyMessage
  181. {
  182. message = $"【CheckDeepUrl异常】{node.description}\n{ex.Message}\n{ex.StackTrace}",
  183. priority = NifyMessagePriority.high,
  184. tags = ["red_circle"]
  185. });
  186. return (node.name, ex.Message);
  187. }
  188. });
  189. return result;
  190. }
  191. }
  192. }