ProxyNodesCore.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if (!node.Contains(",")) return node;
  20. // 处理包含逗号的节点参数
  21. string selectedNode = node;
  22. var nodes = node.Split(',', StringSplitOptions.RemoveEmptyEntries)
  23. .Select(n => n.Trim())
  24. .Where(n => !string.IsNullOrEmpty(n))
  25. .ToArray();
  26. if (nodes.Length > 0)
  27. {
  28. // 随机选择一个节点
  29. var random = new Random();
  30. selectedNode = nodes[random.Next(nodes.Length)];
  31. }
  32. return selectedNode;
  33. }
  34. public static WebProxy? GetOne(string node, bool all_node = false)
  35. {
  36. var list = all_node ? AllList() : List();
  37. if (!list.Any()) return null;
  38. // 处理包含逗号的节点参数
  39. string selectedNode = node;
  40. if (node.Contains(","))
  41. {
  42. var nodes = node.Split(',', StringSplitOptions.RemoveEmptyEntries)
  43. .Select(n => n.Trim())
  44. .Where(n => !string.IsNullOrEmpty(n))
  45. .ToArray();
  46. if (nodes.Length > 0)
  47. {
  48. // 随机选择一个节点
  49. var random = new Random();
  50. selectedNode = nodes[random.Next(nodes.Length)];
  51. }
  52. }
  53. var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, selectedNode))
  54. .OrderBy(l => Guid.NewGuid())
  55. .FirstOrDefault();
  56. if (ProxyNodesDTO == null) return null;
  57. return new WebProxy
  58. {
  59. Address = new Uri(ProxyNodesDTO.server),
  60. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  61. };
  62. }
  63. public static WebProxy? GetOne(int nodeid)
  64. {
  65. var list = List();
  66. if (!list.Any()) return null;
  67. var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, nodeid)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  68. if (ProxyNodesDTO == null) return null;
  69. return new WebProxy
  70. {
  71. Address = new Uri(ProxyNodesDTO.server),
  72. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  73. };
  74. }
  75. private static bool SelectEndPointUseProxy(ProxyNodesDTO item, string node = "")
  76. {
  77. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  78. {
  79. if (item.end_point != _end_point) return false;
  80. }
  81. return string.IsNullOrEmpty(node) || item.name == node;
  82. }
  83. private static bool SelectEndPointUseProxy(ProxyNodesDTO item, int nodeid)
  84. {
  85. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  86. {
  87. if (item.end_point != _end_point) return false;
  88. }
  89. return nodeid > 0 || item.id == nodeid;
  90. }
  91. public static WebProxy? RandomOne(string excludeNode = "")
  92. {
  93. #if DEBUG
  94. return null;
  95. #endif
  96. var list = List();
  97. if (!list.Any()) return null;
  98. var ProxyNodesDTO = list.Where(e => SelectEndPointExcludeNodeUseProxy(e, string.Empty)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  99. if (ProxyNodesDTO == null) return null;
  100. return new WebProxy
  101. {
  102. Address = new Uri(ProxyNodesDTO.server),
  103. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  104. };
  105. }
  106. private static bool SelectEndPointExcludeNodeUseProxy(ProxyNodesDTO item, string excludeNode = "")
  107. {
  108. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  109. {
  110. if (item.end_point != _end_point) return false;
  111. }
  112. return string.IsNullOrEmpty(excludeNode) || item.name != excludeNode;
  113. }
  114. public static IEnumerable<ProxyNodesDTO> List(bool force = false)
  115. {
  116. if (!force && _cached != null) return _cached;
  117. string cache_key = $"cache:proxy_nodes";
  118. var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
  119. if (force || list == null)
  120. {
  121. lock (_lockObj)
  122. {
  123. list = new DBContext.Table("proxy_nodes")
  124. .Where("status=@status", new { status = 1 })
  125. .Select<ProxyNodesDTO>();
  126. if (list == null) return default;
  127. RedisHelper.Set(cache_key, list, 30 * 86400);
  128. }
  129. }
  130. _cached = list;
  131. return list;
  132. }
  133. public static IEnumerable<ProxyNodesDTO> AllList(bool force = false)
  134. {
  135. if (!force && _all_cached != null) return _all_cached;
  136. string cache_key = $"cache:all_proxy_nodes";
  137. var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
  138. if (force || list == null)
  139. {
  140. lock (_lockObj)
  141. {
  142. list = new DBContext.Table("proxy_nodes")
  143. .Where("", new { status = 1 })
  144. .Select<ProxyNodesDTO>();
  145. if (list == null) return default;
  146. RedisHelper.Set(cache_key, list, 30 * 86400);
  147. }
  148. }
  149. _all_cached = list;
  150. return list;
  151. }
  152. public static void Refresh()
  153. {
  154. _ = List(true);
  155. _ = AllList(true);
  156. }
  157. }
  158. }