Skip to content

Commit 139bda6

Browse files
authored
chore: Fix cargo audit warnings up to but excluding behaviour changes (#456)
* Fix libgit2-sys vuln by upgrading vergen * Remove atty dependency (unmaintained) * Update clap to version 4 * Remove rustls-pemfile dependency (unmaintained) * Fix rustls issues Restore rustls-native-certs and upgrade version Switch to tokio_rustlrs pki_types * Run cargo fmt * Test single threading tests * Fix use of rustls-native-certs * Update packages to resolve vulnerabilities
1 parent 0f41051 commit 139bda6

11 files changed

Lines changed: 541 additions & 380 deletions

File tree

Cargo.lock

Lines changed: 373 additions & 274 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ client = []
3030
native-tls = ["tokio-native-tls"]
3131
rustls = [
3232
"tokio-rustls",
33-
"rustls-pemfile",
3433
"rustls-native-certs",
3534
"p12",
3635
]
@@ -87,7 +86,7 @@ codegen-units = 1
8786
[dependencies]
8887
tokio = { version = "1", features = ["full"] }
8988
bytes = { version = "1", features = ["serde"] }
90-
clap = { version = "3.0", features = ["derive"] }
89+
clap = { version = "4.0", features = ["derive"] }
9190
toml = "0.5"
9291
serde = { version = "1.0", features = ["derive"] }
9392
anyhow = "1.0"
@@ -110,7 +109,6 @@ notify = { version = "5.0.0-pre.13", optional = true }
110109
console-subscriber = { version = "0.1", optional = true, features = [
111110
"parking_lot",
112111
] }
113-
atty = "0.2"
114112
async-http-proxy = { version = "1.2", features = [
115113
"runtime-tokio",
116114
"basic-auth",
@@ -123,18 +121,15 @@ futures-core = { version = "0.3.28", optional = true }
123121
futures-sink = { version = "0.3.28", optional = true }
124122
tokio-native-tls = { version = "0.3", optional = true }
125123
tokio-rustls = { version = "0.25", optional = true }
126-
rustls-native-certs = { version = "0.7", optional = true }
127-
rustls-pemfile = { version = "2.0", optional = true }
124+
rustls-native-certs = { version = "0.8.3", optional = true }
128125
p12 = { version = "0.6.3", optional = true }
129126

130127
[target.'cfg(target_env = "musl")'.dependencies]
131128
openssl = { version = "0.10", features = ["vendored"], optional = true }
132129

133130
[build-dependencies]
134-
vergen = { version = "7.4.2", default-features = false, features = [
131+
vergen-gitcl = { version = "9.1.0", default-features = false, features = [
135132
"build",
136-
"git",
137-
"cargo",
133+
"cargo"
138134
] }
139135
anyhow = "1.0"
140-

build.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
use anyhow::Result;
2-
use vergen::{vergen, Config, SemverKind};
2+
use vergen_gitcl::{BuildBuilder, CargoBuilder, Emitter, GitclBuilder};
33

44
fn main() -> Result<()> {
5-
let mut config = Config::default();
6-
// Change the SEMVER output to the lightweight variant
7-
*config.git_mut().semver_kind_mut() = SemverKind::Lightweight;
8-
// Add a `-dirty` flag to the SEMVER output
9-
*config.git_mut().semver_dirty_mut() = Some("-dirty");
10-
// Generate the instructions
11-
if let Err(e) = vergen(config) {
12-
eprintln!("error occurred while generating instructions: {:?}", e);
13-
let mut config = Config::default();
14-
*config.git_mut().enabled_mut() = false;
15-
vergen(config)
16-
} else {
17-
Ok(())
5+
// Manually define compile time env vars that were auto
6+
// generated by earlier versions of vergen.
7+
println!(
8+
"cargo:rustc-env=VERGEN_BUILD_SEMVER={}",
9+
env!("CARGO_PKG_VERSION")
10+
);
11+
println!(
12+
"cargo:rustc-env=VERGEN_CARGO_PROFILE={}",
13+
std::env::var("PROFILE").unwrap()
14+
);
15+
16+
let build = BuildBuilder::all_build()?;
17+
let cargo = CargoBuilder::all_cargo()?;
18+
19+
let mut emitter = Emitter::default();
20+
emitter.add_instructions(&build)?;
21+
emitter.add_instructions(&cargo)?;
22+
23+
match GitclBuilder::default()
24+
.describe(true, true, None)
25+
.sha(false)
26+
.commit_timestamp(true)
27+
.branch(true)
28+
.build()
29+
{
30+
Ok(git) => {
31+
emitter.add_instructions(&git)?;
32+
}
33+
Err(e) => {
34+
println!("cargo:warning=vergen-gitcl failed, building without git info: {e}");
35+
}
1836
}
37+
38+
emitter.emit()?;
39+
Ok(())
1940
}

src/cli.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use clap::{AppSettings, ArgGroup, Parser};
1+
use clap::{ArgGroup, Parser, ValueEnum};
22
use lazy_static::lazy_static;
33

4-
#[derive(clap::ArgEnum, Clone, Debug, Copy)]
4+
#[derive(ValueEnum, Clone, Debug, Copy)]
55
pub enum KeypairType {
66
X25519,
77
X448,
88
}
99

1010
lazy_static! {
1111
static ref VERSION: &'static str =
12-
option_env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT").unwrap_or(env!("VERGEN_BUILD_SEMVER"));
12+
option_env!("VERGEN_GIT_DESCRIBE").unwrap_or(env!("VERGEN_BUILD_SEMVER"));
1313
static ref LONG_VERSION: String = format!(
1414
"
1515
Build Timestamp: {}
@@ -33,36 +33,43 @@ cargo Features: {}
3333
}
3434

3535
#[derive(Parser, Debug, Default, Clone)]
36-
#[clap(
36+
#[command(
3737
about,
38-
version(*VERSION),
39-
long_version(LONG_VERSION.as_str()),
40-
setting(AppSettings::DeriveDisplayOrder)
38+
version = *VERSION,
39+
long_version = LONG_VERSION.as_str(),
40+
// AppSettings::DeriveDisplayOrder has no direct v4 enum replacement.
41+
// clap v4 generally respects declaration order; keep or add explicit display_order if needed.
4142
)]
42-
#[clap(group(
43-
ArgGroup::new("cmds")
44-
.required(true)
45-
.args(&["CONFIG", "genkey"]),
46-
))]
43+
#[command(group(
44+
ArgGroup::new("cmds")
45+
.required(true)
46+
.args(&["CONFIG", "genkey"]),
47+
))]
4748
pub struct Cli {
4849
/// The path to the configuration file
4950
///
5051
/// Running as a client or a server is automatically determined
5152
/// according to the configuration file.
52-
#[clap(parse(from_os_str), name = "CONFIG")]
53+
#[arg(value_name = "CONFIG")]
5354
pub config_path: Option<std::path::PathBuf>,
5455

5556
/// Run as a server
56-
#[clap(long, short, group = "mode")]
57+
#[arg(long, short, group = "mode")]
5758
pub server: bool,
5859

5960
/// Run as a client
60-
#[clap(long, short, group = "mode")]
61+
#[arg(long, short, group = "mode")]
6162
pub client: bool,
6263

6364
/// Generate a keypair for the use of the noise protocol
6465
///
6566
/// The DH function to use is x25519
66-
#[clap(long, arg_enum, value_name = "CURVE")]
67+
#[arg(
68+
long,
69+
value_enum,
70+
value_name = "CURVE",
71+
num_args = 0..=1,
72+
default_missing_value = "x25519"
73+
)]
6774
pub genkey: Option<Option<KeypairType>>,
6875
}

