diff --git a/switch-desktop/src/main.rs b/switch-desktop/src/main.rs index 4f2a2d1..6d2b828 100644 --- a/switch-desktop/src/main.rs +++ b/switch-desktop/src/main.rs @@ -4,20 +4,21 @@ use std::path::PathBuf; use clap::Parser; use console::style; - -use switch::handle::{PeerDeviceStatus, RouteType}; use switch::*; +use switch::handle::{PeerDeviceStatus, RouteType}; +#[cfg(windows)] mod command; +#[cfg(windows)] mod config; #[cfg(windows)] mod windows; #[derive(Parser, Debug)] #[command( - author = "Lu Beilin", - version, - about = "一个虚拟网络工具,启动后会获取一个ip,相同token下的设备之间可以用ip直接通信" +author = "Lu Beilin", +version, +about = "一个虚拟网络工具,启动后会获取一个ip,相同token下的设备之间可以用ip直接通信" )] struct Args { /// 32位字符 @@ -33,6 +34,7 @@ struct Args { name: Option, } +#[cfg(windows)] fn log_init_service(home: PathBuf) -> io::Result<()> { if !home.exists() { std::fs::create_dir(&home)?; @@ -115,7 +117,7 @@ fn main() { #[cfg(any(target_os = "linux", target_os = "macos"))] fn main() { - log_init(); + let _ = log_init(); let args = Args::parse(); if sudo::RunningAs::Root != sudo::check() { println!("{}", style("需要使用root权限执行...").red()); diff --git a/switch-desktop/src/windows/mod.rs b/switch-desktop/src/windows/mod.rs index 9f4c066..647fcd1 100644 --- a/switch-desktop/src/windows/mod.rs +++ b/switch-desktop/src/windows/mod.rs @@ -1,15 +1,15 @@ -use std::{io, thread}; use std::ffi::OsString; use std::path::PathBuf; use std::time::Duration; +use std::{io, thread}; use clap::Parser; use console::style; -use windows_service::Error; use windows_service::service::{ ServiceAccess, ServiceErrorControl, ServiceInfo, ServiceStartType, ServiceState, ServiceType, }; use windows_service::service_manager::{ServiceManager, ServiceManagerAccess}; +use windows_service::Error; use crate::config; @@ -18,9 +18,9 @@ mod windows_admin_check; #[derive(Parser, Debug)] #[command( -author = "Lu Beilin", -version, -about = "一个虚拟网络工具,启动后会获取一个ip,相同token下的设备之间可以用ip直接通信" + author = "Lu Beilin", + version, + about = "一个虚拟网络工具,启动后会获取一个ip,相同token下的设备之间可以用ip直接通信" )] struct Args { /// 32位字符 @@ -120,7 +120,7 @@ pub fn main0() { token.clone(), args.name.clone(), )) - .unwrap(); + .unwrap(); match start() { Ok(_) => { //需要检查启动状态 diff --git a/switch-desktop/src/windows/service.rs b/switch-desktop/src/windows/service.rs index e5b0972..bdb12d1 100644 --- a/switch-desktop/src/windows/service.rs +++ b/switch-desktop/src/windows/service.rs @@ -6,7 +6,6 @@ use std::sync::Arc; use std::thread; use std::time::Duration; - use windows_service::service::{ ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus, }; diff --git a/switch-jni/src/lib.rs b/switch-jni/src/lib.rs index dc9ce82..e069301 100644 --- a/switch-jni/src/lib.rs +++ b/switch-jni/src/lib.rs @@ -2,16 +2,16 @@ use std::net::{IpAddr, Ipv4Addr}; use jni::errors::Error; use jni::objects::{JClass, JObject, JString, JValue}; -use jni::sys::{jbyte, jint, jintArray, jlong, jobject, jobjectArray, jsize}; +use jni::sys::{jbyte, jint, jlong, jobject, jobjectArray, jsize}; use jni::JNIEnv; use switch::handle::{CurrentDeviceInfo, PeerDeviceInfo, Route}; use switch::{Config, Switch}; -fn to_string_not_null(env: &JNIEnv, config: JObject, name: &str) -> Result { +fn to_string_not_null(env: &JNIEnv, config: JObject, name: &'static str) -> Result { let value = env.get_field(config, name, "Ljava/lang/String;")?.l()?; if value.is_null() { - env.throw_new("Ljava/lang/NullPointerException", &name) + env.throw_new("Ljava/lang/NullPointerException", name) .expect("throw"); return Err(Error::NullPtr(name)); } @@ -187,7 +187,7 @@ fn device_list(env: &JNIEnv, device_list: Vec) -> Result ( @@ -381,11 +381,7 @@ fn send_punch(udp: &UdpSocket, cur_info: &CurrentDeviceInfo, nat_info: NatInfo) Ok(()) } -fn punch_packet( - virtual_ip: Ipv4Addr, - nat_info: NatInfo, - dest: Ipv4Addr, -) -> Result> { +fn punch_packet(virtual_ip: Ipv4Addr, nat_info: NatInfo, dest: Ipv4Addr) -> Result> { let mut punch_reply = Punch::new(); punch_reply.reply = false; punch_reply.virtual_ip = u32::from_be_bytes(virtual_ip.octets()); diff --git a/switch/src/lib.rs b/switch/src/lib.rs index 0485c88..f23cd43 100644 --- a/switch/src/lib.rs +++ b/switch/src/lib.rs @@ -1,8 +1,8 @@ - use std::io; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, ToSocketAddrs, UdpSocket}; -use std::sync::atomic::{Ordering}; use std::sync::Arc; +use std::sync::atomic::Ordering; +use std::time::Duration; use crossbeam::atomic::AtomicCell; use crossbeam::sync::WaitGroup; @@ -11,11 +11,11 @@ use tokio::sync::watch; use error::*; -use crate::handle::registration_handler::CONNECTION_STATUS; use crate::handle::{ - ApplicationStatus, ConnectStatus, CurrentDeviceInfo, PeerDeviceInfo, Route, RouteType, - DEVICE_LIST, DIRECT_ROUTE_TABLE, SERVER_RT, + ApplicationStatus, ConnectStatus, CurrentDeviceInfo, DEVICE_LIST, DIRECT_ROUTE_TABLE, PeerDeviceInfo, + Route, RouteType, SERVER_RT, }; +use crate::handle::registration_handler::CONNECTION_STATUS; pub mod error; pub mod handle; @@ -39,8 +39,8 @@ impl Config { name: Option, abnormal_call: F, ) -> Result - where - F: FnOnce() + Send + 'static, + where + F: FnOnce() + Send + 'static, { if token.is_empty() || token.len() > 64 { return Err(Error::Stop("token invalid".to_string())); @@ -84,8 +84,8 @@ pub struct Switch { impl Switch { pub fn start(config: Config) -> Result - where - F: FnOnce() + Send + 'static, + where + F: FnOnce() + Send + 'static, { let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() @@ -141,8 +141,8 @@ impl Switch { return status == ApplicationStatus::Starting; } pub async fn start_(config: Config) -> Result - where - F: FnOnce() + Send + 'static, + where + F: FnOnce() + Send + 'static, { // let server_address = "nat1.wherewego.top:29876" let server_address = "127.0.0.1:29876".to_socket_addrs().unwrap().next().unwrap(); @@ -162,6 +162,7 @@ impl Switch { } } }; + udp.set_write_timeout(Some(Duration::from_millis(2000)))?; //注册 let response = handle::registration_handler::registration( &udp, @@ -214,7 +215,7 @@ impl Switch { drop(wait_group1); }, ) - .await; + .await; } //初始化nat数据 handle::init_nat_info(response.public_ip, response.public_port as u16); @@ -248,7 +249,7 @@ impl Switch { drop(wait_group1); }, ) - .await; + .await; let udp1 = udp.try_clone()?; let wait_group1 = wait_group.clone(); let status_sender1 = status_sender.clone(); @@ -268,7 +269,7 @@ impl Switch { drop(wait_group1); }, ) - .await; + .await; } //打洞处理 { @@ -290,7 +291,7 @@ impl Switch { drop(wait_group1); }, ) - .await; + .await; let udp1 = udp.try_clone()?; let wait_group1 = wait_group.clone(); let status_sender1 = status_sender.clone(); @@ -309,7 +310,7 @@ impl Switch { drop(wait_group1); }, ) - .await; + .await; let udp1 = udp.try_clone()?; let wait_group1 = wait_group.clone(); let status_sender1 = status_sender.clone(); @@ -328,7 +329,7 @@ impl Switch { drop(wait_group1); }, ) - .await; + .await; } //tun数据处理 { @@ -349,7 +350,7 @@ impl Switch { drop(wait_group1); }, ) - .await; + .await; } Ok(Switch { current_device,