diff --git a/src/main/java/io/castle/client/internal/backend/OkHttpFactory.java b/src/main/java/io/castle/client/internal/backend/OkHttpFactory.java index 4b32028..4d3b4f5 100644 --- a/src/main/java/io/castle/client/internal/backend/OkHttpFactory.java +++ b/src/main/java/io/castle/client/internal/backend/OkHttpFactory.java @@ -7,6 +7,8 @@ import okhttp3.logging.HttpLoggingInterceptor; import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.util.concurrent.TimeUnit; public class OkHttpFactory implements RestApiFactory { @@ -18,7 +20,7 @@ public class OkHttpFactory implements RestApiFactory { public OkHttpFactory(CastleConfiguration configuration, CastleGsonModel modelInstance) { this.configuration = configuration; this.modelInstance = modelInstance; - client = createOkHttpClient(); + this.client = createOkHttpClient(); } private OkHttpClient createOkHttpClient() { @@ -41,6 +43,14 @@ private OkHttpClient createOkHttpClient() { builder = builder.addInterceptor(logging); } + String httpProxyAddress = configuration.getHttpProxyAddress(); + if (httpProxyAddress != null && !httpProxyAddress.isEmpty()) { + String[] proxyParts = httpProxyAddress.split(":"); + String proxyHost = proxyParts[0]; + int proxyPort = Integer.parseInt(proxyParts[1]); + builder = builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort))); + } + OkHttpClient client = builder .addInterceptor(new Interceptor() { @Override diff --git a/src/main/java/io/castle/client/internal/config/CastleConfiguration.java b/src/main/java/io/castle/client/internal/config/CastleConfiguration.java index d4b5f73..4e63a01 100644 --- a/src/main/java/io/castle/client/internal/config/CastleConfiguration.java +++ b/src/main/java/io/castle/client/internal/config/CastleConfiguration.java @@ -66,7 +66,12 @@ public class CastleConfiguration { */ private final int maxRequests; - public CastleConfiguration(String apiBaseUrl, int timeout, AuthenticateFailoverStrategy authenticateFailoverStrategy, List allowListHeaders, List denyListHeaders, String apiSecret, String castleAppId, CastleBackendProvider backendProvider, boolean logHttpRequests, List ipHeaders, Integer maxRequests) { + /** + * Address to proxy the HTTP requests through. + */ + private final String httpProxyAddress; + + public CastleConfiguration(String apiBaseUrl, int timeout, AuthenticateFailoverStrategy authenticateFailoverStrategy, List allowListHeaders, List denyListHeaders, String apiSecret, String castleAppId, CastleBackendProvider backendProvider, boolean logHttpRequests, List ipHeaders, Integer maxRequests, String httpProxyAddress) { this.apiBaseUrl = apiBaseUrl; this.timeout = timeout; this.authenticateFailoverStrategy = authenticateFailoverStrategy; @@ -78,6 +83,7 @@ public CastleConfiguration(String apiBaseUrl, int timeout, AuthenticateFailoverS this.logHttpRequests = logHttpRequests; this.ipHeaders = ipHeaders; this.maxRequests = maxRequests; + this.httpProxyAddress = httpProxyAddress; } public String getApiBaseUrl() { @@ -133,4 +139,8 @@ public List getIpHeaders() { public int getMaxRequests() { return maxRequests; } + + public String getHttpProxyAddress() { + return httpProxyAddress; + } } diff --git a/src/main/java/io/castle/client/internal/config/CastleConfigurationBuilder.java b/src/main/java/io/castle/client/internal/config/CastleConfigurationBuilder.java index ab177de..dcaf768 100644 --- a/src/main/java/io/castle/client/internal/config/CastleConfigurationBuilder.java +++ b/src/main/java/io/castle/client/internal/config/CastleConfigurationBuilder.java @@ -92,6 +92,11 @@ public class CastleConfigurationBuilder { */ private int maxRequests = 5; + /** + * Address to proxy the HTTP requests through. + */ + private String httpProxyAddress; + private CastleConfigurationBuilder() { } @@ -336,7 +341,8 @@ public CastleConfiguration build() throws CastleSdkConfigurationException { backendProvider, logHttpRequests, ipHeaders, - maxRequests); + maxRequests, + httpProxyAddress); } /** @@ -407,4 +413,20 @@ public CastleConfigurationBuilder withIPHeaders(List ipHeaders) { public CastleConfigurationBuilder ipHeaders(List ipHeaders) { return withIPHeaders(ipHeaders); } + + /** + * Address to proxy the HTTP requests through. + * + * @param httpProxyAddress address (host:port) to proxy the HTTP requests through. + * @return a castleConfigurationBuilder with HTTP proxy address set + */ + public CastleConfigurationBuilder withHttpProxyAddress(String httpProxyAddress) { + this.httpProxyAddress = httpProxyAddress; + return this; + } + + /* alias for withHttpProxyAddress */ + public CastleConfigurationBuilder httpProxyAddress(String httpProxyAddress) { + return withHttpProxyAddress(httpProxyAddress); + } } \ No newline at end of file diff --git a/src/main/java/io/castle/client/internal/config/ConfigurationLoader.java b/src/main/java/io/castle/client/internal/config/ConfigurationLoader.java index 30513ab..08bb2ae 100644 --- a/src/main/java/io/castle/client/internal/config/ConfigurationLoader.java +++ b/src/main/java/io/castle/client/internal/config/ConfigurationLoader.java @@ -150,6 +150,11 @@ public CastleConfigurationBuilder loadConfigurationBuilder() { "ip_headers", "CASTLE_SDK_IP_HEADERS" ); + String httpProxyAddress = loadConfigurationValue( + castleConfigurationProperties, + "http_proxy_address", + "CASTLE_SDK_HTTP_PROXY_ADDRESS" + ); CastleConfigurationBuilder builder = CastleConfigurationBuilder .defaultConfigBuilder() .withApiSecret(envApiSecret) @@ -194,6 +199,9 @@ public CastleConfigurationBuilder loadConfigurationBuilder() { if (ipHeadersValue != null) { builder.withIPHeaders(Splitter.on(",").splitToList(ipHeadersValue)); } + if (httpProxyAddress != null) { + builder.withHttpProxyAddress(httpProxyAddress); + } return builder; }