GenericHTTPBasicDOSProtector.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System.Collections;
  28. namespace OpenSim.Framework.Servers.HttpServer
  29. {
  30. public class GenericHTTPDOSProtector
  31. {
  32. private readonly GenericHTTPMethod _normalMethod;
  33. private readonly GenericHTTPMethod _throttledMethod;
  34. private readonly BasicDosProtectorOptions _options;
  35. private readonly BasicDOSProtector _dosProtector;
  36. public GenericHTTPDOSProtector(GenericHTTPMethod normalMethod, GenericHTTPMethod throttledMethod, BasicDosProtectorOptions options)
  37. {
  38. _normalMethod = normalMethod;
  39. _throttledMethod = throttledMethod;
  40. _options = options;
  41. _dosProtector = new BasicDOSProtector(_options);
  42. }
  43. public Hashtable Process(Hashtable request)
  44. {
  45. Hashtable process = null;
  46. string clientstring= GetClientString(request);
  47. string endpoint = GetRemoteAddr(request);
  48. if (_dosProtector.Process(clientstring, endpoint))
  49. process = _normalMethod(request);
  50. else
  51. process = _throttledMethod(request);
  52. if (_options.MaxConcurrentSessions>0)
  53. _dosProtector.ProcessEnd(clientstring, endpoint);
  54. return process;
  55. }
  56. private string GetRemoteAddr(Hashtable request)
  57. {
  58. string remoteaddr = "";
  59. if (!request.ContainsKey("headers"))
  60. return remoteaddr;
  61. Hashtable requestinfo = (Hashtable)request["headers"];
  62. if (!requestinfo.ContainsKey("remote_addr"))
  63. return remoteaddr;
  64. object remote_addrobj = requestinfo["remote_addr"];
  65. if (remote_addrobj != null)
  66. {
  67. if (!string.IsNullOrEmpty(remote_addrobj.ToString()))
  68. {
  69. remoteaddr = remote_addrobj.ToString();
  70. }
  71. }
  72. return remoteaddr;
  73. }
  74. private string GetClientString(Hashtable request)
  75. {
  76. string clientstring = "";
  77. if (!request.ContainsKey("headers"))
  78. return clientstring;
  79. Hashtable requestinfo = (Hashtable)request["headers"];
  80. if (_options.AllowXForwardedFor && requestinfo.ContainsKey("x-forwarded-for"))
  81. {
  82. object str = requestinfo["x-forwarded-for"];
  83. if (str != null)
  84. {
  85. if (!string.IsNullOrEmpty(str.ToString()))
  86. {
  87. return str.ToString();
  88. }
  89. }
  90. }
  91. if (!requestinfo.ContainsKey("remote_addr"))
  92. return clientstring;
  93. object remote_addrobj = requestinfo["remote_addr"];
  94. if (remote_addrobj != null)
  95. {
  96. if (!string.IsNullOrEmpty(remote_addrobj.ToString()))
  97. {
  98. clientstring = remote_addrobj.ToString();
  99. }
  100. }
  101. return clientstring;
  102. }
  103. }
  104. }