Skip to content

Instantly share code, notes, and snippets.

@avarabyeu
Last active January 10, 2017 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avarabyeu/102686781eda3f780e20064e6f940c6c to your computer and use it in GitHub Desktop.
Save avarabyeu/102686781eda3f780e20064e6f940c6c to your computer and use it in GitHub Desktop.
RP socks proxy
public class SocksExtensionModule extends AbstractModule {
@Override
protected void configure() {
}
@Provides
public HttpClient provideHttpClient(@ListenerPropertyValue(ListenerProperty.UUID) String uuid) throws MalformedURLException {
HttpClientFactory httpClientFactory;
List<HttpRequestInterceptor> interceptors = new ArrayList<HttpRequestInterceptor>(1);
interceptors.add(new BearerAuthorizationInterceptor(uuid));
httpClientFactory = new SocksAwareClientFactory(null, interceptors);
return httpClientFactory.createHttpClient();
}
static class SocksAwareClientFactory extends AuthClientFactory {
SocksAwareClientFactory(Credentials credentials, List<HttpRequestInterceptor> interceptors) {
super(credentials, interceptors);
}
@Override
protected HttpClientBuilder initDefaultBuilder() {
HttpClientBuilder builder = super.initDefaultBuilder();
builder.setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.createDefault()) {
@Override
public Socket createSocket(HttpContext context) throws IOException {
//TODO add validation here
InetSocketAddress socksaddr = new InetSocketAddress(System.getProperty("socksProxyHost"),
Integer.parseInt(System.getProperty("socksProxyPort")));
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
return new Socket(proxy);
}
@Override
public Socket connectSocket(int connectTimeout, Socket socket, HttpHost host, InetSocketAddress remoteAddress,
InetSocketAddress localAddress, HttpContext context) throws IOException {
// Convert address to unresolved
InetSocketAddress unresolvedRemote = InetSocketAddress
.createUnresolved(host.getHostName(), remoteAddress.getPort());
return super.connectSocket(connectTimeout, socket, host, unresolvedRemote, localAddress, context);
}
});
return builder;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment