使用Java发送Tor NEWNYM请求 [英] Send Tor NEWNYM request using Java

查看:165
本文介绍了使用Java发送Tor NEWNYM请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序成功使用Tor,但我想发送NEWNYM请求以获取新身份。我可以杀死并重新启动进程,或者可能使用shell脚本,但我更喜欢在本机Java中进行。

My application uses Tor successfully, but I would like to send a NEWNYM request to obtain a new identity. I could kill and restart the process, or possibly use a shell script, but I'd prefer to do it in native Java.

有一个Python解决方案,但我不太确定如何将其映射到Java连接类型。我想它与我的Tor HTTP代码非常相似,但是我对Java连接类型不够熟悉才能实现它。

There is a Python solution, but I'm not quite sure how to map it to Java connection types. I imagine it's fairly similar to my Tor HTTP code, but I'm not familiar enough with Java connection types to make it happen.

// My starter code.
public static String torNewIP() throws Exception {
   Proxy proxy = new Proxy(Proxy.Type.HTTP, 
              new InetSocketAddress(TOR_IP, TOR_CONTROL_PORT));     
   // This is how I open a web page using Tor, but the control
   // port is probably not HTTP.
   // HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
   // connection.connect();
   // Something like connection.send("NEWNYM");
}   


推荐答案

仔细检查以确保Tor使用选项ControlPort运行[TOR_CONTROL_PORT]

Double check to make sure Tor is run with the option ControlPort [TOR_CONTROL_PORT]

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();
}

不完全确定原因,但可能需要一些时间才能按预期运行 - 也许是10分钟的IP轮换。对不起,我无法提供更好的细节,但它对我有用。

Not entirely sure why, but it may take some time to function as expected - perhaps the 10 minute IP rotation. Sorry I can't provide better details, but it worked for me.

这篇关于使用Java发送Tor NEWNYM请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