ProxyNodesCore.cs 5.8 KB

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