ProxyNodesCore.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 WebProxy? GetOne(string node)
  27. {
  28. var list = List();
  29. if (!list.Any()) return null;
  30. // 处理包含逗号的节点参数
  31. string selectedNode = node;
  32. if (node.Contains(","))
  33. {
  34. var nodes = node.Split(',', StringSplitOptions.RemoveEmptyEntries)
  35. .Select(n => n.Trim())
  36. .Where(n => !string.IsNullOrEmpty(n))
  37. .ToArray();
  38. if (nodes.Length > 0)
  39. {
  40. // 随机选择一个节点
  41. var random = new Random();
  42. selectedNode = nodes[random.Next(nodes.Length)];
  43. }
  44. }
  45. var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, selectedNode))
  46. .OrderBy(l => Guid.NewGuid())
  47. .FirstOrDefault();
  48. if (ProxyNodesDTO == null) return null;
  49. #if DEBUG
  50. return new WebProxy
  51. {
  52. Address = new Uri("http://bjapi.molilian.com:20010"),
  53. Credentials = new NetworkCredential("molilian", "Bhquvmkn8zLO36lGjuqk")
  54. };
  55. #endif
  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. var list = List();
  93. if (!list.Any()) return null;
  94. var ProxyNodesDTO = list.Where(e => SelectEndPointExcludeNodeUseProxy(e, string.Empty)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  95. if (ProxyNodesDTO == null) return null;
  96. return new WebProxy
  97. {
  98. Address = new Uri(ProxyNodesDTO.server),
  99. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  100. };
  101. }
  102. private static bool SelectEndPointExcludeNodeUseProxy(ProxyNodesDTO item, string excludeNode = "")
  103. {
  104. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  105. {
  106. if (item.end_point != _end_point) return false;
  107. }
  108. return string.IsNullOrEmpty(excludeNode) || item.name != excludeNode;
  109. }
  110. public static IEnumerable<ProxyNodesDTO> List(bool force = false)
  111. {
  112. if (!force && _cached != null) return _cached;
  113. string cache_key = $"cache:proxy_nodes";
  114. var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
  115. if (force || list == null)
  116. {
  117. lock (_lockObj)
  118. {
  119. list = new DBContext.Table("proxy_nodes")
  120. .Where("status=@status", new { status = 1 })
  121. .Select<ProxyNodesDTO>();
  122. if (list == null) return default;
  123. RedisHelper.Set(cache_key, list, 30 * 86400);
  124. }
  125. }
  126. _cached = list;
  127. return list;
  128. }
  129. public static void Refresh()
  130. {
  131. _ = List(true);
  132. }
  133. }
  134. }