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

Delete tor files at startup #2344

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/java/bisq/core/CoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bisq.core.app.BisqEnvironment;
import bisq.core.app.BisqSetup;
import bisq.core.app.P2PNetworkSetup;
import bisq.core.app.TorSetup;
import bisq.core.app.WalletAppSetup;
import bisq.core.arbitration.ArbitratorModule;
import bisq.core.btc.BitcoinModule;
Expand Down Expand Up @@ -80,6 +81,7 @@ public CoreModule(Environment environment) {
@Override
protected void configure() {
bind(BisqSetup.class).in(Singleton.class);
bind(TorSetup.class).in(Singleton.class);
bind(P2PNetworkSetup.class).in(Singleton.class);
bind(WalletAppSetup.class).in(Singleton.class);

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/bisq/core/app/BisqSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public interface BisqSetupCompleteListener {
private final VoteResultService voteResultService;
private final AssetTradeActivityCheck tradeActivityCheck;
private final AssetService assetService;
private final TorSetup torSetup;
private final BSFormatter formatter;
@Setter
@Nullable
Expand Down Expand Up @@ -226,6 +227,7 @@ public BisqSetup(P2PNetworkSetup p2PNetworkSetup,
VoteResultService voteResultService,
AssetTradeActivityCheck tradeActivityCheck,
AssetService assetService,
TorSetup torSetup,
BSFormatter formatter) {


Expand Down Expand Up @@ -264,6 +266,7 @@ public BisqSetup(P2PNetworkSetup p2PNetworkSetup,
this.voteResultService = voteResultService;
this.tradeActivityCheck = tradeActivityCheck;
this.assetService = assetService;
this.torSetup = torSetup;
this.formatter = formatter;
}

Expand All @@ -286,6 +289,7 @@ private void step2() {
}

private void step3() {
torSetup.cleanupTorFiles();
readMapsFromResources();
checkCryptoSetup();
checkForCorrectOSArchitecture();
Expand Down
69 changes: 69 additions & 0 deletions core/src/main/java/bisq/core/app/TorSetup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.core.app;

import bisq.network.NetworkOptionKeys;

import bisq.common.handlers.ErrorMessageHandler;
import bisq.common.storage.FileUtil;

import com.google.inject.name.Named;

import javax.inject.Inject;

import java.nio.file.Paths;

import java.io.File;
import java.io.IOException;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

@Slf4j
public class TorSetup {
private File torDir;

@Inject
public TorSetup(@Named(NetworkOptionKeys.TOR_DIR) File torDir) {
this.torDir = torDir;
}

public void cleanupTorFiles() {
cleanupTorFiles(null, null);
}

// We get sometimes Tor startup problems which is related to some tor files in the tor directory. It happens
// more often if the application got killed (not graceful shutdown).
// Creating all tor files newly takes about 3-4 sec. longer and it does not benefit from cache files.
// TODO: We should fix those startup problems in the netlayer library, once fixed there we can remove that call at the
// Bisq startup again.
public void cleanupTorFiles(@Nullable Runnable resultHandler, @Nullable ErrorMessageHandler errorMessageHandler) {
File hiddenservice = new File(Paths.get(torDir.getAbsolutePath(), "hiddenservice").toString());
try {
FileUtil.deleteDirectory(torDir, hiddenservice, true);
if (resultHandler != null)
resultHandler.run();
} catch (IOException e) {
e.printStackTrace();
log.error(e.toString());
if (errorMessageHandler != null)
errorMessageHandler.handleErrorMessage(e.toString());
}
}
}
9 changes: 7 additions & 2 deletions core/src/main/java/bisq/core/app/misc/AppSetupWithP2P.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package bisq.core.app.misc;

import bisq.core.app.SetupUtils;
import bisq.core.app.TorSetup;
import bisq.core.filter.FilterManager;
import bisq.core.payment.AccountAgeWitnessService;
import bisq.core.trade.statistics.TradeStatisticsManager;
Expand Down Expand Up @@ -46,6 +47,7 @@ public class AppSetupWithP2P extends AppSetup {
protected final P2PService p2PService;
protected final AccountAgeWitnessService accountAgeWitnessService;
protected final FilterManager filterManager;
private final TorSetup torSetup;
protected BooleanProperty p2pNetWorkReady;
protected final TradeStatisticsManager tradeStatisticsManager;
protected ArrayList<PersistedDataHost> persistedDataHosts;
Expand All @@ -56,21 +58,24 @@ public AppSetupWithP2P(EncryptionService encryptionService,
P2PService p2PService,
TradeStatisticsManager tradeStatisticsManager,
AccountAgeWitnessService accountAgeWitnessService,
FilterManager filterManager) {
FilterManager filterManager,
TorSetup torSetup) {
super(encryptionService, keyRing);
this.p2PService = p2PService;
this.tradeStatisticsManager = tradeStatisticsManager;
this.accountAgeWitnessService = accountAgeWitnessService;
this.filterManager = filterManager;
this.torSetup = torSetup;
this.persistedDataHosts = new ArrayList<>();
}

@Override
public void initPersistedDataHosts() {
torSetup.cleanupTorFiles();
persistedDataHosts.add(p2PService);

// we apply at startup the reading of persisted data but don't want to get it triggered in the constructor
persistedDataHosts.stream().forEach(e -> {
persistedDataHosts.forEach(e -> {
try {
log.info("call readPersisted at " + e.getClass().getSimpleName());
e.readPersisted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package bisq.core.app.misc;

import bisq.core.app.TorSetup;
import bisq.core.dao.DaoOptionKeys;
import bisq.core.dao.DaoSetup;
import bisq.core.dao.governance.ballot.BallotListService;
Expand Down Expand Up @@ -57,13 +58,15 @@ public AppSetupWithP2PAndDAO(EncryptionService encryptionService,
MyProposalListService myProposalListService,
MyReputationListService myReputationListService,
MyProofOfBurnListService myProofOfBurnListService,
TorSetup torSetup,
@Named(DaoOptionKeys.DAO_ACTIVATED) boolean daoActivated) {
super(encryptionService,
keyRing,
p2PService,
tradeStatisticsManager,
accountAgeWitnessService,
filterManager);
filterManager,
torSetup);

this.daoSetup = daoSetup;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bisq.core.alert.AlertModule;
import bisq.core.app.AppOptionKeys;
import bisq.core.app.BisqEnvironment;
import bisq.core.app.TorSetup;
import bisq.core.arbitration.ArbitratorModule;
import bisq.core.btc.BitcoinModule;
import bisq.core.dao.DaoModule;
Expand Down Expand Up @@ -74,6 +75,7 @@ protected void configure() {
bind(PersistenceProtoResolver.class).to(CorePersistenceProtoResolver.class).in(Singleton.class);
bind(Preferences.class).in(Singleton.class);
bind(BridgeAddressProvider.class).to(Preferences.class).in(Singleton.class);
bind(TorSetup.class).in(Singleton.class);

bind(SeedNodeAddressLookup.class).in(Singleton.class);
bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class).in(Singleton.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,18 @@
import bisq.desktop.main.overlays.popups.Popup;
import bisq.desktop.util.Layout;

import bisq.core.app.TorSetup;
import bisq.core.locale.Res;
import bisq.core.user.Preferences;

import bisq.network.NetworkOptionKeys;
import bisq.network.p2p.network.DefaultPluggableTransports;
import bisq.network.p2p.network.NetworkNode;

import bisq.common.UserThread;
import bisq.common.storage.FileUtil;
import bisq.common.util.Tuple2;
import bisq.common.util.Tuple4;
import bisq.common.util.Utilities;

import com.google.inject.name.Named;

import javax.inject.Inject;

import javafx.scene.Scene;
Expand All @@ -65,9 +62,6 @@

import java.net.URI;

import java.nio.file.Paths;

import java.io.File;
import java.io.IOException;

import java.util.Arrays;
Expand All @@ -94,8 +88,8 @@ public enum Transport {
}

private final Preferences preferences;
private NetworkNode networkNode;
private final File torDir;
private final NetworkNode networkNode;
private final TorSetup torSetup;
private Label enterBridgeLabel;
private ComboBox<Transport> transportTypeComboBox;
private TextArea bridgeEntriesTextArea;
Expand All @@ -106,10 +100,10 @@ public enum Transport {
@Inject
public TorNetworkSettingsWindow(Preferences preferences,
NetworkNode networkNode,
@Named(NetworkOptionKeys.TOR_DIR) File torDir) {
TorSetup torSetup) {
this.preferences = preferences;
this.networkNode = networkNode;
this.torDir = torDir;
this.torSetup = torSetup;

type = Type.Attention;

Expand Down Expand Up @@ -342,15 +336,7 @@ private void cleanTorDir(Runnable resultHandler) {
networkNode.shutDown(() -> {
// We give it a bit extra time to be sure that OS locks are removed
UserThread.runAfter(() -> {
final File hiddenservice = new File(Paths.get(torDir.getAbsolutePath(), "hiddenservice").toString());
try {
FileUtil.deleteDirectory(torDir, hiddenservice, true);
resultHandler.run();
} catch (IOException e) {
e.printStackTrace();
log.error(e.toString());
new Popup<>().error(e.toString()).show();
}
torSetup.cleanupTorFiles(resultHandler, errorMessage -> new Popup<>().error(errorMessage).show());
}, 3);
});
}
Expand Down