forked from cloudflare/ModCloudFlareIIS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModCloudFlare.cs
More file actions
86 lines (70 loc) · 2.73 KB
/
ModCloudFlare.cs
File metadata and controls
86 lines (70 loc) · 2.73 KB
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
using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using LukeSkywalker.IPNetwork;
namespace ModCloudFlareIIS
{
public class ModCloudFlare : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
context.PostLogRequest += new EventHandler(PostLogEvent);
}
#endregion
private static List<string> CloudFlareIPRanges = new List<string>()
{
"204.93.240.0/24",
"204.93.177.0/24",
"199.27.128.0/21",
"173.245.48.0/20",
"103.21.244.0/22",
"103.22.200.0/22",
"103.31.4.0/22",
"141.101.64.0/18",
"108.162.192.0/18",
"190.93.240.0/20",
"188.114.96.0/20",
"197.234.240.0/22",
"198.41.128.0/17",
"162.158.0.0/15",
"104.16.0.0/12"
};
public static bool IsCloudFlareIP(string ip)
{
foreach(string block in CloudFlareIPRanges)
{
IPNetwork network = IPNetwork.Parse(block);
if (IPNetwork.Contains(network, IPAddress.Parse(ip)))
return true;
}
return false;
}
public void OnPreRequestHandlerExecute(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpRequest request = app.Context.Request;
if (!String.IsNullOrEmpty(request.ServerVariables["HTTP_CF_CONNECTING_IP"]))
{
if (IsCloudFlareIP(request.ServerVariables["REMOTE_ADDR"]))
{
request.ServerVariables.Set("REMOTE_ADDR", request.ServerVariables["HTTP_CF_CONNECTING_IP"]);
}
}
}
public void PostLogEvent(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpRequest request = app.Context.Request;
if (!String.IsNullOrEmpty(request.ServerVariables["HTTP_CF_CONNECTING_IP"]))
app.Response.AppendToLog("[CloudFlare_Visitor_IP:" + request.ServerVariables["HTTP_CF_CONNECTING_IP"] + "]");
if (!String.IsNullOrEmpty(request.ServerVariables["HTTP_CF_RAY"]))
app.Response.AppendToLog("[CF_RAY:" + request.ServerVariables["HTTP_CF_RAY"] + "]");
}
}
}