ProxyNodesCore.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using dodohold.core;
  2. using System.Net;
  3. namespace molilian.core
  4. {
  5. public partial class ProxyNodesCore
  6. {
  7. private static string _end_point;
  8. static ProxyNodesCore()
  9. {
  10. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  11. }
  12. private static readonly object _lockObj = new();
  13. private static IEnumerable<ProxyNodesDTO> _cached;
  14. private static IEnumerable<ProxyNodesDTO> _all_cached;
  15. public static string GetSelectOne(string node, bool all_node = false)
  16. {
  17. var list = all_node ? AllList() : List();
  18. if (!list.Any()) return null;
  19. var nodes = SplitNodeNames(node).ToArray();
  20. if (nodes.Length == 0) return node?.Trim();
  21. if (nodes.Length == 1) return nodes[0];
  22. // 处理包含逗号的节点参数
  23. string selectedNode = node;
  24. var random = new Random();
  25. selectedNode = nodes[random.Next(nodes.Length)];
  26. return selectedNode;
  27. }
  28. public static WebProxy? GetOne(string node, bool all_node = false)
  29. {
  30. var list = all_node ? AllList() : List();
  31. if (!list.Any()) return null;
  32. // 处理包含逗号的节点参数
  33. string selectedNode = node;
  34. var nodes = SplitNodeNames(node).ToArray();
  35. if (nodes.Length > 1)
  36. {
  37. var random = new Random();
  38. selectedNode = nodes[random.Next(nodes.Length)];
  39. }
  40. else if (nodes.Length == 1)
  41. {
  42. selectedNode = nodes[0];
  43. }
  44. var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, selectedNode))
  45. .OrderBy(l => Guid.NewGuid())
  46. .FirstOrDefault();
  47. if (ProxyNodesDTO == null) return null;
  48. return new WebProxy
  49. {
  50. Address = new Uri(ProxyNodesDTO.server),
  51. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  52. };
  53. }
  54. public static WebProxy? GetOne(int nodeid)
  55. {
  56. var list = List();
  57. if (!list.Any()) return null;
  58. var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, nodeid)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  59. if (ProxyNodesDTO == null) return null;
  60. return new WebProxy
  61. {
  62. Address = new Uri(ProxyNodesDTO.server),
  63. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  64. };
  65. }
  66. private static bool SelectEndPointUseProxy(ProxyNodesDTO item, string node = "")
  67. {
  68. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  69. {
  70. if (item.end_point != _end_point) return false;
  71. }
  72. return string.IsNullOrEmpty(node) || IsMatchNode(item, node);
  73. }
  74. private static bool SelectEndPointUseProxy(ProxyNodesDTO item, int nodeid)
  75. {
  76. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  77. {
  78. if (item.end_point != _end_point) return false;
  79. }
  80. return nodeid <= 0 || item.id == nodeid;
  81. }
  82. public static WebProxy? RandomOne(string excludeNode = "")
  83. {
  84. #if DEBUG
  85. return null;
  86. #endif
  87. var list = List();
  88. if (!list.Any()) return null;
  89. var ProxyNodesDTO = list.Where(e => SelectEndPointExcludeNodeUseProxy(e, string.Empty)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  90. if (ProxyNodesDTO == null) return null;
  91. return new WebProxy
  92. {
  93. Address = new Uri(ProxyNodesDTO.server),
  94. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  95. };
  96. }
  97. private static bool SelectEndPointExcludeNodeUseProxy(ProxyNodesDTO item, string excludeNode = "")
  98. {
  99. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  100. {
  101. if (item.end_point != _end_point) return false;
  102. }
  103. return string.IsNullOrEmpty(excludeNode) || !IsMatchNode(item, excludeNode);
  104. }
  105. public static IEnumerable<string> SplitNodeNames(string nodeNames)
  106. {
  107. if (string.IsNullOrWhiteSpace(nodeNames))
  108. {
  109. return Array.Empty<string>();
  110. }
  111. return nodeNames
  112. .Replace(',', ',')
  113. .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
  114. .Where(item => !string.IsNullOrWhiteSpace(item));
  115. }
  116. public static bool IsMatchNode(ProxyNodesDTO item, string nodeName)
  117. {
  118. if (string.IsNullOrWhiteSpace(nodeName))
  119. {
  120. return false;
  121. }
  122. var configuredNodes = SplitNodeNames(nodeName)
  123. .ToHashSet(StringComparer.OrdinalIgnoreCase);
  124. if (configuredNodes.Count == 0)
  125. {
  126. return false;
  127. }
  128. return new[] { item.nodeName, item.name }
  129. .Where(value => !string.IsNullOrWhiteSpace(value))
  130. .Select(value => value.Trim())
  131. .Any(value => configuredNodes.Contains(value));
  132. }
  133. public static IEnumerable<ProxyNodesDTO> List(bool force = false)
  134. {
  135. if (!force && _cached != null) return _cached;
  136. string cache_key = $"cache:proxy_nodes";
  137. var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
  138. if (!force && NeedReloadNodeNameCache(list))
  139. {
  140. list = null;
  141. }
  142. if (force || list == null)
  143. {
  144. lock (_lockObj)
  145. {
  146. list = new DBContext.Table("proxy_nodes")
  147. .Where("status=@status", new { status = 1 })
  148. .Select<ProxyNodesDTO>();
  149. if (list == null) return default;
  150. RedisHelper.Set(cache_key, list, 30 * 86400);
  151. }
  152. }
  153. _cached = list;
  154. return list;
  155. }
  156. public static IEnumerable<ProxyNodesDTO> AllList(bool force = false)
  157. {
  158. if (!force && _all_cached != null) return _all_cached;
  159. string cache_key = $"cache:all_proxy_nodes";
  160. var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
  161. if (!force && NeedReloadNodeNameCache(list))
  162. {
  163. list = null;
  164. }
  165. if (force || list == null)
  166. {
  167. lock (_lockObj)
  168. {
  169. list = new DBContext.Table("proxy_nodes")
  170. .Where("", new { status = 1 })
  171. .Select<ProxyNodesDTO>();
  172. if (list == null) return default;
  173. RedisHelper.Set(cache_key, list, 30 * 86400);
  174. }
  175. }
  176. _all_cached = list;
  177. return list;
  178. }
  179. public static void Refresh()
  180. {
  181. _ = List(true);
  182. _ = AllList(true);
  183. }
  184. private static bool NeedReloadNodeNameCache(IEnumerable<ProxyNodesDTO> list)
  185. {
  186. if (list == null)
  187. {
  188. return false;
  189. }
  190. var items = list.ToArray();
  191. return items.Length > 0 && items.All(item => string.IsNullOrWhiteSpace(item.nodeName));
  192. }
  193. }
  194. }