本文整理汇总了Java中org.apache.commons.net.util.SubnetUtils.SubnetInfo方法的典型用法代码示例。如果您正苦于以下问题:Java SubnetUtils.SubnetInfo方法的具体用法?Java SubnetUtils.SubnetInfo怎么用?Java SubnetUtils.SubnetInfo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.net.util.SubnetUtils
的用法示例。
在下文中一共展示了SubnetUtils.SubnetInfo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCollection
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
/**
* returns the contents of the MachineList as a Collection<String>
* This can be used for testing
* @return contents of the MachineList
*/
@VisibleForTesting
public Collection<String> getCollection() {
Collection<String> list = new ArrayList<String>();
if (all) {
list.add("*");
} else {
if (ipAddresses != null) {
list.addAll(ipAddresses);
}
if (hostNames != null) {
list.addAll(hostNames);
}
if (cidrAddresses != null) {
for(SubnetUtils.SubnetInfo cidrAddress : cidrAddresses) {
list.add(cidrAddress.getCidrSignature());
}
}
}
return list;
}
示例2: testThatFilterAndTruncateViolatorsFiltersIPsInDoNotBlockRangeSet
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
@Test
public void testThatFilterAndTruncateViolatorsFiltersIPsInDoNotBlockRangeSet() {
config.setRateLimitViolationBlacklistPeriodInMinutes(1);
RangeSet<Integer> doNotAutoBlockIpRangeSet = TreeRangeSet.create();
SubnetUtils subnetUtils = new SubnetUtils("50.39.100.193/32");
subnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo subnetInfo = subnetUtils.getInfo();
Integer lowIpAsInt = subnetInfo.asInteger(subnetInfo.getLowAddress());
Integer highIpAsInt = subnetInfo.asInteger(subnetInfo.getHighAddress());
doNotAutoBlockIpRangeSet.add(Range.closed(lowIpAsInt, highIpAsInt));
Map<String, ViolationMetaData> violators = new HashMap<>();
violators.put("50.39.100.193", new ViolationMetaData(new Date(), 2));
Map<String, ViolationMetaData> actual = processor.filterAndTruncateViolators(config, doNotAutoBlockIpRangeSet,
violators);
assertTrue("The violators map should be empty after filtering", actual.size() == 0);
}
示例3: testThatProcessViolatorsDoesNotReAddWhatIsAlreadyBlocked
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
@Test
public void testThatProcessViolatorsDoesNotReAddWhatIsAlreadyBlocked() {
String fakeIpSet = "foo";
config.setRateLimitAutoBlackListIpSetId(fakeIpSet);
List<SubnetUtils.SubnetInfo> currentlyAutoBlocked = new LinkedList<>();
SubnetUtils subnetUtils = new SubnetUtils("192.168.0.1/32");
subnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo info = subnetUtils.getInfo();
currentlyAutoBlocked.add(info);
doReturn(currentlyAutoBlocked).when(processor).getIpSet(fakeIpSet);
Map<String, ViolationMetaData> violators = new HashMap<>();
violators.put("192.168.0.1", new ViolationMetaData(new Date(), 10));
processor.processViolators(config, violators);
verify(awswaf, times(0)).updateIPSet(isA(UpdateIPSetRequest.class));
}
示例4: testThatProcessViolatorsAddsNewIpsToIpSet
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
@Test
public void testThatProcessViolatorsAddsNewIpsToIpSet() {
String fakeIpSet = "foo";
config.setRateLimitAutoBlackListIpSetId(fakeIpSet);
doReturn(new LinkedList<>()).when(processor).getIpSet(fakeIpSet);
Map<String, ViolationMetaData> violators = new HashMap<>();
violators.put("192.168.0.1", new ViolationMetaData(new Date(), 10));
processor.processViolators(config, violators);
List<IPSetUpdate> expectedUpdates = new LinkedList<>();
SubnetUtils subnetUtils = new SubnetUtils("192.168.0.1/32");
subnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo info = subnetUtils.getInfo();
IPSetDescriptor descriptor = new IPSetDescriptor()
.withType(IPSetDescriptorType.IPV4)
.withValue(info.getCidrSignature());
IPSetUpdate update = new IPSetUpdate().withIPSetDescriptor(descriptor).withAction(ChangeAction.INSERT);
expectedUpdates.add(update);
verify(awswaf, times(1)).updateIPSet(new UpdateIPSetRequest()
.withIPSetId(config.getRateLimitAutoBlackListIpSetId())
.withUpdates(expectedUpdates)
.withChangeToken(CHANGE_TOKEN));
}
示例5: testThatGetIpSetReturnsAListOfSubNetInfoObjects
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
@Test
public void testThatGetIpSetReturnsAListOfSubNetInfoObjects() {
GetIPSetResult result = mock(GetIPSetResult.class);
IPSet ipSet = mock(IPSet.class);
when(result.getIPSet()).thenReturn(ipSet);
List<IPSetDescriptor> descriptors = new LinkedList<>();
descriptors.add(new IPSetDescriptor().withType(IPSetDescriptorType.IPV4).withValue("192.168.0.1/32"));
descriptors.add(new IPSetDescriptor().withType(IPSetDescriptorType.IPV4).withValue("192.168.0.2/32"));
descriptors.add(new IPSetDescriptor().withType(IPSetDescriptorType.IPV4).withValue("192.168.0.3/32"));
when(ipSet.getIPSetDescriptors()).thenReturn(descriptors);
when(awswaf.getIPSet(isA(GetIPSetRequest.class))).thenReturn(result);
List<SubnetUtils.SubnetInfo> list = processor.getIpSet("DOES NOT MATTER");
assertTrue("The list should contain 3 items", list.size() == 3);
}
示例6: testIp
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
@Test
public void testIp()
{
SubnetUtils utils = new SubnetUtils( "10.12.0.1/24" );
final SubnetUtils.SubnetInfo info = utils.getInfo();
assertEquals( "10.12.0.1", info.getAddress() );
assertEquals( "10.12.0.0", info.getNetworkAddress() );
assertEquals( "10.12.0.255", info.getBroadcastAddress() );
assertEquals( 254, info.getAddressCount() );
assertEquals( "10.12.0.1/24", info.getCidrSignature() );
assertEquals( "10.12.0.1", info.getLowAddress() );
assertEquals( "10.12.0.254", info.getHighAddress() );
assertEquals( "255.255.255.0", info.getNetmask() );
assertEquals( true, info.isInRange( "10.12.0.100" ) );
assertEquals( false, info.isInRange( "10.11.0.1" ) );
}
示例7: belong
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
public boolean belong(InetAddress ipAddress) {
String checkedIp = ipAddress.getHostAddress();
if (plainAddresses.contains(checkedIp)) {
return true;
}
for (SubnetUtils.SubnetInfo subnetInfo : cidrAddresses) {
if (subnetInfo.isInRange(checkedIp)) {
return true;
}
}
return false;
}
示例8: getIpSet
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
/**
* Retrieves ip set info for a given ip set id and returns a list of subnet info objects
*
* @param ipSetId The IP Set Id to look up in AWS
* @return A List of Subnet Info objects that contain the IP Cidr info on what is in the IP Set
*/
protected List<SubnetUtils.SubnetInfo> getIpSet(String ipSetId) {
List<SubnetUtils.SubnetInfo> ips = new LinkedList<>();
GetIPSetResult result = awsWaf.getIPSet(new GetIPSetRequest().withIPSetId(ipSetId));
result.getIPSet().getIPSetDescriptors().forEach(ipSetDescriptor -> {
if (IPSetDescriptorType.IPV4.toString().equals(ipSetDescriptor.getType())) {
SubnetUtils subnetUtils = new SubnetUtils(ipSetDescriptor.getValue());
subnetUtils.setInclusiveHostCount(true);
ips.add(subnetUtils.getInfo());
}
});
return ips;
}
示例9: testThatProcessViolatorsRemovesIpsThatShouldNoLongerBeInIpSet
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
@Test
public void testThatProcessViolatorsRemovesIpsThatShouldNoLongerBeInIpSet() {
String fakeIpSet = "foo";
config.setRateLimitAutoBlackListIpSetId(fakeIpSet);
List<SubnetUtils.SubnetInfo> currentlyAutoBlocked = new LinkedList<>();
SubnetUtils subnetUtils = new SubnetUtils("192.168.0.1/32");
subnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo info = subnetUtils.getInfo();
currentlyAutoBlocked.add(info);
doReturn(currentlyAutoBlocked).when(processor).getIpSet(fakeIpSet);
Map<String, ViolationMetaData> violators = new HashMap<>();
processor.processViolators(config, violators);
List<IPSetUpdate> expectedUpdates = new LinkedList<>();
IPSetDescriptor descriptor = new IPSetDescriptor()
.withType(IPSetDescriptorType.IPV4)
.withValue(info.getCidrSignature());
IPSetUpdate update = new IPSetUpdate().withIPSetDescriptor(descriptor).withAction(ChangeAction.DELETE);
expectedUpdates.add(update);
verify(awswaf, times(1)).updateIPSet(new UpdateIPSetRequest()
.withIPSetId(config.getRateLimitAutoBlackListIpSetId())
.withUpdates(expectedUpdates)
.withChangeToken(CHANGE_TOKEN));
}
示例10: testThatGetDoNotBlockRangeSetBuildsARangeSet
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
@Test
public void testThatGetDoNotBlockRangeSetBuildsARangeSet() {
// stub the black list
String black = "black";
config.setManualBlackListIpSetId(black);
List<SubnetUtils.SubnetInfo> blackList = Lists.newLinkedList();
// 192.168.0.0-192.168.0.255
SubnetUtils bSubnetUtils = new SubnetUtils("192.168.0.0/24");
bSubnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo bInfo = bSubnetUtils.getInfo();
blackList.add(bInfo);
doReturn(blackList).when(processor).getIpSet(black);
// stub the white list
String white = "white";
config.setManualWhiteListIpSetId(white);
List<SubnetUtils.SubnetInfo> whiteList = Lists.newLinkedList();
// 192.150.0.0-192.150.0.255
SubnetUtils wSubnetUtils = new SubnetUtils("192.150.0.0/24");
wSubnetUtils.setInclusiveHostCount(true);
SubnetUtils.SubnetInfo wInfo = wSubnetUtils.getInfo();
whiteList.add(wInfo);
doReturn(whiteList).when(processor).getIpSet(white);
RangeSet<Integer> doNotBlock = processor.getDoNotBlockRangeSet(config);
SubnetUtils utils = new SubnetUtils("0.0.0.0/24");
Integer onTheBlackListLow = utils.getInfo().asInteger("192.168.0.0");
Integer onTheBlackListHigh = utils.getInfo().asInteger("192.168.0.255");
Integer onTheWhiteListLow = utils.getInfo().asInteger("192.150.0.0");
Integer onTheWhiteListHigh = utils.getInfo().asInteger("192.150.0.255");
Integer shouldBeBlocked = utils.getInfo().asInteger("192.168.1.1");
assertTrue("192.168.0.0 is in the ip range represented by 192.168.0.0/24 and therefor should be in the doNotBlock range set", doNotBlock.contains(onTheBlackListLow));
assertTrue("192.168.0.255 is in the ip range represented by 192.168.0.0/24 and therefor should be in the doNotBlock range set", doNotBlock.contains(onTheBlackListHigh));
assertTrue("192.150.0.0 is in the ip range represented by 192.150.0.0/24 and therefor should be in the doNotBlock range set", doNotBlock.contains(onTheWhiteListLow));
assertTrue("192.150.0.255 is in the ip range represented by 192.150.0.0/24 and therefor should be in the doNotBlock range set", doNotBlock.contains(onTheWhiteListHigh));
assertFalse("192.168.1.1 is not in 192.168.0.0/24 or 192.150.0.0/24 and should not be in the doNotBlock range set", doNotBlock.contains(shouldBeBlocked));
}
示例11: getTotalIpInCidr
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
public static int getTotalIpInCidr(String cidr) {
if (!isCidr(cidr)) {
throw new IllegalArgumentException(String.format("%s is not a valid cidr", cidr));
}
SubnetUtils.SubnetInfo range = new SubnetUtils(cidr).getInfo();
return getTotalIpInRange(range.getLowAddress(), range.getHighAddress());
}
示例12: isCidrOverlap
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
public static boolean isCidrOverlap(String cidr1, String cidr2) {
DebugUtils.Assert(isCidr(cidr1), String.format("%s is not a cidr", cidr1));
DebugUtils.Assert(isCidr(cidr2), String.format("%s is not a cidr", cidr2));
SubnetUtils su1 = new SubnetUtils(cidr1);
SubnetUtils su2 = new SubnetUtils(cidr2);
SubnetUtils.SubnetInfo info1 = su1.getInfo();
SubnetUtils.SubnetInfo info2 = su2.getInfo();
return isIpv4RangeOverlap(info1.getLowAddress(), info1.getHighAddress(), info2.getLowAddress(), info2.getHighAddress());
}
示例13: isIpv4InCidr
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
public static boolean isIpv4InCidr(String ipv4, String cidr) {
DebugUtils.Assert(isCidr(cidr), String.format("%s is not a cidr", cidr));
validateIp(ipv4);
SubnetUtils.SubnetInfo info = new SubnetUtils(cidr).getInfo();
return isIpv4InRange(ipv4, info.getLowAddress(), info.getHighAddress());
}
示例14: isSubCidr
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
public static boolean isSubCidr(String cidr, String subCidr) {
DebugUtils.Assert(isCidr(cidr), String.format("%s is not a cidr", cidr));
DebugUtils.Assert(isCidr(subCidr), String.format("%s is not a cidr", subCidr));
SubnetUtils.SubnetInfo range = new SubnetUtils(cidr).getInfo();
SubnetUtils.SubnetInfo sub = new SubnetUtils(subCidr).getInfo();
return range.isInRange(sub.getLowAddress()) && range.isInRange(sub.getHighAddress());
}
示例15: matchIPList
import org.apache.commons.net.util.SubnetUtils; //导入方法依赖的package包/类
/**
* Match an address against a list of IP CIDR addresses
*
* @param addrlist
* The comma-separated list of addresses
* @param addr
* The IP address to match
* @return true if address is contained in one or more of the CIDR network blocks listed in addrlist, false if not
*/
public static boolean matchIPList(String addrlist, String addr)
{
log.info("Checking login IP '" + addr + "' is contained in whitelist '" + addrlist + "'");
// TODO Support IPv6
if (StringUtils.isBlank(addrlist) || StringUtils.isBlank(addr))
return false;
boolean match = false;
for (String netaddr : Arrays.asList(addrlist.split(","))) {
if (netaddr.contains("/")) {
// Contained in subnet?
try {
SubnetUtils.SubnetInfo subnet = new SubnetUtils(netaddr.trim()).getInfo();
if (subnet.isInRange(addr)) {
log.debug("IP Address " + addr + " is in network range " + subnet.getCidrSignature());
match = true;
break;
}
} catch (IllegalArgumentException e) {
log.warn("IP network address '" + netaddr + "' is not a valid CIDR format");
}
} else {
// Exact match?
if (netaddr.trim().equals(addr)) {
match = true;
break;
}
}
}
return match;
}