ProxyNodesCore.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. namespace molilian.core
  14. {
  15. public partial class ProxyNodesCore
  16. {
  17. private static readonly object _lockObj = new();
  18. private static IEnumerable<ProxyNodesDTO> _cached;
  19. public static WebProxy? GetOne(string node)
  20. {
  21. var list = List();
  22. if (!list.Any()) return null;
  23. var ProxyNodesDTO = list.Where(s => s.name == node).FirstOrDefault();
  24. if (ProxyNodesDTO == null) return null;
  25. return new WebProxy
  26. {
  27. Address = new Uri(ProxyNodesDTO.server),
  28. Credentials = new NetworkCredential(ProxyNodesDTO.username, ProxyNodesDTO.password)
  29. };
  30. }
  31. public static IEnumerable<ProxyNodesDTO> List(bool force = false)
  32. {
  33. if (!force && _cached != null) return _cached;
  34. string cache_key = $"cache:proxy_nodes";
  35. var list = RedisHelper.Get<IEnumerable<ProxyNodesDTO>>(cache_key);
  36. if (force || list == null)
  37. {
  38. lock (_lockObj)
  39. {
  40. list = new DBContext.Table("proxy_nodes")
  41. .Where("status=@status", new { status = 1 })
  42. .Select<ProxyNodesDTO>();
  43. if (list == null) return default;
  44. RedisHelper.Set(cache_key, list, 30 * 86400);
  45. }
  46. }
  47. _cached = list;
  48. return list;
  49. }
  50. public static void Refresh()
  51. {
  52. _cached = null;
  53. _ = List(true);
  54. }
  55. }
  56. }