diff --git a/.gitignore b/.gitignore index 9a7f190..d209945 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ target-*/ *.pdb .vscode +/.idea # perf & flamegraph perf.data diff --git a/easytier/Cargo.toml b/easytier/Cargo.toml index 74e9538..516a898 100644 --- a/easytier/Cargo.toml +++ b/easytier/Cargo.toml @@ -21,12 +21,10 @@ path = "src/easytier-core.rs" [[bin]] name = "easytier-cli" path = "src/easytier-cli.rs" -test = false [lib] name = "easytier" path = "src/lib.rs" -test = false [dependencies] git-version = "0.3.9" diff --git a/easytier/build.rs b/easytier/build.rs index 53179e5..499346d 100644 --- a/easytier/build.rs +++ b/easytier/build.rs @@ -147,7 +147,7 @@ fn main() -> Result<(), Box> { .type_attribute("peer_rpc.ForeignNetworkRouteInfoKey", "#[derive(Hash, Eq)]") .type_attribute("common.RpcDescriptor", "#[derive(Hash, Eq)]") .service_generator(Box::new(rpc_build::ServiceGenerator::new())) - .btree_map(&["."]) + .btree_map(["."]) .compile_protos(&proto_files, &["src/proto/"]) .unwrap(); diff --git a/easytier/src/easytier-cli.rs b/easytier/src/easytier-cli.rs index f68e1e5..7cfce53 100644 --- a/easytier/src/easytier-cli.rs +++ b/easytier/src/easytier-cli.rs @@ -1,33 +1,29 @@ -#![allow(dead_code)] - use std::{net::SocketAddr, sync::Mutex, time::Duration, vec}; use anyhow::{Context, Ok}; use clap::{command, Args, Parser, Subcommand}; -use common::{constants::EASYTIER_VERSION, stun::StunInfoCollectorTrait}; -use proto::{ - common::NatType, - peer_rpc::{GetGlobalPeerMapRequest, PeerCenterRpc, PeerCenterRpcClientFactory}, - rpc_impl::standalone::StandAloneClient, - rpc_types::controller::BaseController, -}; -use tokio::time::timeout; -use tunnel::tcp::TcpTunnelConnector; -use utils::{list_peer_route_pair, PeerRoutePair}; - -mod arch; -mod common; -mod proto; -mod tunnel; -mod utils; - -use crate::{ - common::stun::StunInfoCollector, - proto::cli::*, - utils::{cost_to_str, float_to_str}, -}; use humansize::format_size; use tabled::settings::Style; +use tokio::time::timeout; + +use easytier::{ + common::{constants::EASYTIER_VERSION, stun::StunInfoCollector, stun::StunInfoCollectorTrait}, + proto::{ + cli::{ + ConnectorManageRpc, ConnectorManageRpcClientFactory, DumpRouteRequest, + GetVpnPortalInfoRequest, ListConnectorRequest, ListForeignNetworkRequest, + ListGlobalForeignNetworkRequest, ListPeerRequest, ListPeerResponse, ListRouteRequest, + ListRouteResponse, NodeInfo, PeerManageRpc, PeerManageRpcClientFactory, + ShowNodeInfoRequest, VpnPortalRpc, VpnPortalRpcClientFactory, + }, + common::NatType, + peer_rpc::{GetGlobalPeerMapRequest, PeerCenterRpc, PeerCenterRpcClientFactory}, + rpc_impl::standalone::StandAloneClient, + rpc_types::controller::BaseController, + }, + tunnel::tcp::TcpTunnelConnector, + utils::{cost_to_str, float_to_str, list_peer_route_pair, PeerRoutePair}, +}; #[derive(Parser, Debug)] #[command(name = "easytier-cli", author, version = EASYTIER_VERSION, about, long_about = None)] @@ -230,7 +226,6 @@ impl CommandHandler { ipv4: p .route .ipv4_addr - .clone() .map(|ip| ip.to_string()) .unwrap_or_default(), hostname: p.route.hostname.clone(), @@ -239,7 +234,7 @@ impl CommandHandler { loss_rate: float_to_str(p.get_loss_rate().unwrap_or(0.0), 3), rx_bytes: format_size(p.get_rx_bytes().unwrap_or(0), humansize::DECIMAL), tx_bytes: format_size(p.get_tx_bytes().unwrap_or(0), humansize::DECIMAL), - tunnel_proto: p.get_conn_protos().unwrap_or(vec![]).join(",").to_string(), + tunnel_proto: p.get_conn_protos().unwrap_or_default().join(",").to_string(), nat_type: p.get_udp_nat_type(), id: p.route.peer_id.to_string(), version: if p.route.version.is_empty() { @@ -294,7 +289,7 @@ impl CommandHandler { println!( "{}", - tabled::Table::new(items).with(Style::modern()).to_string() + tabled::Table::new(items).with(Style::modern()) ); Ok(()) @@ -421,7 +416,6 @@ impl CommandHandler { ipv4: p .route .ipv4_addr - .clone() .map(|ip| ip.to_string()) .unwrap_or_default(), hostname: p.route.hostname.clone(), @@ -441,7 +435,6 @@ impl CommandHandler { ipv4: p .route .ipv4_addr - .clone() .map(|ip| ip.to_string()) .unwrap_or_default(), hostname: p.route.hostname.clone(), @@ -449,7 +442,6 @@ impl CommandHandler { next_hop_ipv4: next_hop_pair .route .ipv4_addr - .clone() .map(|ip| ip.to_string()) .unwrap_or_default(), next_hop_hostname: next_hop_pair.route.hostname.clone(), @@ -466,7 +458,7 @@ impl CommandHandler { println!( "{}", - tabled::Table::new(items).with(Style::modern()).to_string() + tabled::Table::new(items).with(Style::modern()) ); Ok(()) @@ -588,7 +580,6 @@ async fn main() -> Result<(), Error> { "{}", tabled::Table::new(table_rows) .with(Style::modern()) - .to_string() ); } SubCommand::VpnPortal => { @@ -643,7 +634,7 @@ async fn main() -> Result<(), Error> { builder.push_record(vec![format!("Listener {}", idx).as_str(), l]); } - println!("{}", builder.build().with(Style::modern()).to_string()); + println!("{}", builder.build().with(Style::modern())); } Some(NodeSubCommand::Config) => { println!("{}", node_info.config); diff --git a/easytier/src/easytier-core.rs b/easytier/src/easytier-core.rs index 2b86714..7b7103e 100644 --- a/easytier/src/easytier-core.rs +++ b/easytier/src/easytier-core.rs @@ -1,51 +1,31 @@ -#![allow(dead_code)] - -#[cfg(test)] -mod tests; +#[macro_use] +extern crate rust_i18n; use std::{ net::{Ipv4Addr, SocketAddr}, path::PathBuf, }; -#[macro_use] -extern crate rust_i18n; - use anyhow::Context; use clap::Parser; - -mod arch; -mod common; -mod connector; -mod gateway; -mod instance; -mod launcher; -mod peer_center; -mod peers; -mod proto; -mod tunnel; -mod utils; -mod vpn_portal; - -use common::{ - config::{ConsoleLoggerConfig, FileLoggerConfig, NetworkIdentity, PeerConfig, VpnPortalConfig}, - constants::EASYTIER_VERSION, - global_ctx::EventBusSubscriber, - scoped_task::ScopedTask, -}; use tokio::net::TcpSocket; -use utils::setup_panic_handler; -use crate::{ +use easytier::{ common::{ - config::{ConfigLoader, TomlConfigLoader}, - global_ctx::GlobalCtxEvent, + config::{ + ConfigLoader, ConsoleLoggerConfig, FileLoggerConfig, NetworkIdentity, PeerConfig, + TomlConfigLoader, VpnPortalConfig, + }, + constants::EASYTIER_VERSION, + global_ctx::{EventBusSubscriber, GlobalCtxEvent}, + scoped_task::ScopedTask, }, - utils::init_logger, + launcher, proto, + utils::{init_logger, setup_panic_handler}, }; #[cfg(feature = "mimalloc")] -use mimalloc_rust::*; +use mimalloc_rust::GlobalMiMalloc; #[cfg(feature = "mimalloc")] #[global_allocator] @@ -509,7 +489,7 @@ impl From for TomlConfigLoader { f.enable_encryption = !cli.disable_encryption; f.enable_ipv6 = !cli.disable_ipv6; f.latency_first = cli.latency_first; - f.dev_name = cli.dev_name.unwrap_or(Default::default()); + f.dev_name = cli.dev_name.unwrap_or_default(); if let Some(mtu) = cli.mtu { f.mtu = mtu; } @@ -544,7 +524,7 @@ fn print_event(msg: String) { ); } -fn peer_conn_info_to_string(p: crate::proto::cli::PeerConnInfo) -> String { +fn peer_conn_info_to_string(p: proto::cli::PeerConnInfo) -> String { format!( "my_peer_id: {}, dst_peer_id: {}, tunnel_info: {:?}", p.my_peer_id, p.peer_id, p.tunnel diff --git a/easytier/src/lib.rs b/easytier/src/lib.rs index e1e0dac..f719d88 100644 --- a/easytier/src/lib.rs +++ b/easytier/src/lib.rs @@ -5,13 +5,16 @@ mod connector; mod gateway; mod instance; mod peer_center; -mod peers; mod vpn_portal; pub mod common; pub mod launcher; +pub mod peers; pub mod proto; pub mod tunnel; pub mod utils; +#[cfg(test)] +mod tests; + pub const VERSION: &str = common::constants::EASYTIER_VERSION; diff --git a/easytier/src/proto/mod.rs b/easytier/src/proto/mod.rs index a05e5e0..4610ba6 100644 --- a/easytier/src/proto/mod.rs +++ b/easytier/src/proto/mod.rs @@ -6,4 +6,5 @@ pub mod common; pub mod error; pub mod peer_rpc; +#[cfg(test)] pub mod tests; diff --git a/easytier/src/tests/mod.rs b/easytier/src/tests/mod.rs index cd6b26c..c51d61d 100644 --- a/easytier/src/tests/mod.rs +++ b/easytier/src/tests/mod.rs @@ -1,8 +1,9 @@ -use crate::common::PeerId; - #[cfg(target_os = "linux")] mod three_node; +use crate::common::PeerId; +use crate::peers::peer_manager::PeerManager; + pub fn get_guest_veth_name(net_ns: &str) -> &str { Box::leak(format!("veth_{}_g", net_ns).into_boxed_str()) } @@ -14,29 +15,29 @@ pub fn get_host_veth_name(net_ns: &str) -> &str { pub fn del_netns(name: &str) { // del veth host let _ = std::process::Command::new("ip") - .args(&["link", "del", get_host_veth_name(name)]) + .args(["link", "del", get_host_veth_name(name)]) .output(); let _ = std::process::Command::new("ip") - .args(&["netns", "del", name]) + .args(["netns", "del", name]) .output(); } pub fn create_netns(name: &str, ipv4: &str) { // create netns let _ = std::process::Command::new("ip") - .args(&["netns", "add", name]) + .args(["netns", "add", name]) .output() .unwrap(); // set lo up let _ = std::process::Command::new("ip") - .args(&["netns", "exec", name, "ip", "link", "set", "lo", "up"]) + .args(["netns", "exec", name, "ip", "link", "set", "lo", "up"]) .output() .unwrap(); let _ = std::process::Command::new("ip") - .args(&[ + .args([ "link", "add", get_host_veth_name(name), @@ -50,12 +51,12 @@ pub fn create_netns(name: &str, ipv4: &str) { .unwrap(); let _ = std::process::Command::new("ip") - .args(&["link", "set", get_guest_veth_name(name), "netns", name]) + .args(["link", "set", get_guest_veth_name(name), "netns", name]) .output() .unwrap(); let _ = std::process::Command::new("ip") - .args(&[ + .args([ "netns", "exec", name, @@ -69,12 +70,12 @@ pub fn create_netns(name: &str, ipv4: &str) { .unwrap(); let _ = std::process::Command::new("ip") - .args(&["link", "set", get_host_veth_name(name), "up"]) + .args(["link", "set", get_host_veth_name(name), "up"]) .output() .unwrap(); let _ = std::process::Command::new("ip") - .args(&[ + .args([ "netns", "exec", name, @@ -92,25 +93,25 @@ pub fn create_netns(name: &str, ipv4: &str) { pub fn prepare_bridge(name: &str) { // del bridge with brctl let _ = std::process::Command::new("brctl") - .args(&["delbr", name]) + .args(["delbr", name]) .output(); // create new br let _ = std::process::Command::new("brctl") - .args(&["addbr", name]) + .args(["addbr", name]) .output(); } pub fn add_ns_to_bridge(br_name: &str, ns_name: &str) { // use brctl to add ns to bridge let _ = std::process::Command::new("brctl") - .args(&["addif", br_name, get_host_veth_name(ns_name)]) + .args(["addif", br_name, get_host_veth_name(ns_name)]) .output() .unwrap(); // set bridge up let _ = std::process::Command::new("ip") - .args(&["link", "set", br_name, "up"]) + .args(["link", "set", br_name, "up"]) .output() .unwrap(); } @@ -143,7 +144,7 @@ fn check_route(ipv4: &str, dst_peer_id: PeerId, routes: Vec, + mgr: &std::sync::Arc, ipv4: &str, dst_peer_id: PeerId, proxy_cidr: &str, @@ -167,7 +168,7 @@ async fn wait_proxy_route_appear( fn set_link_status(net_ns: &str, up: bool) { let _ = std::process::Command::new("ip") - .args(&[ + .args([ "netns", "exec", net_ns,