-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from blocklessnetwork/feature/nat
Feature/nat
- Loading branch information
Showing
6 changed files
with
103 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::{ | ||
fs::OpenOptions, io::{self, Write}, process::{Command, Stdio} | ||
Check warning on line 2 in crates/wasi/src/nat/mod.rs GitHub Actions / v86-wasi (linux, linux-latest, stable, ubuntu-latest, x86_64-unknown-linux-gnu, x86_64, false)
|
||
}; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum NatError { | ||
#[error("execute command error.")] | ||
CommandError, | ||
#[error("io error. detail: {0}.")] | ||
IoError(io::Error), | ||
#[error("utf8 code error.")] | ||
Utf8CodeError, | ||
} | ||
|
||
pub(crate)struct Nat { | ||
tap_name: String, | ||
} | ||
|
||
impl Nat { | ||
pub fn new(name: &str) -> Self { | ||
Self { | ||
tap_name: name.to_string() | ||
} | ||
} | ||
} | ||
|
||
#[cfg(target_os="macos")] | ||
impl Nat { | ||
fn sysctl(enable: bool) -> Result<(), NatError> { | ||
let mut command = Command::new("sysctl"); | ||
let enable = if enable { 1 } else { 0 }; | ||
command.args(&["-w", &format!("net.inet.ip.forwarding={enable}")]); | ||
command.stdout(Stdio::piped()); | ||
command.stderr(Stdio::piped()); | ||
let mut child = command.spawn().map_err(|e| NatError::IoError(e))?; | ||
let exit_code = child.wait().map_err(|e| NatError::IoError(e))?; | ||
if exit_code.success() { | ||
Ok(()) | ||
} else { | ||
Err(NatError::CommandError) | ||
} | ||
} | ||
|
||
fn pfctl() -> Result<(), NatError> { | ||
let mut command = Command::new("pfctl"); | ||
let child = command.args( &["-f", "/etc/pf.anchors/bls-vm-nat", "-e" ]) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::piped()) | ||
.spawn().map_err(|e| NatError::IoError(e))?; | ||
let output = child.wait_with_output().map_err(|e| NatError::IoError(e))?; | ||
if output.status.success() { | ||
return Ok(()); | ||
} else { | ||
let out_string = String::from_utf8(output.stderr).map_err(|_| NatError::Utf8CodeError)?; | ||
if let Some(_) = out_string.find("pf already enabled") { | ||
return Ok(()); | ||
} | ||
} | ||
Err(NatError::CommandError) | ||
} | ||
|
||
|
||
pub fn enable(&self) -> anyhow::Result<()> { | ||
let mut pfctl = OpenOptions::new() | ||
.write(true) | ||
.create(true) | ||
.open("/etc/pf.anchors/bls-vm-nat")?; | ||
let cmd = format!("nat on en0 from {}:network to any -> (en0)\n", &self.tap_name); | ||
pfctl.write_all(cmd.as_bytes())?; | ||
Self::sysctl(true)?; | ||
Self::pfctl()?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters