Skip to content
Open
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 @@ -19,6 +19,11 @@
import org.web3j.protocol.ipc.WindowsIpcService;
import org.web3j.spring.actuate.Web3jHealthIndicator;

import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -72,6 +77,7 @@ private Web3jService buildService(String clientAddress) {

private OkHttpClient createOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
configureProxy(builder);
configureLogging(builder);
configureTimeouts(builder);
return builder.build();
Expand All @@ -86,6 +92,17 @@ private void configureTimeouts(OkHttpClient.Builder builder) {
}
}

private void configureProxy(OkHttpClient.Builder builder) {
if (properties.getProxyUrl() != null) {
try {
URL proxyUrl = new URL(properties.getProxyUrl());
Proxy proxy = new Proxy(Objects.equals(proxyUrl.getProtocol(), "http") ? Proxy.Type.HTTP : Proxy.Type.SOCKS, new InetSocketAddress(proxyUrl.getHost(), proxyUrl.getPort()));
builder.proxy(proxy);
} catch (MalformedURLException ignored) {
}
}
}

private static void configureLogging(OkHttpClient.Builder builder) {
if (log.isDebugEnabled()) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(log::debug);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/web3j/spring/autoconfigure/Web3jProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class Web3jProperties {

private Long httpTimeoutSeconds;


private String proxyUrl;

public String getClientAddress() {
return clientAddress;
}
Expand Down Expand Up @@ -51,5 +54,12 @@ public Long getHttpTimeoutSeconds() {
public void setHttpTimeoutSeconds(Long httpTimeoutSeconds) {
this.httpTimeoutSeconds = httpTimeoutSeconds;
}

public String getProxyUrl() { return proxyUrl; }

public void setProxyUrl(String proxyUrl) {
this.proxyUrl = proxyUrl;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public void testHttpClient() throws Exception {
"https://localhost:12345", HttpService.class);
}

@Test
public void testHttpProxy() throws Exception {
verifyHttpProxyConnection(
"https://localhost:12345", "http://proxy.fakecompany.com:8080", HttpService.class);
}


@Test
public void testUnixIpcClient() throws IOException {
Path path = Files.createTempFile("unix", "ipc");
Expand Down Expand Up @@ -129,6 +136,30 @@ private void verifyHttpConnection(
assertThat(url, equalTo(expectedClientAddress));
}

private void verifyHttpProxyConnection(
String clientAddress, String proxyUrl, Class<? extends Service> cls) throws Exception {
verifyHttpProxyConnection(clientAddress, clientAddress, proxyUrl, cls);
}

private void verifyHttpProxyConnection(
String clientAddress, String expectedClientAddress, String proxyUrl, Class<? extends Service> cls)
throws Exception {
load(EmptyConfiguration.class, "web3j.client-address=" + clientAddress, "web3j.proxy-url=http://proxy.fakecompany.com:8080");
Web3j web3j = this.context.getBean(Web3j.class);

Field web3jServiceField = JsonRpc2_0Web3j.class.getDeclaredField("web3jService");
web3jServiceField.setAccessible(true);
Web3jService web3jService = (Web3jService) web3jServiceField.get(web3j);

assertTrue(cls.isInstance(web3jService));

Field urlField = HttpService.class.getDeclaredField("url");
urlField.setAccessible(true);
String url = (String) urlField.get(web3jService);

assertThat(url, equalTo(expectedClientAddress));
}

@Configuration
static class EmptyConfiguration {
}
Expand Down