From c655d9650bcf6317962014ebbbc2480350eefc8b Mon Sep 17 00:00:00 2001
From: lubeilin <1791778603@qq.com>
Date: Mon, 9 Jan 2023 20:26:25 +0800
Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
switch-desktop/src/main.rs | 51 ++++++--
switch-desktop/src/windows_admin_check.rs | 2 +-
switch-jni/src/lib.rs | 115 ++++++++++-------
switch/packet/src/lib.rs | 1 -
switch/rust-tun/src/platform/linux/device.rs | 8 +-
switch/src/handle/heartbeat_handler.rs | 23 ++--
switch/src/handle/mod.rs | 23 ++--
switch/src/handle/punch_handler.rs | 117 ++++++++++-------
switch/src/handle/registration_handler.rs | 6 +-
switch/src/handle/tun_handler.rs | 65 ++++++----
switch/src/handle/udp_recv_handler.rs | 75 ++++++-----
switch/src/lib.rs | 126 ++++++++++++-------
switch/src/nat/check.rs | 4 +-
switch/src/nat/mod.rs | 2 +-
switch/src/protocol/control_packet.rs | 1 -
switch/src/tun_device/linux.rs | 3 +-
switch/src/tun_device/mac.rs | 13 +-
switch/src/tun_device/mod.rs | 10 +-
18 files changed, 398 insertions(+), 247 deletions(-)
diff --git a/switch-desktop/src/main.rs b/switch-desktop/src/main.rs
index b2648b1..b52589e 100644
--- a/switch-desktop/src/main.rs
+++ b/switch-desktop/src/main.rs
@@ -1,14 +1,18 @@
use clap::Parser;
use console::style;
-use switch::*;
use switch::handle::RouteType;
+use switch::*;
#[cfg(windows)]
mod windows_admin_check;
#[derive(Parser, Debug)]
-#[command(author = "Lu Beilin", version, about = "一个虚拟网络工具,启动后会获取一个ip,相同token下的设备之间可以用ip直接通信")]
+#[command(
+ author = "Lu Beilin",
+ version,
+ about = "一个虚拟网络工具,启动后会获取一个ip,相同token下的设备之间可以用ip直接通信"
+)]
struct Args {
/// 32位字符
/// 相同token的设备之间才能通信。
@@ -27,7 +31,9 @@ fn log_init() {
}
let logfile = log4rs::append::file::FileAppender::builder()
// Pattern: https://docs.rs/log4rs/*/log4rs/encode/pattern/index.html
- .encoder(Box::new(log4rs::encode::pattern::PatternEncoder::new("{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}\n")))
+ .encoder(Box::new(log4rs::encode::pattern::PatternEncoder::new(
+ "{d(%+)(utc)} [{f}:{L}] {h({l})} {M}:{m}{n}\n",
+ )))
.build(home.join("switch.log"))
.unwrap();
let config = log4rs::Config::builder()
@@ -52,7 +58,9 @@ fn main() {
.ok()
.and_then(|p| p.to_str().map(|p| p.to_string()))
{
- let _ = runas::Command::new(&absolute_path).args(&args[1..]).status()
+ let _ = runas::Command::new(&absolute_path)
+ .args(&args[1..])
+ .status()
.expect("failed to execute");
} else {
panic!("failed to execute")
@@ -72,10 +80,19 @@ fn main() {
let term = Term::stdout();
println!("{}", style("started").green());
let current_device = switch.current_device();
- println!("当前虚拟ip(virtual ip): {:?}", style(current_device.virtual_ip).green());
- println!("虚拟网关(virtual gateway): {:?}", style(current_device.virtual_gateway).green());
+ println!(
+ "当前虚拟ip(virtual ip): {:?}",
+ style(current_device.virtual_ip).green()
+ );
+ println!(
+ "虚拟网关(virtual gateway): {:?}",
+ style(current_device.virtual_gateway).green()
+ );
loop {
- println!("{}", style("Please enter the command (Usage: list,status,exit,help):").color256(102));
+ println!(
+ "{}",
+ style("Please enter the command (Usage: list,status,exit,help):").color256(102)
+ );
match term.read_line() {
Ok(cmd) => {
if command(cmd.trim(), &switch).is_err() {
@@ -128,16 +145,28 @@ fn command(cmd: &str, switch: &Switch) -> Result<(), ()> {
let server_rt = switch.server_rt();
let current_device = switch.current_device();
println!("Virtual ip:{}", style(current_device.virtual_ip).green());
- println!("Virtual gateway:{}", style(current_device.virtual_gateway).green());
- println!("Connection status :{}", style(format!("{:?}", switch.connection_status())).green());
- println!("Relay server :{}", style(current_device.connect_server).green());
+ println!(
+ "Virtual gateway:{}",
+ style(current_device.virtual_gateway).green()
+ );
+ println!(
+ "Connection status :{}",
+ style(format!("{:?}", switch.connection_status())).green()
+ );
+ println!(
+ "Relay server :{}",
+ style(current_device.connect_server).green()
+ );
if server_rt >= 0 {
println!("Delay of relay server :{}ms", style(server_rt).green());
}
}
"help" | "h" => {
println!("Options: ");
- println!("{} , Query the virtual IP of other devices", style("list").green());
+ println!(
+ "{} , Query the virtual IP of other devices",
+ style("list").green()
+ );
println!("{} , View current device status", style("status").green());
println!("{} , Exit the program", style("exit").green());
}
diff --git a/switch-desktop/src/windows_admin_check.rs b/switch-desktop/src/windows_admin_check.rs
index c818547..c3531bf 100644
--- a/switch-desktop/src/windows_admin_check.rs
+++ b/switch-desktop/src/windows_admin_check.rs
@@ -5,7 +5,7 @@ use std::ptr;
use winapi::um::handleapi::CloseHandle;
use winapi::um::processthreadsapi::{GetCurrentProcess, OpenProcessToken};
use winapi::um::securitybaseapi::GetTokenInformation;
-use winapi::um::winnt::{HANDLE, TOKEN_ELEVATION, TOKEN_QUERY, TokenElevation};
+use winapi::um::winnt::{TokenElevation, HANDLE, TOKEN_ELEVATION, TOKEN_QUERY};
// Use std::io::Error::last_os_error for errors.
// NOTE: For this example I'm simple passing on the OS error.
diff --git a/switch-jni/src/lib.rs b/switch-jni/src/lib.rs
index d11572f..ec77bb7 100644
--- a/switch-jni/src/lib.rs
+++ b/switch-jni/src/lib.rs
@@ -2,26 +2,26 @@ use std::net::{IpAddr, Ipv4Addr};
use std::str::Utf8Error;
use jni::errors::Error;
-use jni::JNIEnv;
use jni::objects::{JClass, JList, JObject, JString, JValue};
use jni::sys::{jbyte, jint, jintArray, jlong, jobject, jobjectArray, jsize};
+use jni::JNIEnv;
-use switch::{Config, Switch};
use switch::handle::{CurrentDeviceInfo, Route};
+use switch::{Config, Switch};
fn to_string(env: &JNIEnv, config: JObject, name: &str) -> Result