added set_route_from_net api
This commit is contained in:
parent
03b8da17dc
commit
51cdeb285e
@ -67,11 +67,13 @@ fn parse_connect_result(res: Result<ConnectResponse>) -> ConnectData {
|
||||
eprintln!("failed to connect: {}", data.message);
|
||||
process::exit(-3);
|
||||
}
|
||||
if data.data.is_none() {
|
||||
eprintln!("connect empty response");
|
||||
process::exit(-3);
|
||||
match data.data {
|
||||
None => {
|
||||
eprintln!("connect empty response");
|
||||
process::exit(-3);
|
||||
}
|
||||
Some(data) => data,
|
||||
}
|
||||
data.data.unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,14 +77,12 @@ pub async fn read_and_parse_packet(
|
||||
*/
|
||||
|
||||
buf.truncate(size);
|
||||
println!("handle udp packet");
|
||||
match handle_packet(eee, from, &buf).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
error!("failed to handle_packet: {:?}", e);
|
||||
}
|
||||
}
|
||||
println!("handle udp packet ok");
|
||||
}
|
||||
}
|
||||
|
||||
@ -832,7 +830,7 @@ async fn renew_identity_request(eee: &Node, identity: u32) {
|
||||
version: 1,
|
||||
};
|
||||
|
||||
println!("policy request: {:?}", policy_request);
|
||||
// println!("policy request: {:?}", policy_request);
|
||||
// debug!("send register super: {:?}", register_super);
|
||||
// let packet_id = edge.get_next_packet_id();
|
||||
let data = encode_to_tcp_message(
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
use std::{collections::HashMap, fs, io::{BufRead, BufReader}, net::Ipv4Addr, sync::atomic::{AtomicBool, Ordering}, time::Duration};
|
||||
use std::{collections::HashMap, fs, io::{BufRead, BufReader}, net::Ipv4Addr, sync::{Arc, atomic::{AtomicBool, Ordering}}, time::Duration};
|
||||
|
||||
use ahash::RandomState;
|
||||
use arc_swap::ArcSwap;
|
||||
use dashmap::{DashMap};
|
||||
use ipnet::Ipv4Net;
|
||||
use sdlan_sn_rs::utils::{Result, SDLanError};
|
||||
@ -10,7 +11,14 @@ use tracing::{debug, error};
|
||||
|
||||
use crate::{RouteTableTrie, network::tun::{add_route, del_route}, pb::{SdlArpResponse, SdlStunReply}};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RouteInfo {
|
||||
pub net: Ipv4Net,
|
||||
pub gw: Ipv4Addr,
|
||||
}
|
||||
|
||||
pub struct RouteTable2 {
|
||||
pub default_gw: ArcSwap<Option<Ipv4Addr>>,
|
||||
pub cache_table: DashMap<(Ipv4Net, Ipv4Addr), AtomicBool, RandomState>,
|
||||
pub route_table: RouteTableTrie,
|
||||
}
|
||||
@ -18,11 +26,32 @@ pub struct RouteTable2 {
|
||||
impl RouteTable2 {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
default_gw: ArcSwap::new(Arc::new(None)),
|
||||
cache_table: DashMap::with_hasher(RandomState::new()),
|
||||
route_table: RouteTableTrie::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_and_add_routes(&self, routes: Vec<RouteInfo>) -> Result<()> {
|
||||
for route in self.cache_table.iter() {
|
||||
let (net, gw) = route.key();
|
||||
if route.value().load(Ordering::Relaxed) {
|
||||
if let Err(e) = del_route(net, gw) {
|
||||
error!("failed to del route: {}", e.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
self.cache_table.clear();
|
||||
self.route_table.clear();
|
||||
|
||||
for route in routes {
|
||||
self.cache_table.insert((route.net, route.gw), AtomicBool::new(false));
|
||||
self.route_table.insert(route.net.addr().into(), route.net.prefix_len(), route.gw);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// parse the route_file or route_str and add to the route table.
|
||||
pub fn parse_and_add_route(&self, route_file: &str, route_str: &str) -> Result<()> {
|
||||
let routes = match true {
|
||||
_ if route_str.len() != 0 => {
|
||||
@ -42,6 +71,17 @@ impl RouteTable2 {
|
||||
}
|
||||
}
|
||||
|
||||
for route in self.cache_table.iter() {
|
||||
let (net, gw) = route.key();
|
||||
if route.value().load(Ordering::Relaxed) {
|
||||
if let Err(e) = del_route(net, gw) {
|
||||
error!("failed to del route: {}", e.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
self.cache_table.clear();
|
||||
self.route_table.clear();
|
||||
|
||||
for route in routes.keys() {
|
||||
self.cache_table.insert(*route, AtomicBool::new(false));
|
||||
self.route_table.insert(route.0.addr().into(), route.0.prefix_len(), route.1);
|
||||
@ -52,13 +92,12 @@ impl RouteTable2 {
|
||||
pub fn apply_system(&self) {
|
||||
for route in &self.cache_table {
|
||||
let (net, gw) = route.key();
|
||||
if let Err(e) = del_route(net, gw) {
|
||||
error!("failed to del route: {}", e.as_str());
|
||||
if !route.value().load(Ordering::Relaxed) {
|
||||
if let Err(e) = add_route(net, gw) {
|
||||
error!("failed to add route: {}", e.as_str());
|
||||
}
|
||||
}
|
||||
// should add to system
|
||||
if let Err(e) = add_route(net, gw) {
|
||||
error!("failed to add route: {}", e.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -287,10 +287,6 @@ impl TunTapPacketHandler for Iface {
|
||||
if let Some(ip) = headers.net {
|
||||
match ip {
|
||||
etherparse::NetHeaders::Ipv4(ipv4, _) => {
|
||||
if u32::from_be_bytes(ipv4.destination) == u32::from_be_bytes([10, 10, 2, 6]) {
|
||||
let raw_data = &data[14..];
|
||||
println!("got packet to 10.10.2.6(size={}): {:?}", raw_data.len(), raw_data);
|
||||
}
|
||||
|
||||
use crate::FiveTuple;
|
||||
use etherparse::IpNumber;
|
||||
|
||||
@ -613,7 +613,7 @@ pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> std::
|
||||
|
||||
//println!("res1: {}", res.status.success());
|
||||
|
||||
println!("route set ok");
|
||||
debug!("route set ok");
|
||||
let res = Command::new("netsh")
|
||||
.arg("dnsclient")
|
||||
.arg("set")
|
||||
@ -626,7 +626,7 @@ pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> std::
|
||||
.output()?;
|
||||
// println!("res2: {}", res.status.success());
|
||||
|
||||
println!("netsh set ok");
|
||||
debug!("netsh set ok");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ use dashmap::DashMap;
|
||||
use once_cell::sync::OnceCell;
|
||||
use sdlan_sn_rs::{
|
||||
config::SDLAN_DEFAULT_TTL,
|
||||
utils::{get_current_timestamp, ip_to_string, Mac},
|
||||
utils::{Mac, Result, get_current_timestamp, ip_to_string},
|
||||
};
|
||||
|
||||
use tracing::debug;
|
||||
@ -14,8 +14,8 @@ use tracing::debug;
|
||||
use tracing::error;
|
||||
|
||||
use crate::{
|
||||
network::{form_ethernet_packet, send_packet_to_net},
|
||||
pb::{encode_to_udp_message, SdlData},
|
||||
network::{RouteInfo, form_ethernet_packet, send_packet_to_net},
|
||||
pb::{SdlData, encode_to_udp_message},
|
||||
tcp::PacketType,
|
||||
utils::mac_to_string,
|
||||
};
|
||||
@ -29,6 +29,14 @@ pub trait TunTapPacketHandler {
|
||||
async fn handle_packet_from_device(&self, data: BytesMut) -> std::io::Result<()>;
|
||||
}
|
||||
|
||||
|
||||
pub fn set_route_from_net(routes: Vec<RouteInfo>) -> Result<()>{
|
||||
let eee = get_edge();
|
||||
eee.route_table.clear_and_add_routes(routes)?;
|
||||
eee.route_table.apply_system();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/*
|
||||
static ARP_WAIT_LIST: OnceCell<ArpWaitList> = OnceCell::new();
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ use sdlan_sn_rs::{config::AF_INET, peer::{SdlanSock, V6Info}, utils::{Result, SD
|
||||
use tokio::{sync::mpsc::{Receiver, Sender, channel}, time::sleep};
|
||||
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::{debug, error, warn};
|
||||
use tracing::{debug, error, warn, info};
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
use crate::network::{set_allow_routing, set_disallow_routing};
|
||||
@ -30,7 +30,7 @@ impl ReadWriterHandle {
|
||||
error!("failed to send to send_to_tcp: {}", e.to_string());
|
||||
return Err(SDLanError::NormalError("failed to send"));
|
||||
};
|
||||
debug!("tcp info sent");
|
||||
// debug!("tcp info sent");
|
||||
} else {
|
||||
error!("tcp not connected, so not sending data");
|
||||
return Err(SDLanError::NormalError("not connected, so not sending"));
|
||||
@ -84,7 +84,7 @@ impl ReadWriterHandle {
|
||||
handle_tcp_message(msg).await;
|
||||
} else {
|
||||
error!("data from tcp exited");
|
||||
println!("data from tcp exited");
|
||||
// println!("data from tcp exited");
|
||||
// eprintln!("data from tcp exited");
|
||||
return;
|
||||
}
|
||||
@ -113,7 +113,7 @@ async fn handle_tcp_message(msg: SdlanTcp) {
|
||||
// let now = get_current_timestamp();
|
||||
// edge.tcp_pong.store(now, Ordering::Relaxed);
|
||||
|
||||
debug!("got tcp message: {:?}", msg.packet_type);
|
||||
debug!("handling tcp message: {:?}", msg.packet_type);
|
||||
match msg.packet_type {
|
||||
PacketType::RegisterSuperACK => {
|
||||
let Ok(ack) = SdlRegisterSuperAck::decode(&msg.current_packet[..]) else {
|
||||
@ -129,7 +129,7 @@ async fn handle_tcp_message(msg: SdlanTcp) {
|
||||
should_exit: false,
|
||||
},
|
||||
);
|
||||
debug!("got register super ack: {:?}", ack);
|
||||
debug!("got register super ack");
|
||||
edge.session_token.set(ack.session_token);
|
||||
let Ok(key) = rsa_decrypt(&edge.rsa_private, &ack.key) else {
|
||||
error!("failed to rsa decrypt aes key");
|
||||
@ -161,6 +161,7 @@ async fn handle_tcp_message(msg: SdlanTcp) {
|
||||
let ip = ip_to_string(&edge.device_config.get_ip());
|
||||
// debug!("aes key is {:?}, ip is {}/{}", aes, ip, dev.net_bit_len,);
|
||||
println!("assigned ip: {}", ip);
|
||||
debug!("assigned ip: {}", ip);
|
||||
// let hostname = edge.hostname.read().unwrap().clone();
|
||||
// println!("network is: {}.{}", hostname, dev.network_domain);
|
||||
/*
|
||||
@ -561,23 +562,10 @@ impl ReadWriteActor {
|
||||
}
|
||||
} else {
|
||||
// None, just return
|
||||
println!("start or stop is None");
|
||||
info!("start or stop is None");
|
||||
return;
|
||||
}
|
||||
}
|
||||
/*
|
||||
while let Some(m) = start_stop_chan.recv().await {
|
||||
println!("4");
|
||||
if m.is_start {
|
||||
// println!("start received");
|
||||
started = true;
|
||||
start_pkt_id = m.pkt_id;
|
||||
break;
|
||||
} else {
|
||||
// println!("stop received");
|
||||
}
|
||||
}
|
||||
*/
|
||||
debug!("start stop chan received: {}", started);
|
||||
continue;
|
||||
}
|
||||
@ -622,7 +610,7 @@ impl ReadWriteActor {
|
||||
let local_ip = conn.local_ip();
|
||||
|
||||
let Ok((mut send, mut recv)) = conn.open_bi().await else {
|
||||
println!("failed to open-bi");
|
||||
error!("failed to open-bi");
|
||||
self.connected.store(false, Ordering::Relaxed);
|
||||
if keep_reconnect {
|
||||
tokio::time::sleep(Duration::from_secs(3)).await;
|
||||
@ -652,7 +640,7 @@ impl ReadWriteActor {
|
||||
loop {
|
||||
match read_a_packet(&mut recv).await {
|
||||
Ok(packet) => {
|
||||
warn!("got packet: {:?}", packet);
|
||||
warn!("got packet: {:?}", packet.packet_type);
|
||||
if let Err(_e) = self.from_tcp.send(packet).await {
|
||||
error!("failed to receive a packet: {:?}", _e);
|
||||
}
|
||||
@ -667,10 +655,10 @@ impl ReadWriteActor {
|
||||
|
||||
let write_to_tcp = async {
|
||||
while let Some(data) = to_tcp.recv().await {
|
||||
debug!("data size = {}", data.len());
|
||||
// debug!("data size = {}", data.len());
|
||||
match send.write(&data).await {
|
||||
Ok(size) => {
|
||||
debug!("{} bytes sent to tcp", size);
|
||||
Ok(_size) => {
|
||||
// debug!("{} bytes sent to tcp", size);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("failed to write to tcp: {}", e.to_string());
|
||||
@ -772,7 +760,7 @@ async fn on_connected_callback(local_ip: Option<IpAddr>, stream: &mut SendStream
|
||||
hostname: edge.hostname.read().unwrap().clone(),
|
||||
};
|
||||
|
||||
println!("register super: {:?}", register_super);
|
||||
// println!("register super: {:?}", register_super);
|
||||
|
||||
// debug!("send register super: {:?}", register_super);
|
||||
// let packet_id = edge.get_next_packet_id();
|
||||
|
||||
@ -74,6 +74,10 @@ impl RouteTableTrie {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear(&self) {
|
||||
self.trie.store(Arc::new(IpTrie::default()));
|
||||
}
|
||||
|
||||
pub fn lookup(&self, ip: u32) -> Option<(u8, Ipv4Addr)> {
|
||||
let trie = self.trie.load();
|
||||
trie.lookup(ip)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user