Skip to content

Requests silently bypass SOCKS proxy when using HttpClientExecutor (Java 11+ default) #2468

Description

@Nnnes

To reproduce:

ssh has built-in SOCKS server functionality with -D:

$ ssh -NvD 1080 hostname

Test the SOCKS server:

$ curl --socks5 localhost:1080 https://eth0.me

If the proxy is working, this prints hostname's public IP address.

Java:

static void test() throws Exception {
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 1080));

    URLConnection urlConnection = URI.create("https://eth0.me").toURL().openConnection(proxy);
    System.out.println(new String(urlConnection.getInputStream().readAllBytes()));

    Connection jsoupConnection = Jsoup.connect("https://eth0.me").proxy(proxy);
    System.out.println(jsoupConnection.get().text());

    System.setProperty("jsoup.useHttpClient", "false");
    Connection jsoupConnection2 = Jsoup.connect("https://eth0.me").proxy(proxy);
    System.out.println(jsoupConnection2.get().text());
}

This prints hostname's public IP address, then yours, then hostname's again.

HttpClient's lack of support for SOCKS proxies is apparently poorly documented intended behavior (see https://stackoverflow.com/a/70011047 and https://bugs.openjdk.org/browse/JDK-8214516).

I think this can be fixed easily by adding a line to RequestDispatch#get(...):

if (request.proxy() != null && request.proxy().type() != Proxy.Type.HTTP) useHttpClient = false;

In the meantime, affected users (perhaps only me!) can use System.setProperty("jsoup.useHttpClient", "false"); as in my example as a workaround.

Activity

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

Metadata

Metadata

Assignees

Labels

bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions