Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fancellu opened this issue Dec 9, 2019 · 7 comments
Open

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

fancellu opened this issue Dec 9, 2019 · 7 comments

Comments

@fancellu
Copy link

fancellu commented Dec 9, 2019

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
Copy link
Author

fancellu commented Dec 9, 2019

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
Copy link
Contributor

@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
Copy link
Contributor

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
Copy link
Author

fancellu commented Dec 9, 2019

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
Copy link
Contributor

I got this working for Gatling (DNS resolution was already working fine for HTTP proxies), see gatling/gatling#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
Copy link
Author

My work around is to use okhttp

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

@vito-c
Copy link

vito-c commented Feb 24, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants