Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import static java.util.Objects.requireNonNull;

import com.google.i18n.phonenumbers.PhoneNumberUtil;
import javax.annotation.Nonnull;

public record AsnInfo(long asn, @Nonnull String regionCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,34 @@ public static AsnInfoProviderImpl fromTsv(@Nonnull final InputStream tsvInputStr
final Map<Long, AsnInfo> asnInfoCache = new HashMap<>();

try (final CSVParser csvParser = CSVFormat.TDF.parse(tsvReader)) {
for (final CSVRecord record : csvParser) {
for (final CSVRecord csvRecord : csvParser) {
// format:
// range_start_ip_string range_end_ip_string AS_number country_code AS_description
final InetAddress startIp = InetAddress.getByName(record.get(0));
final InetAddress endIp = InetAddress.getByName(record.get(1));
final long asn = Long.parseLong(record.get(2));
final String regionCode = record.get(3);
final InetAddress startIp = InetAddress.getByName(csvRecord.get(0));
final InetAddress endIp = InetAddress.getByName(csvRecord.get(1));
final long asn = Long.parseLong(csvRecord.get(2));
final String regionCode = csvRecord.get(3);
// country code should be the same for any ASN, so we're caching AsnInfo objects
// not to have multiple instances with the same values
final AsnInfo asnInfo = asnInfoCache.computeIfAbsent(asn, k -> new AsnInfo(asn, regionCode));
final AsnInfo asnInfo = asnInfoCache.computeIfAbsent(asn, _ -> new AsnInfo(asn, regionCode));
if (!regionCode.equals(asnInfo.regionCode())) {
log.warn("ASN {} mapped to country codes {} and {}", asn, regionCode, asnInfo.regionCode());
}

// IPv4
if (startIp instanceof Inet4Address) {
if (startIp instanceof Inet4Address inet4address) {
final AsnRange<Long> asnRange = new AsnRange<>(
ip4BytesToLong((Inet4Address) startIp),
ip4BytesToLong(inet4address),
ip4BytesToLong((Inet4Address) endIp),
asnInfo
);
ip4asns.put(asnRange.from(), asnRange);
}

// IPv6
if (startIp instanceof Inet6Address) {
if (startIp instanceof Inet6Address inet6address) {
final AsnRange<BigInteger> asnRange = new AsnRange<>(
ip6BytesToBigInteger((Inet6Address) startIp),
ip6BytesToBigInteger(inet6address),
ip6BytesToBigInteger((Inet6Address) endIp),
asnInfo
);
Expand Down Expand Up @@ -134,7 +134,7 @@ public Optional<AsnInfo> lookup(@Nonnull final String ipString) {
}
// safety net, should never happen
log.warn("Unknown InetAddress implementation: {}", address.getClass().getName());
} catch (final Exception e) {
} catch (final Exception _) {
log.error("Could not resolve ASN for IP string {}", ipString);
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.net.InetAddress;
import org.junit.jupiter.api.Test;

public class AsnInfoProviderImplTest {
class AsnInfoProviderImplTest {

private static final String RESOURCE_NAME = "ip2asn-test.tsv";

Expand Down Expand Up @@ -53,7 +53,7 @@ void testAsnInfo() throws IOException {
}

@Test
public void testBytesToLong() throws Exception {
void testBytesToLong() throws Exception {
assertEquals(0x00000000ffffffffL, ip4BytesToLong((Inet4Address) InetAddress.getByName("255.255.255.255")));
assertEquals(0x0000000000000001L, ip4BytesToLong((Inet4Address) InetAddress.getByName("0.0.0.1")));
assertEquals(0x00000000ff00ff01L, ip4BytesToLong((Inet4Address) InetAddress.getByName("255.0.255.1")));
Expand Down