android socks5 proxy,How to use socks5 proxy in android app?

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?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值