Skip to content

Commit

Permalink
ensure_root_privileges
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Jan 27, 2024
1 parent 0dd0cea commit 4fb9c10
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() {
#[cfg(target_os = "linux")]
config.platform_config(|config| {
config.packet_information(true);
config.ask_permission(true);
config.ensure_root_privileges(true);
});

let mut dev = tun2::create(&config).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/ping-tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn main_entry(
#[cfg(target_os = "linux")]
config.platform_config(|config| {
config.packet_information(true);
config.ask_permission(true);
config.ensure_root_privileges(true);
});

#[cfg(target_os = "windows")]
Expand Down
2 changes: 1 addition & 1 deletion examples/read-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn main_entry(
#[cfg(target_os = "linux")]
config.platform_config(|config| {
config.packet_information(true);
config.ask_permission(true);
config.ensure_root_privileges(true);
});

let mut dev = tun2::create_as_async(&config)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main_entry(quit: Receiver<()>) -> Result<(), Box<dyn std::error::Error + Send
#[cfg(target_os = "linux")]
config.platform_config(|config| {
config.packet_information(true);
config.ask_permission(true);
config.ensure_root_privileges(true);
});

let mut dev = tun2::create(&config)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main_entry(quit: Receiver<()>) -> Result<(), Box<dyn std::error::Error + Send
#[cfg(target_os = "linux")]
config.platform_config(|config| {
config.packet_information(true);
config.ask_permission(true);
config.ensure_root_privileges(true);
});

let dev = tun2::create(&config)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main_entry(quit: Receiver<()>) -> Result<(), Box<dyn std::error::Error + Send
#[cfg(target_os = "linux")]
config.platform_config(|config| {
config.packet_information(true);
config.ask_permission(true);
config.ensure_root_privileges(true);
});

let mut dev = tun2::create(&config)?;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/linux/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Device {
}
};

if config.platform_config.ask_permission {
if config.platform_config.ensure_root_privileges {
device.configure(config)?;
}

Expand Down
10 changes: 5 additions & 5 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ pub struct PlatformConfig {
/// switch of Enable/Disable packet information for network driver
pub(crate) packet_information: bool,
/// root privileges required or not
pub(crate) ask_permission: bool,
pub(crate) ensure_root_privileges: bool,
}

impl Default for PlatformConfig {
fn default() -> Self {
PlatformConfig {
packet_information: false,
ask_permission: true,
ensure_root_privileges: true,
}
}
}
Expand All @@ -48,10 +48,10 @@ impl PlatformConfig {
self
}

/// Indicated if tun2 running in root privilege,
/// Indicated whether tun2 running in root privilege,
/// since some operations need it such as assigning IP/netmask/destination etc.
pub fn ask_permission(&mut self, value: bool) -> &mut Self {
self.ask_permission = value;
pub fn ensure_root_privileges(&mut self, value: bool) -> &mut Self {
self.ensure_root_privileges = value;
self
}
}
Expand Down

0 comments on commit 4fb9c10

Please sign in to comment.