src/client.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ async fn run_data_channel<T: Transport>(args: Arc<RunDataChannelArgs<T>>) -> Res
227227
if args.service.service_type != ServiceType::Udp {
228228
bail!("Expect UDP traffic. Please check the configuration.")
229229
}
230-
run_data_channel_for_udp::<T>(conn, &args.service.local_addr, args.service.prefer_ipv6).await?;
230+
run_data_channel_for_udp::<T>(conn, &args.service.local_addr, args.service.prefer_ipv6)
231+
.await?;
231232
}
232233
}
233234
Ok(())
@@ -255,7 +256,11 @@ async fn run_data_channel_for_tcp<T: Transport>(
255256
type UdpPortMap = Arc<RwLock<HashMap<SocketAddr, mpsc::Sender<Bytes>>>>;
256257

257258
#[instrument(skip(conn))]
258-
async fn run_data_channel_for_udp<T: Transport>(conn: T::Stream, local_addr: &str, prefer_ipv6: bool) -> Result<()> {
259+
async fn run_data_channel_for_udp<T: Transport>(
260+
conn: T::Stream,
261+
local_addr: &str,
262+
prefer_ipv6: bool,
263+
) -> Result<()> {
259264
debug!("New data channel starts forwarding");
260265

261266
let port_map: UdpPortMap = Arc::new(RwLock::new(HashMap::new()));

src/helper.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ pub fn host_port_pair(s: &str) -> Result<(&str, u16)> {
6565

6666
/// Create a UDP socket and connect to `addr`
6767
pub async fn udp_connect<A: ToSocketAddrs>(addr: A, prefer_ipv6: bool) -> Result<UdpSocket> {
68-
6968
let (socket_addr, bind_addr);
7069

7170
match prefer_ipv6 {
@@ -76,7 +75,7 @@ pub async fn udp_connect<A: ToSocketAddrs>(addr: A, prefer_ipv6: bool) -> Result
7675
SocketAddr::V4(_) => "0.0.0.0:0",
7776
SocketAddr::V6(_) => ":::0",
7877
};
79-
},
78+
}
8079
true => {
8180
let all_host_addresses: Vec<SocketAddr> = lookup_host(addr).await?.collect();
8281

@@ -85,7 +84,7 @@ pub async fn udp_connect<A: ToSocketAddrs>(addr: A, prefer_ipv6: bool) -> Result
8584
Some(socket_addr_ipv6) => {
8685
socket_addr = *socket_addr_ipv6;
8786
bind_addr = ":::0";
88-
},
87+
}
8988
None => {
9089
let socket_addr_ipv4 = all_host_addresses.iter().find(|x| x.is_ipv4());
9190
match socket_addr_ipv4 {
@@ -194,30 +193,42 @@ where
194193
Ok(())
195194
}
196195

197-
pub fn generate_proxy_protocol_header(s: &TcpStream, proxy_protocol: &str) -> Result<Vec<u8>, anyhow::Error> {
196+
pub fn generate_proxy_protocol_header(
197+
s: &TcpStream,
198+
proxy_protocol: &str,
199+
) -> Result<Vec<u8>, anyhow::Error> {
198200
let local_addr = s.local_addr()?;
199201
let remote_addr = s.peer_addr()?;
200202

201203
match proxy_protocol {
202204
"v1" => {
203205
let proto = if local_addr.is_ipv4() { "TCP4" } else { "TCP6" };
204206
let header = format!(
205-
"PROXY {} {} {} {} {}\r\n",
206-
proto,
207-
remote_addr.ip(),
208-
local_addr.ip(),
209-
remote_addr.port(),
207+
"PROXY {} {} {} {} {}\r\n",
208+
proto,
209+
remote_addr.ip(),
210+
local_addr.ip(),
211+
remote_addr.port(),
210212
local_addr.port()
211213
);
212214

213215
Ok(header.into_bytes())
214216
}
215217
"v2" => {
216-
217-
let v2sig: &[u8] = &[0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A];
218+
let v2sig: &[u8] = &[
219+
0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A,
220+
];
218221
let ver_cmd = &[0x21]; // 0x21 version 2 and PROXY command
219-
let proto = if local_addr.is_ipv4() { &[0x11] } else { &[0x21] }; // 0x11 for TCP IPv4 and 0x21 for TCP IPv6, TODO: support UNIX
220-
let addrs_length: &[u8] = if local_addr.is_ipv4() { &[0, 12] } else { &[0, 36] }; // 12 for IPv4 and 36 for IPv6, TOOD: support UNIX
222+
let proto = if local_addr.is_ipv4() {
223+
&[0x11]
224+
} else {
225+
&[0x21]
226+
}; // 0x11 for TCP IPv4 and 0x21 for TCP IPv6, TODO: support UNIX
227+
let addrs_length: &[u8] = if local_addr.is_ipv4() {
228+
&[0, 12]
229+
} else {
230+
&[0, 36]
231+
}; // 12 for IPv4 and 36 for IPv6, TOOD: support UNIX
221232
let src_addr = match remote_addr {
222233
SocketAddr::V4(v4) => v4.ip().octets().to_vec(),
223234
SocketAddr::V6(v6) => v6.ip().octets().to_vec(),
@@ -226,28 +237,25 @@ pub fn generate_proxy_protocol_header(s: &TcpStream, proxy_protocol: &str) -> Re
226237
SocketAddr::V4(v4) => v4.ip().octets().to_vec(),
227238
SocketAddr::V6(v6) => v6.ip().octets().to_vec(),
228239
};
229-
230-
let header:Vec<u8> = [
231-
v2sig,
232-
ver_cmd,
233-
proto,
240+
241+
let header: Vec<u8> = [
242+
v2sig,
243+
ver_cmd,
244+
proto,
234245
addrs_length,
235246
&src_addr,
236247
&dst_addr,
237248
&remote_addr.port().to_be_bytes(),
238-
&local_addr.port().to_be_bytes()
239-
].concat();
240-
249+
&local_addr.port().to_be_bytes(),
250+
]
251+
.concat();
252+
241253
trace!("Proxy protocol v2 header: {:02x?}", header);
242-
243-
Ok(header)
244254

245-
},
246-
_ => {
247-
Err(anyhow!("Unknown proxy protocol {}", proxy_protocol))
255+
Ok(header)
248256
}
257+
_ => Err(anyhow!("Unknown proxy protocol {}", proxy_protocol)),
249258
}
250-
251259
}
252260

253261
#[cfg(test)]

src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ use anyhow::Result;
22
use clap::Parser;
33
use rathole::{run, Cli};
44
use tokio::{signal, sync::broadcast};
5+
6+
#[cfg(not(feature = "console"))]
7+
use std::io::{self, IsTerminal};
8+
59
#[cfg(not(feature = "console"))]
610
use tracing_subscriber::EnvFilter;
711

@@ -31,14 +35,14 @@ async fn main() -> Result<()> {
3135
}
3236
#[cfg(not(feature = "console"))]
3337
{
34-
let is_atty = atty::is(atty::Stream::Stdout);
38+
let ansi = io::stdout().is_terminal();
3539

3640
let level = "info"; // if RUST_LOG not present, use `info` level
3741
tracing_subscriber::fmt()
3842
.with_env_filter(
3943
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::from(level)),
4044
)
41-
.with_ansi(is_atty)
45+
.with_ansi(ansi)
4246
.init();
4347
}
4448

src/server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,13 @@ async fn run_tcp_connection_pool<T: Transport>(
649649
let proxy_proto = proxy_protocol.clone();
650650
tokio::spawn(async move {
651651
if !proxy_proto.is_empty() {
652-
let proxy_proto_header = generate_proxy_protocol_header(&visitor, &proxy_proto);
652+
let proxy_proto_header =
653+
generate_proxy_protocol_header(&visitor, &proxy_proto);
653654
match proxy_proto_header {
654655
Ok(header) => {
655656
let _ = ch.write_all(&header).await;
656657
let _ = ch.flush().await;
657-
},
658+
}
658659
Err(e) => {
659660
error!("Failed to generate proxy protocol header: {}", e);
660661
}

0 commit comments

Comments
 (0)