ProxyNodesCore.cs 5.5 KB

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