The aim is to use socks5 for all connections in android app. As it said here https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html,
I just need to use system properties to set port, hostname and socks version:
System.setProperty("socksProxyHost",addr);
System.setProperty("socksProxyPort",port);
System.setProperty("socksProxyVersion","5")
Then create a connection:
protected String doInBackground(String... sUrl) {
OutputStream output = null;
InputStream input = null;
byte data[] = new byte[4096];
int total = 0, fileNameLength = sUrl[0].length();
int count;
try {
// Download file from URL
URL url = new URL(sUrl[0]);
SocketAddress socketAddress;
connection = (HttpsURLConnection)url.openConnection();
//Accepting all certs
TrustManager[] trustManagers = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
}
};
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return s.equals(sslSession.getPeerHost());
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, null);
connection.setSSLSocketFactory(sslContext.getSocketFactory());
connection.setHostnameVerifier(hostnameVerifier);
input = connection.getInputStream();
But when I sniffer traffic, it always uses socks4. Http, Https proxies work as expected. Then I tryed to find out which type of socket creates, if I call Socket constructor with a proxy specified directly:
Socket socket = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxyAddr, proxyPort)));
Class clazzSocks = socket.getClass();
Method setSockVersion = null;
Field sockImplField = null;
SocketImpl socksimpl = null;
sockImplField = clazzSocks.getDeclaredField("impl");
sockImplField.setAccessible(true);
socksimpl = (SocketImpl) sockImplField.get(socket);
Class clazzSocksImpl = socksimpl.getClass();
And it shows that clazzSocksImpl is PlainSocketImpl instead of SocksSocketImpl that I expected. Can anybody tell me what am i doing wrong?