mod config; mod network; mod pb; mod tcp; mod utils; use std::time::Duration; use std::net::SocketAddr; pub use network::get_edge; use network::{async_main, init_edge, NodeConfig}; use tokio::sync::mpsc::{channel, Receiver}; use tokio_util::sync::CancellationToken; use tracing::{debug, error}; pub use utils::CommandLine; use sdlan_sn_rs::{ log, peer::SdlanSock, utils::{create_or_load_uuid, get_sdlan_sock_from_socketaddr, Result, SDLanError}, }; pub async fn run_sdlan( args: CommandLine, // start_stop_sender: Sender, // start_stop_receiver: Receiver, ) -> Result<()> { debug!("run_sdlan"); let (start_stop_sender, start_stop_chan) = channel(20); let edge_uuid = create_or_load_uuid("")?; let node_conf = parse_config(edge_uuid, &args).await?; debug!("initing edge"); if let Err(e) = init_edge(&args.token, node_conf, args.tos, start_stop_sender).await { panic!("failed to init edge: {:?}", e); } debug!("edge inited"); let cancel = CancellationToken::new(); tokio::spawn(async move { if let Err(e) = async_main("linux".to_owned(), args, start_stop_chan, cancel).await { error!("failed to run async main: {}", e.as_str()); } }); Ok(()) } async fn parse_config(uuid: String, args: &CommandLine) -> Result { if args.sn.len() == 0 { return Err(SDLanError::NormalError("no sn is specified")); } let mut node_conf = NodeConfig::new(); let sns: Vec<&str> = args.sn.split(",").collect(); let mut correct_sns: Vec<_>; let mut sockaddr: Vec<_>; loop { debug!("parsing sns"); (correct_sns, sockaddr) = parse_sns(&sns); if sockaddr.len() < 1 { if correct_sns.len() > 0 { tokio::time::sleep(Duration::from_millis(500)).await; continue; // (correct_sns, sockaddr) = parse_sns(&correct_sns); } else { // correct_sns == 0, just error panic!("no correct sns specified"); } } node_conf.super_nodes = sockaddr; break; } node_conf.name = args.name.to_owned(); node_conf.mtu = args.mtu; node_conf.node_uuid = uuid; if args.register_ttl > 1 { node_conf.register_ttl = args.register_ttl; } Ok(node_conf) } fn parse_sns<'a>(sns: &'a Vec<&'a str>) -> (Vec<&'a str>, Vec) { let mut correct_sns = vec![]; let mut result = vec![]; for sn in sns { let addr: Vec<_> = sn.split(":").collect(); if addr.len() != 2 { error!("sn format error: [host:port] => {}", sn); continue; } let Ok(port) = addr[1].parse::() else { continue; }; // store correct format sns correct_sns.push(*sn); // node_conf.super_nodes.push(sock); if let Ok(ipaddr) = dns_lookup::lookup_host(addr[0]) { for ip in ipaddr { let sockaddr = SocketAddr::new(ip, port); let Ok(sock) = get_sdlan_sock_from_socketaddr(sockaddr) else { error!("failed to parse sn: {}", sn); continue; }; result.push(sock); } } else { error!("failed to parse host: {}", sn); continue; } } (correct_sns, result) } #[cfg(test)] mod test { use sdlan_sn_rs::config::AF_INET; use super::*; #[test] #[ignore] fn test_parse_dns() { let sns = vec!["git.asxalex.pw:80", "121.4.79.234:81"]; let (correct, res) = parse_sns(&sns); assert_eq!(correct, sns); assert_eq!( res, vec![ SdlanSock { family: AF_INET, port: 80, v4: [10, 167, 69, 157], v6: [0; 16], }, SdlanSock { family: AF_INET, port: 81, v4: [121, 4, 79, 234], v6: [0; 16], } ] ) } }