Commit 2179807d authored by zeroleak's avatar zeroleak
Browse files

cleanup

parent 572be624
......@@ -3,6 +3,7 @@ package com.samourai.tor.client;
import com.msopentech.thali.toronionproxy.OsData;
import com.msopentech.thali.toronionproxy.TorSettings;
import com.samourai.tor.client.utils.WhirlpoolTorInstaller;
import com.samourai.whirlpool.cli.beans.CliTorExecutableMode;
import com.samourai.whirlpool.cli.config.CliConfig;
import com.samourai.whirlpool.cli.utils.CliUtils;
import com.samourai.whirlpool.client.exception.NotifiableException;
......@@ -44,29 +45,28 @@ public class JavaTorClient {
}
private Optional<File> computeTorExecutableAndVerify() throws Exception {
boolean torExecutableAuto = cliConfig.getTorConfig().isExecutableAuto();
boolean torExecutableLocal = cliConfig.getTorConfig().isExecutableLocal();
String executablePath = cliConfig.getTorConfig().getExecutable();
CliTorExecutableMode executableMode = cliConfig.getTorConfig().getExecutableMode();
String executablePath =
CliTorExecutableMode.SPECIFIED.equals(executableMode)
? cliConfig.getTorConfig().getExecutable()
: null;
boolean tryEmbedded = CliTorExecutableMode.AUTO.equals(executableMode);
// try with embedded
boolean torExecutableEmbedded = true;
Optional<File> torExecutable =
computeTorExecutable(
torExecutableAuto, torExecutableEmbedded, torExecutableLocal, executablePath);
computeTorExecutable(executableMode, executablePath, tryEmbedded);
try {
// verify Tor executable is supported
checkTorExecutable(torExecutable); // throws exception when Tor not supported
} catch (Exception e) {
if (torExecutableAuto && torExecutableEmbedded) {
if (tryEmbedded) {
log.warn(
"Tor executable failed ("
+ (torExecutable.isPresent() ? torExecutable.get().getAbsolutePath() : "embedded")
+ ") => trying fallback...");
// retry without embedded
torExecutableEmbedded = false;
torExecutable =
computeTorExecutable(
torExecutableAuto, torExecutableEmbedded, torExecutableLocal, executablePath);
tryEmbedded = false;
torExecutable = computeTorExecutable(executableMode, executablePath, tryEmbedded);
try {
checkTorExecutable(torExecutable); // throws exception when Tor not supported
} catch (Exception ee) {
......@@ -90,41 +90,37 @@ public class JavaTorClient {
}
private Optional<File> computeTorExecutable(
boolean torExecutableAuto,
boolean torExecutableEmbedded,
boolean torExecutableLocal,
String executablePath)
CliTorExecutableMode executableMode, String executablePath, boolean tryEmbedded)
throws NotifiableException {
if (!torExecutableAuto && !torExecutableLocal) {
// use specified path for Tor executable
// path specified
if (CliTorExecutableMode.SPECIFIED.equals(executableMode)) {
if (log.isDebugEnabled()) {
log.debug("Using tor executable: " + executablePath);
log.debug("Using tor executable (specified): " + executablePath);
}
return Optional.of(getTorExecutablePath(executablePath));
}
// auto => embedded supported?
if (torExecutableAuto
&& torExecutableEmbedded
&& !OsData.OsType.UNSUPPORTED.equals(OsData.getOsType())) {
// use embedded Tor
// embedded
if (tryEmbedded && !OsData.OsType.UNSUPPORTED.equals(OsData.getOsType())) {
if (log.isDebugEnabled()) {
log.debug("Using tor executable: embedded");
log.debug("Using tor executable (embedded): " + OsData.getOsType());
}
return Optional.empty();
}
// auto + embedded not supported => search for local Tor executable
if (log.isDebugEnabled()) {
log.debug("Using tor executable: OS not supported, looking for existing local install");
}
// find local Tor executable
Optional<File> torExecutable = findTorExecutableLocal();
// no Tor executable found
if (!torExecutable.isPresent()) {
// not found
if (log.isDebugEnabled()) {
log.debug("Using tor executable (local): no local install found");
}
throw new NotifiableException(
"No local Tor executable found on your system, please install Tor.");
}
if (log.isDebugEnabled()) {
log.debug("Using tor executable (local): " + torExecutable.get());
}
return torExecutable;
}
......
package com.samourai.whirlpool.cli.beans;
import java.util.Optional;
public enum CliTorExecutableMode {
AUTO, // try embedded then find local
LOCAL, // find local install
SPECIFIED; // custom path specified
public static Optional<CliTorExecutableMode> find(String value) {
try {
return Optional.of(valueOf(value));
} catch (Exception e) {
return Optional.empty();
}
}
}
......@@ -4,6 +4,7 @@ import com.samourai.http.client.IHttpClient;
import com.samourai.stomp.client.IStompClientService;
import com.samourai.wallet.api.backend.BackendApi;
import com.samourai.whirlpool.cli.beans.CliProxy;
import com.samourai.whirlpool.cli.beans.CliTorExecutableMode;
import com.samourai.whirlpool.cli.utils.CliUtils;
import com.samourai.whirlpool.client.utils.ClientUtils;
import com.samourai.whirlpool.client.wallet.WhirlpoolWalletConfig;
......@@ -357,9 +358,8 @@ public abstract class CliConfigFile {
}
public static class TorConfig {
public static final String EXECUTABLE_AUTO = "auto";
public static final String EXECUTABLE_LOCAL = "local";
@NotEmpty private String executable;
private CliTorExecutableMode executableMode;
@NotEmpty private boolean onionServer;
@NotEmpty private boolean onionBackend;
......@@ -379,12 +379,13 @@ public abstract class CliConfigFile {
this.executable = executable;
}
public boolean isExecutableAuto() {
return EXECUTABLE_AUTO.equals(this.executable);
}
public boolean isExecutableLocal() {
return EXECUTABLE_LOCAL.equals(this.executable);
public CliTorExecutableMode getExecutableMode() {
if (executableMode == null) {
executableMode =
CliTorExecutableMode.find(this.executable.toUpperCase())
.orElse(CliTorExecutableMode.SPECIFIED);
}
return executableMode;
}
public boolean isOnionServer() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment