pref: 如果系统设置了http代理则创建httpClient时设置代理 (#956)

* 修改缓存时长

* 如果系统设置了http代理则创建httpClient时设置代理

* 如果系统设置了http代理则创建httpClient时设置代理

* 如果系统设置了http代理则创建httpClient时设置代理

* 如果系统设置了http代理则创建httpClient时设置代理

* 如果系统设置了http代理则创建httpClient时设置代理
添加注释

* 添加单元测试
没有useJUnitPlatform()单元测试跑不起来

* 修改单元测试,删除zxing的依赖

* 修正错误的http配置格式

Co-authored-by: Sheiy <35429674@qq.com>
pull/959/head
sheiy 2020-07-10 12:04:43 +08:00 committed by GitHub
parent feefebe07f
commit d4b23afb55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 14 deletions

View File

@ -61,7 +61,6 @@ ext {
levelDbVersion = "0.12"
annotationsVersion = "3.0.1u2"
jedisVersion = '3.3.0'
zxingVersion = "3.4.0"
huaweiObsVersion = "3.19.7"
githubApiVersion = "1.84"
powermockVersion = "1.6.6"
@ -114,8 +113,6 @@ dependencies {
implementation "net.sf.image4j:image4j:$image4jVersion"
implementation "org.flywaydb:flyway-core:$flywayVersion"
implementation "com.google.zxing:core:$zxingVersion"
implementation "org.iq80.leveldb:leveldb:$levelDbVersion"
implementation "redis.clients:jedis:$jedisVersion"
runtimeOnly "com.h2database:h2:$h2Version"

View File

@ -1,17 +1,27 @@
package run.halo.app.utils;
import cn.hutool.core.lang.Tuple;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.lang.NonNull;
import javax.net.ssl.SSLContext;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
/**
* Http client utilities.
@ -41,14 +51,71 @@ public class HttpClientUtils {
@NonNull
public static CloseableHttpClient createHttpsClient(int timeout) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, (certificate, authType) -> true)
.build();
.loadTrustMaterial(null, (certificate, authType) -> true)
.build();
return HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setDefaultRequestConfig(getRequestConfig(timeout))
.build();
return resolveProxySetting(HttpClients.custom())
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setDefaultRequestConfig(getRequestConfig(timeout))
.build();
}
/**
* resolve system proxy config
*
* @param httpClientBuilder the httpClientBuilder
* @return the argument
*/
private static HttpClientBuilder resolveProxySetting(final HttpClientBuilder httpClientBuilder) {
final String httpProxyEnv = System.getenv("http_proxy");
if (StringUtils.isNotBlank(httpProxyEnv)) {
final Tuple httpProxy = resolveHttpProxy(httpProxyEnv);
final HttpHost httpHost = HttpHost.create(httpProxy.get(0));
httpClientBuilder.setProxy(httpHost);
if (httpProxy.getMembers().length == 3) {
//set proxy credentials
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(httpHost.getHostName(), httpHost.getPort()),
new UsernamePasswordCredentials(httpProxy.get(1), httpProxy.get(2)));
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}
return httpClientBuilder;
}
/**
* @param httpProxy http proxy env values
* @return resolved http proxy values; first is host(@nonNull), second is username(@nullable), third is password(@nullable)
*/
private static Tuple resolveHttpProxy(final String httpProxy) {
final URI proxyUri = URI.create(httpProxy);
int port = proxyUri.getPort();
if (port == -1) {
if (Objects.equals("http", proxyUri.getScheme())) {
port = 80;
}
if (Objects.equals("https", proxyUri.getScheme())) {
port = 443;
}
}
final String hostUrl = proxyUri.getScheme() + "://" + proxyUri.getHost() + ":" + port;
final String usernamePassword = proxyUri.getUserInfo();
if (StringUtils.isNotBlank(usernamePassword)) {
final String username;
final String password;
final int atColon = usernamePassword.indexOf(':');
if (atColon >= 0) {
username = usernamePassword.substring(0, atColon);
password = usernamePassword.substring(atColon + 1);
} else {
username = usernamePassword;
password = null;
}
return new Tuple(hostUrl, username, password);
} else {
return new Tuple(hostUrl);
}
}
/**
@ -59,10 +126,10 @@ public class HttpClientUtils {
*/
private static RequestConfig getRequestConfig(int timeout) {
return RequestConfig.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout)
.build();
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout)
.build();
}

View File

@ -0,0 +1,34 @@
package run.halo.app.utils;
import cn.hutool.core.lang.Tuple;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class HttpClientUtilsTest {
@Test
void resolveHttpProxyTest() throws Exception {
final Method resolveHttpProxy = HttpClientUtils.class.getDeclaredMethod("resolveHttpProxy", String.class);
resolveHttpProxy.setAccessible(true);
Tuple result = (Tuple) resolveHttpProxy.invoke(null, "http://127.0.0.1");
assertEquals(result.get(0), "http://127.0.0.1:80");
result = (Tuple) resolveHttpProxy.invoke(null, "https://127.0.0.1");
assertEquals(result.get(0), "https://127.0.0.1:443");
result = (Tuple) resolveHttpProxy.invoke(null, "https://127.0.0.1:123");
assertEquals(result.get(0), "https://127.0.0.1:123");
result = (Tuple) resolveHttpProxy.invoke(null, "https://u:p@127.0.0.1:123");
assertArrayEquals(result.getMembers(), new Object[]{"https://127.0.0.1:123", "u", "p"});
result = (Tuple) resolveHttpProxy.invoke(null, "https://u@127.0.0.1");
assertArrayEquals(result.getMembers(), new Object[]{"https://127.0.0.1:443", "u", null});
}
}