forked from cloudflare/boringtun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rs
186 lines (167 loc) · 6.33 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2019 Cloudflare, Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
use boringtun::device::drop_privileges::drop_privileges;
use boringtun::device::{DeviceConfig, DeviceHandle};
use clap::{Arg, Command};
use daemonize::Daemonize;
use std::fs::File;
use std::os::unix::net::UnixDatagram;
use std::process::exit;
use std::sync::Arc;
use tracing::Level;
fn check_tun_name(_v: String) -> Result<(), String> {
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
if boringtun::device::tun::parse_utun_name(&_v).is_ok() {
Ok(())
} else {
Err("Tunnel name must have the format 'utun[0-9]+', use 'utun' for automatic assignment".to_owned())
}
}
#[cfg(not(target_os = "macos"))]
{
Ok(())
}
}
fn main() {
let matches = Command::new("boringtun")
.version(env!("CARGO_PKG_VERSION"))
.author("Vlad Krasnov <vlad@cloudflare.com>")
.args(&[
Arg::new("INTERFACE_NAME")
.required(true)
.takes_value(true)
.validator(|tunname| check_tun_name(tunname.to_string()))
.help("The name of the created interface"),
Arg::new("foreground")
.long("foreground")
.short('f')
.help("Run and log in the foreground"),
Arg::new("threads")
.takes_value(true)
.long("threads")
.short('t')
.env("WG_THREADS")
.help("Number of OS threads to use")
.default_value("4"),
Arg::new("verbosity")
.takes_value(true)
.long("verbosity")
.short('v')
.env("WG_LOG_LEVEL")
.possible_values(["error", "info", "debug", "trace"])
.help("Log verbosity")
.default_value("error"),
Arg::new("uapi-fd")
.long("uapi-fd")
.env("WG_UAPI_FD")
.help("File descriptor for the user API")
.default_value("-1"),
Arg::new("tun-fd")
.long("tun-fd")
.env("WG_TUN_FD")
.help("File descriptor for an already-existing TUN device")
.default_value("-1"),
Arg::new("log")
.takes_value(true)
.long("log")
.short('l')
.env("WG_LOG_FILE")
.help("Log file")
.default_value("/tmp/boringtun.out"),
Arg::new("disable-drop-privileges")
.long("disable-drop-privileges")
.env("WG_SUDO")
.help("Do not drop sudo privileges"),
Arg::new("disable-connected-udp")
.long("disable-connected-udp")
.help("Disable connected UDP sockets to each peer"),
#[cfg(target_os = "linux")]
Arg::new("disable-multi-queue")
.long("disable-multi-queue")
.help("Disable using multiple queues for the tunnel interface"),
])
.get_matches();
let background = !matches.is_present("foreground");
#[cfg(target_os = "linux")]
let uapi_fd: i32 = matches.value_of_t("uapi-fd").unwrap_or_else(|e| e.exit());
let tun_fd: isize = matches.value_of_t("tun-fd").unwrap_or_else(|e| e.exit());
let mut tun_name = matches.value_of("INTERFACE_NAME").unwrap();
if tun_fd >= 0 {
tun_name = matches.value_of("tun-fd").unwrap();
}
let n_threads: usize = matches.value_of_t("threads").unwrap_or_else(|e| e.exit());
let log_level: Level = matches.value_of_t("verbosity").unwrap_or_else(|e| e.exit());
// Create a socketpair to communicate between forked processes
let (sock1, sock2) = UnixDatagram::pair().unwrap();
let _ = sock1.set_nonblocking(true);
let _guard;
if background {
let log = matches.value_of("log").unwrap();
let log_file =
File::create(log).unwrap_or_else(|_| panic!("Could not create log file {}", log));
let (non_blocking, guard) = tracing_appender::non_blocking(log_file);
_guard = guard;
tracing_subscriber::fmt()
.with_max_level(log_level)
.with_writer(non_blocking)
.with_ansi(false)
.init();
let daemonize = Daemonize::new()
.working_directory("/tmp")
.exit_action(move || {
let mut b = [0u8; 1];
if sock2.recv(&mut b).is_ok() && b[0] == 1 {
println!("BoringTun started successfully");
} else {
eprintln!("BoringTun failed to start");
exit(1);
};
});
match daemonize.start() {
Ok(_) => tracing::info!("BoringTun started successfully"),
Err(e) => {
tracing::error!(error = ?e);
exit(1);
}
}
} else {
tracing_subscriber::fmt()
.pretty()
.with_max_level(log_level)
.init();
}
let config = DeviceConfig {
n_threads,
#[cfg(target_os = "linux")]
uapi_fd,
use_connected_socket: !matches.is_present("disable-connected-udp"),
#[cfg(target_os = "linux")]
use_multi_queue: !matches.is_present("disable-multi-queue"),
open_uapi_socket: false,
protect: Arc::new(boringtun::device::MakeExternalBoringtunNoop),
firewall_process_inbound_callback: None,
firewall_process_outbound_callback: None,
};
let mut device_handle: DeviceHandle = match DeviceHandle::new(tun_name, config) {
Ok(d) => d,
Err(e) => {
// Notify parent that tunnel initialization failed
tracing::error!(message = "Failed to initialize tunnel", error=?e);
sock1.send(&[0]).unwrap();
exit(1);
}
};
if !matches.is_present("disable-drop-privileges") {
if let Err(e) = drop_privileges() {
tracing::error!(message = "Failed to drop privileges", error = ?e);
sock1.send(&[0]).unwrap();
exit(1);
}
}
// Notify parent that tunnel initialization succeeded
sock1.send(&[1]).unwrap();
drop(sock1);
tracing::info!("BoringTun started successfully");
device_handle.wait();
}