AsyncHttpClient / async-http-client

Asynchronous Http and WebSocket Client library for Java
Other
6.29k stars 1.59k forks source link

Problem with DNS name resolution via socks (Tor) proxy #1682

Open fancellu opened 4 years ago

fancellu commented 4 years ago

Run up a socks proxy, for example Tor browser proxy on port 9150

Run this code

> import org.asynchttpclient.AsyncHttpClient;
> import org.asynchttpclient.Response;
> import org.asynchttpclient.proxy.ProxyType;
> 
> import java.util.concurrent.Future;
> 
> import static org.asynchttpclient.Dsl.*;
> 
> public class TorDNSIssue {
> 
>     public static void main(String[] args) throws Exception {
> 
>         AsyncHttpClient c = asyncHttpClient(config().setProxyServer(proxyServer("localhost", 9150).setProxyType(ProxyType.SOCKS_V5)));
> 
>         Future<Response> whenResponse = c.prepareGet("http://check.expyuzz4wqqyqhjn.onion").execute();
> 
>         Response response = whenResponse.get();
> 
>         System.out.println(response);
> 
>     }
> }

I get

Exception in thread "main" java.util.concurrent.ExecutionException: java.net.UnknownHostException: check.expyuzz4wqqyqhjn.onion
    at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
    at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
    at org.asynchttpclient.netty.NettyResponseFuture.get(NettyResponseFuture.java:201)
    at playpen.sttp.TorDNSIssue.main(TorDNSIssue.java:19)
Caused by: java.net.UnknownHostException: check.expyuzz4wqqyqhjn.onion
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1277)
    at java.net.InetAddress.getAllByName(InetAddress.java:1193)
    at java.net.InetAddress.getAllByName(InetAddress.java:1127)
    at io.netty.util.internal.SocketUtils$9.run(SocketUtils.java:159)
    at io.netty.util.internal.SocketUtils$9.run(SocketUtils.java:156)
    at java.security.AccessController.doPrivileged(Native Method)
    at io.netty.util.internal.SocketUtils.allAddressesByName(SocketUtils.java:156)

If I point it at https://check.torproject.org/ it works fine

Do I have to do something else to make it not fail?

Thanks

fancellu commented 4 years ago

BTW, this is an example of it working just fine with HttpURLConnection


import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Proxy;
import java.net.URL;

import java.net.*;

public class TorNoDNSIssue {

    public static String readInputStreamAsString(InputStream in)
            throws IOException {

        BufferedInputStream bis = new BufferedInputStream(in);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        int result = bis.read();
        while(result != -1) {
            byte b = (byte)result;
            buf.write(b);
            result = bis.read();
        }
        return buf.toString();
    }

    public static void main(String[] args) throws Exception {

        Proxy proxy =  new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 9150));
        URL url = new URL("http://check.expyuzz4wqqyqhjn.onion");

        InputStream is=url.openConnection(proxy).getInputStream();

        String string = readInputStreamAsString(is);

        is.close();

        System.out.println(string);

    }
}
slandelle commented 4 years ago

@fancellu Your HttpURLConnection based sample doesn't work for me. And as far as I know, Java doesn't support this, see https://bugs.openjdk.java.net/browse/JDK-8028776.

slandelle commented 4 years ago

Mmm, actually, I see the DNS resolution in Charles, not in AHC, so I guess it works now and the Java issue should be closed.

fancellu commented 4 years ago

Charles?

It very much does work for me:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
   <TITLE>Welcome to hetzner-hel1-03!</TITLE>
</HEAD>
<BODY>

<H1>Welcome to hetzner-hel1-03!</H1>

This is hetzner-hel1-03, a system run by and for the <a href="http://www.torproject.org/">Tor Project</a>.
She does stuff.
What kind of stuff and who our kind sponsors are you might learn on
<a href="http://db.torproject.org/machines.cgi?host=hetzner-hel1-03">db.torproject.org</a>.

<P>
<HR NOSHADE />
<FONT size="-1">torproject-admin</FONT>

</BODY>
</HTML>

Process finished with exit code 0

BTW, I love Gatling,

slandelle commented 4 years ago

I got this working for Gatling (DNS resolution was already working fine for HTTP proxies), see https://github.com/gatling/gatling/issues/3835.

Now, regarding AHC, honestly, the code base is very different now and I don't now if/when I'll have time for this someday.

The best solution would be to contribute a fix.

fancellu commented 4 years ago

My work around is to use okhttp

https://gist.github.com/fancellu/df6dde59249edf8401b28bbc44a78cbc

vito-c commented 3 years ago

@TomGranot I'm also noticing this for other Socks 5 proxy traffic