Java Tor Lib:如何使用 Java 设置 Orchid Tor Lib? [英] Java Tor Lib : How to setup Orchid Tor Lib with Java?

查看:19
本文介绍了Java Tor Lib:如何使用 Java 设置 Orchid Tor Lib?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java 代码实现 Orchid 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);

}

推荐答案

这似乎可以使用 Java8.

This seems to work using Java8.

依赖项:orchid-1.0.0.jar、jsoup-1.8.2.jar &commons-io-2.4.jar

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

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

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

干杯

这篇关于Java Tor Lib:如何使用 Java 设置 Orchid Tor Lib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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