ProxyNodesCore.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. namespace molilian.core
  15. {
  16. public partial class ProxyNodesCore
  17. {
  18. private static string _end_point;
  19. static ProxyNodesCore()
  20. {
  21. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  22. }
  23. private static readonly object _lockObj = new();
  24. private static IEnumerable<ProxyNodesDTO> _cached;
  25. public static WebProxy? GetOne(string node)
  26. {
  27. var list = List();
  28. if (!list.Any()) return null;
  29. var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, node)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  30. if (ProxyNodesDTO == null) return null;
  31. return new WebProxy
  32. {
  33. Address = new Uri(ProxyNodesDTO.server),
  34. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  35. };
  36. }
  37. private static bool SelectEndPointUseProxy(ProxyNodesDTO item, string node = "")
  38. {
  39. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  40. {
  41. if (item.end_point != _end_point) return false;
  42. }
  43. return string.IsNullOrEmpty(node) || item.name == node;
  44. }
  45. public static WebProxy? RandomOne(string excludeNode = "")
  46. {
  47. var list = List();
  48. if (!list.Any()) return null;
  49. var ProxyNodesDTO = list.Where(e => SelectEndPointExcludeNodeUseProxy(e, string.Empty)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  50. if (ProxyNodesDTO == null) return null;
  51. return new WebProxy
  52. {
  53. Address = new Uri(ProxyNodesDTO.server),
  54. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  55. };
  56. }
  57. private static bool SelectEndPointExcludeNodeUseProxy(ProxyNodesDTO item, string excludeNode = "")
  58. {
  59. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  60. {
  61. if (item.end_point != _end_point) return false;
  62. }
  63. return string.IsNullOrEmpty(excludeNode) || item.name != excludeNode;
  64. }
  65. public static IEnumerable<ProxyNodesDTO> List(bool force = false)
  66. {
  67. if (!force && _cached != null) return _cached;
  68. string cache_key = $"cache:proxy_nodes";
  69. var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
  70. if (force || list == null)
  71. {
  72. lock (_lockObj)
  73. {
  74. list = new DBContext.Table("proxy_nodes")
  75. .Where("status=@status", new { status = 1 })
  76. .Select<ProxyNodesDTO>();
  77. if (list == null) return default;
  78. RedisHelper.Set(cache_key, list, 30 * 86400);
  79. }
  80. }
  81. _cached = list;
  82. return list;
  83. }
  84. public static void Refresh()
  85. {
  86. _ = List(true);
  87. }
  88. }
  89. }