在復雜的網絡環境中,很多使用socks5代理服務器,那么在代碼中通常一些socket連接操作需要針對socks5協議做一些修改。下面我們主要介紹java中對于使用socks5代理的改造。
1.基于jsch進行ssh連接
public void connect() throws JSchException{
int i = 0;
for (; i < this.tryConnectTimes; i++) {
try {
this.session = jsch.getSession(this.user, this.host, this.port);
this.session.setConfig(this.properties);
this.session.setPassword(this.password);
this.session.setSocketFactory(this.socketFactory);
this.session.setTimeout(this.readTimeout);
//加上這段設置代理
ProxySOCKS5 proxy = new ProxySOCKS5("172.16.0.14", 1002);
proxy.setUserPasswd("dds", "ueJ20@d8in25");
session.setProxy(proxy);
this.session.connect();
if (this.session.isConnected()) {
logger.debug(this.host + " : " + this.port +" 連接成功, 用戶 : " + this.user);
break;
}
} catch (JSchException e) {
logger.warn("第"+(i+1) + "次連接失敗 " + this.host + " : " + this.port + ", 用戶: " + this.user, e);
}
//sleep 后,嘗試重新連接
if (i != tryConnectTimes - 1) {
try {
Thread.sleep(this.sleepTime);
} catch (InterruptedException e) {
logger.warn("connect() : sleep 出現異常");
}
}
}
//達到最大連接次數,拋出異常
if (i >= tryConnectTimes) {
throw new JSchException(this.host + " : " + this.port + " 連接超時, 用戶: " + this.user);
}
}
2.Spring中RestTemplate通過socks5連接
public static RestTemplate createRestTemplateWithSocks5Proxy(String socks5Host, int socks5Port, String username, String password) {
// 創建 SOCKS5 代理的主機
HttpHost socks5Proxy = new HttpHost(socks5Host, socks5Port, "socks");
// 創建憑據提供程序,并設置憑據
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
// 創建 HttpClient,并配置 SOCKS5 代理和憑據提供程序
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultCredentialsProvider(credentialsProvider)
.setRoutePlanner(new DefaultProxyRoutePlanner(socks5Proxy) {
@Override
public HttpHost determineProxy(org.apache.http.HttpHost target,
org.apache.http.HttpRequest request,
org.apache.http.protocol.HttpContext context) throws HttpException {
// 如果目標主機是需要走代理的,返回代理主機
if ("example.com".equals(target.getHostName())) {
return super.determineProxy(target, request, context);
}
// 否則返回 null,表示直接連接目標主機
return null;
}
})
.build();
// 創建 HttpComponentsClientHttpRequestFactory,并設置 HttpClient
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
// 創建 RestTemplate,并設置請求工廠
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}