From 8122cab77a96f40c77203f622cc1ab5c639a0098 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 14:48:58 -0500 Subject: [PATCH 01/21] Use Lombok setter and getters --- .../bisq/network/http/HttpClientImpl.java | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 57b2ac86eae..3e87ea292f7 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -49,6 +49,7 @@ import java.util.concurrent.TimeUnit; import lombok.Getter; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import javax.annotation.Nullable; @@ -61,8 +62,11 @@ public class HttpClientImpl implements HttpClient { @Nullable private Socks5ProxyProvider socks5ProxyProvider; @Getter + @Setter private String baseUrl; + @Setter private boolean ignoreSocks5Proxy; + @Getter private final String uid; @Nullable private HttpURLConnection connection; @@ -93,16 +97,6 @@ public void shutDown() { } } - @Override - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - - @Override - public void setIgnoreSocks5Proxy(boolean ignoreSocks5Proxy) { - this.ignoreSocks5Proxy = ignoreSocks5Proxy; - } - @Override public String requestWithGET(String param, @Nullable String headerKey, @@ -165,12 +159,6 @@ public String requestWithGETNoProxy(String param, } } - @Override - public String getUid() { - return uid; - } - - /** * Make an HTTP Get request routed over socks5 proxy. */ From 4c81c442ee7533f25f921f87fc87f3c3cf64cf9b Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 14:49:38 -0500 Subject: [PATCH 02/21] Update toString method --- .../main/java/bisq/network/http/HttpClientImpl.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 3e87ea292f7..4deca2f4350 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -220,10 +220,13 @@ private String convertInputStreamToString(InputStream inputStream) throws IOExce @Override public String toString() { - return "HttpClient{" + - "socks5ProxyProvider=" + socks5ProxyProvider + - ", baseUrl='" + baseUrl + '\'' + - ", ignoreSocks5Proxy=" + ignoreSocks5Proxy + - '}'; + return "HttpClientImpl{" + + "\n socks5ProxyProvider=" + socks5ProxyProvider + + ",\n baseUrl='" + baseUrl + '\'' + + ",\n ignoreSocks5Proxy=" + ignoreSocks5Proxy + + ",\n uid='" + uid + '\'' + + ",\n connection=" + connection + + ",\n httpclient=" + httpclient + + "\n}"; } } From 77f46a0a5e37d98301e4e1e9555a8f0d8a09f286 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 14:50:53 -0500 Subject: [PATCH 03/21] Remove requestWithGETNoProxy methods from interface (not used outside) --- p2p/src/main/java/bisq/network/http/HttpClient.java | 4 ---- .../main/java/bisq/network/http/HttpClientImpl.java | 10 +++++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClient.java b/p2p/src/main/java/bisq/network/http/HttpClient.java index fff78049b6d..4461da052e9 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClient.java +++ b/p2p/src/main/java/bisq/network/http/HttpClient.java @@ -30,10 +30,6 @@ String requestWithGET(String param, @Nullable String headerKey, @Nullable String headerValue) throws IOException; - String requestWithGETNoProxy(String param, - @Nullable String headerKey, - @Nullable String headerValue) throws IOException; - String getUid(); String getBaseUrl(); diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 4deca2f4350..0dc51ba7514 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -61,6 +61,11 @@ public class HttpClientImpl implements HttpClient { @Nullable private Socks5ProxyProvider socks5ProxyProvider; + @Nullable + private HttpURLConnection connection; + @Nullable + private CloseableHttpClient httpclient; + @Getter @Setter private String baseUrl; @@ -68,10 +73,6 @@ public class HttpClientImpl implements HttpClient { private boolean ignoreSocks5Proxy; @Getter private final String uid; - @Nullable - private HttpURLConnection connection; - @Nullable - private CloseableHttpClient httpclient; @Inject public HttpClientImpl(@Nullable Socks5ProxyProvider socks5ProxyProvider) { @@ -124,7 +125,6 @@ public String requestWithGET(String param, /** * Make an HTTP Get request directly (not routed over socks5 proxy). */ - @Override public String requestWithGETNoProxy(String param, @Nullable String headerKey, @Nullable String headerValue) throws IOException { From 3a2e4f1d69eaf883ecd2b57585c04841f86737eb Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 14:52:06 -0500 Subject: [PATCH 04/21] Refactor: Extract method getSocks5Proxy --- .../bisq/network/http/HttpClientImpl.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 0dc51ba7514..04a8c7168d3 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -104,14 +104,7 @@ public String requestWithGET(String param, @Nullable String headerValue) throws IOException { checkNotNull(baseUrl, "baseUrl must be set before calling requestWithGET"); - Socks5Proxy socks5Proxy = null; - if (socks5ProxyProvider != null) { - // We use the custom socks5ProxyHttp. If not set we request socks5ProxyProvider.getSocks5ProxyBtc() - // which delivers the btc proxy if set, otherwise the internal proxy. - socks5Proxy = socks5ProxyProvider.getSocks5ProxyHttp(); - if (socks5Proxy == null) - socks5Proxy = socks5ProxyProvider.getSocks5Proxy(); - } + Socks5Proxy socks5Proxy = getSocks5Proxy(socks5ProxyProvider); if (ignoreSocks5Proxy || socks5Proxy == null || baseUrl.contains("localhost")) { log.debug("Use clear net for HttpClient. socks5Proxy={}, ignoreSocks5Proxy={}, baseUrl={}", socks5Proxy, ignoreSocks5Proxy, baseUrl); @@ -122,6 +115,18 @@ public String requestWithGET(String param, } } + private Socks5Proxy getSocks5Proxy(Socks5ProxyProvider socks5ProxyProvider) { + Socks5Proxy socks5Proxy = null; + if (socks5ProxyProvider != null) { + // We use the custom socks5ProxyHttp. If not set we request socks5ProxyProvider.getSocks5ProxyBtc() + // which delivers the btc proxy if set, otherwise the internal proxy. + socks5Proxy = socks5ProxyProvider.getSocks5ProxyHttp(); + if (socks5Proxy == null) + socks5Proxy = socks5ProxyProvider.getSocks5Proxy(); + } + return socks5Proxy; + } + /** * Make an HTTP Get request directly (not routed over socks5 proxy). */ From 7f85fd9b7db3455139f90a89950b7f5f274c5250 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 14:54:28 -0500 Subject: [PATCH 05/21] Refactor getSocks5Proxy method: Return early, make flow more clear --- .../bisq/network/http/HttpClientImpl.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 04a8c7168d3..0dfbcbe0d20 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -115,16 +115,21 @@ public String requestWithGET(String param, } } + @Nullable private Socks5Proxy getSocks5Proxy(Socks5ProxyProvider socks5ProxyProvider) { - Socks5Proxy socks5Proxy = null; - if (socks5ProxyProvider != null) { - // We use the custom socks5ProxyHttp. If not set we request socks5ProxyProvider.getSocks5ProxyBtc() - // which delivers the btc proxy if set, otherwise the internal proxy. - socks5Proxy = socks5ProxyProvider.getSocks5ProxyHttp(); - if (socks5Proxy == null) - socks5Proxy = socks5ProxyProvider.getSocks5Proxy(); + if (socks5ProxyProvider == null) { + return null; + } + + // We use the custom socks5ProxyHttp. + Socks5Proxy socks5Proxy = socks5ProxyProvider.getSocks5ProxyHttp(); + if (socks5Proxy != null) { + return socks5Proxy; } - return socks5Proxy; + + // If not set we request socks5ProxyProvider.getSocks5Proxy() + // which delivers the btc proxy if set, otherwise the internal proxy. + return socks5ProxyProvider.getSocks5Proxy(); } /** From 138019ed257b62dbf4caebdfbbf6972603282e0d Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 14:54:47 -0500 Subject: [PATCH 06/21] Rearrange: move method down --- .../bisq/network/http/HttpClientImpl.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 0dfbcbe0d20..600e999a531 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -115,23 +115,6 @@ public String requestWithGET(String param, } } - @Nullable - private Socks5Proxy getSocks5Proxy(Socks5ProxyProvider socks5ProxyProvider) { - if (socks5ProxyProvider == null) { - return null; - } - - // We use the custom socks5ProxyHttp. - Socks5Proxy socks5Proxy = socks5ProxyProvider.getSocks5ProxyHttp(); - if (socks5Proxy != null) { - return socks5Proxy; - } - - // If not set we request socks5ProxyProvider.getSocks5Proxy() - // which delivers the btc proxy if set, otherwise the internal proxy. - return socks5ProxyProvider.getSocks5Proxy(); - } - /** * Make an HTTP Get request directly (not routed over socks5 proxy). */ @@ -218,6 +201,23 @@ private String doRequestWithGETProxy(String param, } } + @Nullable + private Socks5Proxy getSocks5Proxy(Socks5ProxyProvider socks5ProxyProvider) { + if (socks5ProxyProvider == null) { + return null; + } + + // We use the custom socks5ProxyHttp. + Socks5Proxy socks5Proxy = socks5ProxyProvider.getSocks5ProxyHttp(); + if (socks5Proxy != null) { + return socks5Proxy; + } + + // If not set we request socks5ProxyProvider.getSocks5Proxy() + // which delivers the btc proxy if set, otherwise the internal proxy. + return socks5ProxyProvider.getSocks5Proxy(); + } + private String convertInputStreamToString(InputStream inputStream) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); From b480f26f81141525b5d48b0a69aa1af3fa480719 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 15:00:48 -0500 Subject: [PATCH 07/21] Add HttpMethod enum Use HttpMethod as param Add getHttpUriRequest method --- .../bisq/network/http/HttpClientImpl.java | 40 +++++++++++++------ .../java/bisq/network/http/HttpMethod.java | 23 +++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 p2p/src/main/java/bisq/network/http/HttpMethod.java diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 600e999a531..ba75f71fd42 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -23,6 +23,8 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; @@ -108,24 +110,25 @@ public String requestWithGET(String param, if (ignoreSocks5Proxy || socks5Proxy == null || baseUrl.contains("localhost")) { log.debug("Use clear net for HttpClient. socks5Proxy={}, ignoreSocks5Proxy={}, baseUrl={}", socks5Proxy, ignoreSocks5Proxy, baseUrl); - return requestWithGETNoProxy(param, headerKey, headerValue); + return requestWithoutProxy(param, HttpMethod.GET, headerKey, headerValue); } else { log.debug("Use socks5Proxy for HttpClient: " + socks5Proxy); - return doRequestWithGETProxy(param, socks5Proxy, headerKey, headerValue); + return doRequestWithProxy(param, HttpMethod.GET, socks5Proxy, headerKey, headerValue); } } /** * Make an HTTP Get request directly (not routed over socks5 proxy). */ - public String requestWithGETNoProxy(String param, - @Nullable String headerKey, - @Nullable String headerValue) throws IOException { + public String requestWithoutProxy(String param, + HttpMethod httpMethod, + @Nullable String headerKey, + @Nullable String headerValue) throws IOException { log.debug("Executing HTTP request " + baseUrl + param + " proxy: none."); URL url = new URL(baseUrl + param); try { connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); + connection.setRequestMethod(httpMethod.name()); connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(120)); connection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(120)); connection.setRequestProperty("User-Agent", "bisq/" + Version.VERSION); @@ -155,10 +158,12 @@ public String requestWithGETNoProxy(String param, /** * Make an HTTP Get request routed over socks5 proxy. */ - private String doRequestWithGETProxy(String param, - Socks5Proxy socks5Proxy, - @Nullable String headerKey, - @Nullable String headerValue) throws IOException { + private String doRequestWithProxy(String param, + HttpMethod httpMethod, + Socks5Proxy socks5Proxy, + @Nullable String headerKey, + @Nullable String headerValue) throws IOException { + String uri = baseUrl + param; log.debug("requestWithGETProxy param=" + param); // This code is adapted from: // http://stackoverflow.com/a/25203021/5616248 @@ -184,7 +189,7 @@ private String doRequestWithGETProxy(String param, HttpClientContext context = HttpClientContext.create(); context.setAttribute("socks.address", socksAddress); - HttpGet request = new HttpGet(baseUrl + param); + HttpUriRequest request = getHttpUriRequest(httpMethod, uri); if (headerKey != null && headerValue != null) request.setHeader(headerKey, headerValue); @@ -193,7 +198,7 @@ private String doRequestWithGETProxy(String param, return convertInputStreamToString(response.getEntity().getContent()); } } catch (Throwable t) { - throw new IOException("Error at requestWithGETProxy with URL: " + (baseUrl + param) + ". Throwable=" + t.getMessage()); + throw new IOException("Error at requestWithGETProxy with URL: " + uri + ". Throwable=" + t.getMessage()); } finally { if (httpclient != null) { httpclient.close(); @@ -201,6 +206,17 @@ private String doRequestWithGETProxy(String param, } } + private HttpUriRequest getHttpUriRequest(HttpMethod httpMethod, String uri) { + switch (httpMethod) { + case GET: + return new HttpGet(uri); + case POST: + return new HttpPost(uri); + default: + throw new IllegalArgumentException("HttpMethod not supported: " + httpMethod); + } + } + @Nullable private Socks5Proxy getSocks5Proxy(Socks5ProxyProvider socks5ProxyProvider) { if (socks5ProxyProvider == null) { diff --git a/p2p/src/main/java/bisq/network/http/HttpMethod.java b/p2p/src/main/java/bisq/network/http/HttpMethod.java new file mode 100644 index 00000000000..b68ea538458 --- /dev/null +++ b/p2p/src/main/java/bisq/network/http/HttpMethod.java @@ -0,0 +1,23 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.network.http; + +public enum HttpMethod { + GET, + POST +} From b9d72b5185074878e71106dd9de7b7261e84b1f8 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 15:04:27 -0500 Subject: [PATCH 08/21] Add baseUrl as param, extract variable --- .../java/bisq/network/http/HttpClientImpl.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index ba75f71fd42..dcb141ca604 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -110,22 +110,24 @@ public String requestWithGET(String param, if (ignoreSocks5Proxy || socks5Proxy == null || baseUrl.contains("localhost")) { log.debug("Use clear net for HttpClient. socks5Proxy={}, ignoreSocks5Proxy={}, baseUrl={}", socks5Proxy, ignoreSocks5Proxy, baseUrl); - return requestWithoutProxy(param, HttpMethod.GET, headerKey, headerValue); + return requestWithoutProxy(baseUrl, param, HttpMethod.GET, headerKey, headerValue); } else { log.debug("Use socks5Proxy for HttpClient: " + socks5Proxy); - return doRequestWithProxy(param, HttpMethod.GET, socks5Proxy, headerKey, headerValue); + return doRequestWithProxy(baseUrl, param, HttpMethod.GET, socks5Proxy, headerKey, headerValue); } } /** * Make an HTTP Get request directly (not routed over socks5 proxy). */ - public String requestWithoutProxy(String param, + public String requestWithoutProxy(String baseUrl, + String param, HttpMethod httpMethod, @Nullable String headerKey, @Nullable String headerValue) throws IOException { log.debug("Executing HTTP request " + baseUrl + param + " proxy: none."); - URL url = new URL(baseUrl + param); + String spec = baseUrl + param; + URL url = new URL(spec); try { connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(httpMethod.name()); @@ -143,7 +145,7 @@ public String requestWithoutProxy(String param, throw new HttpException(error); } } catch (Throwable t) { - final String message = "Error at requestWithGETNoProxy with URL: " + (baseUrl + param) + ". Throwable=" + t.getMessage(); + final String message = "Error at requestWithGETNoProxy with URL: " + spec + ". Throwable=" + t.getMessage(); log.error(message); throw new IOException(message); } finally { @@ -158,7 +160,8 @@ public String requestWithoutProxy(String param, /** * Make an HTTP Get request routed over socks5 proxy. */ - private String doRequestWithProxy(String param, + private String doRequestWithProxy(String baseUrl, + String param, HttpMethod httpMethod, Socks5Proxy socks5Proxy, @Nullable String headerKey, From 5a1a9006e2d67cf1fd138bb6a399e83c26e0e2f0 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 15:13:45 -0500 Subject: [PATCH 09/21] Improve logging --- .../bisq/network/http/HttpClientImpl.java | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index dcb141ca604..40b9f5ea84b 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -20,6 +20,7 @@ import bisq.network.Socks5ProxyProvider; import bisq.common.app.Version; +import bisq.common.util.Utilities; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; @@ -105,14 +106,10 @@ public String requestWithGET(String param, @Nullable String headerKey, @Nullable String headerValue) throws IOException { checkNotNull(baseUrl, "baseUrl must be set before calling requestWithGET"); - Socks5Proxy socks5Proxy = getSocks5Proxy(socks5ProxyProvider); if (ignoreSocks5Proxy || socks5Proxy == null || baseUrl.contains("localhost")) { - log.debug("Use clear net for HttpClient. socks5Proxy={}, ignoreSocks5Proxy={}, baseUrl={}", - socks5Proxy, ignoreSocks5Proxy, baseUrl); return requestWithoutProxy(baseUrl, param, HttpMethod.GET, headerKey, headerValue); } else { - log.debug("Use socks5Proxy for HttpClient: " + socks5Proxy); return doRequestWithProxy(baseUrl, param, HttpMethod.GET, socks5Proxy, headerKey, headerValue); } } @@ -125,8 +122,9 @@ public String requestWithoutProxy(String baseUrl, HttpMethod httpMethod, @Nullable String headerKey, @Nullable String headerValue) throws IOException { - log.debug("Executing HTTP request " + baseUrl + param + " proxy: none."); + long ts = System.currentTimeMillis(); String spec = baseUrl + param; + log.info("requestWithoutProxy: URL={}, httpMethod={}", spec, httpMethod); URL url = new URL(spec); try { connection = (HttpURLConnection) url.openConnection(); @@ -134,18 +132,25 @@ public String requestWithoutProxy(String baseUrl, connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(120)); connection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(120)); connection.setRequestProperty("User-Agent", "bisq/" + Version.VERSION); - if (headerKey != null && headerValue != null) + if (headerKey != null && headerValue != null) { connection.setRequestProperty(headerKey, headerValue); + } if (connection.getResponseCode() == 200) { - return convertInputStreamToString(connection.getInputStream()); + String response = convertInputStreamToString(connection.getInputStream()); + log.info("Response for {} took {} ms. Data size:{}, response: {}", + spec, + System.currentTimeMillis() - ts, + Utilities.readableFileSize(response.getBytes().length), + Utilities.toTruncatedString(response)); + return response; } else { String error = convertInputStreamToString(connection.getErrorStream()); connection.getErrorStream().close(); throw new HttpException(error); } } catch (Throwable t) { - final String message = "Error at requestWithGETNoProxy with URL: " + spec + ". Throwable=" + t.getMessage(); + String message = "Error at requestWithoutProxy with URL: " + spec + ". Throwable=" + t.getMessage(); log.error(message); throw new IOException(message); } finally { @@ -166,8 +171,9 @@ private String doRequestWithProxy(String baseUrl, Socks5Proxy socks5Proxy, @Nullable String headerKey, @Nullable String headerValue) throws IOException { + long ts = System.currentTimeMillis(); String uri = baseUrl + param; - log.debug("requestWithGETProxy param=" + param); + log.info("requestWithoutProxy: uri={}, httpMethod={}", uri, httpMethod); // This code is adapted from: // http://stackoverflow.com/a/25203021/5616248 @@ -196,12 +202,19 @@ private String doRequestWithProxy(String baseUrl, if (headerKey != null && headerValue != null) request.setHeader(headerKey, headerValue); - log.debug("Executing request " + request + " proxy: " + socksAddress); - try (CloseableHttpResponse response = checkNotNull(httpclient).execute(request, context)) { - return convertInputStreamToString(response.getEntity().getContent()); + try (CloseableHttpResponse httpResponse = checkNotNull(httpclient).execute(request, context)) { + String response = convertInputStreamToString(httpResponse.getEntity().getContent()); + log.info("Response for {} took {} ms. Data size:{}, response: {}", + uri, + System.currentTimeMillis() - ts, + Utilities.readableFileSize(response.getBytes().length), + Utilities.toTruncatedString(response)); + return response; } } catch (Throwable t) { - throw new IOException("Error at requestWithGETProxy with URL: " + uri + ". Throwable=" + t.getMessage()); + String message = "Error at doRequestWithProxy with URL: " + uri + ". Throwable=" + t.getMessage(); + log.error(message); + throw new IOException(message); } finally { if (httpclient != null) { httpclient.close(); From 3cf97c706ad51a4c12769849c903ab70c504f6e3 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 15:14:07 -0500 Subject: [PATCH 10/21] Rename httpclient (make it more distinct to Bisq Httpclient) --- .../java/bisq/network/http/HttpClientImpl.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 40b9f5ea84b..e16830b97dc 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -67,7 +67,7 @@ public class HttpClientImpl implements HttpClient { @Nullable private HttpURLConnection connection; @Nullable - private CloseableHttpClient httpclient; + private CloseableHttpClient closeableHttpClient; @Getter @Setter @@ -93,9 +93,9 @@ public void shutDown() { if (connection != null) { connection.disconnect(); } - if (httpclient != null) { + if (closeableHttpClient != null) { try { - httpclient.close(); + closeableHttpClient.close(); } catch (IOException ignore) { } } @@ -189,7 +189,7 @@ private String doRequestWithProxy(String baseUrl, new PoolingHttpClientConnectionManager(reg) : new PoolingHttpClientConnectionManager(reg, new FakeDnsResolver()); try { - httpclient = HttpClients.custom().setConnectionManager(cm).build(); + closeableHttpClient = HttpClients.custom().setConnectionManager(cm).build(); InetSocketAddress socksAddress = new InetSocketAddress(socks5Proxy.getInetAddress(), socks5Proxy.getPort()); // remove me: Use this to test with system-wide Tor proxy, or change port for another proxy. @@ -202,7 +202,7 @@ private String doRequestWithProxy(String baseUrl, if (headerKey != null && headerValue != null) request.setHeader(headerKey, headerValue); - try (CloseableHttpResponse httpResponse = checkNotNull(httpclient).execute(request, context)) { + try (CloseableHttpResponse httpResponse = checkNotNull(closeableHttpClient).execute(request, context)) { String response = convertInputStreamToString(httpResponse.getEntity().getContent()); log.info("Response for {} took {} ms. Data size:{}, response: {}", uri, @@ -216,8 +216,8 @@ private String doRequestWithProxy(String baseUrl, log.error(message); throw new IOException(message); } finally { - if (httpclient != null) { - httpclient.close(); + if (closeableHttpClient != null) { + closeableHttpClient.close(); } } } @@ -268,7 +268,7 @@ public String toString() { ",\n ignoreSocks5Proxy=" + ignoreSocks5Proxy + ",\n uid='" + uid + '\'' + ",\n connection=" + connection + - ",\n httpclient=" + httpclient + + ",\n httpclient=" + closeableHttpClient + "\n}"; } } From de2ba82b3d205c4ce1fb31fd0439dee67e8c0b95 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 15:20:26 -0500 Subject: [PATCH 11/21] Add post method. Add doRequest method --- .../java/bisq/network/http/HttpClient.java | 4 +++ .../bisq/network/http/HttpClientImpl.java | 36 +++++++++++-------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClient.java b/p2p/src/main/java/bisq/network/http/HttpClient.java index 4461da052e9..b1f15ea5ed8 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClient.java +++ b/p2p/src/main/java/bisq/network/http/HttpClient.java @@ -30,6 +30,10 @@ String requestWithGET(String param, @Nullable String headerKey, @Nullable String headerValue) throws IOException; + String post(String param, + @Nullable String headerKey, + @Nullable String headerValue) throws IOException; + String getUid(); String getBaseUrl(); diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index e16830b97dc..3d8f22718f6 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -105,23 +105,34 @@ public void shutDown() { public String requestWithGET(String param, @Nullable String headerKey, @Nullable String headerValue) throws IOException { - checkNotNull(baseUrl, "baseUrl must be set before calling requestWithGET"); + return doRequest(param, HttpMethod.GET, headerKey, headerValue); + } + + @Override + public String post(String param, + @Nullable String headerKey, + @Nullable String headerValue) throws IOException { + return doRequest(param, HttpMethod.POST, headerKey, headerValue); + } + + private String doRequest(String param, + HttpMethod httpMethod, + @Nullable String headerKey, + @Nullable String headerValue) throws IOException { + checkNotNull(baseUrl, "baseUrl must be set before calling post"); Socks5Proxy socks5Proxy = getSocks5Proxy(socks5ProxyProvider); if (ignoreSocks5Proxy || socks5Proxy == null || baseUrl.contains("localhost")) { - return requestWithoutProxy(baseUrl, param, HttpMethod.GET, headerKey, headerValue); + return requestWithoutProxy(baseUrl, param, httpMethod, headerKey, headerValue); } else { - return doRequestWithProxy(baseUrl, param, HttpMethod.GET, socks5Proxy, headerKey, headerValue); + return doRequestWithProxy(baseUrl, param, httpMethod, socks5Proxy, headerKey, headerValue); } } - /** - * Make an HTTP Get request directly (not routed over socks5 proxy). - */ - public String requestWithoutProxy(String baseUrl, - String param, - HttpMethod httpMethod, - @Nullable String headerKey, - @Nullable String headerValue) throws IOException { + private String requestWithoutProxy(String baseUrl, + String param, + HttpMethod httpMethod, + @Nullable String headerKey, + @Nullable String headerValue) throws IOException { long ts = System.currentTimeMillis(); String spec = baseUrl + param; log.info("requestWithoutProxy: URL={}, httpMethod={}", spec, httpMethod); @@ -162,9 +173,6 @@ public String requestWithoutProxy(String baseUrl, } } - /** - * Make an HTTP Get request routed over socks5 proxy. - */ private String doRequestWithProxy(String baseUrl, String param, HttpMethod httpMethod, From 9496691f7620eecef6d99901e76a1942a6b881a8 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 15:23:36 -0500 Subject: [PATCH 12/21] Refactor: Rename 'requestWithGET' to 'get' --- .../bisq/core/notifications/MobileNotificationService.java | 2 +- core/src/main/java/bisq/core/provider/fee/FeeProvider.java | 2 +- .../main/java/bisq/core/provider/price/PriceProvider.java | 2 +- .../java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java | 2 +- p2p/src/main/java/bisq/network/http/HttpClient.java | 6 +++--- p2p/src/main/java/bisq/network/http/HttpClientImpl.java | 6 +++--- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/bisq/core/notifications/MobileNotificationService.java b/core/src/main/java/bisq/core/notifications/MobileNotificationService.java index 1d924316c0e..3f87173e335 100644 --- a/core/src/main/java/bisq/core/notifications/MobileNotificationService.java +++ b/core/src/main/java/bisq/core/notifications/MobileNotificationService.java @@ -297,7 +297,7 @@ private void doSendMessage(String iv, String threadName = "sendMobileNotification-" + msgAsHex.substring(0, 5) + "..."; ListenableFuture future = executorService.submit(() -> { Thread.currentThread().setName(threadName); - String result = httpClient.requestWithGET(param, "User-Agent", + String result = httpClient.get(param, "User-Agent", "bisq/" + Version.VERSION + ", uid:" + httpClient.getUid()); log.info("sendMobileNotification result: " + result); checkArgument(result.equals(SUCCESS), "Result was not 'success'. result=" + result); diff --git a/core/src/main/java/bisq/core/provider/fee/FeeProvider.java b/core/src/main/java/bisq/core/provider/fee/FeeProvider.java index 3fe55c7f74e..39da08fc343 100644 --- a/core/src/main/java/bisq/core/provider/fee/FeeProvider.java +++ b/core/src/main/java/bisq/core/provider/fee/FeeProvider.java @@ -45,7 +45,7 @@ public FeeProvider(PriceNodeHttpClient httpClient, ProvidersRepository providers } public Tuple2, Map> getFees() throws IOException { - String json = httpClient.requestWithGET("getFees", "User-Agent", "bisq/" + Version.VERSION); + String json = httpClient.get("getFees", "User-Agent", "bisq/" + Version.VERSION); LinkedTreeMap linkedTreeMap = new Gson().fromJson(json, LinkedTreeMap.class); Map tsMap = new HashMap<>(); diff --git a/core/src/main/java/bisq/core/provider/price/PriceProvider.java b/core/src/main/java/bisq/core/provider/price/PriceProvider.java index 3d733fa145e..3a69bb0f61b 100644 --- a/core/src/main/java/bisq/core/provider/price/PriceProvider.java +++ b/core/src/main/java/bisq/core/provider/price/PriceProvider.java @@ -58,7 +58,7 @@ public Tuple2, Map> getAll() throws IOExc if (P2PService.getMyNodeAddress() != null) hsVersion = P2PService.getMyNodeAddress().getHostName().length() > 22 ? ", HSv3" : ", HSv2"; - String json = httpClient.requestWithGET("getAllMarketPrices", "User-Agent", "bisq/" + String json = httpClient.get("getAllMarketPrices", "User-Agent", "bisq/" + Version.VERSION + hsVersion); diff --git a/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java b/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java index 438d19121b3..a7798990ab9 100644 --- a/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java +++ b/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java @@ -206,7 +206,7 @@ public void requestFromService(Consumer resultHandler, FaultHandler faul "&viewkey=" + model.getTxKey() + "&txprove=1"; log.info("Param {} for {}", param, this); - String json = httpClient.requestWithGET(param, "User-Agent", "bisq/" + Version.VERSION); + String json = httpClient.get(param, "User-Agent", "bisq/" + Version.VERSION); try { String prettyJson = new GsonBuilder().setPrettyPrinting().create().toJson(new JsonParser().parse(json)); log.info("Response json from {}\n{}", this, prettyJson); diff --git a/p2p/src/main/java/bisq/network/http/HttpClient.java b/p2p/src/main/java/bisq/network/http/HttpClient.java index b1f15ea5ed8..01bd8ff70ee 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClient.java +++ b/p2p/src/main/java/bisq/network/http/HttpClient.java @@ -26,9 +26,9 @@ public interface HttpClient { void setIgnoreSocks5Proxy(boolean ignoreSocks5Proxy); - String requestWithGET(String param, - @Nullable String headerKey, - @Nullable String headerValue) throws IOException; + String get(String param, + @Nullable String headerKey, + @Nullable String headerValue) throws IOException; String post(String param, @Nullable String headerKey, diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 3d8f22718f6..9abd3b4a3fc 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -102,9 +102,9 @@ public void shutDown() { } @Override - public String requestWithGET(String param, - @Nullable String headerKey, - @Nullable String headerValue) throws IOException { + public String get(String param, + @Nullable String headerKey, + @Nullable String headerValue) throws IOException { return doRequest(param, HttpMethod.GET, headerKey, headerValue); } From d61a07f6bbbb308ab1886ba60949a009692f9f62 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 17:03:45 -0500 Subject: [PATCH 13/21] - Add hasPendingRequest() getter and block repeated requests while hasPendingRequest is true - Throw exception if we get a request before previous request is terminated (happens with priceFee at startup, on regtest as startup is fast, but can happen also on mainnet) - Improve shutDown - Improve finally clause --- .../java/bisq/network/http/HttpClient.java | 2 ++ .../bisq/network/http/HttpClientImpl.java | 35 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClient.java b/p2p/src/main/java/bisq/network/http/HttpClient.java index 01bd8ff70ee..bb507c0e677 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClient.java +++ b/p2p/src/main/java/bisq/network/http/HttpClient.java @@ -38,5 +38,7 @@ String post(String param, String getBaseUrl(); + boolean hasPendingRequest(); + void shutDown(); } diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index 9abd3b4a3fc..a234e669921 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -57,6 +57,7 @@ import javax.annotation.Nullable; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; // TODO close connection if failing @@ -76,6 +77,7 @@ public class HttpClientImpl implements HttpClient { private boolean ignoreSocks5Proxy; @Getter private final String uid; + private boolean hasPendingRequest; @Inject public HttpClientImpl(@Nullable Socks5ProxyProvider socks5ProxyProvider) { @@ -90,17 +92,23 @@ public HttpClientImpl(String baseUrl) { @Override public void shutDown() { - if (connection != null) { - connection.disconnect(); - } - if (closeableHttpClient != null) { - try { + try { + if (connection != null) { + connection.getInputStream().close(); + connection.disconnect(); + } + if (closeableHttpClient != null) { closeableHttpClient.close(); - } catch (IOException ignore) { } + } catch (IOException ignore) { } } + @Override + public boolean hasPendingRequest() { + return hasPendingRequest; + } + @Override public String get(String param, @Nullable String headerKey, @@ -119,7 +127,10 @@ private String doRequest(String param, HttpMethod httpMethod, @Nullable String headerKey, @Nullable String headerValue) throws IOException { - checkNotNull(baseUrl, "baseUrl must be set before calling post"); + checkNotNull(baseUrl, "baseUrl must be set before calling doRequest"); + checkArgument(hasPendingRequest, "We got called on the same HttpClient again while a request is still open."); + + hasPendingRequest = true; Socks5Proxy socks5Proxy = getSocks5Proxy(socks5ProxyProvider); if (ignoreSocks5Proxy || socks5Proxy == null || baseUrl.contains("localhost")) { return requestWithoutProxy(baseUrl, param, httpMethod, headerKey, headerValue); @@ -136,8 +147,8 @@ private String requestWithoutProxy(String baseUrl, long ts = System.currentTimeMillis(); String spec = baseUrl + param; log.info("requestWithoutProxy: URL={}, httpMethod={}", spec, httpMethod); - URL url = new URL(spec); try { + URL url = new URL(spec); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(httpMethod.name()); connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(120)); @@ -166,10 +177,14 @@ private String requestWithoutProxy(String baseUrl, throw new IOException(message); } finally { try { - if (connection != null) + if (connection != null) { connection.getInputStream().close(); + connection.disconnect(); + connection = null; + } } catch (Throwable ignore) { } + hasPendingRequest = false; } } @@ -226,7 +241,9 @@ private String doRequestWithProxy(String baseUrl, } finally { if (closeableHttpClient != null) { closeableHttpClient.close(); + closeableHttpClient = null; } + hasPendingRequest = false; } } From 561639f046c61ee2409d2d0b6795c36f778b2252 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 17:04:24 -0500 Subject: [PATCH 14/21] Check if we have a pending request and return if thats the case --- .../bisq/core/notifications/MobileNotificationService.java | 5 +++++ core/src/main/java/bisq/core/provider/fee/FeeProvider.java | 6 ++++++ core/src/main/java/bisq/core/provider/fee/FeeService.java | 5 +++++ .../java/bisq/core/provider/price/PriceFeedService.java | 5 +++++ .../java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java | 5 +++++ 5 files changed, 26 insertions(+) diff --git a/core/src/main/java/bisq/core/notifications/MobileNotificationService.java b/core/src/main/java/bisq/core/notifications/MobileNotificationService.java index 3f87173e335..6dbcb19ab42 100644 --- a/core/src/main/java/bisq/core/notifications/MobileNotificationService.java +++ b/core/src/main/java/bisq/core/notifications/MobileNotificationService.java @@ -256,6 +256,11 @@ private void doSendMessage(String iv, boolean useSound, Consumer resultHandler, Consumer errorHandler) throws Exception { + if (httpClient.hasPendingRequest()) { + log.warn("We have a pending request open on httpClient {}", httpClient); + return; + } + String msg; if (mobileModel.getOs() == null) throw new RuntimeException("No mobileModel OS set"); diff --git a/core/src/main/java/bisq/core/provider/fee/FeeProvider.java b/core/src/main/java/bisq/core/provider/fee/FeeProvider.java index 39da08fc343..a4e5532da60 100644 --- a/core/src/main/java/bisq/core/provider/fee/FeeProvider.java +++ b/core/src/main/java/bisq/core/provider/fee/FeeProvider.java @@ -21,6 +21,8 @@ import bisq.core.provider.PriceNodeHttpClient; import bisq.core.provider.ProvidersRepository; +import bisq.network.http.HttpClient; + import bisq.common.app.Version; import bisq.common.util.Tuple2; @@ -64,4 +66,8 @@ public Tuple2, Map> getFees() throws IOException } return new Tuple2<>(tsMap, map); } + + public HttpClient getHttpClient() { + return httpClient; + } } diff --git a/core/src/main/java/bisq/core/provider/fee/FeeService.java b/core/src/main/java/bisq/core/provider/fee/FeeService.java index 66723b904c3..113293b1027 100644 --- a/core/src/main/java/bisq/core/provider/fee/FeeService.java +++ b/core/src/main/java/bisq/core/provider/fee/FeeService.java @@ -137,6 +137,11 @@ public void requestFees(Runnable resultHandler) { } public void requestFees(@Nullable Runnable resultHandler, @Nullable FaultHandler faultHandler) { + if (feeProvider.getHttpClient().hasPendingRequest()) { + log.warn("We have a pending request open on httpClient {}", feeProvider.getHttpClient()); + return; + } + long now = Instant.now().getEpochSecond(); // We all requests only each 2 minutes if (now - lastRequest > MIN_PAUSE_BETWEEN_REQUESTS_IN_MIN * 60) { diff --git a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java index 24a50430f92..5b970e13b51 100644 --- a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java +++ b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java @@ -393,6 +393,11 @@ private boolean applyPriceToConsumer() { } private void requestAllPrices(PriceProvider provider, Runnable resultHandler, FaultHandler faultHandler) { + if (httpClient.hasPendingRequest()) { + log.warn("We have a pending request open on httpClient {}", httpClient); + return; + } + priceRequest = new PriceRequest(); SettableFuture, Map>> future = priceRequest.requestAllPrices(provider); Futures.addCallback(future, new FutureCallback<>() { diff --git a/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java b/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java index a7798990ab9..02cd6609464 100644 --- a/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java +++ b/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java @@ -197,6 +197,11 @@ public void requestFromService(Consumer resultHandler, FaultHandler faul return; } + if (httpClient.hasPendingRequest()) { + log.warn("We have a pending request open on httpClient {}", httpClient); + return; + } + // Timeout handing is delegated to the connection timeout handling in httpClient. ListenableFuture future = executorService.submit(() -> { From 5be2ea58305b2d3677d6623312b3f0f43fab60e0 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 17:17:52 -0500 Subject: [PATCH 15/21] Use dedicated HttpClient for fee requests We used the same HttpClient for fee and price requests, which cause problems when one request got completed and closed the connection at finalize. --- .../bisq/core/provider/FeeHttpClient.java | 34 +++++++++++++++++++ .../bisq/core/provider/fee/FeeProvider.java | 4 +-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/bisq/core/provider/FeeHttpClient.java diff --git a/core/src/main/java/bisq/core/provider/FeeHttpClient.java b/core/src/main/java/bisq/core/provider/FeeHttpClient.java new file mode 100644 index 00000000000..2579c6ffb2a --- /dev/null +++ b/core/src/main/java/bisq/core/provider/FeeHttpClient.java @@ -0,0 +1,34 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.core.provider; + +import bisq.network.Socks5ProxyProvider; +import bisq.network.http.HttpClientImpl; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import javax.annotation.Nullable; + +@Singleton +public class FeeHttpClient extends HttpClientImpl { + @Inject + public FeeHttpClient(@Nullable Socks5ProxyProvider socks5ProxyProvider) { + super(socks5ProxyProvider); + } +} diff --git a/core/src/main/java/bisq/core/provider/fee/FeeProvider.java b/core/src/main/java/bisq/core/provider/fee/FeeProvider.java index a4e5532da60..3a1e4f4f807 100644 --- a/core/src/main/java/bisq/core/provider/fee/FeeProvider.java +++ b/core/src/main/java/bisq/core/provider/fee/FeeProvider.java @@ -17,8 +17,8 @@ package bisq.core.provider.fee; +import bisq.core.provider.FeeHttpClient; import bisq.core.provider.HttpClientProvider; -import bisq.core.provider.PriceNodeHttpClient; import bisq.core.provider.ProvidersRepository; import bisq.network.http.HttpClient; @@ -42,7 +42,7 @@ public class FeeProvider extends HttpClientProvider { @Inject - public FeeProvider(PriceNodeHttpClient httpClient, ProvidersRepository providersRepository) { + public FeeProvider(FeeHttpClient httpClient, ProvidersRepository providersRepository) { super(httpClient, providersRepository.getBaseUrl(), false); } From 3b0ad356b4374a5124a623861c77f074fd876596 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 17:18:07 -0500 Subject: [PATCH 16/21] Fix incorrect checkArgument --- p2p/src/main/java/bisq/network/http/HttpClientImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java index a234e669921..3f5a6ee4299 100644 --- a/p2p/src/main/java/bisq/network/http/HttpClientImpl.java +++ b/p2p/src/main/java/bisq/network/http/HttpClientImpl.java @@ -128,7 +128,7 @@ private String doRequest(String param, @Nullable String headerKey, @Nullable String headerValue) throws IOException { checkNotNull(baseUrl, "baseUrl must be set before calling doRequest"); - checkArgument(hasPendingRequest, "We got called on the same HttpClient again while a request is still open."); + checkArgument(!hasPendingRequest, "We got called on the same HttpClient again while a request is still open."); hasPendingRequest = true; Socks5Proxy socks5Proxy = getSocks5Proxy(socks5ProxyProvider); From 5d664e915e817d6cb6ada60a02af7211b5a5f498 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 17:18:36 -0500 Subject: [PATCH 17/21] Improve logs --- .../bisq/core/provider/price/PriceFeedService.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java index 5b970e13b51..a8e11e50d63 100644 --- a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java +++ b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java @@ -101,7 +101,7 @@ public class PriceFeedService { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public PriceFeedService(@SuppressWarnings("SameParameterValue") PriceNodeHttpClient httpClient, + public PriceFeedService(PriceNodeHttpClient httpClient, @SuppressWarnings("SameParameterValue") ProvidersRepository providersRepository, @SuppressWarnings("SameParameterValue") Preferences preferences) { this.httpClient = httpClient; @@ -194,9 +194,9 @@ private void request(boolean repeatRequests) { if (throwable instanceof PriceRequestException) { String baseUrlOfFaultyRequest = ((PriceRequestException) throwable).priceProviderBaseUrl; String baseUrlOfCurrentRequest = priceProvider.getBaseUrl(); - if (baseUrlOfFaultyRequest != null && baseUrlOfCurrentRequest.equals(baseUrlOfFaultyRequest)) { - log.warn("We received an error: baseUrlOfCurrentRequest={}, baseUrlOfFaultyRequest={}", - baseUrlOfCurrentRequest, baseUrlOfFaultyRequest); + if (baseUrlOfCurrentRequest.equals(baseUrlOfFaultyRequest)) { + log.warn("We received an error: baseUrlOfCurrentRequest={}, baseUrlOfFaultyRequest={}, error={}", + baseUrlOfCurrentRequest, baseUrlOfFaultyRequest, throwable.toString()); retryWithNewProvider(); } else { log.debug("We received an error from an earlier request. We have started a new request already so we ignore that error. " + @@ -204,7 +204,7 @@ private void request(boolean repeatRequests) { baseUrlOfCurrentRequest, baseUrlOfFaultyRequest); } } else { - log.warn("We received an error with throwable={}", throwable); + log.warn("We received an error with throwable={}", throwable.toString()); retryWithNewProvider(); } From 3d8d44599459da71b1f6469fc88986eeac3fc3f2 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 17:19:02 -0500 Subject: [PATCH 18/21] Use @Singleton annotation instead of definition in module --- core/src/main/java/bisq/core/btc/BitcoinModule.java | 3 --- core/src/main/java/bisq/core/provider/PriceNodeHttpClient.java | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/bisq/core/btc/BitcoinModule.java b/core/src/main/java/bisq/core/btc/BitcoinModule.java index 053f61c32d7..3f733fb570a 100644 --- a/core/src/main/java/bisq/core/btc/BitcoinModule.java +++ b/core/src/main/java/bisq/core/btc/BitcoinModule.java @@ -26,7 +26,6 @@ import bisq.core.btc.wallet.BtcWalletService; import bisq.core.btc.wallet.NonBsqCoinSelector; import bisq.core.btc.wallet.TradeWalletService; -import bisq.core.provider.PriceNodeHttpClient; import bisq.core.provider.ProvidersRepository; import bisq.core.provider.fee.FeeProvider; import bisq.core.provider.fee.FeeService; @@ -95,8 +94,6 @@ protected void configure() { bind(BtcNodes.class).in(Singleton.class); bind(Balances.class).in(Singleton.class); - bind(PriceNodeHttpClient.class).in(Singleton.class); - bind(ProvidersRepository.class).in(Singleton.class); bind(FeeProvider.class).in(Singleton.class); bind(PriceFeedService.class).in(Singleton.class); diff --git a/core/src/main/java/bisq/core/provider/PriceNodeHttpClient.java b/core/src/main/java/bisq/core/provider/PriceNodeHttpClient.java index fbe396f1d8a..8464e1d22ed 100644 --- a/core/src/main/java/bisq/core/provider/PriceNodeHttpClient.java +++ b/core/src/main/java/bisq/core/provider/PriceNodeHttpClient.java @@ -21,9 +21,11 @@ import bisq.network.http.HttpClientImpl; import javax.inject.Inject; +import javax.inject.Singleton; import javax.annotation.Nullable; +@Singleton public class PriceNodeHttpClient extends HttpClientImpl { @Inject public PriceNodeHttpClient(@Nullable Socks5ProxyProvider socks5ProxyProvider) { From f7790a6b50b19b65ffb21b95e0189b645d959d36 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 17:19:39 -0500 Subject: [PATCH 19/21] Rename PriceNodeHttpClient to PriceHttpClient --- .../{PriceNodeHttpClient.java => PriceHttpClient.java} | 4 ++-- .../main/java/bisq/core/provider/price/PriceFeedService.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename core/src/main/java/bisq/core/provider/{PriceNodeHttpClient.java => PriceHttpClient.java} (87%) diff --git a/core/src/main/java/bisq/core/provider/PriceNodeHttpClient.java b/core/src/main/java/bisq/core/provider/PriceHttpClient.java similarity index 87% rename from core/src/main/java/bisq/core/provider/PriceNodeHttpClient.java rename to core/src/main/java/bisq/core/provider/PriceHttpClient.java index 8464e1d22ed..1f0b47d8e8f 100644 --- a/core/src/main/java/bisq/core/provider/PriceNodeHttpClient.java +++ b/core/src/main/java/bisq/core/provider/PriceHttpClient.java @@ -26,9 +26,9 @@ import javax.annotation.Nullable; @Singleton -public class PriceNodeHttpClient extends HttpClientImpl { +public class PriceHttpClient extends HttpClientImpl { @Inject - public PriceNodeHttpClient(@Nullable Socks5ProxyProvider socks5ProxyProvider) { + public PriceHttpClient(@Nullable Socks5ProxyProvider socks5ProxyProvider) { super(socks5ProxyProvider); } } diff --git a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java index a8e11e50d63..0c868f159b1 100644 --- a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java +++ b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java @@ -21,7 +21,7 @@ import bisq.core.locale.TradeCurrency; import bisq.core.monetary.Altcoin; import bisq.core.monetary.Price; -import bisq.core.provider.PriceNodeHttpClient; +import bisq.core.provider.PriceHttpClient; import bisq.core.provider.ProvidersRepository; import bisq.core.trade.statistics.TradeStatistics3; import bisq.core.user.Preferences; @@ -101,7 +101,7 @@ public class PriceFeedService { /////////////////////////////////////////////////////////////////////////////////////////// @Inject - public PriceFeedService(PriceNodeHttpClient httpClient, + public PriceFeedService(PriceHttpClient httpClient, @SuppressWarnings("SameParameterValue") ProvidersRepository providersRepository, @SuppressWarnings("SameParameterValue") Preferences preferences) { this.httpClient = httpClient; From 06ce1b6787f1d634ad002ec8b098f0e623cfbcb8 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 17:25:49 -0500 Subject: [PATCH 20/21] Improve thread name --- core/src/main/java/bisq/core/provider/fee/FeeRequest.java | 2 +- core/src/main/java/bisq/core/provider/price/PriceRequest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/bisq/core/provider/fee/FeeRequest.java b/core/src/main/java/bisq/core/provider/fee/FeeRequest.java index 5dcb7e2ae25..007a5d39594 100644 --- a/core/src/main/java/bisq/core/provider/fee/FeeRequest.java +++ b/core/src/main/java/bisq/core/provider/fee/FeeRequest.java @@ -45,7 +45,7 @@ public FeeRequest() { public SettableFuture, Map>> getFees(FeeProvider provider) { final SettableFuture, Map>> resultFuture = SettableFuture.create(); ListenableFuture, Map>> future = executorService.submit(() -> { - Thread.currentThread().setName("FeeRequest-" + provider.toString()); + Thread.currentThread().setName("FeeRequest @ " + provider.getHttpClient().getBaseUrl()); return provider.getFees(); }); diff --git a/core/src/main/java/bisq/core/provider/price/PriceRequest.java b/core/src/main/java/bisq/core/provider/price/PriceRequest.java index 80f276e796c..8d5025d3c7c 100644 --- a/core/src/main/java/bisq/core/provider/price/PriceRequest.java +++ b/core/src/main/java/bisq/core/provider/price/PriceRequest.java @@ -50,7 +50,7 @@ public SettableFuture, Map>> reque String baseUrl = provider.getBaseUrl(); SettableFuture, Map>> resultFuture = SettableFuture.create(); ListenableFuture, Map>> future = executorService.submit(() -> { - Thread.currentThread().setName("PriceRequest-" + baseUrl); + Thread.currentThread().setName("PriceRequest @ " + baseUrl); return provider.getAll(); }); From ad2a329e089ceaaf91e2bfee3e70a41941a3245a Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Sun, 13 Dec 2020 17:26:09 -0500 Subject: [PATCH 21/21] Improve log --- .../java/bisq/core/notifications/MobileNotificationService.java | 2 +- core/src/main/java/bisq/core/provider/fee/FeeService.java | 2 +- .../main/java/bisq/core/provider/price/PriceFeedService.java | 2 +- .../java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/bisq/core/notifications/MobileNotificationService.java b/core/src/main/java/bisq/core/notifications/MobileNotificationService.java index 6dbcb19ab42..76ba9313df7 100644 --- a/core/src/main/java/bisq/core/notifications/MobileNotificationService.java +++ b/core/src/main/java/bisq/core/notifications/MobileNotificationService.java @@ -257,7 +257,7 @@ private void doSendMessage(String iv, Consumer resultHandler, Consumer errorHandler) throws Exception { if (httpClient.hasPendingRequest()) { - log.warn("We have a pending request open on httpClient {}", httpClient); + log.warn("We have a pending request open. We ignore that request. httpClient {}", httpClient); return; } diff --git a/core/src/main/java/bisq/core/provider/fee/FeeService.java b/core/src/main/java/bisq/core/provider/fee/FeeService.java index 113293b1027..46ba82e4f62 100644 --- a/core/src/main/java/bisq/core/provider/fee/FeeService.java +++ b/core/src/main/java/bisq/core/provider/fee/FeeService.java @@ -138,7 +138,7 @@ public void requestFees(Runnable resultHandler) { public void requestFees(@Nullable Runnable resultHandler, @Nullable FaultHandler faultHandler) { if (feeProvider.getHttpClient().hasPendingRequest()) { - log.warn("We have a pending request open on httpClient {}", feeProvider.getHttpClient()); + log.warn("We have a pending request open. We ignore that request. httpClient {}", feeProvider.getHttpClient()); return; } diff --git a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java index 0c868f159b1..57e9a5197dc 100644 --- a/core/src/main/java/bisq/core/provider/price/PriceFeedService.java +++ b/core/src/main/java/bisq/core/provider/price/PriceFeedService.java @@ -394,7 +394,7 @@ private boolean applyPriceToConsumer() { private void requestAllPrices(PriceProvider provider, Runnable resultHandler, FaultHandler faultHandler) { if (httpClient.hasPendingRequest()) { - log.warn("We have a pending request open on httpClient {}", httpClient); + log.warn("We have a pending request open. We ignore that request. httpClient {}", httpClient); return; } diff --git a/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java b/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java index 02cd6609464..5e4e21dd472 100644 --- a/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java +++ b/core/src/main/java/bisq/core/trade/txproof/xmr/XmrTxProofRequest.java @@ -198,7 +198,7 @@ public void requestFromService(Consumer resultHandler, FaultHandler faul } if (httpClient.hasPendingRequest()) { - log.warn("We have a pending request open on httpClient {}", httpClient); + log.warn("We have a pending request open. We ignore that request. httpClient {}", httpClient); return; }