Last active
January 10, 2017 12:17
-
-
Save avarabyeu/102686781eda3f780e20064e6f940c6c to your computer and use it in GitHub Desktop.
RP socks proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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