-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathIpAddressHelper.java
More file actions
146 lines (121 loc) · 5.08 KB
/
IpAddressHelper.java
File metadata and controls
146 lines (121 loc) · 5.08 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
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
package org.prebid.server.auction;
import inet.ipaddr.AddressStringException;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressString;
import inet.ipaddr.IPAddressStringParameters;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.auction.model.IpAddress;
import org.prebid.server.log.Logger;
import org.prebid.server.log.LoggerFactory;
import java.util.List;
import java.util.regex.Pattern;
public class IpAddressHelper {
private static final Logger logger = LoggerFactory.getLogger(IpAddressHelper.class);
private static final Pattern IP_V4_PATTERN = Pattern.compile("""
^\
(?:[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.\
(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){2}\
(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
""");
private static final IPAddressStringParameters IP_ADDRESS_VALIDATION_OPTIONS =
IPAddressString.DEFAULT_VALIDATION_OPTIONS.toBuilder()
.allowSingleSegment(false)
.allowEmpty(false)
.toParams();
private final IPAddress ipv6AlwaysMaskAddress;
private final IPAddress ipv6AnonLeftMaskAddress;
private final List<IPAddress> ipv6LocalNetworkMaskAddresses;
public IpAddressHelper(int ipv6AlwaysMaskBits, int ipv6AnonLeftMaskBits, List<String> ipv6LocalNetworks) {
ipv6AlwaysMaskAddress =
toAddress("::/" + validateIpv6AlwaysMaskBits(ipv6AlwaysMaskBits)).getNetworkMask();
ipv6AnonLeftMaskAddress =
toAddress("::/" + validateIpv6AnonLeftMaskBits(ipv6AnonLeftMaskBits)).getNetworkMask();
ipv6LocalNetworkMaskAddresses = ipv6LocalNetworks.stream()
.map(this::toAddress)
.toList();
}
public String anonymizeIpv6(String ip) {
try {
final IPAddressString ipAddressString = new IPAddressString(ip);
return ipAddressString.isIPv6()
? ipAddressString.toAddress().mask(ipv6AnonLeftMaskAddress).toCanonicalString()
: null;
} catch (AddressStringException e) {
logger.debug("Exception occurred while anonymizing IPv6 address: {}", e.getMessage());
return null;
}
}
public IpAddress toIpAddress(String ip) {
final IPAddress ipAddress = toIpAddressInternal(ip);
if (ipAddress == null) {
return null;
}
final IpAddress.IP version;
if (ipAddress.isIPv4()) {
version = IpAddress.IP.v4;
} else if (ipAddress.isIPv6()) {
version = IpAddress.IP.v6;
} else {
return null;
}
if (isIpPublic(ipAddress)) {
final String sanitizedIp = version == IpAddress.IP.v6 ? maskIpv6(ipAddress) : ip;
return IpAddress.of(sanitizedIp, version);
}
return null;
}
public String maskIpv4(String ip) {
if (StringUtils.isBlank(ip) || !IP_V4_PATTERN.matcher(ip).matches()) {
return ip;
}
String maskedIp = ip;
final int lastDotIndex = maskedIp.lastIndexOf(".");
if (lastDotIndex != -1) {
maskedIp = maskedIp.substring(0, lastDotIndex);
} else {
// ip is malformed
return ip;
}
return maskedIp + ".0";
}
private String maskIpv6(IPAddress ipAddress) {
return ipAddress.mask(ipv6AlwaysMaskAddress).toCanonicalString();
}
private static int validateIpv6AlwaysMaskBits(int ipv6AlwaysMaskBits) {
if (ipv6AlwaysMaskBits < 1 || ipv6AlwaysMaskBits > 128) {
throw new IllegalArgumentException("IPv6 always mask bits should be between 1 and 128 inclusive");
}
return ipv6AlwaysMaskBits;
}
private static int validateIpv6AnonLeftMaskBits(int ipv6AnonLeftMaskBits) {
if (ipv6AnonLeftMaskBits < 1
|| ipv6AnonLeftMaskBits > 128
|| (ipv6AnonLeftMaskBits > 32 && ipv6AnonLeftMaskBits < 56)) {
throw new IllegalArgumentException(
"IPv6 anonymize mask bits should be between 1 and 32 or 56 and 128 inclusive");
}
return ipv6AnonLeftMaskBits;
}
private IPAddress toAddress(String address) {
try {
return new IPAddressString(address).toAddress();
} catch (AddressStringException e) {
throw new IllegalArgumentException("Unable to process IPv6-related configuration", e);
}
}
private static IPAddress toIpAddressInternal(String ip) {
try {
return new IPAddressString(ip, IP_ADDRESS_VALIDATION_OPTIONS).toAddress();
} catch (AddressStringException e) {
return null;
}
}
private boolean isIpPublic(IPAddress ipAddress) {
return ipAddress != null
&& !ipAddress.isLocal()
&& !ipAddress.isLoopback()
&& !ipAddress.isMulticast()
&& !ipAddress.isMax()
&& ipv6LocalNetworkMaskAddresses.stream().noneMatch(network -> network.contains(ipAddress));
}
}