You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.3 KiB

  1. #if DELETED
  2. using Apewer;
  3. using Apewer.Network;
  4. using Apewer.Web;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. namespace Apewer.Internals
  9. {
  10. internal class WebFromTrusted
  11. {
  12. #region Instance
  13. private List<string> _presetips = new List<string>();
  14. public List<string> PresetIPs { get { return _presetips; } }
  15. public string ClientIP { get; private set; }
  16. public string Mode { get; private set; }
  17. public string RemoteIP { get; private set; }
  18. public string ProxyIP { get; private set; }
  19. public bool Trusted { get; private set; }
  20. internal WebFromTrusted(IEnumerable<string> hosts)
  21. {
  22. foreach (var domain in hosts) AddPreset(domain);
  23. ClientIP = PageUtility.ClientIP;
  24. Trusted = GetTrusted();
  25. }
  26. internal WebFromTrusted(params string[] hosts) : this((IEnumerable<string>)hosts) { }
  27. private bool AddPreset(string domain)
  28. {
  29. if (domain.IsEmpty()) return false;
  30. var ip = null as string;
  31. ip = NetworkUtility.IsIP(domain) ? domain : NetworkUtility.Resolve(domain);
  32. if (ip.IsEmpty()) return false;
  33. if (!NetworkUtility.IsIP(ip)) return false;
  34. if (!_presetips.Contains(ip)) _presetips.Add(ip);
  35. return true;
  36. }
  37. private bool GetTrusted()
  38. {
  39. var ips = ClientIP.Split(",");
  40. switch (ips.Length)
  41. {
  42. case 1:
  43. {
  44. Mode = "direct";
  45. RemoteIP = ips[0].Split(":")[0];
  46. // 信任局域网访问。
  47. if (NetworkUtility.FromLAN(RemoteIP)) return true;
  48. // 检测公网地址。
  49. if (PresetIPs.Contains(RemoteIP)) return true;
  50. }
  51. break;
  52. case 2:
  53. {
  54. Mode = "proxy";
  55. ProxyIP = ips[1].Split(":")[0];
  56. RemoteIP = ips[0].Split(":")[0];
  57. // 拒绝匿源代理。
  58. if (RemoteIP.IsEmpty()) return false;
  59. // 信任局域网中的代理。
  60. if (NetworkUtility.FromLAN(ProxyIP)) return true;
  61. // 信任公网远端。(CDN:Remote=Preset Proxy=CDN)
  62. if (PresetIPs.Contains(RemoteIP)) return true;
  63. #if DEBUG
  64. // 信任预置 IP 代理。(本地调试:Remote=127.0.0.1 Proxy=Preset)
  65. if (PresetIPs.Contains(ProxyIP)) return true;
  66. #endif
  67. }
  68. break;
  69. default:
  70. {
  71. Mode = "other";
  72. }
  73. break;
  74. }
  75. return false;
  76. }
  77. #endregion
  78. #region Static Methods
  79. /// <summary>获取远端 IP。</summary>
  80. public static string GetRemoteIP()
  81. {
  82. var ip = PageUtility.ClientIP;
  83. var iof = ip.IndexOf(",");
  84. if (iof > -1) ip = ip.Substring(0, iof);
  85. ip = ip.Split(":")[0];
  86. return ip;
  87. }
  88. #endregion
  89. }
  90. }
  91. #endif