From d2c5aac178263857a20289fe78b836bd1bf76db4 Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Wed, 13 Mar 2024 21:17:01 +0800 Subject: [PATCH] =?UTF-8?q?[mio]=20=E5=AE=8C=E5=96=84=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt-cli/src/callback.rs | 12 +++++++++++- vnt-cli/src/command/server.rs | 7 +++++-- vnt-cli/src/config/mod.rs | 2 +- vnt-cli/src/main.rs | 20 ++++++++++++++------ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/vnt-cli/src/callback.rs b/vnt-cli/src/callback.rs index 72698c7..69a7d7d 100644 --- a/vnt-cli/src/callback.rs +++ b/vnt-cli/src/callback.rs @@ -2,7 +2,7 @@ use std::process; use console::style; -use vnt::handle::callback::ConnectInfo; +use vnt::handle::callback::{ConnectInfo, ErrorType}; use vnt::{DeviceInfo, ErrorInfo, HandshakeInfo, RegisterInfo, VntCallback}; #[derive(Clone)] @@ -32,6 +32,16 @@ impl VntCallback for VntHandler { fn error(&self, info: ErrorInfo) { println!("{}", style(format!("error {}", info)).red()); + match info.code { + ErrorType::TokenError + | ErrorType::AddressExhausted + | ErrorType::IpAlreadyExists + | ErrorType::InvalidIp + | ErrorType::LocalIpExists => { + self.stop(); + } + _ => {} + } } fn stop(&self) { diff --git a/vnt-cli/src/command/server.rs b/vnt-cli/src/command/server.rs index b0a56ea..4719c73 100644 --- a/vnt-cli/src/command/server.rs +++ b/vnt-cli/src/command/server.rs @@ -30,7 +30,6 @@ impl CommandServer { let (len, addr) = udp.recv_from(&mut buf)?; match std::str::from_utf8(&buf[..len]) { Ok(cmd) => { - log::info!("收到cmd={:?}", cmd); if let Ok(out) = command(cmd, &vnt) { if let Err(e) = udp.send_to(out.as_bytes(), addr) { log::warn!("cmd={},err={:?}", cmd, e); @@ -56,6 +55,7 @@ fn save_port(port: u16) -> io::Result<()> { } fn command(cmd: &str, vnt: &Vnt) -> io::Result { + let cmd = cmd.trim(); let out_str = match cmd { "route" => serde_yaml::to_string(&crate::command::command_route(vnt)) .unwrap_or_else(|e| format!("error {:?}", e)), @@ -68,7 +68,10 @@ fn command(cmd: &str, vnt: &Vnt) -> io::Result { "stopped".to_string() } _ => { - format!("command '{}' not found. \n Try to enter: 'help'\n", cmd) + format!( + "command '{}' not found. Try to enter: 'route'/'list'/'stop' \n", + cmd + ) } }; Ok(out_str) diff --git a/vnt-cli/src/config/mod.rs b/vnt-cli/src/config/mod.rs index 4416ffa..c1f8870 100644 --- a/vnt-cli/src/config/mod.rs +++ b/vnt-cli/src/config/mod.rs @@ -171,7 +171,7 @@ pub fn read_config(file_path: &str) -> io::Result<(Config, bool)> { file_conf.device_name, use_channel_type, file_conf.packet_loss, - file_conf.packet_delay + file_conf.packet_delay, ) .unwrap(); Ok((config, file_conf.cmd)) diff --git a/vnt-cli/src/main.rs b/vnt-cli/src/main.rs index 1202c26..e70ec34 100644 --- a/vnt-cli/src/main.rs +++ b/vnt-cli/src/main.rs @@ -296,8 +296,9 @@ fn main() { .expect("--packet-loss"); let packet_delay = matches .opt_get::("packet-delay") - .expect("--packet-delay").unwrap_or(0); - let config = Config::new( + .expect("--packet-delay") + .unwrap_or(0); + let config = match Config::new( #[cfg(any(target_os = "windows", target_os = "linux"))] tap, token, @@ -324,9 +325,14 @@ fn main() { device_name, use_channel_type, packet_loss, - packet_delay - ) - .unwrap(); + packet_delay, + ) { + Ok(config) => config, + Err(e) => { + println!("config error: {}", e); + return; + } + }; (config, cmd) }; println!("version {}", vnt::VNT_VERSION); @@ -467,7 +473,9 @@ fn print_usage(program: &str, _opts: Options) { println!(" --use-channel 使用通道 relay/p2p/all,默认两者都使用"); println!(" --nic 指定虚拟网卡名称"); println!(" --packet-loss <0> 模拟丢包,取值0~1之间的小数,程序会按设定的概率主动丢包,可用于模拟弱网"); - println!(" --packet-delay <0> 模拟延迟,整数,单位毫秒(ms),程序会按设定的值延迟发包,可用于模拟弱网"); + println!( + " --packet-delay <0> 模拟延迟,整数,单位毫秒(ms),程序会按设定的值延迟发包,可用于模拟弱网" + ); println!(); println!(