-
Notifications
You must be signed in to change notification settings - Fork 0
/
14_SourceIPBlocking.cs
40 lines (36 loc) · 1.31 KB
/
14_SourceIPBlocking.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
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace ProxyKit.Recipes
{
public class SourceIPBlocking : Recipe<SourceIPBlocking.Startup>
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddProxy();
}
public void Configure(IApplicationBuilder app)
{
var ipNetwork = IPNetwork.Parse("10.0.0.1", "255.255.255.0");
app.RunProxy(context =>
{
// If the source IP is outside the specified range then return Forbidden.
// This uses the IPNetwork2 package from https://github.com/lduchosal/ipnetwork
if (!ipNetwork.Contains(context.Connection.RemoteIpAddress))
{
var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
return Task.FromResult(response);
}
return context
.ForwardTo("http://localhost:5001")
.AddXForwardedHeaders()
.Send();
});
}
}
}
}