Created
March 17, 2014 08:25
-
-
Save iconara/9595735 to your computer and use it in GitHub Desktop.
Testing the full IPv4 range with GeoIPCity.dat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.maxmind.geoip.*; | |
import java.io.IOException; | |
import java.util.concurrent.*; | |
class Test { | |
public static void main(String[] args) throws IOException { | |
if (args.length == 0 || args[0] == "all") { | |
checkAll(); | |
} else { | |
checkSome(args); | |
} | |
} | |
private static void checkAll() { | |
ForkJoinPool pool = new ForkJoinPool(); | |
pool.invoke(new CheckTask(0, (long) Math.pow(2, 32))); | |
} | |
private static void checkSome(String[] ips) throws IOException { | |
LookupService lookup = new LookupService("GeoIP-133_20140304/GeoIPCity.dat", LookupService.GEOIP_INDEX_CACHE); | |
for (String ip : ips) { | |
try { | |
lookup.getLocation(ip); | |
System.err.printf("%s: ok\n", ip); | |
} catch (RuntimeException re) { | |
System.err.printf("%s failed: %s (%s)\n", ip, re.getMessage(), re.getClass().getName()); | |
} | |
} | |
} | |
static class CheckTask extends RecursiveAction { | |
private long start; | |
private long end; | |
public CheckTask(long start, long end) { | |
this.start = start; | |
this.end = end; | |
} | |
protected void compute() { | |
long range = end - start; | |
if (range <= 256) { | |
int i0 = (int) (start >> 24) & 0xff; | |
int i1 = (int) (start >> 16) & 0xff; | |
int i2 = (int) (start >> 8) & 0xff; | |
int i3 = (int) (start >> 0) & 0xff; | |
System.err.printf("%d.%d.%d.%d\n", i0, i1, i2, i3); | |
try { | |
LookupService lookup = new LookupService("GeoIP-133_20140304/GeoIPCity.dat", LookupService.GEOIP_INDEX_CACHE); | |
for (long i = start; i < end; i++) { | |
try { | |
lookup.getLocation(i); | |
} catch (RuntimeException re) { | |
System.err.printf("%x failed: %s\n", i, re.getMessage()); | |
} | |
} | |
lookup.close(); | |
} catch (IOException ioe) { | |
System.err.println("Could not open database"); | |
} | |
} else { | |
invokeAll(new CheckTask(start, start + range/2), new CheckTask(start + range/2, end)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment