|
| 1 | +// Copyright 2024 - Nym Technologies SA <[email protected]> |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use anyhow::{anyhow, Result}; |
| 5 | +use clap::{Args, Parser, Subcommand}; |
| 6 | +use nym_gateway_directory::{EntryPoint, ExitPoint, NodeIdentity, Recipient}; |
| 7 | +use std::path::PathBuf; |
| 8 | + |
| 9 | +#[derive(Parser)] |
| 10 | +#[clap(author = "Nymtech", version, about)] |
| 11 | +pub(crate) struct CliArgs { |
| 12 | + /// Use HTTP instead of socket file for IPC with the daemon. |
| 13 | + #[arg(long)] |
| 14 | + pub(crate) http: bool, |
| 15 | + |
| 16 | + #[command(subcommand)] |
| 17 | + pub(crate) command: Command, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Subcommand)] |
| 21 | +pub(crate) enum Command { |
| 22 | + Connect(ConnectArgs), |
| 23 | + Disconnect, |
| 24 | + Status, |
| 25 | + ImportCredential(ImportCredentialArgs), |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Args)] |
| 29 | +pub(crate) struct ConnectArgs { |
| 30 | + #[command(flatten)] |
| 31 | + pub(crate) entry: CliEntry, |
| 32 | + |
| 33 | + #[command(flatten)] |
| 34 | + pub(crate) exit: CliExit, |
| 35 | + |
| 36 | + /// Disable routing all traffic through the nym TUN device. When the flag is set, the nym TUN |
| 37 | + /// device will be created, but to route traffic through it you will need to do it manually, |
| 38 | + /// e.g. ping -Itun0. |
| 39 | + #[arg(long)] |
| 40 | + pub(crate) disable_routing: bool, |
| 41 | + |
| 42 | + /// Enable two-hop mixnet traffic. This means that traffic jumps directly from entry gateway to |
| 43 | + /// exit gateway. |
| 44 | + #[arg(long)] |
| 45 | + pub(crate) enable_two_hop: bool, |
| 46 | + |
| 47 | + /// Enable Poisson process rate limiting of outbound traffic. |
| 48 | + #[arg(long)] |
| 49 | + pub(crate) enable_poisson_rate: bool, |
| 50 | + |
| 51 | + /// Disable constant rate background loop cover traffic. |
| 52 | + #[arg(long)] |
| 53 | + pub(crate) disable_background_cover_traffic: bool, |
| 54 | + |
| 55 | + /// Enable credentials mode. |
| 56 | + #[arg(long)] |
| 57 | + pub(crate) enable_credentials_mode: bool, |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Args)] |
| 61 | +#[group(multiple = false)] |
| 62 | +pub(crate) struct CliEntry { |
| 63 | + /// Mixnet public ID of the entry gateway. |
| 64 | + #[clap(long, alias = "entry-id")] |
| 65 | + pub(crate) entry_gateway_id: Option<String>, |
| 66 | + |
| 67 | + /// Auto-select entry gateway by country ISO. |
| 68 | + #[clap(long, alias = "entry-country")] |
| 69 | + pub(crate) entry_gateway_country: Option<String>, |
| 70 | + |
| 71 | + /// Auto-select entry gateway by latency |
| 72 | + #[clap(long, alias = "entry-fastest")] |
| 73 | + pub(crate) entry_gateway_low_latency: bool, |
| 74 | + |
| 75 | + /// Auto-select entry gateway randomly. |
| 76 | + #[clap(long, alias = "entry-random")] |
| 77 | + pub(crate) entry_gateway_random: bool, |
| 78 | +} |
| 79 | + |
| 80 | +#[derive(Args)] |
| 81 | +#[group(multiple = false)] |
| 82 | +pub(crate) struct CliExit { |
| 83 | + /// Mixnet recipient address. |
| 84 | + #[clap(long, alias = "exit-address")] |
| 85 | + pub(crate) exit_router_address: Option<String>, |
| 86 | + |
| 87 | + /// Mixnet public ID of the exit gateway. |
| 88 | + #[clap(long, alias = "exit-id")] |
| 89 | + pub(crate) exit_gateway_id: Option<String>, |
| 90 | + |
| 91 | + /// Auto-select exit gateway by country ISO. |
| 92 | + #[clap(long, alias = "exit-country")] |
| 93 | + pub(crate) exit_gateway_country: Option<String>, |
| 94 | + |
| 95 | + /// Auto-select exit gateway randomly. |
| 96 | + #[clap(long, alias = "exit-random")] |
| 97 | + pub(crate) exit_gateway_random: bool, |
| 98 | +} |
| 99 | + |
| 100 | +#[derive(Args)] |
| 101 | +pub(crate) struct ImportCredentialArgs { |
| 102 | + #[command(flatten)] |
| 103 | + pub(crate) credential_type: ImportCredentialType, |
| 104 | + |
| 105 | + // currently hidden as there exists only a single serialization standard |
| 106 | + #[arg(long, hide = true)] |
| 107 | + pub(crate) version: Option<u8>, |
| 108 | +} |
| 109 | + |
| 110 | +#[derive(Args, Clone)] |
| 111 | +#[group(required = true, multiple = false)] |
| 112 | +pub(crate) struct ImportCredentialType { |
| 113 | + /// Credential encoded using base58. |
| 114 | + #[arg(long)] |
| 115 | + pub(crate) credential_data: Option<String>, |
| 116 | + |
| 117 | + /// Path to the credential file. |
| 118 | + #[arg(long)] |
| 119 | + pub(crate) credential_path: Option<PathBuf>, |
| 120 | +} |
| 121 | + |
| 122 | +// Workaround until clap supports enums for ArgGroups |
| 123 | +pub(crate) enum ImportCredentialTypeEnum { |
| 124 | + Path(PathBuf), |
| 125 | + Data(String), |
| 126 | +} |
| 127 | + |
| 128 | +impl From<ImportCredentialType> for ImportCredentialTypeEnum { |
| 129 | + fn from(ict: ImportCredentialType) -> Self { |
| 130 | + match (ict.credential_data, ict.credential_path) { |
| 131 | + (Some(data), None) => ImportCredentialTypeEnum::Data(data), |
| 132 | + (None, Some(path)) => ImportCredentialTypeEnum::Path(path), |
| 133 | + _ => unreachable!(), |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +pub(crate) fn parse_entry_point(args: &ConnectArgs) -> Result<Option<EntryPoint>> { |
| 139 | + if let Some(ref entry_gateway_id) = args.entry.entry_gateway_id { |
| 140 | + Ok(Some(EntryPoint::Gateway { |
| 141 | + identity: NodeIdentity::from_base58_string(entry_gateway_id.clone()) |
| 142 | + .map_err(|_| anyhow!("Failed to parse gateway id"))?, |
| 143 | + })) |
| 144 | + } else if let Some(ref entry_gateway_country) = args.entry.entry_gateway_country { |
| 145 | + Ok(Some(EntryPoint::Location { |
| 146 | + location: entry_gateway_country.clone(), |
| 147 | + })) |
| 148 | + } else if args.entry.entry_gateway_low_latency { |
| 149 | + Ok(Some(EntryPoint::RandomLowLatency)) |
| 150 | + } else if args.entry.entry_gateway_random { |
| 151 | + Ok(Some(EntryPoint::Random)) |
| 152 | + } else { |
| 153 | + Ok(None) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +pub(crate) fn parse_exit_point(args: &ConnectArgs) -> Result<Option<ExitPoint>> { |
| 158 | + if let Some(ref exit_router_address) = args.exit.exit_router_address { |
| 159 | + Ok(Some(ExitPoint::Address { |
| 160 | + address: Recipient::try_from_base58_string(exit_router_address.clone()) |
| 161 | + .map_err(|_| anyhow!("Failed to parse exit node address"))?, |
| 162 | + })) |
| 163 | + } else if let Some(ref exit_router_id) = args.exit.exit_gateway_id { |
| 164 | + Ok(Some(ExitPoint::Gateway { |
| 165 | + identity: NodeIdentity::from_base58_string(exit_router_id.clone()) |
| 166 | + .map_err(|_| anyhow!("Failed to parse gateway id"))?, |
| 167 | + })) |
| 168 | + } else if let Some(ref exit_gateway_country) = args.exit.exit_gateway_country { |
| 169 | + Ok(Some(ExitPoint::Location { |
| 170 | + location: exit_gateway_country.clone(), |
| 171 | + })) |
| 172 | + } else if args.exit.exit_gateway_random { |
| 173 | + Ok(Some(ExitPoint::Random)) |
| 174 | + } else { |
| 175 | + Ok(None) |
| 176 | + } |
| 177 | +} |
0 commit comments