@@ -18,12 +18,27 @@ public override void OnActionExecuting(HttpActionContext actionContext)
18
18
bool authenticated = false ;
19
19
string message = "Access denied." ;
20
20
21
+ string forwardingAddress = null ;
21
22
string clientIpAddress = null ;
22
23
23
24
try
24
25
{
26
+ // There is a strong possibility that this is not the ip address of the machine
27
+ // that sent the request. Being behind a load balancer with transparancy switched
28
+ // off or being served through CloudFlare will both affect this value.
25
29
clientIpAddress = HttpContext . Current . Request . UserHostAddress ;
26
30
31
+ // We need to get the X-Forwarded-For header from the request, if this is set we
32
+ // should use it instead of the ip address from the request.
33
+ string forwardedFor = HttpContext . Current . Request . Headers . Get ( "X-Forwarded-For" ) ;
34
+
35
+ // Forwarded for set?
36
+ if ( forwardedFor != null )
37
+ {
38
+ forwardingAddress = clientIpAddress ;
39
+ clientIpAddress = forwardedFor ;
40
+ }
41
+
27
42
// Got the ip address?
28
43
if ( ! string . IsNullOrEmpty ( clientIpAddress ) )
29
44
{
@@ -49,6 +64,12 @@ public override void OnActionExecuting(HttpActionContext actionContext)
49
64
50
65
string log = string . Format ( "(IP: {0}) {1}" , clientIpAddress , message ) ;
51
66
67
+ // Was it forwarded?
68
+ if ( forwardingAddress != null )
69
+ {
70
+ log = string . Format ( "(IP: {0} | Forwarded by: {1}) {2}" , clientIpAddress , forwardingAddress , message ) ;
71
+ }
72
+
52
73
elc . AddLog ( "PolyDeploy" , log , EventLogController . EventLogType . HOST_ALERT ) ;
53
74
54
75
actionContext . Response = actionContext . Request . CreateErrorResponse ( HttpStatusCode . Forbidden , message ) ;
0 commit comments