diff --git a/.vscode/settings.json b/.vscode/settings.json index fb0827b..9eb94d8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,4 @@ { - // "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu", + "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu", // "rust-analyzer.cargo.features": ["tun"] } \ No newline at end of file diff --git a/src/bin/punchnet/main.rs b/src/bin/punchnet/main.rs index c507824..cb2253a 100755 --- a/src/bin/punchnet/main.rs +++ b/src/bin/punchnet/main.rs @@ -2,19 +2,19 @@ mod api; mod local_udp_info; use std::fs; -use std::fs::File; use std::fs::OpenOptions; -use std::io::stdout; use std::process; use std::env; -use std::time::Duration; use clap::Parser; + +#[cfg(not(target_os = "windows"))] use daemonize::Daemonize; -use etherparse::icmpv6::CODE_PARAM_PROBLEM_UNRECOG_NEXT_HEADER_BY_INTERMEDIATE_NODE; + use futures_util::io; -use libc::SIGTERM; -use libc::kill; + +#[cfg(not(target_os = "windows"))] +use libc::{SIGTERM, kill}; use punchnet::CachedLoginInfo; use punchnet::CommandLineInput2; use punchnet::Commands; @@ -23,26 +23,20 @@ use punchnet::get_access_token; use punchnet::get_base_dir; use punchnet::get_edge; use punchnet::ip_string_to_u32; -use punchnet::mod_hostname; use punchnet::restore_dns; use punchnet::run_sdlan; use punchnet::set_access_token; use punchnet::set_base_dir; use punchnet::CommandLine; -use punchnet::CommandLineInput; use sdlan_sn_rs::log; use sdlan_sn_rs::utils::Mac; use sdlan_sn_rs::utils::Result; use sdlan_sn_rs::utils::create_or_load_uuid; -use tokio::io::AsyncWriteExt; -use tokio::net::UdpSocket; use tokio::runtime::Runtime; -use tokio::time::sleep; use tracing::error; use std::net::ToSocketAddrs; -use structopt::StructOpt; use crate::api::ConnectData; use crate::api::ConnectResponse; @@ -273,15 +267,27 @@ async fn daemonize_me( } +#[cfg(target_os = "windows")] +const SYSTEM: &'static str = "windows"; +#[cfg(target_os = "windows")] +const DEFAULT_BASE_DIR: &'static str = "."; + + +#[cfg(not(target_os = "windows"))] +const SYSTEM: &'static str = "linux"; +#[cfg(target_os = "linux")] +const DEFAULT_BASE_DIR: &'static str = "/usr/local/punchnet"; + + fn main() { - set_base_dir("/usr/local/punchnet"); + set_base_dir(DEFAULT_BASE_DIR); // let _guard = log::init_log(&format!("{}/.output", get_base_dir())); let client_id = create_or_load_uuid(&format!("{}/.id", get_base_dir()), None).unwrap(); let mac = create_or_load_mac(); - let system = "linux"; - let version = "1.0.0"; + let system = SYSTEM; + let version = env!("CARGO_PKG_VERSION"); // let cmd = CommandLineInput::from_args(); let cmd = CommandLineInput2::parse(); @@ -307,6 +313,8 @@ fn main() { }); process::exit(0); } + + #[cfg(not(target_os = "windows"))] Commands::Stop => { match fs::read_to_string("/tmp/punchnet.pid") { Ok(content) => { @@ -348,6 +356,7 @@ fn main() { let should_daemonize = true; + #[cfg(not(target_os = "windows"))] if should_daemonize { let out = OpenOptions::new() .create(true) @@ -377,11 +386,13 @@ fn main() { eprintln!("failed to daemonize"); } } - } else { run_it(cmd, client_id, mac, system, version); } + #[cfg(target_os = "windows")] + run_it(cmd, client_id, mac, system, version); + } fn run_it(cmd: CommandLineInput2, client_id: String, mac: Mac, system: &str, version: &str) { diff --git a/src/network/packet.rs b/src/network/packet.rs index 2f8c02e..0c7ba0e 100755 --- a/src/network/packet.rs +++ b/src/network/packet.rs @@ -21,7 +21,7 @@ use sdlan_sn_rs::{ config::{AF_INET, AF_INET6}, peer::{is_sdlan_sock_equal, SdlanSock, V6Info}, utils::{ - aes_decrypt, get_current_timestamp, get_sdlan_sock_from_socketaddr, is_multi_broadcast, + get_current_timestamp, get_sdlan_sock_from_socketaddr, is_multi_broadcast, Mac, Result, SDLanError, }, }; diff --git a/src/network/tun_linux.rs b/src/network/tun_linux.rs index 52ad658..398e7b1 100755 --- a/src/network/tun_linux.rs +++ b/src/network/tun_linux.rs @@ -321,7 +321,7 @@ impl TunTapPacketHandler for Iface { #[cfg(feature = "tun")] impl TunTapPacketHandler for Iface { - async fn handle_packet_from_net(&self, data: &[u8], key: &[u8]) -> std::io::Result<()> { + async fn handle_packet_from_net(&self, data: &[u8]) -> std::io::Result<()> { debug!("in tun mode"); // got layer 2 frame @@ -401,7 +401,8 @@ impl TunTapPacketHandler for Iface { [((self_ip >> 16) & 0xffff) as u16, (self_ip & 0xffff) as u16]; let data = arp.marshal_to_bytes(); - let Ok(encrypted) = aes_encrypt(key, &data) else { + // let Ok(encrypted) = aes_encrypt(key, &data) else { + let Ok(encrypted) = edge.encryptor.read().unwrap().encrypt(&data) else { error!("failed to encrypt arp reply"); return Ok(()); }; @@ -497,7 +498,6 @@ impl TunTapPacketHandler for Iface { async fn handle_packet_from_device( &self, data: Vec, - encrypt_key: &[u8], ) -> std::io::Result<()> { use etherparse::IpHeaders; @@ -560,7 +560,8 @@ impl TunTapPacketHandler for Iface { ); let arp_msg = generate_arp_request(src_mac, ip, eee.device_config.get_ip()); - let Ok(encrypted) = aes_encrypt(&encrypt_key, &arp_msg) else { + // let Ok(encrypted) = aes_encrypt(&encrypt_key, &arp_msg) else { + let Ok(encrypted) = eee.encryptor.read().unwrap().encrypt(&arp_msg) else { error!("failed to encrypt arp request"); return Ok(()); }; @@ -600,7 +601,8 @@ impl TunTapPacketHandler for Iface { let pkt_size = packet.len(); // println!("sending data with mac"); - let Ok(encrypted) = aes_encrypt(&encrypt_key, &packet) else { + // let Ok(encrypted) = aes_encrypt(&encrypt_key, &packet) else { + let Ok(encrypted) = eee.encryptor.read().unwrap().encrypt(&packet) else { error!("failed to encrypt packet request"); return Ok(()); }; diff --git a/src/network/tun_win.rs b/src/network/tun_win.rs index 49d6bf1..765d6fe 100755 --- a/src/network/tun_win.rs +++ b/src/network/tun_win.rs @@ -133,7 +133,7 @@ impl Iface { } impl TunTapPacketHandler for Iface { - async fn handle_packet_from_net(&self, data: &[u8], key: &[u8]) -> std::io::Result<()> { + async fn handle_packet_from_net(&self, data: &[u8]) -> std::io::Result<()> { // got layer 2 frame match Ethernet2Header::from_slice(&data) { Ok((hdr, rest)) => { @@ -199,7 +199,10 @@ impl TunTapPacketHandler for Iface { [((self_ip >> 16) & 0xffff) as u16, (self_ip & 0xffff) as u16]; let data = arp.marshal_to_bytes(); - let Ok(encrypted) = aes_encrypt(key, &data) else { + + + // let Ok(encrypted) = aes_encrypt(key, &data) else { + let Ok(encrypted) = edge.encryptor.read().unwrap().encrypt(&data) else { error!("failed to encrypt arp reply"); return Ok(()); }; @@ -285,7 +288,7 @@ impl TunTapPacketHandler for Iface { async fn handle_packet_from_device( &self, data: Vec, - encrypt_key: &[u8], + // encrypt_key: &[u8], ) -> std::io::Result<()> { let eee = get_edge(); @@ -336,7 +339,9 @@ impl TunTapPacketHandler for Iface { ); let arp_msg = generate_arp_request(src_mac, ip, eee.device_config.get_ip()); - let Ok(encrypted) = aes_encrypt(&encrypt_key, &arp_msg) else { + + let Ok(encrypted) = eee.encryptor.read().unwrap().encrypt(&arp_msg) else { + // let Ok(encrypted) = aes_encrypt(&encrypt_key, &arp_msg) else { error!("failed to encrypt arp request"); return Ok(()); }; @@ -380,7 +385,8 @@ impl TunTapPacketHandler for Iface { let pkt_size = packet.len(); // println!("sending data with mac"); - let Ok(encrypted) = aes_encrypt(&encrypt_key, &packet) else { + // let Ok(encrypted) = aes_encrypt(&encrypt_key, &packet) else { + let Ok(encrypted) = eee.encryptor.read().unwrap().encrypt(&packet) else { error!("failed to encrypt packet request"); return Ok(()); };