2013-07-29 97 views
1

正如標題所示,我目前嘗試通過tor代理解析主機名。如何通過使用Java的tor代理進行DNS查找?

Tor正在專用服務器(192.168.1.15)上運行。獲取網站沒有問題,但是如果我嘗試獲取主機的IP,Java仍會直接查找並忽略代理。

我已經嘗試過這種方法:

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

如何強制Java通過TOR代理進行查找?

回答

1

通過Tor代理實現它很困難,因爲無法修改或實現自己的NetAddressNameService,InetAddressNameservice無法通過Tor進行路由。 正常nslookups通過端口53(UDP)完成,Tor目前僅支持TCP。

因此,使用解析你需要實現自己的「托爾 - 客戶端」,因爲你需要發送RELAY_RESOLVE細胞中的主機名的托爾路(檢查tor-spec.txt chapter 6.4

一個簡單的辦法是使用SilverTunnel-NG。 該庫還使用Tor網絡來執行ns查找。