Skip to content

Commit 8f62866

Browse files
authored
Merge pull request #326 from nymtech/jon/pass-entry-gateway-in-connect
Support connect arguments
2 parents 3b481ee + 594e03a commit 8f62866

File tree

14 files changed

+555
-155
lines changed

14 files changed

+555
-155
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/nym-gateway-directory/src/entries/entry_point.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub enum EntryPoint {
1919
// An explicit entry gateway identity.
2020
Gateway { identity: NodeIdentity },
2121
// Select a random entry gateway in a specific location.
22-
// NOTE: Consider using a crate with strongly typed country codes instead of strings
2322
Location { location: String },
2423
// Select a random entry gateway but increasey probability of selecting a low latency gateway
2524
// as determined by ping times.

nym-vpn-cli/src/commands.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ pub(crate) struct CliExit {
129129
#[clap(long, alias = "exit-address")]
130130
pub(crate) exit_router_address: Option<String>,
131131

132+
/// Mixnet public ID of the exit gateway.
132133
#[clap(long, alias = "exit-id")]
133134
pub(crate) exit_gateway_id: Option<String>,
134135

135-
/// Mixnet recipient address.
136+
/// Auto-select exit gateway by country ISO.
136137
#[clap(long, alias = "exit-country")]
137138
pub(crate) exit_gateway_country: Option<String>,
138139
}

nym-vpnc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tokio = { workspace = true, features = ["macros", "rt-multi-thread"]}
2020
tonic.workspace = true
2121
tower.workspace = true
2222

23+
nym-gateway-directory = { path = "../crates/nym-gateway-directory" }
2324
nym-vpn-proto = { path = "../crates/nym-vpn-proto" }
2425

2526
[build-dependencies]

nym-vpnc/src/cli.rs

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
}

nym-vpnc/src/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2024 - Nym Technologies SA <[email protected]>
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::path::{Path, PathBuf};
5+
6+
pub(crate) fn get_socket_path() -> PathBuf {
7+
Path::new("/var/run/nym-vpn.sock").to_path_buf()
8+
}
9+
10+
pub(crate) fn default_endpoint() -> String {
11+
"http://[::1]:53181".to_string()
12+
}

0 commit comments

Comments
 (0)