OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
BaseStreamHandlerBasicDOSProtector.cs
Go to the documentation of this file.
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 OpenSim.Framework;
28 using System.IO;
29 
30 namespace OpenSim.Framework.Servers.HttpServer
31 {
39  {
40 
41  private readonly BasicDosProtectorOptions _options;
42  private readonly BasicDOSProtector _dosProtector;
43 
44  protected BaseStreamHandlerBasicDOSProtector(string httpMethod, string path, BasicDosProtectorOptions options) : this(httpMethod, path, null, null, options) {}
45 
46  protected BaseStreamHandlerBasicDOSProtector(string httpMethod, string path, string name, string description, BasicDosProtectorOptions options)
47  : base(httpMethod, path, name, description)
48  {
49  _options = options;
50  _dosProtector = new BasicDOSProtector(_options);
51  }
52 
53  public virtual byte[] Handle(
54  string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
55  {
56  byte[] result;
57  RequestsReceived++;
58  string clientstring = GetClientString(httpRequest);
59  string endpoint = GetRemoteAddr(httpRequest);
60  if (_dosProtector.Process(clientstring, endpoint))
61  result = ProcessRequest(path, request, httpRequest, httpResponse);
62  else
63  result = ThrottledRequest(path, request, httpRequest, httpResponse);
64  if (_options.MaxConcurrentSessions > 0)
65  _dosProtector.ProcessEnd(clientstring, endpoint);
66 
67  RequestsHandled++;
68 
69  return result;
70  }
71 
72  protected virtual byte[] ProcessRequest(
73  string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
74  {
75  return null;
76  }
77 
78  protected virtual byte[] ThrottledRequest(
79  string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
80  {
81  return new byte[0];
82  }
83 
84 
85  private string GetRemoteAddr(IOSHttpRequest httpRequest)
86  {
87  string remoteaddr = string.Empty;
88  if (httpRequest.Headers["remote_addr"] != null)
89  remoteaddr = httpRequest.Headers["remote_addr"];
90 
91  return remoteaddr;
92  }
93 
94  private string GetClientString(IOSHttpRequest httpRequest)
95  {
96  string clientstring = string.Empty;
97 
98  if (_options.AllowXForwardedFor && httpRequest.Headers["x-forwarded-for"] != null)
99  clientstring = httpRequest.Headers["x-forwarded-for"];
100  else
101  clientstring = GetRemoteAddr(httpRequest);
102 
103  return clientstring;
104 
105  }
106  }
107 }
virtual byte[] ThrottledRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
BaseStreamHandlerBasicDOSProtector(string httpMethod, string path, BasicDosProtectorOptions options)
virtual byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
BaseStreamHandlerBasicDOSProtector(string httpMethod, string path, string name, string description, BasicDosProtectorOptions options)
virtual byte[] Handle(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
BaseStreamHandlerBasicDOSProtector Base streamed request handler.