Skip to content

feat(gui): Tor toggle #300

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

Merged
merged 5 commits into from
Apr 22, 2025
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: 1 addition & 1 deletion src-gui/src/renderer/components/pages/help/HelpPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const useStyles = makeStyles((theme) => ({

export default function HelpPage() {
const classes = useStyles();
const location = useLocation();
const location = useLocation();

useEffect(() => {
if (location.hash) {
Expand Down
24 changes: 23 additions & 1 deletion src-gui/src/renderer/components/pages/help/SettingsBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import { Theme } from "renderer/components/theme";
import { Add, ArrowUpward, Delete, Edit, HourglassEmpty } from "@material-ui/icons";
import { getNetwork } from "store/config";
import { currencySymbol } from "utils/formatUtils";
import { setTorEnabled } from "store/features/settingsSlice";


const PLACEHOLDER_ELECTRUM_RPC_URL = "ssl://blockstream.info:700";
const PLACEHOLDER_MONERO_NODE_URL = "http://xmr-node.cakewallet.com:18081";
Expand Down Expand Up @@ -82,6 +84,7 @@ export default function SettingsBox() {
<TableContainer>
<Table>
<TableBody>
<TorSettings />
<ElectrumRpcUrlSetting />
<MoneroNodeUrlSetting />
<FetchFiatPricesSetting />
Expand Down Expand Up @@ -489,4 +492,23 @@ function NodeTable({
</Table>
</TableContainer>
)
}
}

export function TorSettings() {
const dispatch = useAppDispatch();
const torEnabled = useSettings((settings) => settings.enableTor)
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => dispatch(setTorEnabled(event.target.checked));
const status = (state: boolean) => state === true ? "enabled" : "disabled";

return (
<TableRow>
<TableCell>
<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." />
</TableCell>

<TableCell>
<Switch checked={torEnabled} onChange={handleChange} color="primary" />
</TableCell>
</TableRow>
)
}
71 changes: 0 additions & 71 deletions src-gui/src/renderer/components/pages/help/TorInfoBox.tsx

This file was deleted.

2 changes: 2 additions & 0 deletions src-gui/src/renderer/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export async function listSellersAtRendezvousPoint(
export async function initializeContext() {
const network = getNetwork();
const testnet = isTestnet();
const useTor = store.getState().settings.enableTor;

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

logger.info("Initializing context with settings", tauriSettings);
Expand Down
7 changes: 7 additions & 0 deletions src-gui/src/store/features/settingsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface SettingsState {
/// Whether to fetch fiat prices from the internet
fetchFiatPrices: boolean;
fiatCurrency: FiatCurrency;
/// Whether to enable Tor for p2p connections
enableTor: boolean
}

export enum FiatCurrency {
Expand Down Expand Up @@ -98,6 +100,7 @@ const initialState: SettingsState = {
theme: Theme.Darker,
fetchFiatPrices: true,
fiatCurrency: FiatCurrency.Usd,
enableTor: true
};

const alertsSlice = createSlice({
Expand Down Expand Up @@ -134,6 +137,9 @@ const alertsSlice = createSlice({
},
resetSettings(_) {
return initialState;
},
setTorEnabled(slice, action: PayloadAction<boolean>) {
slice.enableTor = action.payload;
}
},
});
Expand All @@ -146,6 +152,7 @@ export const {
resetSettings,
setFetchFiatPrices,
setFiatCurrency,
setTorEnabled,
} = alertsSlice.actions;

export default alertsSlice.reducer;
3 changes: 1 addition & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use swap::cli::{
command::{Bitcoin, Monero},
};
use tauri::{async_runtime::RwLock, Manager, RunEvent};
use uuid::Uuid;

/// Trait to convert Result<T, E> to Result<T, String>
/// Tauri commands require the error type to be a string
Expand Down Expand Up @@ -327,7 +326,7 @@ async fn initialize_context(
})
.with_json(false)
.with_debug(true)
.with_tor(true)
.with_tor(settings.use_tor)
.with_tauri(tauri_handle.clone())
.build()
.await;
Expand Down
6 changes: 6 additions & 0 deletions swap/src/cli/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ impl ContextBuilder {
};

let initialize_tor_client = async {
// Don't init a tor client unless we should use it.
if !self.tor {
tracing::warn!("Internal Tor client not enabled, skipping initialization");
return Ok(None);
}

self.tauri_handle.emit_context_init_progress_event(
TauriContextStatusEvent::Initializing(vec![
TauriPartialInitProgress::EstablishingTorCircuits(
Expand Down
4 changes: 3 additions & 1 deletion swap/src/cli/api/tauri_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl TauriHandle {
let request_id = Uuid::new_v4();
let now_secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.expect("it is later than the begin of the unix epoch")
.as_secs();
let expiration_ts = now_secs + timeout_secs;

Expand Down Expand Up @@ -501,4 +501,6 @@ pub struct TauriSettings {
/// The URL of the Electrum RPC server e.g `ssl://bitcoin.com:50001`
#[typeshare(serialized_as = "string")]
pub electrum_rpc_url: Option<Url>,
/// Whether to initialize and use a tor client.
pub use_tor: bool,
}
2 changes: 1 addition & 1 deletion swap/src/protocol/bob/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async fn next_state(
let btc_lock_amount = bitcoin::Amount::from_sat(
signed_tx
.output
.get(0)
.first()
.context("Failed to get lock amount")?
.value,
);
Expand Down
Loading