Skip to content

Commit ed2e3fc

Browse files
committed
feat(proxy): support http(s) proxy
1 parent 073f8a1 commit ed2e3fc

File tree

1 file changed

+118
-13
lines changed

1 file changed

+118
-13
lines changed

volcengine-java-sdk-core/src/main/java/com/volcengine/ApiClient.java

Lines changed: 118 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@
7272
import java.lang.reflect.Field;
7373
import java.lang.reflect.Method;
7474
import java.lang.reflect.Type;
75-
import java.net.URLConnection;
76-
import java.net.URLEncoder;
75+
import java.net.*;
7776
import java.nio.file.Files;
7877
import java.nio.file.Paths;
7978
import java.security.GeneralSecurityException;
@@ -141,18 +140,19 @@ public class ApiClient extends BaseClient{
141140
private Boolean useDualStack;
142141

143142
private boolean autoRetry = DefaultRetryerSetting.DEFAULT_AUTO_RETRY_ENABLED;
144-
145143
private final Retryer retryer = DefaultRetryerSetting.DEFAULT_RETRYER;
146144

145+
private String httpProxy;
146+
private String httpsProxy;
147+
private String noProxy;
148+
147149
/*
148150
* Constructor for ApiClient
149151
*/
150152
public ApiClient() {
151153
ConnectionPool connectionPool = new ConnectionPool(maxIdleConns, keepAliveDurationMs);
152154
httpClient = new OkHttpClient();
153155
httpClient.setConnectionPool(connectionPool);
154-
155-
156156
verifyingSsl = true;
157157

158158
json = new JSON();
@@ -591,25 +591,130 @@ public ApiClient setCustomBootstrapRegion(Set<String> customBootstrapRegion) {
591591
}
592592

593593
/**
594-
* Get the use dual stack flag.
594+
* Get the http proxy.
595+
*
596+
* @return http proxy
597+
*/
598+
public String getHttpProxy() {
599+
return this.httpProxy;
600+
}
601+
602+
/**
603+
* Set the http proxy.
604+
*
605+
* @return Api client
606+
*/
607+
public ApiClient setHttpProxy(String httpProxy) {
608+
this.httpProxy = httpProxy;
609+
updateClientProxy();
610+
return this;
611+
}
612+
613+
/**
614+
* Get the https proxy.
615+
*
616+
* @return https proxy
617+
*/
618+
public String getHttpsProxy() {
619+
return this.httpsProxy;
620+
}
621+
622+
/**
623+
* Set the https proxy.
624+
*
625+
* @return Api client
626+
*/
627+
public ApiClient setHttpsProxy(String httpsProxy) {
628+
this.httpsProxy = httpsProxy;
629+
updateClientProxy();
630+
return this;
631+
}
632+
633+
/**
634+
* Get the no proxy.
595635
*
596-
* @return use dual stack flag
636+
* @return no proxy
597637
*/
598-
public Boolean getUseDualStack() {
599-
return this.useDualStack;
638+
public String getNoProxy() {
639+
return this.noProxy;
600640
}
601641

602642
/**
603-
* Set the use dual stack flag.
643+
* Set the no proxy.
604644
*
605-
* @param useDualStack boolean
606645
* @return Api client
607646
*/
608-
public ApiClient setUseDualStack(boolean useDualStack) {
609-
this.useDualStack = useDualStack;
647+
public ApiClient setNoProxy(String noProxy) {
648+
this.noProxy = noProxy;
649+
updateClientProxy();
610650
return this;
611651
}
612652

653+
private void updateClientProxy() {
654+
httpClient.setProxySelector(new ProxySelector() {
655+
@Override
656+
public List<Proxy> select(URI uri) {
657+
String targetHost = uri.getHost();
658+
659+
String envNoProxy = System.getenv("NO_PROXY");
660+
if (StringUtils.isEmpty(envNoProxy)) {
661+
envNoProxy = System.getenv("no_proxy");
662+
}
663+
664+
boolean noProxyFlag = false;
665+
String noProxyList = (StringUtils.isEmpty(noProxy) ? envNoProxy : noProxy);
666+
if (!StringUtils.isEmpty(noProxyList)) {
667+
String[] noProxyArr = noProxyList.split(",");
668+
for (String noProxyHost : noProxyArr) {
669+
if (noProxyHost.equals(targetHost)) {
670+
noProxyFlag = true;
671+
break;
672+
}
673+
}
674+
}
675+
676+
List<Proxy> proxies = new ArrayList<>();
677+
if (noProxyFlag) {
678+
proxies.add(Proxy.NO_PROXY);
679+
return proxies;
680+
}
681+
682+
addProxy(proxies, httpProxy, "HTTP_PROXY");
683+
addProxy(proxies, httpsProxy, "HTTPS_PROXY");
684+
685+
return proxies;
686+
}
687+
688+
@Override
689+
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
690+
}
691+
});
692+
}
693+
694+
private void addProxy(List<Proxy> proxies, String proxy, String env) {
695+
if (!StringUtils.isEmpty(proxy)) {
696+
try {
697+
URI u = new URI(proxy);
698+
proxies.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(u.getHost(), u.getPort())));
699+
} catch (URISyntaxException e) {
700+
throw new RuntimeException(e);
701+
}
702+
} else if (!StringUtils.isEmpty(env)) {
703+
String envProxy = System.getenv(env.toUpperCase());
704+
if (StringUtils.isEmpty(envProxy)) {
705+
envProxy = System.getenv(env.toLowerCase());
706+
}
707+
if (!StringUtils.isEmpty(envProxy)) {
708+
try {
709+
URI u = new URI(envProxy);
710+
proxies.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(u.getHost(), u.getPort())));
711+
} catch (URISyntaxException e) {
712+
throw new RuntimeException(e);
713+
}
714+
}
715+
}
716+
}
717+
613718
/**
614719
* Format the given parameter object into string.
615720
*

0 commit comments

Comments
 (0)