Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Tor Lib : How to setup Orchid Tor Lib with Java?

Tags:

java

proxy

tor

I am trying to implement Orchid Tor lib with Java code; unfortunately and because the lack of documentation I am not able to make it work, this is what I did:

....................

private final static String DEFAULT_SOCKS_PORT = "9050";

 TorClient torClient = new TorClient();

 torClient.addInitializationListener(new TorInitializationListener() {

    @Override
    public void initializationProgress(String string, int i) {
       System.out.println(">>> [ "+ i + "% ]: "+ string);
    // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void initializationCompleted() {
        try {

            System.out.println("Tor is ready to go!");

            setSystemProperties("127.0.0.1","8118");

            System.out.println("is online "+isOnline()); //isOnilne is just function return true if connected by pinging google.com

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

torClient.enableDashboard(8118);

torClient.enableSocksListener(9050);

torClient.start();

private static void setSystemProperties(String host, String port)
{

    System.setProperty("proxyHost", host);
    System.setProperty("proxyPort", port);

    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port);

    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port);


    System.setProperty("socks.proxyHost", host);
    System.setProperty("socks.proxyPort", DEFAULT_SOCKS_PORT);

    System.setProperty("socksProxyHost", host);
    System.setProperty("socksProxyPort", DEFAULT_SOCKS_PORT);

}
like image 384
MoadKey Avatar asked Mar 20 '15 16:03

MoadKey


1 Answers

This seems to work using Java8.

Dependencies: orchid-1.0.0.jar, jsoup-1.8.2.jar & commons-io-2.4.jar

package orchiddemo;

import com.subgraph.orchid.TorClient;
import com.subgraph.orchid.TorInitializationListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class OrchidDemo {

private static TorClient client;

public static void main(String[] args) {
    startOrchid();
}

private static void startOrchid() {
    //listen on 127.0.0.1:9150 (default)
    client = new TorClient();
    client.addInitializationListener(createInitalizationListner());
    client.start();
    client.enableSocksListener();//or client.enableSocksListener(yourPortNum);
}

private static void stopOrchid() {
    client.stop();
}

public static TorInitializationListener createInitalizationListner() {
    return new TorInitializationListener() {
        @Override
        public void initializationProgress(String message, int percent) {
            System.out.println(">>> [ " + percent + "% ]: " + message);
        }

        @Override
        public void initializationCompleted() {
            System.out.println("Tor is ready to go!");
            doTests();
        }
    };
}

private static void doTests() {
    testOrchidUsingProxyObject();
    testOrchidUsingSystemPropsProxy();
    testOrchidUsingSocket();
}

private static void testOrchidUsingProxyObject() {
    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                //Caution: Native Java DNS lookup will occur outside of the tor network.  
                //Monitor traffic on port 53 using tcpdump or equivalent.
                URL url = new URL("https://wtfismyip.com/");
                Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 9150));
                HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
                uc.setConnectTimeout(10000);
                Document document = Jsoup.parse(IOUtils.toString(uc.getInputStream()));
                String result = document.select("div[id=tor").text();
                System.out.println("testOrchidUsingProxyObject: " + result);
            } catch (Exception ex) {
                Logger.getLogger(OrchidDemo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    thread.start();
}

private static void testOrchidUsingSystemPropsProxy() {
    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                //Caution: Native Java DNS lookup will occur outside of the tor network. 
                //Monitor traffic on port 53 using tcpdump or equivalent.
                System.setProperty("socksProxyHost", "127.0.0.1");
                System.setProperty("socksProxyPort", "9150");
                Document document = Jsoup.connect("https://wtfismyip.com/").get();
                String result = document.select("div[id=tor").text();
                System.out.println("testOrchidUsingSystemPropsProxy: " + result);
                System.setProperty("socksProxyHost", "");
                System.setProperty("socksProxyPort", "");
            } catch (Exception ex) {
                Logger.getLogger(OrchidDemo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    thread.start();
}

private static void testOrchidUsingSocket() {
    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                // This does not appear to leak the DNS lookup, but requires confirmation!
                Socket socket = client.getSocketFactory().createSocket("www.google.com", 80);
                PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                writer.println("GET /");
                String line;
                System.out.println("testOrchidUsingSocket: ");
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                socket.close();
            } catch (Exception ex) {
                Logger.getLogger(OrchidDemo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };
    thread.start();
  }
}

The DNS leak is a drama, but silvertunnel can help: NetAddressNameService I'm hoping someone might know of a better way....

Cheers

like image 160
Wayno Avatar answered Oct 04 '22 03:10

Wayno