ProxyNodesCore.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. return new WebProxy
  50. {
  51. Address = new Uri(ProxyNodesDTO.server),
  52. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  53. };
  54. }
  55. public static WebProxy? GetOne(int nodeid)
  56. {
  57. var list = List();
  58. if (!list.Any()) return null;
  59. var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, nodeid)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  60. if (ProxyNodesDTO == null) return null;
  61. return new WebProxy
  62. {
  63. Address = new Uri(ProxyNodesDTO.server),
  64. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  65. };
  66. }
  67. private static bool SelectEndPointUseProxy(ProxyNodesDTO item, string node = "")
  68. {
  69. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  70. {
  71. if (item.end_point != _end_point) return false;
  72. }
  73. return string.IsNullOrEmpty(node) || item.name == node;
  74. }
  75. private static bool SelectEndPointUseProxy(ProxyNodesDTO item, int nodeid)
  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 nodeid > 0 || item.id == nodeid;
  82. }
  83. public static WebProxy? RandomOne(string excludeNode = "")
  84. {
  85. var list = List();
  86. if (!list.Any()) return null;
  87. var ProxyNodesDTO = list.Where(e => SelectEndPointExcludeNodeUseProxy(e, string.Empty)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  88. if (ProxyNodesDTO == null) return null;
  89. return new WebProxy
  90. {
  91. Address = new Uri(ProxyNodesDTO.server),
  92. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  93. };
  94. }
  95. private static bool SelectEndPointExcludeNodeUseProxy(ProxyNodesDTO item, string excludeNode = "")
  96. {
  97. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  98. {
  99. if (item.end_point != _end_point) return false;
  100. }
  101. return string.IsNullOrEmpty(excludeNode) || item.name != excludeNode;
  102. }
  103. public static IEnumerable<ProxyNodesDTO> List(bool force = false)
  104. {
  105. if (!force && _cached != null) return _cached;
  106. string cache_key = $"cache:proxy_nodes";
  107. var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
  108. if (force || list == null)
  109. {
  110. lock (_lockObj)
  111. {
  112. list = new DBContext.Table("proxy_nodes")
  113. .Where("status=@status", new { status = 1 })
  114. .Select<ProxyNodesDTO>();
  115. if (list == null) return default;
  116. RedisHelper.Set(cache_key, list, 30 * 86400);
  117. }
  118. }
  119. _cached = list;
  120. return list;
  121. }
  122. public static void Refresh()
  123. {
  124. _ = List(true);
  125. }
  126. }
  127. }