如何让Tor服务使用Socket执行DNS解析?

我想用SOCKS代理的Java Socket来连接Tor(我正在使用Orbot和Tor服务在端口9050上运行)。

我得到了错误:

I / System.out:java.net.UnknownHostException:主机未解决:3g2upl4pq6kufc4m.onion

我的代码如下:

// setup proxy
address = new InetSocketAddress('localhost', 9050);
proxy = new java.net.Proxy(java.net.Proxy.Type.SOCKS,address);
// setup socket
client = new Socket(proxy);
inet = new InetSocketAddress("3g2upl4pq6kufc4m.onion", 80);
// connect
client.connect(inet);

这是合法的,因为Android可能会通过网络配置中指定的DNS服务器执行DNS解析,并且洋葱地址的解析不起作用。

如何指定让Tor服务执行DNS解析(Tor通常会处理来自TCP数据包的主机名)?


这样做的正确方法是使用InetSocketAddress.createUnresolved(...)方法,但在Android上引发异常。

作为解决方法,我们需要像这样实现Socks:http://www.mit.edu/~foley/TinFoil/src/tinfoil/TorLib.java

解释是在这里:https://github.com/acomminos/OnionKit/blob/master/libnetcipher/src/info/guardianproject/onionkit/proxy/SocksProxyClientConnOperator.java

基本上:

        socket = new Socket();
        socket.setSoTimeout(READ_TIMEOUT_MILLISECONDS);
        socket.connect(new InetSocketAddress(mProxyHost, mProxyPort), CONNECT_TIMEOUT_MILLISECONDS);

        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
        outputStream.write((byte)0x04);
        outputStream.write((byte)0x01);
        outputStream.writeShort((short)port);
        outputStream.writeInt(0x01);
        outputStream.write((byte)0x00);
        outputStream.write(host.getBytes());
        outputStream.write((byte)0x00);

        DataInputStream inputStream = new DataInputStream(socket.getInputStream());
        if (inputStream.readByte() != (byte)0x00 || inputStream.readByte() != (byte)0x5a) {
            throw new IOException("SOCKS4a connect failed");
        }
        inputStream.readShort();
        inputStream.readInt();
        //socket is ready to use

您应该将未解析的套接字传递给Orbot库。 它应该像这样创建:

InetSocketAddress unresolvedRemote = InetSocketAddress.
createUnresolved(host.getHostName(), remoteAddress.getPort());

顺便说一句,如果你使用apache的httpClient,你可以在这里找到一个示例。

链接地址: http://www.djcxy.com/p/48547.html

上一篇: How to let Tor service to perform the DNS resolution using Socket?

下一篇: cURL not working sometimes and gives empty result