Skip to content

Commit 97761f2

Browse files
authored
Merge pull request #119 from jamesmcm/customname
Use hash for custom provider name
2 parents affff5e + 9205267 commit 97761f2

File tree

5 files changed

+112
-82
lines changed

5 files changed

+112
-82
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "vopono"
33
description = "Launch applications via VPN tunnels using temporary network namespaces"
4-
version = "0.8.8"
4+
version = "0.8.9"
55
authors = ["James McMurray <[email protected]>"]
66
edition = "2018"
77
license = "GPL-3.0-or-later"
@@ -34,7 +34,7 @@ chrono = "0.4"
3434
compound_duration = "1"
3535
ipnet = {version = "2", features = ["serde"]}
3636
reqwest = {default-features = false, version = "0.11", features = ["blocking", "json", "rustls-tls"]}
37-
sysinfo = "0.21"
37+
sysinfo = "0.22"
3838
base64 = "0.13"
3939
x25519-dalek = "1"
4040
strum = "0.23"
@@ -45,6 +45,7 @@ webbrowser = "0.5"
4545
basic_tcp_proxy = "0.3"
4646
signal-hook = "0.3"
4747
config = "0.11"
48+
bs58 = "0.4"
4849

4950
[package.metadata.rpm]
5051
package = "vopono"

src/exec.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,10 @@ pub fn exec(command: ExecCommand) -> anyhow::Result<()> {
123123
.protocol
124124
.unwrap_or_else(|| get_config_file_protocol(path));
125125
provider = VpnProvider::Custom;
126-
// Could hash filename with CRC and use base64 but chars are limited
127-
server_name = String::from(
128-
&path
129-
.as_path()
130-
.file_name()
131-
.unwrap()
132-
.to_str()
133-
.unwrap()
134-
.chars()
135-
.filter(|&x| x != ' ' && x != '-')
136-
.collect::<String>()[0..4],
137-
);
126+
// Encode filename with base58 so we can fit it within 16 chars for the veth pair name
127+
let sname = bs58::encode(&path.to_str().unwrap().to_string()).into_string();
128+
129+
server_name = sname[0..std::cmp::min(11, sname.len())].to_string();
138130
} else {
139131
// Get server and provider
140132
provider = command
@@ -202,7 +194,7 @@ pub fn exec(command: ExecCommand) -> anyhow::Result<()> {
202194
}
203195

204196
let alias = match provider {
205-
VpnProvider::Custom => "custom".to_string(),
197+
VpnProvider::Custom => "c".to_string(),
206198
_ => provider.get_dyn_provider().alias(),
207199
};
208200

@@ -216,7 +208,7 @@ pub fn exec(command: ExecCommand) -> anyhow::Result<()> {
216208
get_active_interfaces()?
217209
.into_iter()
218210
.next()
219-
.ok_or_else(|| anyhow!("No active network interface"))?,
211+
.ok_or_else(|| anyhow!("No active network interface - consider overriding network interface selection with -i argument"))?,
220212
)?),
221213
}?;
222214
debug!("Interface: {}", &interface.name);

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![allow(clippy::upper_case_acronyms)]
2+
#![allow(clippy::large_enum_variant)]
3+
#![allow(dead_code)]
24

35
mod application_wrapper;
46
mod args;

