Simple project to tunnel your connections through tor network.
Download and extract tor from here.
Linux executable should be here: tor-browser_en-US/Browser/TorBrowser/Tor/tor
.
Windows executable should be here: Tor Browser\Browser\TorBrowser\Tor\tor.exe
The TorController
has three simple methods:
-
startUp()
to start a tor process and connect to the control server -
changeIdentity()
to change your identity (ip) -
shutDown()
to disconnect from control server and terminate the tor process
String executable = "tor-browser_en-US/Browser/TorBrowser/Tor/tor";
TorController tor = new TorController(executable);
if(tor.startUp()){
//...
if(tor.changeIdentity()){
//...
}
if(tor.shutDown()){
//...
}
}
String url = "http://api.ipify.org/?format=text";
TorController tor = new TorController(...);
tor.startUp()
SocketAddress proxyAddr = new InetSocketAddress("127.0.0.1", tor.getSocksPort());
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
URLConnection connection = new URL(url).openConnection(proxy);
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
Have a look at these examples
The TorController uses 9150 and 9151 as default ports for socks and controll server. Set them in constructor if you want. You can also specify a password to protect your local tor control server from other connections. Make sure your set the password before you start the TorController:
int socks_port = 1337;
int control_port = 1338
String path = ".../tor-browser_en-US/Browser/TorBrowser/Tor/tor";
TorController tor = new TorController(path,socks_port,control_port);
tor.setPassword("Mb2.r5oHf-0t".toCharArray());
tor.startUp()