gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:11 29 4
gpt4 key购买 nike

我正在尝试用 Java 代码实现 Orchid Tor 库;不幸的是,由于缺乏文档,我无法让它工作,这就是我所做的:

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

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

}

最佳答案

这似乎适用于 Java8。

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

DNS 泄漏是一出戏剧,但 silvertunnel 可以提供帮助:NetAddressNameService我希望有人知道更好的方法....

干杯

关于Java Tor 库 : How to setup Orchid Tor Lib with Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29171643/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com