Skip to content

Commit

Permalink
WhonixTorController: Implement AUTHENTICATE command
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed May 21, 2024
1 parent 89f0b5a commit 69ceb57
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package bisq.tor.controller;

public class TorControlAuthenticationFailed extends RuntimeException {
public TorControlAuthenticationFailed(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bisq.tor.controller;

import bisq.common.encoding.Hex;
import bisq.security.keys.TorKeyPair;
import net.freehaven.tor.control.PasswordDigest;

import java.io.*;
import java.net.Socket;
Expand All @@ -25,17 +27,42 @@ public void close() throws IOException {
controlSocket.close();
}

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();

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";
byte[] commandBytes = command.getBytes(StandardCharsets.US_ASCII);

sendCommand(command);
String reply = receiveReply();
}

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

private String receiveReply() throws IOException {
String reply = bufferedReader.readLine();
if (reply.equals("510 Command filtered")) {
throw new TorCommandFilteredException();
}
return reply;
}
}

0 comments on commit 69ceb57

Please sign in to comment.