简体   繁体   中英

How to do a DNS lookup through a tor proxy with Java?

As the headline says I currently try to resolve a hostname through a tor proxy.

Tor is running on a dedicated server (192.168.1.15). Getting a website is no problem, but if I try to get the IP of the host, Java still does a direct lookup and ignores the proxy.

I already tried this ways:

//Trying lib from: www.xbill.org/dnsjava
import org.xbill.DNS.*;
[...]
public void lookup(){
    //Lookup without proxy
    try {
        InetAddress addr = Address.getByName("stackoverflow.com");
        System.out.println(addr);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    //set socks v5 proxy
    //http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html#Proxies
    System.setProperty("socksProxyHost", "192.168.1.15");
    System.setProperty("socksProxyPort", "9050");

    //trying to resolve with dnsjava
    try {
        Record [] records = new Lookup("stackoverflow.com", Type.A).run();
        for (Record record : records) {
            System.out.println(record);
        }
    } catch (TextParseException e) {
        e.printStackTrace();
    }

    //trying to resolve without lib
    try {
        System.out.println(InetAddress.getByName("stackoverflow.com"));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

public void request(){
    InetSocketAddress torProxyAddress = new InetSocketAddress("192.168.1.15", 9050);
    Proxy torProxy = new Proxy(Proxy.Type.SOCKS, torProxyAddress);
    Socket underlying = new Socket(torProxy);

    InetSocketAddress unresolvedAdr = InetSocketAddress.createUnresolved("stackoverflow.com", 80);

    try {
        underlying.connect(unresolvedAdr);

        BufferedWriter out = new BufferedWriter( new OutputStreamWriter(underlying.getOutputStream()));
        BufferedReader in = new BufferedReader( new InputStreamReader(underlying.getInputStream()));

        out.write("GET / HTTP/1.1\nHost: stackoverflow.com\n\n");
        out.flush();
        String line;
        while((line = in.readLine()) != null){
            System.out.println(line);
        }
    } catch (IOException e) {

    } finally {
        try {
            underlying.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I force Java to do a lookup through the tor proxy?

Doing it through a Tor proxy is hard as the InetAddressNameservice cannot be routed through Tor without modifying or implementing an own NetAddressNameService. Normal nslookups are done through port 53 (UDP) and Tor is currently only supporting TCP.

So using the Tor-way of resolving the hostnames you need to implement your own "Tor-client" as you need to send RELAY_RESOLVE cells (check tor-spec.txt chapter 6.4 )

One easy option would be to use SilverTunnel-NG . This library also uses the Tor network for doing the ns-lookups.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM