ProxyNodesCore.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(s => s.name == node).FirstOrDefault();
  30. var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, node)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  31. if (ProxyNodesDTO == null) return null;
  32. return new WebProxy
  33. {
  34. Address = new Uri(ProxyNodesDTO.server),
  35. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  36. };
  37. }
  38. public static WebProxy? RandomOne()
  39. {
  40. var list = List();
  41. if (!list.Any()) return null;
  42. var ProxyNodesDTO = list.Where(e => SelectEndPointUseProxy(e, string.Empty)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  43. if (ProxyNodesDTO == null) return null;
  44. return new WebProxy
  45. {
  46. Address = new Uri(ProxyNodesDTO.server),
  47. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  48. };
  49. }
  50. private static bool SelectEndPointUseProxy(ProxyNodesDTO item, string node="")
  51. {
  52. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  53. {
  54. if (item.end_point != _end_point) return false;
  55. }
  56. return string.IsNullOrEmpty(node) || item.name == node;
  57. }
  58. public static IEnumerable<ProxyNodesDTO> List(bool force = false)
  59. {
  60. if (!force && _cached != null) return _cached;
  61. string cache_key = $"cache:proxy_nodes";
  62. var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
  63. if (force || list == null)
  64. {
  65. lock (_lockObj)
  66. {
  67. list = new DBContext.Table("proxy_nodes")
  68. .Where("status=@status", new { status = 1 })
  69. .Select<ProxyNodesDTO>();
  70. if (list == null) return default;
  71. RedisHelper.Set(cache_key, list, 30 * 86400);
  72. }
  73. }
  74. _cached = list;
  75. return list;
  76. }
  77. public static void Refresh()
  78. {
  79. _ = List(true);
  80. }
  81. }
  82. }