src/network_interface.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::{anyhow, Context};
2-
use log::debug;
2+
use log::{debug, warn};
33
use serde::{Deserialize, Serialize};
44
use std::process::Command;
55
use std::str::FromStr;
@@ -19,15 +19,22 @@ impl FromStr for NetworkInterface {
1919
type Err = anyhow::Error;
2020

2121
fn from_str(s: &str) -> anyhow::Result<Self> {
22-
let interfaces = get_active_interfaces()?;
23-
24-
if interfaces.iter().any(|x| x == s) {
25-
Ok(Self {
26-
name: String::from(s),
27-
})
28-
} else {
29-
Err(anyhow!("{} is not an active interface!", s))
22+
let interfaces = get_active_interfaces();
23+
24+
let cond = interfaces.map(|is| is.iter().any(|x| x == s));
25+
26+
match cond {
27+
Ok(true) => {}
28+
_ => {
29+
warn!(
30+
"{} may not be an active network interface, using anyway since manually set",
31+
s
32+
);
33+
}
3034
}
35+
Ok(Self {
36+
name: String::from(s),
37+
})
3138
}
3239
}
3340

@@ -51,6 +58,6 @@ pub fn get_active_interfaces() -> anyhow::Result<Vec<String>> {
5158
if !out.is_empty() {
5259
Ok(out)
5360
} else {
54-
Err(anyhow!("Failed to get active network interface"))
61+
Err(anyhow!("Failed to get active network interface - consider using -i argument to override network interface"))
5562
}
5663
}

src/util/mod.rs

Lines changed: 84 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,63 +18,88 @@ use std::net::Ipv4Addr;
1818
use std::path::{Path, PathBuf};
1919
use std::process::Command;
2020
use std::str::FromStr;
21-
use sysinfo::{ProcessExt, RefreshKind, System, SystemExt};
21+
use sysinfo::{ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt};
2222
use users::{get_current_uid, get_user_by_uid};
2323
use walkdir::WalkDir;
2424

2525
pub fn config_dir() -> anyhow::Result<PathBuf> {
26-
let mut pathbuf = PathBuf::new();
27-
let _res: () = if let Ok(home) = std::env::var("HOME") {
28-
let confpath = format!("{}/.config", home);
29-
let path = Path::new(&confpath);
30-
debug!(
31-
"Using config dir from $HOME config: {}",
32-
path.to_string_lossy()
33-
);
34-
if path.exists() {
35-
pathbuf.push(path);
36-
Ok(())
37-
} else {
38-
Err(anyhow!("Could not find valid config directory!"))
39-
}
40-
} else if let Ok(user) = std::env::var("SUDO_USER") {
41-
// TODO: DRY
42-
let confpath = format!("/home/{}/.config", user);
43-
let path = Path::new(&confpath);
44-
debug!(
45-
"Using config dir from $SUDO_USER config: {}",
46-
path.to_string_lossy()
47-
);
48-
if path.exists() {
49-
pathbuf.push(path);
50-
Ok(())
51-
} else {
52-
Err(anyhow!("Could not find valid config directory!"))
53-
}
54-
} else if let Some(base_dirs) = BaseDirs::new() {
55-
debug!(
56-
"Using config dir from XDG dirs: {}",
57-
base_dirs.config_dir().to_string_lossy()
58-
);
59-
pathbuf.push(base_dirs.config_dir());
60-
Ok(())
61-
} else if let Some(user) = get_user_by_uid(get_current_uid()) {
62-
let confpath = format!("/home/{}/.config", user.name().to_str().unwrap());
63-
let path = Path::new(&confpath);
64-
debug!(
65-
"Using config dir from current user config: {}",
66-
path.to_string_lossy()
67-
);
68-
if path.exists() {
69-
pathbuf.push(path);
70-
Ok(())
71-
} else {
72-
Err(anyhow!("Could not find valid config directory!"))
73-
}
74-
} else {
75-
Err(anyhow!("Could not find valid config directory!"))
76-
}?;
77-
Ok(pathbuf)
26+
let path: Option<PathBuf> = None
27+
.or_else(|| {
28+
if let Ok(home) = std::env::var("HOME") {
29+
let confpath = format!("{}/.config", home);
30+
let path = Path::new(&confpath);
31+
debug!(
32+
"Using config dir from $HOME config: {}",
33+
path.to_string_lossy()
34+
);
35+
if path.exists() {
36+
// Work-around for case when root $HOME is set but user's is not
37+
// It seems we cannot distinguish these cases
38+
if path.to_string_lossy().contains("/root") {
39+
None
40+
} else {
41+
Some(path.into())
42+
}
43+
} else {
44+
None
45+
}
46+
} else {
47+
None
48+
}
49+
})
50+
.or_else(|| {
51+
if let Ok(user) = std::env::var("SUDO_USER") {
52+
// TODO: DRY
53+
let confpath = format!("/home/{}/.config", user);
54+
let path = Path::new(&confpath);
55+
debug!(
56+
"Using config dir from $SUDO_USER config: {}",
57+
path.to_string_lossy()
58+
);
59+
if path.exists() {
60+
Some(path.into())
61+
} else {
62+
None
63+
}
64+
} else {
65+
None
66+
}
67+
})
68+
.or_else(|| {
69+
if let Some(base_dirs) = BaseDirs::new() {
70+
debug!(
71+
"Using config dir from XDG dirs: {}",
72+
base_dirs.config_dir().to_string_lossy()
73+
);
74+
Some(base_dirs.config_dir().into())
75+
} else {
76+
None
77+
}
78+
})
79+
.or_else(|| {
80+
if let Some(user) = get_user_by_uid(get_current_uid()) {
81+
// Handles case when run as root directly
82+
let confpath = if get_current_uid() == 0 {
83+
"/root/.config".to_string()
84+
} else {
85+
format!("/home/{}/.config", user.name().to_str().unwrap())
86+
};
87+
let path = Path::new(&confpath);
88+
debug!(
89+
"Using config dir from current user config: {}",
90+
path.to_string_lossy()
91+
);
92+
if path.exists() {
93+
Some(path.into())
94+
} else {
95+
None
96+
}
97+
} else {
98+
None
99+
}
100+
});
101+
102+
path.ok_or_else(|| anyhow!("Could not find valid config directory!"))
78103
}
79104

80105
pub fn vopono_dir() -> anyhow::Result<PathBuf> {
@@ -161,17 +186,20 @@ pub fn get_existing_namespaces() -> anyhow::Result<Vec<String>> {
161186
}
162187

163188
pub fn check_process_running(pid: u32) -> bool {
164-
let s = System::new_with_specifics(RefreshKind::new().with_processes());
189+
let s =
190+
System::new_with_specifics(RefreshKind::new().with_processes(ProcessRefreshKind::new()));
165191
s.process(pid as i32).is_some()
166192
}
167193

168194
pub fn get_all_running_pids() -> Vec<u32> {
169-
let s = System::new_with_specifics(RefreshKind::new().with_processes());
195+
let s =
196+
System::new_with_specifics(RefreshKind::new().with_processes(ProcessRefreshKind::new()));
170197
s.processes().keys().map(|x| *x as u32).collect()
171198
}
172199

173200
pub fn get_all_running_process_names() -> Vec<String> {
174-
let s = System::new_with_specifics(RefreshKind::new().with_processes());
201+
let s =
202+
System::new_with_specifics(RefreshKind::new().with_processes(ProcessRefreshKind::new()));
175203
s.processes()
176204
.values()
177205
.map(|x| x.name().to_string())

0 commit comments

Comments
 (0)