using dodohold.core; using System.Net; namespace molilian.core { public partial class ProxyNodesCore { private static string _end_point; static ProxyNodesCore() { _end_point = Environment.GetEnvironmentVariable("EndPoint"); } private static readonly object _lockObj = new(); private static IEnumerable _cached; public static string GetSelectOne(string node) { var list = List(); if (!list.Any()) return null; if (!node.Contains(",")) return node; // 处理包含逗号的节点参数 string selectedNode = node; var nodes = node.Split(',', StringSplitOptions.RemoveEmptyEntries) .Select(n => n.Trim()) .Where(n => !string.IsNullOrEmpty(n)) .ToArray(); if (nodes.Length > 0) { // 随机选择一个节点 var random = new Random(); selectedNode = nodes[random.Next(nodes.Length)]; } return selectedNode; } public static WebProxy? GetOne(string node) { var list = List(); if (!list.Any()) return null; // 处理包含逗号的节点参数 string selectedNode = node; if (node.Contains(",")) { var nodes = node.Split(',', StringSplitOptions.RemoveEmptyEntries) .Select(n => n.Trim()) .Where(n => !string.IsNullOrEmpty(n)) .ToArray(); if (nodes.Length > 0) { // 随机选择一个节点 var random = new Random(); selectedNode = nodes[random.Next(nodes.Length)]; } } var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, selectedNode)) .OrderBy(l => Guid.NewGuid()) .FirstOrDefault(); if (ProxyNodesDTO == null) return null; return new WebProxy { Address = new Uri(ProxyNodesDTO.server), Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password) }; } public static WebProxy? GetOne(int nodeid) { var list = List(); if (!list.Any()) return null; var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, nodeid)).OrderBy(l => Guid.NewGuid()).FirstOrDefault(); if (ProxyNodesDTO == null) return null; return new WebProxy { Address = new Uri(ProxyNodesDTO.server), Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password) }; } private static bool SelectEndPointUseProxy(ProxyNodesDTO item, string node = "") { if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point)) { if (item.end_point != _end_point) return false; } return string.IsNullOrEmpty(node) || item.name == node; } private static bool SelectEndPointUseProxy(ProxyNodesDTO item, int nodeid) { if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point)) { if (item.end_point != _end_point) return false; } return nodeid > 0 || item.id == nodeid; } public static WebProxy? RandomOne(string excludeNode = "") { #if DEBUG return null; #endif var list = List(); if (!list.Any()) return null; var ProxyNodesDTO = list.Where(e => SelectEndPointExcludeNodeUseProxy(e, string.Empty)).OrderBy(l => Guid.NewGuid()).FirstOrDefault(); if (ProxyNodesDTO == null) return null; return new WebProxy { Address = new Uri(ProxyNodesDTO.server), Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password) }; } private static bool SelectEndPointExcludeNodeUseProxy(ProxyNodesDTO item, string excludeNode = "") { if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point)) { if (item.end_point != _end_point) return false; } return string.IsNullOrEmpty(excludeNode) || item.name != excludeNode; } public static IEnumerable List(bool force = false) { if (!force && _cached != null) return _cached; string cache_key = $"cache:proxy_nodes"; var list = RedisHelper.Get>(cache_key); if (force || list == null) { lock (_lockObj) { list = new DBContext.Table("proxy_nodes") .Where("status=@status", new { status = 1 }) .Select(); if (list == null) return default; RedisHelper.Set(cache_key, list, 30 * 86400); } } _cached = list; return list; } public static void Refresh() { _ = List(true); } } }