Loading...
субота, 8 червня 2013 р.

Conecting to TOR via org.silvertunnel.netlib.api

Робочий приклад програми, яка заходить на geoiptool через TOR і друкує в консоль айпішник ноди та країну.


import org.silvertunnel.netlib.api.NetFactory;
import org.silvertunnel.netlib.api.NetLayer;
import org.silvertunnel.netlib.api.NetLayerIDs;
import java.net.URI;

public class Main {
public static void main(String[] args) throws Exception {
Client client;
NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(
NetLayerIDs.TOR);
lowerNetLayer.waitUntilReady();
client = new Client(lowerNetLayer);
client.download(new URI("http://geoiptool.com"));
}
}





public class Client  {
 
    private NetlibURLStreamHandlerFactory factory;
 
    public Client(NetLayer lowerNetLayer) {
        // prepare URL handling on top of the lowerNetLayer
        factory = new NetlibURLStreamHandlerFactory(false);
        // the following method could be called multiple times
        // to change layer used by the factory over the time:
        factory.setNetLayerForHttpHttpsFtp(lowerNetLayer);
    }
 
    public void download(URI source) throws IOException {
        // create the suitable URL object
        URLStreamHandler handler = factory.createURLStreamHandler(source.getScheme());
        URL context = null;
        URL url = new URL(context, source.toString(), handler);
         // send GET request
        URLConnection urlConnection = url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setAllowUserInteraction(false);
        urlConnection.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
        urlConnection.connect();
        // receive the response
        StringBuilder sb = new StringBuilder();
        String line;
        //build string and print to console
        InputStream responseBodyIS = urlConnection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyIS, "UTF-8"));
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        String result = sb.toString();
        if (result.contains("Country code:") && result.contains("IP Address:")){
        System.out.println("Country: "+result.substring(result.indexOf("Country code:")+78, result.indexOf("Region:")-97));
        System.out.println("IP: "+result.substring(result.indexOf("IP Address:")+76, result.indexOf("Country:")-97));
        System.exit(0);
        }
    }
}

Працює повільно :(

Start: 11:52:13 - End: 12:01:38

0 коментарі:

 
TOP