解决掉线问题
This commit is contained in:
@@ -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<String>,
|
||||
}
|
||||
|
||||
#[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());
|
||||
|
||||
@@ -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(_) => {
|
||||
//需要检查启动状态
|
||||
|
||||
@@ -6,7 +6,6 @@ use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
use windows_service::service::{
|
||||
ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus,
|
||||
};
|
||||
|
||||
@@ -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<String, Error> {
|
||||
fn to_string_not_null(env: &JNIEnv, config: JObject, name: &'static str) -> Result<String, Error> {
|
||||
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<PeerDeviceInfo>) -> Result<jobject
|
||||
for peer_info in device_list {
|
||||
let virtual_ip: u32 = peer_info.virtual_ip.into();
|
||||
let name = peer_info.name;
|
||||
let status = peer_info.status.into();
|
||||
let status:u8 = peer_info.status.into();
|
||||
let info = env.new_object(
|
||||
"org/switches/jni/PeerDeviceInfo",
|
||||
"(BLjava/lang/String;J)V",
|
||||
|
||||
@@ -52,7 +52,8 @@ async fn handle_loop(
|
||||
ping.set_epoch(epoch);
|
||||
}
|
||||
let _ = udp.send_to(net_packet.buffer(), server_addr);
|
||||
for x in DIRECT_ROUTE_TABLE.iter() {
|
||||
// 不clone会死锁?
|
||||
for x in DIRECT_ROUTE_TABLE.clone().iter() {
|
||||
let virtual_ip = x.key().clone();
|
||||
let route = x.value().clone();
|
||||
drop(x);
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
use std::{io, thread};
|
||||
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
|
||||
use std::time::Duration;
|
||||
use std::{io, thread};
|
||||
|
||||
use protobuf::Message;
|
||||
use rand::prelude::SliceRandom;
|
||||
use tokio::sync::mpsc::{Receiver, Sender};
|
||||
use tokio::sync::mpsc::error::TrySendError;
|
||||
use tokio::sync::mpsc::{Receiver, Sender};
|
||||
use tokio::sync::watch;
|
||||
|
||||
use crate::{CurrentDeviceInfo, DEVICE_LIST, handle::NAT_INFO, handle::NatInfo};
|
||||
use crate::error::*;
|
||||
use crate::handle::{ApplicationStatus, DIRECT_ROUTE_TABLE};
|
||||
use crate::proto::message::{NatType, Punch};
|
||||
use crate::protocol::{control_packet, NetPacket, Protocol, turn_packet, Version};
|
||||
use crate::protocol::control_packet::PunchRequestPacket;
|
||||
use crate::protocol::turn_packet::TurnPacket;
|
||||
use crate::protocol::{control_packet, turn_packet, NetPacket, Protocol, Version};
|
||||
use crate::{handle::NatInfo, handle::NAT_INFO, CurrentDeviceInfo, DEVICE_LIST};
|
||||
|
||||
/// 每一种类型一个通道,减少相互干扰
|
||||
pub fn bounded() -> (
|
||||
@@ -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<Vec<u8>> {
|
||||
fn punch_packet(virtual_ip: Ipv4Addr, nat_info: NatInfo, dest: Ipv4Addr) -> Result<Vec<u8>> {
|
||||
let mut punch_reply = Punch::new();
|
||||
punch_reply.reply = false;
|
||||
punch_reply.virtual_ip = u32::from_be_bytes(virtual_ip.octets());
|
||||
|
||||
+19
-18
@@ -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<F> Config<F> {
|
||||
name: Option<String>,
|
||||
abnormal_call: F,
|
||||
) -> Result<Self>
|
||||
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<F>(config: Config<F>) -> Result<Self>
|
||||
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_<F>(config: Config<F>) -> Result<Self>
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user