完善配置

This commit is contained in:
lubeilin
2023-02-07 21:31:42 +08:00
parent efb7053931
commit 6872ec3618
11 changed files with 242 additions and 72 deletions
+8 -1
View File
@@ -1,6 +1,5 @@
use std::io;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
use std::sync::Arc;
use console::style;
@@ -54,6 +53,7 @@ impl CommandServer {
}
}
}
fn command(cmd: &str, switch: &Switch) -> io::Result<String> {
let mut out_str = String::new();
match cmd {
@@ -129,6 +129,13 @@ fn command(cmd: &str, switch: &Switch) -> io::Result<String> {
let str = format!("Delay of relay server :{}ms\n", style(server_rt).green());
out_str.push_str(&str);
}
if let Some(nat_info) = switch.nat_info() {
let str = format!(
"NAT type :{}",
style(format!("{:?}", nat_info.nat_type)).green()
);
out_str.push_str(&str);
}
}
"help" | "h" => {
let str = format!("Options: \n");
+51 -6
View File
@@ -1,11 +1,12 @@
use std::io;
use std::net::ToSocketAddrs;
use std::path::PathBuf;
use clap::Parser;
use console::style;
use switch::*;
use switch::handle::{PeerDeviceStatus, RouteType};
use switch::*;
#[cfg(windows)]
mod command;
@@ -16,9 +17,9 @@ 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位字符
@@ -30,6 +31,7 @@ struct Args {
#[arg(long)]
token: String,
/// 给设备一个名称,为空时默认用系统版本信息
/// Give the device a name. If it is blank, the system version information will be used by default
#[arg(long)]
name: Option<String>,
}
@@ -120,7 +122,10 @@ fn main() {
let _ = log_init();
let args = Args::parse();
if sudo::RunningAs::Root != sudo::check() {
println!("{}", style("需要使用root权限执行...").red());
println!(
"{}",
style("需要使用root权限执行(Need to execute with root permission)...").red()
);
sudo::escalate_if_needed().unwrap();
}
println!("{}", style("starting...").green());
@@ -129,7 +134,41 @@ fn main() {
pub fn start(token: String, name: Option<String>) {
let mac_address = mac_address::get_mac_address().unwrap().unwrap().to_string();
let switch = match Config::new(token, mac_address, name, || {}) {
let server_address = "nat1.wherewego.top:29875"
.to_socket_addrs()
.unwrap()
.next()
.unwrap();
let nat_test_server = vec![
"nat1.wherewego.top:35061"
.to_socket_addrs()
.unwrap()
.next()
.unwrap(),
"nat1.wherewego.top:35062"
.to_socket_addrs()
.unwrap()
.next()
.unwrap(),
"nat2.wherewego.top:35061"
.to_socket_addrs()
.unwrap()
.next()
.unwrap(),
"nat2.wherewego.top:35062"
.to_socket_addrs()
.unwrap()
.next()
.unwrap(),
];
let switch = match Config::new(
token,
mac_address,
name,
server_address,
nat_test_server,
|| {},
) {
Ok(config) => match Switch::start(config) {
Ok(switch) => switch,
Err(e) => {
@@ -248,6 +287,12 @@ fn command(cmd: &str, switch: &Switch) -> Result<(), ()> {
if server_rt >= 0 {
println!("Delay of relay server :{}ms", style(server_rt).green());
}
if let Some(nat_info) = switch.nat_info() {
println!(
"NAT type :{}",
style(format!("{:?}", nat_info.nat_type)).green()
);
}
}
"help" | "h" => {
println!("Options: ");
+31 -17
View File
@@ -1,15 +1,16 @@
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 +19,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位字符
@@ -32,25 +33,32 @@ struct Args {
#[arg(long)]
token: Option<String>,
/// 给设备一个名称,为空时默认用系统版本信息
/// Give the device a name. If it is blank, the system version information will be used by default
#[arg(long)]
name: Option<String>,
/// 安装服务,安装后可以后台运行,需要指定安装路径
/// The installation service can run in the background after installation, and the installation path needs to be specified
#[arg(long)]
install: Option<String>,
/// 卸载服务
/// Uninstall service
#[arg(long)]
uninstall: bool,
/// 启动,启动时可以附加参数 --token,如果没有token,则会读取配置文件中上一次使用的token
/// 安装服务后,会以服务的方式在后台启动,此时可以关闭命令行窗口
/// When starting, you can attach the parameter -- token. If there is no token, the last token used in the configuration file will be read. After installing the service, it will be started in the background as a service. At this time, you can close the command line window
#[arg(long)]
start: bool,
#[arg(long)]
/// 停止,安装服务后,使用 --stop停止服务
/// Stop. After installing the service, use -- stop to stop the service
stop: bool,
/// 启动服务后,使用 --list 查看设备列表
/// After starting the service, use -- list to view the device list
#[arg(long)]
list: bool,
/// 启动服务后,使用 --status 查看设备状态
/// After starting the service, use -- status to view the device status
#[arg(long)]
status: bool,
}
@@ -85,7 +93,10 @@ pub fn main0() {
return;
}
if !windows_admin_check::is_app_elevated() {
println!("{}", style("请使用管理员权限运行").red());
println!(
"{}",
style("请使用管理员权限运行(Please run with administrator privileges)").red()
);
return;
}
if let Some(path) = args.install {
@@ -94,23 +105,23 @@ pub fn main0() {
std::fs::create_dir_all(&path).unwrap();
}
if !path.is_dir() {
println!("参数必须为文件目录");
println!("参数必须为文件目录(Parameter must be a file directory)");
} else {
if let Err(e) = install(path) {
log::error!("{:?}", e);
} else {
println!("{}", style("安装成功").green())
println!("{}", style("安装成功(Installation succeeded)").green())
}
}
} else if args.uninstall {
if let Err(e) = uninstall() {
log::error!("{:?}", e);
} else {
println!("{}", style("卸载成功").green())
println!("{}", style("卸载成功(Uninstall succeeded)").green())
}
} else if args.start {
if args.token.is_none() {
println!("{}", style("需要参数 --token").red());
println!("{}", style("需要参数(require parameters) --token").red());
} else {
let token = args.token.clone().unwrap();
match service_state() {
@@ -120,18 +131,18 @@ pub fn main0() {
token.clone(),
args.name.clone(),
))
.unwrap();
.unwrap();
match start() {
Ok(_) => {
//需要检查启动状态
println!("{}", style("启动成功").green())
println!("{}", style("启动成功(Start successfully)").green())
}
Err(e) => {
log::error!("{:?}", e);
}
}
} else {
println!("服务未停止");
println!("服务未停止(Service not stopped)");
}
}
Err(e) => {
@@ -142,7 +153,7 @@ pub fn main0() {
//指定的服务未安装。
println!(
"{}",
style("服务未安装,在当前进程启动").red()
style("服务未安装,在当前进程启动(The service is not installed and started in the current process)").red()
);
crate::start(token, args.name);
return;
@@ -158,20 +169,23 @@ pub fn main0() {
} else if args.stop {
match stop() {
Ok(_) => {
println!("{}", style("停止成功").green())
println!("{}", style("停止成功(Stopped successfully)").green())
}
Err(e) => {
log::error!("{:?}", e);
}
}
} else {
println!("使用参数 -h 查看帮助")
println!("使用参数 -h 查看帮助(Use the parameter - h to view help)")
}
pause();
}
fn pause() {
println!("{}", style("按任意键退出...").green());
println!(
"{}",
style("按任意键退出(Press any key to exit)...").green()
);
use console::Term;
let term = Term::stdout();
let _ = term.read_char().unwrap();
+38 -5
View File
@@ -6,14 +6,13 @@ use std::sync::Arc;
use std::thread;
use std::time::Duration;
use switch::{Config, Switch};
use windows_service::service::{
ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus,
};
use windows_service::service_control_handler::ServiceControlHandlerResult;
use windows_service::{define_windows_service, service_control_handler, service_dispatcher};
use switch::{Config, Switch};
use crate::windows::config::read_config;
define_windows_service!(ffi_service_main, switch_service_main);
@@ -63,9 +62,43 @@ fn service_main() -> windows_service::Result<()> {
if let Some(config) = read_config() {
let mac_address = mac_address::get_mac_address().unwrap().unwrap().to_string();
let un_parker = parker.unparker().clone();
match Config::new(config.token, mac_address, config.name, move || {
un_parker.unpark();
}) {
let server_address = "nat1.wherewego.top:29875"
.to_socket_addrs()
.unwrap()
.next()
.unwrap();
let nat_test_server = vec![
"nat1.wherewego.top:35061"
.to_socket_addrs()
.unwrap()
.next()
.unwrap(),
"nat1.wherewego.top:35062"
.to_socket_addrs()
.unwrap()
.next()
.unwrap(),
"nat2.wherewego.top:35061"
.to_socket_addrs()
.unwrap()
.next()
.unwrap(),
"nat2.wherewego.top:35062"
.to_socket_addrs()
.unwrap()
.next()
.unwrap(),
];
match Config::new(
config.token,
mac_address,
config.name,
server_address,
nat_test_server,
move || {
un_parker.unpark();
},
) {
Ok(config) => match Switch::start(config) {
Ok(switch) => {
log::info!("switch-service服务启动");