-
Notifications
You must be signed in to change notification settings - Fork 750
/
UserRequestIPAddressController.cs
100 lines (86 loc) · 3.54 KB
/
UserRequestIPAddressController.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DotNetNuke.Services.UserRequest
{
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Web;
using DotNetNuke.Entities.Controllers;
using DotNetNuke.Framework;
public class UserRequestIPAddressController : ServiceLocator<IUserRequestIPAddressController, UserRequestIPAddressController>, IUserRequestIPAddressController
{
/// <inheritdoc/>
public string GetUserRequestIPAddress(HttpRequestBase request)
{
return this.GetUserRequestIPAddress(request, IPAddressFamily.IPv4);
}
/// <inheritdoc/>
public string GetUserRequestIPAddress(HttpRequestBase request, IPAddressFamily ipFamily)
{
var userRequestIPHeader = HostController.Instance.GetString("UserRequestIPHeader", "X-Forwarded-For");
var userIPAddress = string.Empty;
if (request.Headers.AllKeys.Contains(userRequestIPHeader))
{
userIPAddress = request.Headers[userRequestIPHeader];
userIPAddress = userIPAddress.Split(',')[0];
if (ipFamily == IPAddressFamily.IPv4 && userIPAddress.Contains(':'))
{
userIPAddress = userIPAddress.Split(':')[0];
}
else if (ipFamily == IPAddressFamily.IPv6
&& userIPAddress.StartsWith("[") && userIPAddress.Contains(']'))
{
userIPAddress = userIPAddress.Split(']')[0].Substring(1);
}
}
if (string.IsNullOrEmpty(userIPAddress))
{
var remoteAddrVariable = "REMOTE_ADDR";
if (request.ServerVariables.AllKeys.Contains(remoteAddrVariable))
{
userIPAddress = request.ServerVariables[remoteAddrVariable];
}
}
if (string.IsNullOrEmpty(userIPAddress))
{
userIPAddress = request.UserHostAddress;
}
if (string.IsNullOrEmpty(userIPAddress) || userIPAddress.Trim() == "::1")
{
userIPAddress = string.Empty;
}
if (!string.IsNullOrEmpty(userIPAddress) && !this.ValidateIP(userIPAddress, ipFamily))
{
userIPAddress = string.Empty;
}
return userIPAddress;
}
/// <inheritdoc/>
protected override Func<IUserRequestIPAddressController> GetFactory()
{
return () => new UserRequestIPAddressController();
}
private bool ValidateIP(string ipString, IPAddressFamily ipFamily)
{
IPAddress address;
if (IPAddress.TryParse(ipString, out address))
{
if (ipFamily == IPAddressFamily.IPv4 &&
address.AddressFamily == AddressFamily.InterNetwork &&
ipString.Split('.').Length == 4)
{
return true;
}
if (ipFamily == IPAddressFamily.IPv6 &&
address.AddressFamily == AddressFamily.InterNetworkV6)
{
return true;
}
}
return false;
}
}
}