forked from dotnet/dev-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRateLimitingPlugin.cs
279 lines (242 loc) · 11.4 KB
/
RateLimitingPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Extensions.Configuration;
using Microsoft.DevProxy.Abstractions;
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
namespace Microsoft.DevProxy.Plugins.Behavior;
public enum RateLimitResponseWhenLimitExceeded
{
Throttle,
Custom
}
public enum RateLimitResetFormat
{
SecondsLeft,
UtcEpochSeconds
}
public class RateLimitConfiguration
{
public string HeaderLimit { get; set; } = "RateLimit-Limit";
public string HeaderRemaining { get; set; } = "RateLimit-Remaining";
public string HeaderReset { get; set; } = "RateLimit-Reset";
public string HeaderRetryAfter { get; set; } = "Retry-After";
public RateLimitResetFormat ResetFormat { get; set; } = RateLimitResetFormat.SecondsLeft;
public int CostPerRequest { get; set; } = 2;
public int ResetTimeWindowSeconds { get; set; } = 60;
public int WarningThresholdPercent { get; set; } = 80;
public int RateLimit { get; set; } = 120;
public RateLimitResponseWhenLimitExceeded WhenLimitExceeded { get; set; } = RateLimitResponseWhenLimitExceeded.Throttle;
public string CustomResponseFile { get; set; } = "rate-limit-response.json";
public MockResponseResponse? CustomResponse { get; set; }
}
public class RateLimitingPlugin : BaseProxyPlugin
{
public override string Name => nameof(RateLimitingPlugin);
private readonly RateLimitConfiguration _configuration = new();
// initial values so that we know when we intercept the
// first request and can set the initial values
private int _resourcesRemaining = -1;
private DateTime _resetTime = DateTime.MinValue;
private RateLimitingCustomResponseLoader? _loader = null;
private ThrottlingInfo ShouldThrottle(Request request, string throttlingKey)
{
var throttleKeyForRequest = BuildThrottleKey(request);
return new ThrottlingInfo(throttleKeyForRequest == throttlingKey ? (int)(_resetTime - DateTime.Now).TotalSeconds : 0, _configuration.HeaderRetryAfter);
}
private void ThrottleResponse(ProxyRequestArgs e) => UpdateProxyResponse(e, HttpStatusCode.TooManyRequests);
private void UpdateProxyResponse(ProxyHttpEventArgsBase e, HttpStatusCode errorStatus)
{
var headers = new List<MockResponseHeader>();
var body = string.Empty;
var request = e.Session.HttpClient.Request;
var response = e.Session.HttpClient.Response;
// resources exceeded
if (errorStatus == HttpStatusCode.TooManyRequests)
{
if (ProxyUtils.IsGraphRequest(request))
{
string requestId = Guid.NewGuid().ToString();
string requestDate = DateTime.Now.ToString();
headers.AddRange(ProxyUtils.BuildGraphResponseHeaders(request, requestId, requestDate));
body = JsonSerializer.Serialize(new GraphErrorResponseBody(
new GraphErrorResponseError
{
Code = new Regex("([A-Z])").Replace(errorStatus.ToString(), m => { return $" {m.Groups[1]}"; }).Trim(),
Message = BuildApiErrorMessage(request),
InnerError = new GraphErrorResponseInnerError
{
RequestId = requestId,
Date = requestDate
}
})
);
}
headers.Add(new(_configuration.HeaderRetryAfter, ((int)(_resetTime - DateTime.Now).TotalSeconds).ToString()));
e.Session.GenericResponse(body ?? string.Empty, errorStatus, headers.Select(h => new HttpHeader(h.Name, h.Value)).ToArray());
return;
}
if (e.PluginData.TryGetValue(Name, out var pluginData) &&
pluginData is List<MockResponseHeader> rateLimitingHeaders)
{
ProxyUtils.MergeHeaders(headers, rateLimitingHeaders);
}
// add headers to the original API response, avoiding duplicates
headers.ForEach(h => e.Session.HttpClient.Response.Headers.RemoveHeader(h.Name));
e.Session.HttpClient.Response.Headers.AddHeaders(headers.Select(h => new HttpHeader(h.Name, h.Value)).ToArray());
}
private static string BuildApiErrorMessage(Request r) => $"Some error was generated by the proxy. {(ProxyUtils.IsGraphRequest(r) ? ProxyUtils.IsSdkRequest(r) ? "" : String.Join(' ', MessageUtils.BuildUseSdkForErrorsMessage(r)) : "")}";
private string BuildThrottleKey(Request r)
{
if (ProxyUtils.IsGraphRequest(r))
{
return GraphUtils.BuildThrottleKey(r);
}
else
{
return r.RequestUri.Host;
}
}
public override void Register(IPluginEvents pluginEvents,
IProxyContext context,
ISet<UrlToWatch> urlsToWatch,
IConfigurationSection? configSection = null)
{
base.Register(pluginEvents, context, urlsToWatch, configSection);
configSection?.Bind(_configuration);
if (_configuration.WhenLimitExceeded == RateLimitResponseWhenLimitExceeded.Custom)
{
_configuration.CustomResponseFile = Path.GetFullPath(ProxyUtils.ReplacePathTokens(_configuration.CustomResponseFile), Path.GetDirectoryName(context.Configuration?.ConfigFile ?? string.Empty) ?? string.Empty);
_loader = new RateLimitingCustomResponseLoader(_logger!, _configuration);
// load the responses from the configured mocks file
_loader.InitResponsesWatcher();
}
pluginEvents.BeforeRequest += OnRequest;
pluginEvents.BeforeResponse += OnResponse;
}
// add rate limiting headers to the response from the API
private Task OnResponse(object? sender, ProxyResponseArgs e)
{
if (_urlsToWatch is null ||
!e.HasRequestUrlMatch(_urlsToWatch))
{
return Task.CompletedTask;
}
UpdateProxyResponse(e, HttpStatusCode.OK);
return Task.CompletedTask;
}
private Task OnRequest(object? sender, ProxyRequestArgs e)
{
var session = e.Session;
var state = e.ResponseState;
if (e.ResponseState.HasBeenSet ||
_urlsToWatch is null ||
!e.ShouldExecute(_urlsToWatch))
{
return Task.CompletedTask;
}
// set the initial values for the first request
if (_resetTime == DateTime.MinValue)
{
_resetTime = DateTime.Now.AddSeconds(_configuration.ResetTimeWindowSeconds);
}
if (_resourcesRemaining == -1)
{
_resourcesRemaining = _configuration.RateLimit;
}
// see if we passed the reset time window
if (DateTime.Now > _resetTime)
{
_resourcesRemaining = _configuration.RateLimit;
_resetTime = DateTime.Now.AddSeconds(_configuration.ResetTimeWindowSeconds);
}
// subtract the cost of the request
_resourcesRemaining -= _configuration.CostPerRequest;
if (_resourcesRemaining < 0)
{
_resourcesRemaining = 0;
var request = e.Session.HttpClient.Request;
_logger?.LogRequest([$"Exceeded resource limit when calling {request.Url}.", "Request will be throttled"], MessageType.Failed, new LoggingContext(e.Session));
if (_configuration.WhenLimitExceeded == RateLimitResponseWhenLimitExceeded.Throttle)
{
e.ThrottledRequests.Add(new ThrottlerInfo(
BuildThrottleKey(request),
ShouldThrottle,
_resetTime
));
ThrottleResponse(e);
state.HasBeenSet = true;
}
else
{
if (_configuration.CustomResponse is not null)
{
var headersList = _configuration.CustomResponse.Headers is not null ?
_configuration.CustomResponse.Headers.Select(h => new HttpHeader(h.Name, h.Value)).ToList() :
new List<HttpHeader>();
var retryAfterHeader = headersList.FirstOrDefault(h => h.Name.Equals(_configuration.HeaderRetryAfter, StringComparison.OrdinalIgnoreCase));
if (retryAfterHeader is not null && retryAfterHeader.Value == "@dynamic")
{
headersList.Add(new HttpHeader(_configuration.HeaderRetryAfter, ((int)(_resetTime - DateTime.Now).TotalSeconds).ToString()));
headersList.Remove(retryAfterHeader);
}
var headers = headersList.ToArray();
// allow custom throttling response
var responseCode = (HttpStatusCode)(_configuration.CustomResponse.StatusCode ?? 200);
if (responseCode == HttpStatusCode.TooManyRequests)
{
e.ThrottledRequests.Add(new ThrottlerInfo(
BuildThrottleKey(request),
ShouldThrottle,
_resetTime
));
}
string body = _configuration.CustomResponse.Body is not null ?
JsonSerializer.Serialize(_configuration.CustomResponse.Body, new JsonSerializerOptions { WriteIndented = true }) :
"";
e.Session.GenericResponse(body, responseCode, headers);
state.HasBeenSet = true;
}
else
{
_logger?.LogRequest([$"Custom behavior not set. {_configuration.CustomResponseFile} not found."], MessageType.Failed, new LoggingContext(e.Session));
}
}
}
StoreRateLimitingHeaders(e);
return Task.CompletedTask;
}
private void StoreRateLimitingHeaders(ProxyRequestArgs e)
{
// add rate limiting headers if reached the threshold percentage
if (_resourcesRemaining > _configuration.RateLimit - (_configuration.RateLimit * _configuration.WarningThresholdPercent / 100))
{
return;
}
var headers = new List<MockResponseHeader>();
var reset = _configuration.ResetFormat == RateLimitResetFormat.SecondsLeft ?
(_resetTime - DateTime.Now).TotalSeconds.ToString("N0") : // drop decimals
new DateTimeOffset(_resetTime).ToUnixTimeSeconds().ToString();
headers.AddRange(new List<MockResponseHeader>
{
new(_configuration.HeaderLimit, _configuration.RateLimit.ToString()),
new(_configuration.HeaderRemaining, _resourcesRemaining.ToString()),
new(_configuration.HeaderReset, reset)
});
ExposeRateLimitingForCors(headers, e);
e.PluginData.Add(Name, headers);
}
private void ExposeRateLimitingForCors(IList<MockResponseHeader> headers, ProxyRequestArgs e)
{
var request = e.Session.HttpClient.Request;
if (request.Headers.FirstOrDefault((h) => h.Name.Equals("Origin", StringComparison.OrdinalIgnoreCase)) is null)
{
return;
}
headers.Add(new("Access-Control-Allow-Origin", "*"));
headers.Add(new("Access-Control-Expose-Headers", $"{_configuration.HeaderLimit}, {_configuration.HeaderRemaining}, {_configuration.HeaderReset}, {_configuration.HeaderRetryAfter}"));
}
}