Skip to content

Instantly share code, notes, and snippets.

@swankjesse
Last active September 17, 2021 14:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swankjesse/132ecc49bee853c3c335d33b9a95bef7 to your computer and use it in GitHub Desktop.
Save swankjesse/132ecc49bee853c3c335d33b9a95bef7 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2018 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.recipes;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import javax.net.SocketFactory;
/**
* Java's built-in SOCKS proxy doesn't do DNS lookups for hostnames, even though that's required
* when using a SOCKS 4 proxy (it isn't necessary for SOCKS 5).
*
* In order to use SOCKS 4 with OkHttp, use this socket factory instead of configuring a proxy in
* the OkHttpClient.
*
* <pre>{@code
* ssh -D 4321 proxy.example.com
* }</pre>
*
* <pre>{@code
* final OkHttpClient client = new OkHttpClient.Builder()
* .socketFactory(new Socks4SocketFactory("localhost", 4321))
* .build();
* }</pre>
*/
public final class Socks4SocketFactory extends SocketFactory {
private final Proxy proxy;
public Socks4SocketFactory(String proxyHost, int proxyPort) {
this.proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxyHost, proxyPort));
}
@Override public Socket createSocket() {
return new Socket(proxy);
}
@Override public Socket createSocket(String host, int port) throws IOException {
Socket result = createSocket();
result.connect(new InetSocketAddress(host, port));
return result;
}
@Override public Socket createSocket(InetAddress address, int port) throws IOException {
Socket result = createSocket();
result.connect(new InetSocketAddress(address, port));
return result;
}
@Override public Socket createSocket(
String host, int port, InetAddress localAddress, int localPort) throws IOException {
Socket result = createSocket();
result.bind(new InetSocketAddress(localAddress, localPort));
result.connect(new InetSocketAddress(host, port));
return result;
}
@Override public Socket createSocket(
InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
Socket result = createSocket();
result.bind(new InetSocketAddress(localAddress, localPort));
result.connect(new InetSocketAddress(address, port));
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment