Skip to content

Instantly share code, notes, and snippets.

@f3lan
Created March 2, 2016 21:03
Show Gist options
  • Save f3lan/a10d23f1aed69fda8ec2 to your computer and use it in GitHub Desktop.
Save f3lan/a10d23f1aed69fda8ec2 to your computer and use it in GitHub Desktop.
Geo Location Ripper for Cell Tower Positions in Switzerland
package pd;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.concurrent.Semaphore;
import java.io.*;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.io.IOUtils;
public class TheRipper {
public enum NetworkType {
gsm, umts, lte
}
static int counter = 0;
static int counterSleep = 0;
// Config for central Zurich only (around 70 images)
// private static int boundaryWest = 682400;
// private static int boundaryEast = 683400;
// private static int boundaryNorth = 247500;
// private static int boundarySouth = 246800;
// Config for whole Switzerland
private static int boundaryWest = 470000;
private static int boundaryEast = 840000;
private static int boundaryNorth = 300000;
private static int boundarySouth = 70000;
private static int width = 500;
private static int height = 500;
private static int step = 500;
private static int concurrentConnectionsLimit = 20;
private static String targetFilePath = "c:\\Users\\Pascal\\Desktop\\pics\\";
private static String comma = "%2C";
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(concurrentConnectionsLimit);
initFakeTrustManager();
System.out.println("Waiting for 5s to get Threadsystem ready...");
try {
Thread.sleep(5000);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
for (int x = boundaryWest; x < boundaryEast; x = x + step) {
for (int y = boundaryNorth; y > boundarySouth; y = y - step) {
counterSleep++;
if (counterSleep > 300) {
counterSleep = 0;
System.out.println(((float) (100 * counter) / (340400 * 3)) + "%: Sleeping for 20s to flush caches... start soon with (" + x + " " + y + ")");
try {
Thread.sleep(20000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
int localX = x;
int localY = y;
Thread tGSM = new Thread(new Runnable() { public void run() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
new TheRipper().establishConnection(localX, localY, NetworkType.gsm);
semaphore.release();
//System.exit(0);
}});
Thread tUMTS = new Thread(new Runnable() { public void run() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
new TheRipper().establishConnection(localX, localY, NetworkType.umts);
semaphore.release();
//System.exit(0);
}});
Thread tLTE = new Thread(new Runnable() { public void run() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
new TheRipper().establishConnection(localX, localY, NetworkType.lte);
semaphore.release();
//System.exit(0);
}});
tGSM.start();
tUMTS.start();
tLTE.start();
}
}
}
private static void initFakeTrustManager() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}
private void establishConnection(int west, int north, NetworkType nType) {
String baseUrl = "https://wms0.geo.admin.ch/?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&LAYERS=ch.bakom.mobil-antennenstandorte-" + nType.name() + "&WIDTH=" + width + "&HEIGHT=" + height + "&CRS=EPSG%3A21781&STYLES=&BBOX=";
// URL SPECS: west - south - east - north
int south = north - height;
int east = west + width;
String https_url = baseUrl + west + comma + south + comma + east + comma + north;
try {
URL url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
saveContent(con, west, north, south, east, nType);
counter++;
//System.out.println(((float) counter / 340400) + "%: " + west + " " + north + " - " + https_url);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveContent(HttpsURLConnection con, int west, int north, int south, int east, NetworkType nType) {
if (con != null) {
try {
IOUtils.copy(con.getInputStream(), new FileOutputStream(new File(targetFilePath + nType.name() + "_n" + north + "_s" + south + "_w" + west + "_e" + east + ".png")));
con.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment