Initialize Tun Interface based on platform

This commit is contained in:
Jett Chen 2023-12-12 15:48:38 +08:00
parent c346ec5b39
commit 54ec260fe3
6 changed files with 24 additions and 14 deletions

View file

@ -28,7 +28,7 @@ struct BurrowStartRequest: Codable {
let name: String? let name: String?
let no_pi: Bool let no_pi: Bool
let tun_excl: Bool let tun_excl: Bool
let seek_utun: Int? let tun_retrieve: Bool
let address: String? let address: String?
} }
struct StartOptions: Codable{ struct StartOptions: Codable{
@ -37,8 +37,8 @@ struct BurrowStartRequest: Codable {
let Start: StartOptions let Start: StartOptions
} }
func start_req_fd(id: UInt, fd: Int) -> BurrowRequest<BurrowStartRequest> { func start_req_fd(id: UInt) -> BurrowRequest<BurrowStartRequest> {
return BurrowRequest(id: id, command: BurrowStartRequest(Start: BurrowStartRequest.StartOptions(tun: BurrowStartRequest.TunOptions(name: nil, no_pi: false, tun_excl: false, seek_utun: fd, address: nil)))) return BurrowRequest(id: id, command: BurrowStartRequest(Start: BurrowStartRequest.StartOptions(tun: BurrowStartRequest.TunOptions(name: nil, no_pi: false, tun_excl: false, tun_retrieve: true, address: nil))))
} }
struct Response<T>: Decodable where T: Decodable { struct Response<T>: Decodable where T: Decodable {

View file

@ -34,7 +34,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
// let tunFd = self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int; // let tunFd = self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int;
// self.logger.info("Found File Descriptor: \(tunFd)") // self.logger.info("Found File Descriptor: \(tunFd)")
let start_command = start_req_fd(id: 1, fd: 0) let start_command = start_req_fd(id: 1)
guard let data = try await client?.request(start_command, type: Response<BurrowResult<String>>.self) guard let data = try await client?.request(start_command, type: Response<BurrowResult<String>>.self)
else { else {
throw BurrowError.cantParseResult throw BurrowError.cantParseResult

View file

@ -54,12 +54,7 @@ impl DaemonInstance {
warn!("Got start, but tun interface already up."); warn!("Got start, but tun interface already up.");
} }
RunState::Idle => { RunState::Idle => {
let raw = tun::TunInterface::retrieve().unwrap(); let tun_if = Arc::new(RwLock::new(st.tun.open()?));
debug!("TunInterface retrieved: {:?}", raw.name()?);
let retrieved = TunInterface::new(raw)?;
let tun_if = Arc::new(RwLock::new(retrieved));
// let tun_if = Arc::new(RwLock::new(TunInterface::new(st.tun.open()?)?));
debug!("Setting tun_interface"); debug!("Setting tun_interface");
self.tun_interface = Some(tun_if.clone()); self.tun_interface = Some(tun_if.clone());

View file

@ -53,9 +53,14 @@ pub async fn daemon_main() -> Result<()> {
let private_key = parse_secret_key("GNqIAOCRxjl/cicZyvkvpTklgQuUmGUIEkH7IXF/sEE=")?; let private_key = parse_secret_key("GNqIAOCRxjl/cicZyvkvpTklgQuUmGUIEkH7IXF/sEE=")?;
let public_key = parse_public_key("uy75leriJay0+oHLhRMpV+A5xAQ0hCJ+q7Ww81AOvT4=")?; let public_key = parse_public_key("uy75leriJay0+oHLhRMpV+A5xAQ0hCJ+q7Ww81AOvT4=")?;
let preshared_key = Some(parse_key("s7lx/mg+reVEMnGnqeyYOQkzD86n2+gYnx1M9ygi08k=")?); let preshared_key = Some(parse_key("s7lx/mg+reVEMnGnqeyYOQkzD86n2+gYnx1M9ygi08k=")?);
let endpoint = "wg.burrow.rs:51820".to_socket_addrs()?.next() tracing::debug!("beginning to find endpoint location");
let endpoint = "wg.burrow.rs:51820".to_socket_addrs()?
.filter(|sock| {sock.is_ipv4()})
.next()
.ok_or(anyhow!("DNS Lookup Fails!"))?; // DNS lookup under macos fails, somehow .ok_or(anyhow!("DNS Lookup Fails!"))?; // DNS lookup under macos fails, somehow
tracing::debug!("endpoint initialized: {:?}", endpoint.to_string());
let iface = Interface::new(vec![Peer { let iface = Interface::new(vec![Peer {
endpoint, endpoint,
private_key, private_key,

View file

@ -2,7 +2,7 @@ use std::io::Error;
use fehler::throws; use fehler::throws;
use super::TunInterface; use super::tokio::TunInterface;
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
#[cfg_attr( #[cfg_attr(
@ -16,6 +16,8 @@ pub struct TunOptions {
pub no_pi: bool, pub no_pi: bool,
/// (Linux) Avoid opening an existing persistant device. /// (Linux) Avoid opening an existing persistant device.
pub tun_excl: bool, pub tun_excl: bool,
/// (Apple) Retrieve the tun interface
pub tun_retrieve: bool,
/// (Linux) The IP address of the tun interface. /// (Linux) The IP address of the tun interface.
pub address: Option<String>, pub address: Option<String>,
} }
@ -47,6 +49,7 @@ impl TunOptions {
#[throws] #[throws]
pub fn open(self) -> TunInterface { pub fn open(self) -> TunInterface {
TunInterface::new_with_options(self)? let ti = super::TunInterface::new_with_options(self)?;
TunInterface::new(ti)?
} }
} }

View file

@ -35,7 +35,14 @@ impl TunInterface {
#[throws] #[throws]
#[instrument] #[instrument]
pub fn new_with_options(options: TunOptions) -> TunInterface { pub fn new_with_options(options: TunOptions) -> TunInterface {
let ti = TunInterface::connect(0)?; let ti = if options.tun_retrieve{
TunInterface::retrieve().ok_or(Error::new(
std::io::ErrorKind::NotFound,
"No tun interface found",
))?
} else {
TunInterface::connect(0)?
};
ti.configure(options)?; ti.configure(options)?;
ti ti
} }