| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- using Spire.Pdf.Exporting.XPS.Schema;
- using System.Xml.Linq;
- using System.Net;
- using TencentCloud.Mrs.V20200910.Models;
- using static molilian.core.TkPoolCore;
- using TencentCloud.Ecm.V20190719.Models;
- 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<ProxyNodesDTO> _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<ProxyNodesDTO> List(bool force = false)
- {
- if (!force && _cached != null) return _cached;
- string cache_key = $"cache:proxy_nodes";
- var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
- if (force || list == null)
- {
- lock (_lockObj)
- {
- list = new DBContext.Table("proxy_nodes")
- .Where("status=@status", new { status = 1 })
- .Select<ProxyNodesDTO>();
- if (list == null) return default;
- RedisHelper.Set(cache_key, list, 30 * 86400);
- }
- }
- _cached = list;
- return list;
- }
- public static void Refresh()
- {
- _ = List(true);
- }
- }
- }
|