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

Support SOCKS proxies #1009

Closed
dlubarov opened this Issue Jul 31, 2014 · 9 comments

Comments

5 participants
@dlubarov
Copy link

dlubarov commented Jul 31, 2014

In RouteSelected.resetNextInetSocketAddress(), if a proxy is configured, we do

socketHost = proxySocketAddress.getHostName();
socketPort = proxySocketAddress.getPort();

which determines the address eventually passed to Socket.connect(). Shouldn't we always pass it the address of the target host rather than the proxy server?

My test seems to confirm - testWithOkHttp() isn't working unless I change the logic in resetNextInetSocketAddress()

public class ProxyTest {
  private static final String DESTINATION_URL = "http://bot.whatismyipaddress.com";

  // A random socks proxy from a database. Use another if this one stops working.
  private static final Proxy PROXY = new Proxy(Proxy.Type.SOCKS,
      InetSocketAddress.createUnresolved("69.127.234.157", 34483));

  @Test
  public void testWithOkHttp() throws IOException {
    OkHttpClient client = new OkHttpClient().setProxy(PROXY);
    Request request = new Request.Builder().url(DESTINATION_URL).build();
    Response response = client.newCall(request).execute();
    String responseString = inputStreamToString(response.body().byteStream());
    System.out.println(responseString);
  }

  @Test
  public void testWithJavaNet() throws IOException {
    InputStream response = new URL(DESTINATION_URL).openConnection(PROXY).getInputStream();
    String responseString = inputStreamToString(response);
    System.out.println(responseString);
  }

  private static String inputStreamToString(InputStream inputStream) throws IOException {
    Reader reader = new InputStreamReader(inputStream, StandardCharsets.US_ASCII);
    StringBuilder sb = new StringBuilder();
    for (int c; (c = reader.read()) != -1; sb.append((char) c));
    return sb.toString();
  }
}
@swankjesse

This comment has been minimized.

Copy link
Member

swankjesse commented Jul 31, 2014

We don't support socks proxies. The code above is for HTTP proxies, where the proxy server is the one making the connection to the origin server.

@dlubarov

This comment has been minimized.

Copy link
Author

dlubarov commented Jul 31, 2014

Oh, gotcha.

I realize there's probably not much demand, but wouldn't supporting socks just involve

-    if (proxy.type() == Proxy.Type.DIRECT) {
+    if (proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.SOCKS) {
       socketHost = uri.getHost();
       socketPort = getEffectivePort(uri);

(in resetNextInetSocketAddress())

It makes my test pass at least!

@hrj

This comment has been minimized.

Copy link

hrj commented Aug 31, 2014

Confirming that the one-line patch from @dlubarov works fine with my SOCKS proxy, AFAICT.

@swankjesse swankjesse changed the title Proxy logic seems wrong? Support SOCKS proxies Sep 28, 2014

@swankjesse

This comment has been minimized.

Copy link
Member

swankjesse commented Sep 28, 2014

Any idea how we'd test SOCKS proxies? I don't know anything about it.

@swankjesse swankjesse added this to the 3.0 milestone Sep 28, 2014

@hrj

This comment has been minimized.

Copy link

hrj commented Sep 28, 2014

Well, I don't know much about the protocol, but an easy way to create a SOCKS proxy is using plain old ssh: ssh -N -D 9000 <user@host>

This opens a local port listening on port 9000 and that acts as a SOCKS 5 proxy. The other end of the proxy will be the remote host.

As you can imagine, this is very handy and useful. It is like a mini VPN. And sometimes even better than a VPN because you can control which applications go through the proxy.

@swankjesse swankjesse modified the milestones: 2.2, 3.0 Nov 5, 2014

@xiongchiamiov

This comment has been minimized.

Copy link

xiongchiamiov commented Dec 12, 2014

Orbot, the Tor app for Android, creates both an HTTP proxy and a SOCKS one, so that's another way to test it out.

hrj added a commit to UprootLabs/gngrPreHistoric that referenced this issue Dec 18, 2014

used socks fix for okhttp
Use a self-built binary with this fix:
square/okhttp#1009

In my okhttp repo, it is built from the branch `socksFix`

hrj added a commit to hrj/gngr that referenced this issue Dec 25, 2014

upgraded to okhttp 2.1.1
has the SOCKS proxy fix from square/okhttp#1009
@hrj

This comment has been minimized.

Copy link

hrj commented Dec 27, 2014

Since you are planning this for 2.2, I would like to mention that we have been using the one-line-patched jar in gngr ( source ) and it seems to be working fine.

In future, it would be nice if the DNS resolution was also delegated to the SOCKS proxy. IIRC, the system properties for that behaviour are already defined. I could create a separate issue for that.

PS. Taking this opportunity to thank square for this project! You are listed on our credits page.

@swankjesse

This comment has been minimized.

Copy link
Member

swankjesse commented Dec 27, 2014

@hrj awesome.

swankjesse added a commit that referenced this issue Dec 28, 2014

Support SOCKS proxies.
The trickiest part of this change is the SOCKS 5 proxy implemented
to make testing possible. Fortunately the protocol is very easy, and
shows off Okio.

Closes #1009

swankjesse added a commit that referenced this issue Dec 28, 2014

Support SOCKS proxies.
The trickiest part of this change is the SOCKS 5 proxy implemented
to make testing possible. Fortunately the protocol is very easy, and
shows off Okio.

Closes #1009

swankjesse added a commit that referenced this issue Dec 28, 2014

Support SOCKS proxies.
The trickiest part of this change is the SOCKS 5 proxy implemented
to make testing possible. Fortunately the protocol is very easy, and
shows off Okio.

Closes #1009

swankjesse added a commit that referenced this issue Dec 28, 2014

Support SOCKS proxies.
The trickiest part of this change is the SOCKS 5 proxy implemented
to make testing possible. Fortunately the protocol is very easy, and
shows off Okio.

Closes #1009

swankjesse added a commit that referenced this issue Dec 28, 2014

Support SOCKS proxies.
The trickiest part of this change is the SOCKS 5 proxy implemented
to make testing possible. Fortunately the protocol is very easy, and
shows off Okio.

Closes #1009

swankjesse added a commit that referenced this issue Dec 28, 2014

Support SOCKS proxies.
The trickiest part of this change is the SOCKS 5 proxy implemented
to make testing possible. Fortunately the protocol is very easy, and
shows off Okio.

Closes #1009

swankjesse added a commit that referenced this issue Dec 28, 2014

Support SOCKS proxies.
The trickiest part of this change is the SOCKS 5 proxy implemented
to make testing possible. Fortunately the protocol is very easy, and
shows off Okio.

Closes #1009
@robsonpeixoto

This comment has been minimized.

Copy link

robsonpeixoto commented Jul 24, 2017

Is there a code sample using authentication?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.