diff options
Diffstat (limited to 'OpenSim/Framework/Servers/HttpServer/GenericHTTPBasicDOSProtector.cs')
-rw-r--r-- | OpenSim/Framework/Servers/HttpServer/GenericHTTPBasicDOSProtector.cs | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/HttpServer/GenericHTTPBasicDOSProtector.cs b/OpenSim/Framework/Servers/HttpServer/GenericHTTPBasicDOSProtector.cs new file mode 100644 index 0000000..5fc999a --- /dev/null +++ b/OpenSim/Framework/Servers/HttpServer/GenericHTTPBasicDOSProtector.cs | |||
@@ -0,0 +1,238 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Reflection; | ||
32 | using System.Net; | ||
33 | using OpenSim.Framework; | ||
34 | using log4net; | ||
35 | |||
36 | namespace OpenSim.Framework.Servers.HttpServer | ||
37 | { | ||
38 | public class GenericHTTPDOSProtector | ||
39 | { | ||
40 | private readonly GenericHTTPMethod _normalMethod; | ||
41 | private readonly GenericHTTPMethod _throttledMethod; | ||
42 | private readonly CircularBuffer<int> _generalRequestTimes; | ||
43 | private readonly BasicDosProtectorOptions _options; | ||
44 | private readonly Dictionary<string, CircularBuffer<int>> _deeperInspection; | ||
45 | private readonly Dictionary<string, int> _tempBlocked; | ||
46 | private readonly System.Timers.Timer _forgetTimer; | ||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | private readonly System.Threading.ReaderWriterLockSlim _lockSlim = new System.Threading.ReaderWriterLockSlim(); | ||
49 | |||
50 | public GenericHTTPDOSProtector(GenericHTTPMethod normalMethod, GenericHTTPMethod throttledMethod, BasicDosProtectorOptions options) | ||
51 | { | ||
52 | _normalMethod = normalMethod; | ||
53 | _throttledMethod = throttledMethod; | ||
54 | _generalRequestTimes = new CircularBuffer<int>(options.MaxRequestsInTimeframe + 1, true); | ||
55 | _generalRequestTimes.Put(0); | ||
56 | _options = options; | ||
57 | _deeperInspection = new Dictionary<string, CircularBuffer<int>>(); | ||
58 | _tempBlocked = new Dictionary<string, int>(); | ||
59 | _forgetTimer = new System.Timers.Timer(); | ||
60 | _forgetTimer.Elapsed += delegate | ||
61 | { | ||
62 | _forgetTimer.Enabled = false; | ||
63 | |||
64 | List<string> removes = new List<string>(); | ||
65 | _lockSlim.EnterReadLock(); | ||
66 | foreach (string str in _tempBlocked.Keys) | ||
67 | { | ||
68 | if ( | ||
69 | Util.EnvironmentTickCountSubtract(Util.EnvironmentTickCount(), | ||
70 | _tempBlocked[str]) > 0) | ||
71 | removes.Add(str); | ||
72 | } | ||
73 | _lockSlim.ExitReadLock(); | ||
74 | lock (_deeperInspection) | ||
75 | { | ||
76 | _lockSlim.EnterWriteLock(); | ||
77 | for (int i = 0; i < removes.Count; i++) | ||
78 | { | ||
79 | _tempBlocked.Remove(removes[i]); | ||
80 | _deeperInspection.Remove(removes[i]); | ||
81 | } | ||
82 | _lockSlim.ExitWriteLock(); | ||
83 | } | ||
84 | foreach (string str in removes) | ||
85 | { | ||
86 | m_log.InfoFormat("[{0}] client: {1} is no longer blocked.", | ||
87 | _options.ReportingName, str); | ||
88 | } | ||
89 | _lockSlim.EnterReadLock(); | ||
90 | if (_tempBlocked.Count > 0) | ||
91 | _forgetTimer.Enabled = true; | ||
92 | _lockSlim.ExitReadLock(); | ||
93 | }; | ||
94 | |||
95 | _forgetTimer.Interval = _options.ForgetTimeSpan.TotalMilliseconds; | ||
96 | } | ||
97 | public Hashtable Process(Hashtable request) | ||
98 | { | ||
99 | if (_options.MaxRequestsInTimeframe < 1) | ||
100 | return _normalMethod(request); | ||
101 | if (_options.RequestTimeSpan.TotalMilliseconds < 1) | ||
102 | return _normalMethod(request); | ||
103 | |||
104 | string clientstring = GetClientString(request); | ||
105 | |||
106 | _lockSlim.EnterReadLock(); | ||
107 | if (_tempBlocked.ContainsKey(clientstring)) | ||
108 | { | ||
109 | _lockSlim.ExitReadLock(); | ||
110 | |||
111 | if (_options.ThrottledAction == ThrottleAction.DoThrottledMethod) | ||
112 | return _throttledMethod(request); | ||
113 | else | ||
114 | throw new System.Security.SecurityException("Throttled"); | ||
115 | } | ||
116 | _lockSlim.ExitReadLock(); | ||
117 | |||
118 | _generalRequestTimes.Put(Util.EnvironmentTickCount()); | ||
119 | |||
120 | if (_generalRequestTimes.Size == _generalRequestTimes.Capacity && | ||
121 | (Util.EnvironmentTickCountSubtract(Util.EnvironmentTickCount(), _generalRequestTimes.Get()) < | ||
122 | _options.RequestTimeSpan.TotalMilliseconds)) | ||
123 | { | ||
124 | //Trigger deeper inspection | ||
125 | if (DeeperInspection(request)) | ||
126 | return _normalMethod(request); | ||
127 | if (_options.ThrottledAction == ThrottleAction.DoThrottledMethod) | ||
128 | return _throttledMethod(request); | ||
129 | else | ||
130 | throw new System.Security.SecurityException("Throttled"); | ||
131 | } | ||
132 | Hashtable resp = null; | ||
133 | try | ||
134 | { | ||
135 | resp = _normalMethod(request); | ||
136 | } | ||
137 | catch (Exception) | ||
138 | { | ||
139 | |||
140 | throw; | ||
141 | } | ||
142 | |||
143 | return resp; | ||
144 | } | ||
145 | private bool DeeperInspection(Hashtable request) | ||
146 | { | ||
147 | lock (_deeperInspection) | ||
148 | { | ||
149 | string clientstring = GetClientString(request); | ||
150 | |||
151 | |||
152 | if (_deeperInspection.ContainsKey(clientstring)) | ||
153 | { | ||
154 | _deeperInspection[clientstring].Put(Util.EnvironmentTickCount()); | ||
155 | if (_deeperInspection[clientstring].Size == _deeperInspection[clientstring].Capacity && | ||
156 | (Util.EnvironmentTickCountSubtract(Util.EnvironmentTickCount(), _deeperInspection[clientstring].Get()) < | ||
157 | _options.RequestTimeSpan.TotalMilliseconds)) | ||
158 | { | ||
159 | _lockSlim.EnterWriteLock(); | ||
160 | if (!_tempBlocked.ContainsKey(clientstring)) | ||
161 | _tempBlocked.Add(clientstring, Util.EnvironmentTickCount() + (int)_options.ForgetTimeSpan.TotalMilliseconds); | ||
162 | else | ||
163 | _tempBlocked[clientstring] = Util.EnvironmentTickCount() + (int)_options.ForgetTimeSpan.TotalMilliseconds; | ||
164 | _lockSlim.ExitWriteLock(); | ||
165 | |||
166 | m_log.WarnFormat("[{0}]: client: {1} is blocked for {2} milliseconds, X-ForwardedForAllowed status is {3}, endpoint:{4}", _options.ReportingName, clientstring, _options.ForgetTimeSpan.TotalMilliseconds, _options.AllowXForwardedFor, GetRemoteAddr(request)); | ||
167 | return false; | ||
168 | } | ||
169 | //else | ||
170 | // return true; | ||
171 | } | ||
172 | else | ||
173 | { | ||
174 | _deeperInspection.Add(clientstring, new CircularBuffer<int>(_options.MaxRequestsInTimeframe + 1, true)); | ||
175 | _deeperInspection[clientstring].Put(Util.EnvironmentTickCount()); | ||
176 | _forgetTimer.Enabled = true; | ||
177 | } | ||
178 | |||
179 | } | ||
180 | return true; | ||
181 | } | ||
182 | |||
183 | private string GetRemoteAddr(Hashtable request) | ||
184 | { | ||
185 | string remoteaddr = ""; | ||
186 | if (!request.ContainsKey("headers")) | ||
187 | return remoteaddr; | ||
188 | Hashtable requestinfo = (Hashtable)request["headers"]; | ||
189 | if (!requestinfo.ContainsKey("remote_addr")) | ||
190 | return remoteaddr; | ||
191 | object remote_addrobj = requestinfo["remote_addr"]; | ||
192 | if (remote_addrobj != null) | ||
193 | { | ||
194 | if (!string.IsNullOrEmpty(remote_addrobj.ToString())) | ||
195 | { | ||
196 | remoteaddr = remote_addrobj.ToString(); | ||
197 | } | ||
198 | |||
199 | } | ||
200 | return remoteaddr; | ||
201 | } | ||
202 | |||
203 | private string GetClientString(Hashtable request) | ||
204 | { | ||
205 | string clientstring = ""; | ||
206 | if (!request.ContainsKey("headers")) | ||
207 | return clientstring; | ||
208 | |||
209 | Hashtable requestinfo = (Hashtable)request["headers"]; | ||
210 | if (_options.AllowXForwardedFor && requestinfo.ContainsKey("x-forwarded-for")) | ||
211 | { | ||
212 | object str = requestinfo["x-forwarded-for"]; | ||
213 | if (str != null) | ||
214 | { | ||
215 | if (!string.IsNullOrEmpty(str.ToString())) | ||
216 | { | ||
217 | return str.ToString(); | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | if (!requestinfo.ContainsKey("remote_addr")) | ||
222 | return clientstring; | ||
223 | |||
224 | object remote_addrobj = requestinfo["remote_addr"]; | ||
225 | if (remote_addrobj != null) | ||
226 | { | ||
227 | if (!string.IsNullOrEmpty(remote_addrobj.ToString())) | ||
228 | { | ||
229 | clientstring = remote_addrobj.ToString(); | ||
230 | } | ||
231 | } | ||
232 | |||
233 | return clientstring; | ||
234 | |||
235 | } | ||
236 | |||
237 | } | ||
238 | } | ||