use of org.berndpruenster.netlayer.tor.TorCtlException in project haveno by haveno-dex.
the class PriceNodeStats method execute.
@Override
protected void execute() {
try {
// fetch proxy
Tor tor = Tor.getDefault();
checkNotNull(tor, "tor must not be null");
Socks5Proxy proxy = tor.getProxy();
String[] hosts = configuration.getProperty(HOSTS, "").split(",");
Collections.shuffle(Arrays.asList(hosts));
// for each configured host
for (String current : hosts) {
Map<String, String> result = new HashMap<>();
// parse Url
NodeAddress tmp = OnionParser.getNodeAddress(current);
// connect
try {
SocksSocket socket = new SocksSocket(proxy, tmp.getHostName(), tmp.getPort());
// prepare to receive data
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// ask for fee data
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
out.println("GET /getFees/");
out.println();
out.flush();
// sift through the received lines and see if we got something json-like
String line;
while ((line = in.readLine()) != null) {
Matcher matcher = stringNumberPattern.matcher(line);
if (matcher.find())
if (!IGNORE.contains(matcher.group(1)))
result.put("fees." + matcher.group(1), matcher.group(2));
}
in.close();
out.close();
socket.close();
// connect
socket = new SocksSocket(proxy, tmp.getHostName(), tmp.getPort());
// prepare to receive data
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// ask for exchange rate data
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
out.println("GET /getAllMarketPrices/");
out.println();
out.flush();
String currencyCode = "";
while ((line = in.readLine()) != null) {
Matcher currencyCodeMatcher = currencyCodePattern.matcher(line);
Matcher priceMatcher = pricePattern.matcher(line);
if (currencyCodeMatcher.find()) {
currencyCode = currencyCodeMatcher.group(1);
if (!assets.contains(currencyCode))
currencyCode = "";
} else if (!"".equals(currencyCode) && priceMatcher.find())
result.put("price." + currencyCode, priceMatcher.group(1));
}
// close all the things
in.close();
out.close();
socket.close();
// report
reporter.report(result, getName());
// only ask for data as long as we got none
if (!result.isEmpty())
break;
} catch (IOException e) {
log.error("{} seems to be down. Trying next configured price node.", tmp.getHostName());
e.printStackTrace();
}
}
} catch (TorCtlException | IOException e) {
e.printStackTrace();
}
}
use of org.berndpruenster.netlayer.tor.TorCtlException in project haveno by haveno-dex.
the class TorNetworkNode method createTorAndHiddenService.
// /////////////////////////////////////////////////////////////////////////////////////////
// create tor
// /////////////////////////////////////////////////////////////////////////////////////////
private void createTorAndHiddenService(int localPort, int servicePort) {
torStartupFuture = executorService.submit(() -> {
try {
// get tor
Tor.setDefault(torMode.getTor());
// start hidden service
long ts2 = new Date().getTime();
hiddenServiceSocket = new HiddenServiceSocket(localPort, torMode.getHiddenServiceDirectory(), servicePort);
nodeAddressProperty.set(new NodeAddress(hiddenServiceSocket.getServiceName() + ":" + hiddenServiceSocket.getHiddenServicePort()));
UserThread.execute(() -> setupListeners.forEach(SetupListener::onTorNodeReady));
hiddenServiceSocket.addReadyListener(socket -> {
try {
log.info("\n################################################################\n" + "Tor hidden service published after {} ms. Socket={}\n" + "################################################################", (new Date().getTime() - ts2), // takes usually 30-40 sec
socket);
new Thread() {
@Override
public void run() {
try {
nodeAddressProperty.set(new NodeAddress(hiddenServiceSocket.getServiceName() + ":" + hiddenServiceSocket.getHiddenServicePort()));
startServer(socket);
UserThread.execute(() -> setupListeners.forEach(SetupListener::onHiddenServicePublished));
} catch (final Exception e1) {
log.error(e1.toString());
e1.printStackTrace();
}
}
}.start();
} catch (final Exception e) {
log.error(e.toString());
e.printStackTrace();
}
return null;
});
} catch (TorCtlException e) {
String msg = e.getCause() != null ? e.getCause().toString() : e.toString();
log.error("Tor node creation failed: {}", msg);
if (e.getCause() instanceof IOException) {
// Since we cannot connect to Tor, we cannot do nothing.
// Furthermore, we have no hidden services started yet, so there is no graceful
// shutdown needed either
UserThread.execute(() -> setupListeners.forEach(s -> s.onSetupFailed(new RuntimeException(msg))));
} else {
restartTor(e.getMessage());
}
} catch (IOException e) {
log.error("Could not connect to running Tor: {}", e.getMessage());
// Since we cannot connect to Tor, we cannot do nothing.
// Furthermore, we have no hidden services started yet, so there is no graceful
// shutdown needed either
UserThread.execute(() -> setupListeners.forEach(s -> s.onSetupFailed(new RuntimeException(e.getMessage()))));
} catch (Throwable ignore) {
}
return null;
});
Futures.addCallback(torStartupFuture, Utilities.failureCallback(throwable -> UserThread.execute(() -> log.error("Hidden service creation failed: " + throwable))), MoreExecutors.directExecutor());
}
use of org.berndpruenster.netlayer.tor.TorCtlException in project haveno by haveno-dex.
the class TorNetworkNode method getSocksProxy.
// TODO handle failure more cleanly
public Socks5Proxy getSocksProxy() {
try {
String stream = null;
if (streamIsolation) {
// create a random string
// note that getProxy does Sha256 that string anyways
byte[] bytes = new byte[512];
new SecureRandom().nextBytes(bytes);
stream = Base64.getEncoder().encodeToString(bytes);
}
if (socksProxy == null || streamIsolation) {
tor = Tor.getDefault();
// ask for the connection
socksProxy = tor != null ? tor.getProxy(stream) : null;
}
return socksProxy;
} catch (TorCtlException e) {
log.error("TorCtlException at getSocksProxy: " + e.toString());
e.printStackTrace();
return null;
} catch (Throwable t) {
log.error("Error at getSocksProxy: " + t.toString());
return null;
}
}
use of org.berndpruenster.netlayer.tor.TorCtlException in project haveno by haveno-dex.
the class TorRoundTripTime method execute.
@Override
protected void execute() {
SocksSocket socket;
try {
// fetch proxy
Tor tor = Tor.getDefault();
checkNotNull(tor, "tor must not be null");
Socks5Proxy proxy = tor.getProxy();
// for each configured host
for (String current : configuration.getProperty(HOSTS, "").split(",")) {
// parse Url
NodeAddress tmp = OnionParser.getNodeAddress(current);
List<Long> samples = new ArrayList<>();
while (samples.size() < Integer.parseInt(configuration.getProperty(SAMPLE_SIZE, "1"))) {
// start timer - we do not need System.nanoTime as we expect our result to be in
// seconds time.
long start = System.currentTimeMillis();
// connect
socket = new SocksSocket(proxy, tmp.getHostName(), tmp.getPort());
// by the time we get here, we are connected
samples.add(System.currentTimeMillis() - start);
// cleanup
socket.close();
}
// report
reporter.report(StatisticsHelper.process(samples), getName());
}
} catch (TorCtlException | IOException e) {
e.printStackTrace();
}
}
use of org.berndpruenster.netlayer.tor.TorCtlException in project haveno by haveno-dex.
the class TorStartupTime method execute.
@Override
protected void execute() {
// cleanup installation
torWorkingDirectory.delete();
Tor tor = null;
// start timer - we do not need System.nanoTime as we expect our result to be in
// tenth of seconds time.
long start = System.currentTimeMillis();
try {
tor = new NativeTor(torWorkingDirectory, null, torOverrides);
// stop the timer and set its timestamp
reporter.report(System.currentTimeMillis() - start, getName());
} catch (TorCtlException e) {
e.printStackTrace();
} finally {
// cleanup
if (tor != null)
tor.shutdown();
}
}
Aggregations