Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TorControlProtocol: Support multiline Tor replies #2255

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package bisq.tor.controller;

import bisq.tor.controller.events.events.BootstrapEvent;

import java.util.Optional;

public class BootstrapEventParser {
public static Optional<BootstrapEvent> tryParse(String line) {
// 650 STATUS_CLIENT NOTICE BOOTSTRAP PROGRESS=50 TAG=loading_descriptors SUMMARY="Loading relay descriptors"
if (isStatusClientEvent(line)) {
String[] parts = line.split(" ");

if (isBootstrapEvent(parts)) {
BootstrapEvent bootstrapEvent = parseBootstrapEvent(parts);
return Optional.of(bootstrapEvent);
}
}

return Optional.empty();
}

private static boolean isStatusClientEvent(String line) {
// 650 STATUS_CLIENT NOTICE CIRCUIT_ESTABLISHED
return line.startsWith("650 STATUS_CLIENT");
}

private static boolean isBootstrapEvent(String[] parts) {
// 650 STATUS_CLIENT NOTICE BOOTSTRAP PROGRESS=50 TAG=loading_descriptors SUMMARY="Loading relay descriptors"
return parts.length >= 7 && parts[3].equals("BOOTSTRAP");
}


private static BootstrapEvent parseBootstrapEvent(String[] parts) {
String progress = parts[4].replace("PROGRESS=", "");
String tag = parts[5].replace("TAG=", "");
String summary = parseBootstrapSummary(parts);

int progressInt = Integer.parseInt(progress);
return new BootstrapEvent(progressInt, tag, summary);
}

private static String parseBootstrapSummary(String[] parts) {
StringBuilder summary = new StringBuilder();

// SUMMARY="Loading relay descriptors" has whitespaces in string
for (int i = 6; i < parts.length; i++) {
String summaryPart = parts[i];
summary.append(summaryPart)
.append(" ");
}

String summaryPrefix = "SUMMARY=\"";
summary.delete(0, summaryPrefix.length());

// ends with `" `
int length = summary.length();
summary.delete(length - 2, length);

return summary.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package bisq.tor.controller;

import bisq.common.encoding.Hex;
import bisq.security.keys.TorKeyPair;
import bisq.tor.controller.events.listener.BootstrapEventListener;
import net.freehaven.tor.control.PasswordDigest;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TorControlProtocol implements AutoCloseable {
private final Socket controlSocket;
private final WhonixTorControlReader whonixTorControlReader;
private final OutputStream outputStream;

// MidReplyLine = StatusCode "-" ReplyLine
private final Pattern midReplyLinePattern = Pattern.compile("^\\d+-.+");
// DataReplyLine = StatusCode "+" ReplyLine CmdData
private final Pattern dataReplyLinePattern = Pattern.compile("^\\d+\\+.+");

public TorControlProtocol(int port) throws IOException {
controlSocket = new Socket("127.0.0.1", port);
whonixTorControlReader = new WhonixTorControlReader(controlSocket.getInputStream());
outputStream = controlSocket.getOutputStream();
}

@Override
public void close() throws IOException {
controlSocket.close();
}

public void initialize() {
whonixTorControlReader.start();
}

public void authenticate(PasswordDigest passwordDigest) throws IOException {
byte[] secret = passwordDigest.getSecret();
String secretHex = Hex.encode(secret);
String command = "AUTHENTICATE " + secretHex + "\r\n";

sendCommand(command);
String reply = receiveReply().findFirst().orElseThrow();

if (reply.equals("250 OK")) {
return;
}

if (reply.startsWith("515 Authentication failed")) {
throw new TorControlAuthenticationFailed(reply);
}
}

public void addOnion(TorKeyPair torKeyPair, int onionPort, int localPort) throws IOException {
String base64SecretScalar = torKeyPair.getBase64SecretScalar();
String command = "ADD_ONION " + "ED25519-V3:" + base64SecretScalar + " Port=" + onionPort + "," + localPort + "\r\n";

sendCommand(command);
Stream<String> replyStream = receiveReply();
assertTwoLineOkReply(replyStream, "ADD_ONION");
}

public String getInfo(String keyword) throws IOException {
String command = "GETINFO " + keyword + "\r\n";
sendCommand(command);
Stream<String> replyStream = receiveReply();
return assertTwoLineOkReply(replyStream, "GETINFO");
}

public void resetConf(String configName) throws IOException {
String command = "RESETCONF " + configName + "\r\n";
sendCommand(command);
String reply = receiveReply().findFirst().orElseThrow();
if (!reply.equals("250 OK")) {
throw new ControlCommandFailedException("Couldn't reset config: " + configName);
}
}

public void setConfig(String configName, String configValue) throws IOException {
String command = "SETCONF " + configName + "=" + configValue + "\r\n";
sendCommand(command);
String reply = receiveReply().findFirst().orElseThrow();
if (!reply.equals("250 OK")) {
throw new ControlCommandFailedException("Couldn't set config: " + configName + "=" + configValue);
}
}

public void setEvents(List<String> events) throws IOException {
var stringBuilder = new StringBuffer("SETEVENTS");
events.forEach(event -> stringBuilder.append(" ").append(event));
stringBuilder.append("\r\n");

String command = stringBuilder.toString();
sendCommand(command);
String reply = receiveReply().findFirst().orElseThrow();
if (!reply.equals("250 OK")) {
throw new ControlCommandFailedException("Couldn't set events: " + events);
}
}

public void takeOwnership() throws IOException {
String command = "TAKEOWNERSHIP\r\n";
sendCommand(command);
String reply = receiveReply().findFirst().orElseThrow();
if (!reply.equals("250 OK")) {
throw new ControlCommandFailedException("Couldn't take ownership");
}
}

public void addBootstrapEventListener(BootstrapEventListener listener) {
whonixTorControlReader.addBootstrapEventListener(listener);
}

public void removeBootstrapEventListener(BootstrapEventListener listener) {
whonixTorControlReader.removeBootstrapEventListener(listener);
}

private void sendCommand(String command) throws IOException {
byte[] commandBytes = command.getBytes(StandardCharsets.US_ASCII);
outputStream.write(commandBytes);
outputStream.flush();
}

private Stream<String> receiveReply() {
String reply = tryReadNextReply();
var streamBuilder = Stream.<String>builder();
streamBuilder.add(reply);

while (isMultilineReply(reply)) {
reply = tryReadNextReply();
streamBuilder.add(reply);
}

return streamBuilder.build();
}

private String tryReadNextReply() {
String reply = whonixTorControlReader.readLine();
if (reply.equals("510 Command filtered")) {
throw new TorCommandFilteredException();
}
return reply;
}

private boolean isMultilineReply(String reply) {
return midReplyLinePattern.matcher(reply).matches() || dataReplyLinePattern.matcher(reply).matches();
}

private String assertTwoLineOkReply(Stream<String> replyStream, String commandName) {
List<String> replies = replyStream.collect(Collectors.toList());
if (replies.size() != 2) {
throw new ControlCommandFailedException("Invalid " + commandName + " reply: " + replies);
}

String firstLine = replies.get(0);
if (!firstLine.startsWith("250-")) {
throw new ControlCommandFailedException("Invalid " + commandName + " reply: " + replies);
}

String secondLine = replies.get(1);
if (!secondLine.equals("250 OK")) {
throw new ControlCommandFailedException("Invalid " + commandName + " reply: " + replies);
}

return firstLine;
}
}
155 changes: 155 additions & 0 deletions network/tor/tor/src/main/java/bisq/tor/controller/TorController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package bisq.tor.controller;

import bisq.common.observable.Observable;
import bisq.tor.TorrcClientConfigFactory;
import bisq.tor.controller.events.events.BootstrapEvent;
import bisq.tor.controller.events.listener.BootstrapEventListener;
import bisq.tor.controller.exceptions.TorBootstrapFailedException;
import bisq.tor.process.NativeTorProcess;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.freehaven.tor.control.PasswordDigest;

import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TorController implements BootstrapEventListener {
private final int bootstrapTimeout;
private final CountDownLatch isBootstrappedCountdownLatch = new CountDownLatch(1);
@Getter
private final Observable<BootstrapEvent> bootstrapEvent = new Observable<>();

private Optional<TorControlProtocol> torControlProtocol = Optional.empty();

public TorController(int bootstrapTimeout) {
this.bootstrapTimeout = bootstrapTimeout;
}

public void initialize(int controlPort) throws IOException {
initialize(controlPort, Optional.empty());
}

public void initialize(int controlPort, PasswordDigest hashedControlPassword) throws IOException {
initialize(controlPort, Optional.of(hashedControlPassword));
}

public void shutdown() {
torControlProtocol.ifPresent(torControlProtocol -> {
try {
torControlProtocol.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

public void bootstrapTor() throws IOException {
bindToBisq();
subscribeToBootstrapEvents();
enableNetworking();
waitUntilBootstrapped();
}

public Optional<Integer> getSocksPort() {
try {
TorControlProtocol torControlProtocol = getTorControlProtocol();
String socksListenersString = torControlProtocol.getInfo("net/listeners/socks");

String socksListener;
if (socksListenersString.contains(" ")) {
String[] socksPorts = socksListenersString.split(" ");
socksListener = socksPorts[0];
} else {
socksListener = socksListenersString;
}

// "127.0.0.1:12345"
socksListener = socksListener.replace("\"", "");
String portString = socksListener.split(":")[1];

int port = Integer.parseInt(portString);
return Optional.of(port);
} catch (IOException e) {
return Optional.empty();
}
}

@Override
public void onBootstrapStatusEvent(BootstrapEvent bootstrapEvent) {
log.info("Tor bootstrap event: {}", bootstrapEvent);
this.bootstrapEvent.set(bootstrapEvent);
if (bootstrapEvent.isDoneEvent()) {
isBootstrappedCountdownLatch.countDown();
}
}

private void initialize(int controlPort, Optional<PasswordDigest> hashedControlPassword) throws IOException {
var torControlProtocol = new TorControlProtocol(controlPort);
this.torControlProtocol = Optional.of(torControlProtocol);

torControlProtocol.initialize();
if (hashedControlPassword.isPresent()) {
torControlProtocol.authenticate(hashedControlPassword.get());
}
}

private void bindToBisq() throws IOException {
TorControlProtocol torControlProtocol = getTorControlProtocol();
torControlProtocol.takeOwnership();
torControlProtocol.resetConf(NativeTorProcess.ARG_OWNER_PID);
}

private void subscribeToBootstrapEvents() throws IOException {
TorControlProtocol torControlProtocol = getTorControlProtocol();
torControlProtocol.addBootstrapEventListener(this);
torControlProtocol.setEvents(List.of("STATUS_CLIENT"));
}

private void enableNetworking() throws IOException {
TorControlProtocol torControlProtocol = getTorControlProtocol();
torControlProtocol.setConfig(TorrcClientConfigFactory.DISABLE_NETWORK_CONFIG_KEY, "0");
}

private void waitUntilBootstrapped() {
try {
while (true) {
if (torControlProtocol.isEmpty()) {
throw new TorBootstrapFailedException("Tor is not initializing.");
}

boolean isSuccess = isBootstrappedCountdownLatch.await(bootstrapTimeout, TimeUnit.MILLISECONDS);

if (isSuccess) {
TorControlProtocol torControlProtocol = this.torControlProtocol.get();
torControlProtocol.removeBootstrapEventListener(this);
torControlProtocol.setEvents(Collections.emptyList());
break;
} else if (isBootstrapTimeoutTriggered()) {
throw new TorBootstrapFailedException("Tor bootstrap timeout triggered.");
}
}
} catch (InterruptedException e) {
throw new TorBootstrapFailedException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private boolean isBootstrapTimeoutTriggered() {
BootstrapEvent bootstrapEvent = this.bootstrapEvent.get();
Instant timestamp = bootstrapEvent.getTimestamp();
Instant bootstrapTimeoutAgo = Instant.now().minus(bootstrapTimeout, ChronoUnit.MILLIS);
return bootstrapTimeoutAgo.isAfter(timestamp);
}

private TorControlProtocol getTorControlProtocol() {
return this.torControlProtocol.orElseThrow();
}
}
Loading
Loading