Skip to content

Commit 3fa31ba

Browse files
feat(gui): Tor toggle (#300)
* re-add tor info box, show switch for toggling tor * add use_tor to TauriSettings, only initialize tor client when it's true * add warning log message when not using tor client * change the label text of the switch, fail to align switch with SettingsBox icons * move Tor settings to SettingsBox
1 parent ffe103c commit 3fa31ba

File tree

9 files changed

+44
-77
lines changed

9 files changed

+44
-77
lines changed

src-gui/src/renderer/components/pages/help/HelpPage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const useStyles = makeStyles((theme) => ({
1818

1919
export default function HelpPage() {
2020
const classes = useStyles();
21-
const location = useLocation();
21+
const location = useLocation();
2222

2323
useEffect(() => {
2424
if (location.hash) {

src-gui/src/renderer/components/pages/help/SettingsBox.tsx

+23-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import { Theme } from "renderer/components/theme";
4444
import { Add, ArrowUpward, Delete, Edit, HourglassEmpty } from "@material-ui/icons";
4545
import { getNetwork } from "store/config";
4646
import { currencySymbol } from "utils/formatUtils";
47+
import { setTorEnabled } from "store/features/settingsSlice";
48+
4749

4850
const PLACEHOLDER_ELECTRUM_RPC_URL = "ssl://blockstream.info:700";
4951
const PLACEHOLDER_MONERO_NODE_URL = "http://xmr-node.cakewallet.com:18081";
@@ -82,6 +84,7 @@ export default function SettingsBox() {
8284
<TableContainer>
8385
<Table>
8486
<TableBody>
87+
<TorSettings />
8588
<ElectrumRpcUrlSetting />
8689
<MoneroNodeUrlSetting />
8790
<FetchFiatPricesSetting />
@@ -489,4 +492,23 @@ function NodeTable({
489492
</Table>
490493
</TableContainer>
491494
)
492-
}
495+
}
496+
497+
export function TorSettings() {
498+
const dispatch = useAppDispatch();
499+
const torEnabled = useSettings((settings) => settings.enableTor)
500+
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => dispatch(setTorEnabled(event.target.checked));
501+
const status = (state: boolean) => state === true ? "enabled" : "disabled";
502+
503+
return (
504+
<TableRow>
505+
<TableCell>
506+
<SettingLabel label="Use Tor" tooltip="Tor (The Onion Router) is a decentralized network allowing for anonymous browsing. If enabled, the app will use its internal Tor client to hide your IP address from the maker. Requires a restart to take effect." />
507+
</TableCell>
508+
509+
<TableCell>
510+
<Switch checked={torEnabled} onChange={handleChange} color="primary" />
511+
</TableCell>
512+
</TableRow>
513+
)
514+
}

src-gui/src/renderer/components/pages/help/TorInfoBox.tsx

-71
This file was deleted.

src-gui/src/renderer/rpc.ts

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export async function listSellersAtRendezvousPoint(
175175
export async function initializeContext() {
176176
const network = getNetwork();
177177
const testnet = isTestnet();
178+
const useTor = store.getState().settings.enableTor;
178179

179180
// This looks convoluted but it does the following:
180181
// - Fetch the status of all nodes for each blockchain in parallel
@@ -208,6 +209,7 @@ export async function initializeContext() {
208209
const tauriSettings: TauriSettings = {
209210
electrum_rpc_url: bitcoinNode,
210211
monero_node_url: moneroNode,
212+
use_tor: useTor
211213
};
212214

213215
logger.info("Initializing context with settings", tauriSettings);

src-gui/src/store/features/settingsSlice.ts

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface SettingsState {
99
/// Whether to fetch fiat prices from the internet
1010
fetchFiatPrices: boolean;
1111
fiatCurrency: FiatCurrency;
12+
/// Whether to enable Tor for p2p connections
13+
enableTor: boolean
1214
}
1315

1416
export enum FiatCurrency {
@@ -98,6 +100,7 @@ const initialState: SettingsState = {
98100
theme: Theme.Darker,
99101
fetchFiatPrices: true,
100102
fiatCurrency: FiatCurrency.Usd,
103+
enableTor: true
101104
};
102105

103106
const alertsSlice = createSlice({
@@ -134,6 +137,9 @@ const alertsSlice = createSlice({
134137
},
135138
resetSettings(_) {
136139
return initialState;
140+
},
141+
setTorEnabled(slice, action: PayloadAction<boolean>) {
142+
slice.enableTor = action.payload;
137143
}
138144
},
139145
});
@@ -146,6 +152,7 @@ export const {
146152
resetSettings,
147153
setFetchFiatPrices,
148154
setFiatCurrency,
155+
setTorEnabled,
149156
} = alertsSlice.actions;
150157

151158
export default alertsSlice.reducer;

src-tauri/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use swap::cli::{
1818
command::{Bitcoin, Monero},
1919
};
2020
use tauri::{async_runtime::RwLock, Manager, RunEvent};
21-
use uuid::Uuid;
2221

2322
/// Trait to convert Result<T, E> to Result<T, String>
2423
/// Tauri commands require the error type to be a string
@@ -327,7 +326,7 @@ async fn initialize_context(
327326
})
328327
.with_json(false)
329328
.with_debug(true)
330-
.with_tor(true)
329+
.with_tor(settings.use_tor)
331330
.with_tauri(tauri_handle.clone())
332331
.build()
333332
.await;

swap/src/cli/api.rs

+6
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,12 @@ impl ContextBuilder {
394394
};
395395

396396
let initialize_tor_client = async {
397+
// Don't init a tor client unless we should use it.
398+
if !self.tor {
399+
tracing::warn!("Internal Tor client not enabled, skipping initialization");
400+
return Ok(None);
401+
}
402+
397403
self.tauri_handle.emit_context_init_progress_event(
398404
TauriContextStatusEvent::Initializing(vec![
399405
TauriPartialInitProgress::EstablishingTorCircuits(

swap/src/cli/api/tauri_bindings.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl TauriHandle {
133133
let request_id = Uuid::new_v4();
134134
let now_secs = SystemTime::now()
135135
.duration_since(UNIX_EPOCH)
136-
.unwrap()
136+
.expect("it is later than the begin of the unix epoch")
137137
.as_secs();
138138
let expiration_ts = now_secs + timeout_secs;
139139

@@ -501,4 +501,6 @@ pub struct TauriSettings {
501501
/// The URL of the Electrum RPC server e.g `ssl://bitcoin.com:50001`
502502
#[typeshare(serialized_as = "string")]
503503
pub electrum_rpc_url: Option<Url>,
504+
/// Whether to initialize and use a tor client.
505+
pub use_tor: bool,
504506
}

swap/src/protocol/bob/swap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ async fn next_state(
164164
let btc_lock_amount = bitcoin::Amount::from_sat(
165165
signed_tx
166166
.output
167-
.get(0)
167+
.first()
168168
.context("Failed to get lock amount")?
169169
.value,
170170
);

0 commit comments

Comments
 (0)