Skip to content

Commit

Permalink
Merge pull request #1169 from alvasw/tor_identity
Browse files Browse the repository at this point in the history
Implement TorIdentity
  • Loading branch information
alvasw authored Sep 11, 2023
2 parents d8012c3 + 6450333 commit 1e91d77
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
34 changes: 34 additions & 0 deletions network/tor/tor/src/main/java/bisq/tor/TorIdentity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.tor;

import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public class TorIdentity {
@ToString.Exclude
private final String privateKey;
private final int port;

public TorIdentity(String privateKey, int port) {
this.privateKey = privateKey;
this.port = port;
}
}
31 changes: 31 additions & 0 deletions network/tor/tor/src/main/java/bisq/tor/TorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
Expand Down Expand Up @@ -110,6 +111,16 @@ public CompletableFuture<Boolean> shutdown() {
return CompletableFuture.completedFuture(true);
}

public CompletableFuture<Boolean> startNode(TorIdentity torIdentity) {
try {
ServerSocket serverSocket = createOnionService(torIdentity).get();
return CompletableFuture.completedFuture(true);

} catch (ExecutionException | InterruptedException e) {
return CompletableFuture.failedFuture(e);
}
}

public CompletableFuture<CreateOnionServiceResponse> createOnionService(int port, String nodeId) {
log.info("Start hidden service with port {} and nodeId {}", port, nodeId);
long ts = System.currentTimeMillis();
Expand All @@ -131,6 +142,26 @@ public CompletableFuture<CreateOnionServiceResponse> createOnionService(int port
}
}

public CompletableFuture<ServerSocket> createOnionService(TorIdentity torIdentity) {
log.info("Start hidden service with {}", torIdentity);
long ts = System.currentTimeMillis();
try {
@SuppressWarnings("resource") ServerSocket localServerSocket = new ServerSocket(RANDOM_PORT);
int localPort = localServerSocket.getLocalPort();

return onionServicePublishService.publish(torIdentity, localPort)
.thenApply(unused -> {
log.info("Tor hidden service Ready. Took {} ms. Onion address={}",
System.currentTimeMillis() - ts, torIdentity);
return localServerSocket;
});

} catch (IOException e) {
log.error("Can't create onion service", e);
return CompletableFuture.failedFuture(e);
}
}

public boolean isOnionServiceOnline(String onionUrl) {
return nativeTorController.isHiddenServiceAvailable(onionUrl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package bisq.tor.onionservice;

import bisq.tor.TorIdentity;
import bisq.tor.controller.NativeTorController;
import lombok.extern.slf4j.Slf4j;
import net.freehaven.tor.control.TorControlConnection;
Expand Down Expand Up @@ -73,6 +74,18 @@ public synchronized CompletableFuture<OnionAddress> publish(String nodeId, int o
return completableFuture;
}

public synchronized CompletableFuture<Void> publish(TorIdentity torIdentity, int localPort) {
try {
Optional<String> privateKey = Optional.of(torIdentity.getPrivateKey());
nativeTorController.createHiddenService(torIdentity.getPort(), localPort, privateKey);
return CompletableFuture.completedFuture(null);

} catch (IOException e) {
log.error("Couldn't create onion service {}", torIdentity);
return CompletableFuture.failedFuture(e);
}
}

public synchronized Optional<OnionAddress> getOnionAddressForNode(String nodeId) {
try {
CompletableFuture<OnionAddress> completableFuture = onionAddressByNodeId.get(nodeId);
Expand Down

0 comments on commit 1e91d77

Please sign in to comment.