Skip to content

Use system proxy settings in REST and GRAPHQL plugins #939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public Mono<QueryExecutionResult> executeQuery(Object o, GraphQLQueryExecutionCo
.then(Mono.defer(() -> {
URI uri = RestApiUriBuilder.buildUri(context.getUrl(), new HashMap<>(), context.getUrlParams());
WebClient.Builder webClientBuilder = WebClientBuildHelper.builder()
.systemProxy()
.disallowedHosts(commonConfig.getDisallowedHosts())
.toWebClientBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public Mono<QueryExecutionResult> executeQuery(Object webClientFilter, RestApiQu
return Mono.defer(() -> authByOauth2InheritFromLogin(context))
.then(Mono.defer(() -> {
WebClient.Builder webClientBuilder = WebClientBuildHelper.builder()
.systemProxy()
.disallowedHosts(commonConfig.getDisallowedHosts())
.sslConfig(context.getSslConfig())
.timeoutMs(context.getTimeoutMs())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import org.lowcoder.sdk.plugin.common.ssl.SslConfig;
import org.lowcoder.sdk.plugin.common.ssl.SslHelper;
import org.lowcoder.sdk.plugin.common.ssl.VerifySelfSignedCertSslConfig;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.Builder;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.SslProvider;
import reactor.netty.transport.ProxyProvider;
import reactor.netty.transport.ProxyProvider.Proxy;

import javax.net.ssl.SSLException;
Expand All @@ -28,6 +30,8 @@ public class WebClientBuildHelper {

private static final String proxyHost;
private static final String proxyPortStr;
private static final String proxyUsername;
private static final String proxyPassword;

private SslConfig sslConfig;
private Set<String> disallowedHosts;
Expand All @@ -39,6 +43,8 @@ public class WebClientBuildHelper {
static {
proxyHost = System.getProperty("http.proxyHost");
proxyPortStr = System.getProperty("http.proxyPort");
proxyUsername = System.getProperty("http.proxyUsername");
proxyPassword = System.getProperty("http.proxyPassword");
}

private WebClientBuildHelper() {
Expand Down Expand Up @@ -92,10 +98,23 @@ public Builder toWebClientBuilder() {
httpClient = httpClient.secure(sslProviderWithSelfSignedCert(verifySelfSignedCertSslConfig));
}
}
if (systemProxy && StringUtils.isNoneBlank(proxyHost, proxyPortStr)) {
httpClient = httpClient.proxy(typeSpec -> typeSpec.type(Proxy.HTTP)
.host(proxyHost)
.port(Integer.parseInt(proxyPortStr)));
if (systemProxy && StringUtils.isNotBlank(proxyHost)) {
httpClient = httpClient.proxy(typeSpec -> {
ProxyProvider.Builder builder = typeSpec
.type(Proxy.HTTP)
.host(proxyHost);
PropertyMapper mapper = PropertyMapper.get();
mapper.from(proxyPortStr)
.whenHasText()
.to(portStr -> builder.port(Integer.parseInt(portStr)));
mapper.from(proxyUsername)
.whenHasText()
.to(builder::username);
mapper.from(proxyPassword)
.whenHasText()
.to(password -> builder.password(s -> password));
}
);
}
if (CollectionUtils.isNotEmpty(disallowedHosts)) {
httpClient = httpClient.resolver(new SafeHostResolverGroup(disallowedHosts));
Expand Down
Loading