Refactored Onion address parsing

This commit is contained in:
Florian Reimair 2019-01-28 13:19:03 +01:00
parent 2c3fcc6229
commit 72075063fb
5 changed files with 76 additions and 25 deletions

View file

@ -0,0 +1,47 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package bisq.monitor;
import java.net.MalformedURLException;
import java.net.URL;
import bisq.network.p2p.NodeAddress;
/**
* Helper for parsing and pretty printing onion addresses.
*
* @author Florian Reimair
*/
public class OnionParser {
public static NodeAddress getNodeAddress(final String current) throws MalformedURLException {
String nodeAddress = current.trim();
if (!nodeAddress.startsWith("http://"))
nodeAddress = "http://" + nodeAddress;
URL tmp = new URL(nodeAddress);
return new NodeAddress(tmp.getHost(), tmp.getPort());
}
public static String prettyPrint(final NodeAddress host) {
return host.getHostNameWithoutPostFix();
}
public static String prettyPrint(String host) throws MalformedURLException {
return prettyPrint(getNodeAddress(host));
}
}

View file

@ -18,7 +18,7 @@
package bisq.monitor.metric;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@ -42,6 +42,7 @@ import bisq.core.proto.network.CoreNetworkProtoResolver;
import bisq.monitor.AvailableTor;
import bisq.monitor.Metric;
import bisq.monitor.Monitor;
import bisq.monitor.OnionParser;
import bisq.monitor.Reporter;
import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.network.Connection;
@ -75,7 +76,7 @@ public class P2PNetworkLoad extends Metric implements MessageListener, SetupList
private final File torHiddenServiceDir = new File("metric_p2pNetworkLoad");
private int nonce;
private Boolean ready = false;
private Map<String, Map<String, Counter>> bucketsPerHost = new ConcurrentHashMap<>();
private Map<NodeAddress, Map<String, Counter>> bucketsPerHost = new ConcurrentHashMap<>();
private CountDownLatch latch;
private Set<byte[]> hashes = new HashSet<>();
private boolean reportFindings;
@ -151,8 +152,7 @@ public class P2PNetworkLoad extends Metric implements MessageListener, SetupList
public void run() {
try {
// parse Url
URL tmp = new URL(current);
NodeAddress target = new NodeAddress(tmp.getHost(), tmp.getPort());
NodeAddress target = OnionParser.getNodeAddress(current);
// do the data request
nonce = new Random().nextInt();
@ -198,22 +198,27 @@ public class P2PNetworkLoad extends Metric implements MessageListener, SetupList
// report
Map<String, String> report = new HashMap<>();
// - assemble histograms
bucketsPerHost.forEach((host, buckets) -> buckets.forEach((type, counter) -> report.put(host.replace("http://", "").trim() + "." + type,
String.valueOf(counter.value()))));
bucketsPerHost.forEach((host, buckets) -> buckets.forEach((type, counter) -> report
.put(OnionParser.prettyPrint(host) + "." + type, String.valueOf(counter.value()))));
// - assemble diffs
Map<String, Integer> messagesPerHost = new HashMap<>();
bucketsPerHost.forEach((host, buckets) -> messagesPerHost.put(host,
bucketsPerHost.forEach((host, buckets) -> messagesPerHost.put(OnionParser.prettyPrint(host),
buckets.values().stream().collect(Collectors.summingInt(Counter::value))));
Optional<String> referenceHost = messagesPerHost.keySet().stream().sorted().findFirst();
Integer referenceValue = messagesPerHost.get(referenceHost.get());
messagesPerHost.forEach(
(host, numberOfMessages) -> {
report.put(host.replace("http://", "").trim() + ".relativeNumberOfMessages",
String.valueOf(numberOfMessages - referenceValue));
report.put(host.replace("http://", "").trim() + ".referenceHost", referenceHost.get());
report.put(host.replace("http://", "").trim() + ".referenceValue", String.valueOf(referenceValue));
try {
report.put(OnionParser.prettyPrint(host) + ".relativeNumberOfMessages",
String.valueOf(numberOfMessages - referenceValue));
report.put(OnionParser.prettyPrint(host) + ".referenceHost", referenceHost.get());
report.put(OnionParser.prettyPrint(host) + ".referenceValue", String.valueOf(referenceValue));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
// when our hash cache exceeds a hard limit, we clear the cache and start anew
@ -270,7 +275,7 @@ public class P2PNetworkLoad extends Metric implements MessageListener, SetupList
});
}
bucketsPerHost.put(connection.peersNodeAddressProperty().getValue().getFullAddress(), buckets);
bucketsPerHost.put(connection.peersNodeAddressProperty().getValue(), buckets);
connection.removeMessageListener(this);
latch.countDown();

View file

@ -18,7 +18,6 @@
package bisq.monitor.metric;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
@ -35,6 +34,7 @@ import bisq.core.proto.network.CoreNetworkProtoResolver;
import bisq.monitor.AvailableTor;
import bisq.monitor.Metric;
import bisq.monitor.Monitor;
import bisq.monitor.OnionParser;
import bisq.monitor.Reporter;
import bisq.monitor.StatisticsHelper;
import bisq.network.p2p.NodeAddress;
@ -120,8 +120,7 @@ public class P2PRoundTripTime extends Metric implements MessageListener, SetupLi
for (String current : configuration.getProperty(HOSTS, "").split(",")) {
try {
// parse Url
URL tmp = new URL(current);
NodeAddress target = new NodeAddress(tmp.getHost(), tmp.getPort());
NodeAddress target = OnionParser.getNodeAddress(current);
// init sample bucket
samples = new ArrayList<>();
@ -149,7 +148,8 @@ public class P2PRoundTripTime extends Metric implements MessageListener, SetupLi
}
// report
reporter.report(StatisticsHelper.process(samples), "bisq." + getName() + "." + target);
reporter.report(StatisticsHelper.process(samples),
"bisq." + getName() + "." + OnionParser.prettyPrint(target));
} catch (Exception e) {
e.printStackTrace();
}

View file

@ -18,8 +18,10 @@
package bisq.monitor.metric;
import bisq.monitor.Metric;
import bisq.monitor.OnionParser;
import bisq.monitor.Reporter;
import bisq.monitor.StatisticsHelper;
import bisq.network.p2p.NodeAddress;
import org.berndpruenster.netlayer.tor.Tor;
import org.berndpruenster.netlayer.tor.TorCtlException;
@ -27,8 +29,6 @@ import org.berndpruenster.netlayer.tor.TorCtlException;
import com.runjva.sourceforge.jsocks.protocol.Socks5Proxy;
import com.runjva.sourceforge.jsocks.protocol.SocksSocket;
import java.net.URL;
import java.io.IOException;
import java.util.ArrayList;
@ -61,7 +61,7 @@ public class TorRoundTripTime extends Metric {
// for each configured host
for (String current : configuration.getProperty(HOSTS, "").split(",")) {
// parse Url
URL tmp = new URL(current);
NodeAddress tmp = OnionParser.getNodeAddress(current);
List<Long> samples = new ArrayList<>();
@ -71,7 +71,7 @@ public class TorRoundTripTime extends Metric {
long start = System.currentTimeMillis();
// connect
socket = new SocksSocket(proxy, tmp.getHost(), tmp.getPort());
socket = new SocksSocket(proxy, tmp.getHostName(), tmp.getPort());
// by the time we get here, we are connected
samples.add(System.currentTimeMillis() - start);

View file

@ -17,12 +17,12 @@
package bisq.monitor.reporter;
import bisq.monitor.OnionParser;
import bisq.monitor.Reporter;
import bisq.network.p2p.NodeAddress;
import org.berndpruenster.netlayer.tor.TorSocket;
import java.net.URL;
import java.io.IOException;
import java.util.HashMap;
@ -55,10 +55,9 @@ public class GraphiteReporter extends Reporter {
String report = prefix + ("".equals(key) ? "" : (prefix.isEmpty() ? "" : ".") + key) + " " + value + " "
+ timestamp + "\n";
URL url;
try {
url = new URL(configuration.getProperty("serviceUrl"));
TorSocket socket = new TorSocket(url.getHost(), url.getPort());
NodeAddress nodeAddress = OnionParser.getNodeAddress(configuration.getProperty("serviceUrl"));
TorSocket socket = new TorSocket(nodeAddress.getHostName(), nodeAddress.getPort());
socket.getOutputStream().write(report.getBytes());
socket.close();