0

anyone knows how i can send the NEWNYM signal to TOR through java code?

Say I have this:

        String adress = "http://some-adress";
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);
        ProxyConfig proxyConfig = new ProxyConfig("127.0.0.1", 9150, true);
        webClient.getOptions().setProxyConfig(proxyConfig);

        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setUseInsecureSSL(true);
        HtmlPage page = (HtmlPage)webClient.getPage(adress);

And technically i can do this to restart the application, and get a new identity:

        Runtime.getRuntime().exec("C:/Users/user/Desktop/Tor Browser/Browser/firefox.exe");

        Runtime.getRuntime().exec("TASKKILL /F /IM firefox.exe");

But there must be a way to send the NEWNYM signal, because to get a new identity with TOR this way is quite messy.

Can anyone help me out?

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Yuomo
  • 1
  • 1
  • 1
  • I don't know tor nor HTMLUnit well enough to add an answer, but it looks like you should be able to craft a [WebRequest](http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/WebRequest.html), use `setHTTPMethod` to make it a NEWNYM (assuming NEWNYM is the HTTP verb/method rather than a query parameter) then send it through your webClient's WebConnection – Aaron Jun 07 '17 at 19:24

1 Answers1

0

HtmlUnit uses Apache HttpClient to connect to the server.

And according to this answer, you can do it by directly calling:

public static void torNewIP() throws Exception {
   Socket socket = new Socket();
   socket.connect(new InetSocketAddress(TOR_IP, TOR_CONTROL_PORT));
   socket.getOutputStream().write(new String("SIGNAL NEWNYM\r\n").getBytes());
   socket.close();
}

After enabling 'TOR_CONTROL_PORT'

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56