This commit is contained in:
asxalex 2024-10-22 10:12:08 +08:00
parent 56d6a35fea
commit 302300294a
9 changed files with 63 additions and 14 deletions

1
Cargo.lock generated
View File

@ -1442,6 +1442,7 @@ dependencies = [
"once_cell", "once_cell",
"prost", "prost",
"prost-build", "prost-build",
"rand",
"rsa", "rsa",
"sdlan-sn-rs", "sdlan-sn-rs",
"structopt", "structopt",

View File

@ -13,6 +13,7 @@ num_enum = "0.7.2"
once_cell = "1.19.0" once_cell = "1.19.0"
prost = "0.12.6" prost = "0.12.6"
prost-build = "0.12.6" prost-build = "0.12.6"
rand = "0.8.5"
rsa = "0.9.6" rsa = "0.9.6"
sdlan-sn-rs = { git = "ssh://git@git.asxalex.pw/sdlan-v2/sdlan-rs.git" } sdlan-sn-rs = { git = "ssh://git@git.asxalex.pw/sdlan-v2/sdlan-rs.git" }
# sdlan-sn-rs = { git = "https://git.asxalex.pw/sdlan-v2/sdlan-rs.git" } # sdlan-sn-rs = { git = "https://git.asxalex.pw/sdlan-v2/sdlan-rs.git" }

View File

@ -4,6 +4,8 @@ use sdlan_rs::CommandLine;
use sdlan_rs::CommandLineInput; use sdlan_rs::CommandLineInput;
use sdlan_sn_rs::log; use sdlan_sn_rs::log;
use tracing::{debug, error};
use std::process::exit; use std::process::exit;
use std::time::Duration; use std::time::Duration;
use structopt::StructOpt; use structopt::StructOpt;
@ -26,7 +28,7 @@ async fn main() {
mtu: 1290, mtu: 1290,
name: "tau".to_owned(), name: "tau".to_owned(),
tos: 0, tos: 0,
token: "".to_owned(), token: "test-token".to_owned(),
allow_p2p: true, allow_p2p: true,
}, },
tx, tx,
@ -39,14 +41,20 @@ async fn main() {
let edge = get_edge(); let edge = get_edge();
// let res = edge.start_without_feedback(cmd.token).await; // let res = edge.start_without_feedback(cmd.token).await;
let Ok(res) = edge /*
let res = match edge
.start_with_feedback(cmd.token, Duration::from_secs(3)) .start_with_feedback(cmd.token, Duration::from_secs(3))
.await .await
else { {
println!("failed to start1"); Ok(res) => res,
Err(e) => {
error!("failed to start: {:?}", e);
exit(0); exit(0);
}
}; };
debug!("here1");
if res.result != 0 { if res.result != 0 {
println!("failed to start: {}", res.message); println!("failed to start: {}", res.message);
if res.should_exit { if res.should_exit {
@ -55,6 +63,10 @@ async fn main() {
// edge.stop().await; // edge.stop().await;
// exit(0); // exit(0);
} }
*/
if let Err(e) = edge.start_without_feedback(cmd.token).await {
error!("failed to start: {:?}", e);
}
/* /*
tokio::time::sleep(Duration::from_secs(20)).await; tokio::time::sleep(Duration::from_secs(20)).await;

View File

@ -162,7 +162,11 @@ impl ArpInfo {
} }
let first_ip = (ip >> 24) as u8 & 0xff; let first_ip = (ip >> 24) as u8 & 0xff;
if first_ip >= 224 && first_ip <= 239 { if first_ip >= 224 && first_ip <= 239 {
return (MULTICAST_MAC, ip, false); let mut multi = MULTICAST_MAC;
multi[3] = (ip >> 16) as u8 & 0x7f;
multi[4] = (ip >> 8) as u8 & 0xff;
multi[5] = (ip) as u8 & 0xff;
return (multi, ip, false);
} }
let mut target_ip = 0; let mut target_ip = 0;
let edge = get_edge(); let edge = get_edge();

View File

@ -76,7 +76,7 @@ async fn handle_tcp_message(msg: SdlanTcp) {
Err(_) => NULL_MAC, Err(_) => NULL_MAC,
Ok(m) => m, Ok(m) => m,
}; };
*edge.device_config.mac.write().unwrap() = mac; // *edge.device_config.mac.write().unwrap() = mac;
edge.device_config edge.device_config
.ip .ip
.net_bit_len .net_bit_len
@ -304,7 +304,7 @@ pub async fn async_main(
installed_channel, installed_channel,
client_id: edge.config.node_uuid.clone(), client_id: edge.config.node_uuid.clone(),
dev_addr: Some(SdlDevAddr { dev_addr: Some(SdlDevAddr {
mac: vec![], mac: Vec::from(edge.device_config.get_mac()),
net_addr: 0, net_addr: 0,
network_id: 0, network_id: 0,
net_bit_len: 0, net_bit_len: 0,

View File

@ -1,6 +1,10 @@
use std::sync::RwLock; use std::sync::RwLock;
use crate::utils::mac_to_string;
use sdlan_sn_rs::{peer::IpSubnet, utils::Mac}; use sdlan_sn_rs::{peer::IpSubnet, utils::Mac};
use tracing::debug;
use crate::utils::generate_mac_address;
pub struct DeviceConfig { pub struct DeviceConfig {
pub mac: RwLock<Mac>, pub mac: RwLock<Mac>,
@ -9,8 +13,10 @@ pub struct DeviceConfig {
impl DeviceConfig { impl DeviceConfig {
pub fn new() -> Self { pub fn new() -> Self {
let mac = generate_mac_address();
debug!("self mac: {}", mac_to_string(&mac));
DeviceConfig { DeviceConfig {
mac: RwLock::new([0; 6]), mac: RwLock::new(mac),
ip: IpSubnet::new(0, 0), ip: IpSubnet::new(0, 0),
} }
} }
@ -34,7 +40,8 @@ impl DeviceConfig {
} }
pub fn get_mac(&self) -> Mac { pub fn get_mac(&self) -> Mac {
*self.mac.read().unwrap() let mac = *self.mac.read().unwrap();
mac
} }
} }

View File

@ -9,7 +9,7 @@ use std::time::Duration;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot; use tokio::sync::oneshot;
use tracing::error; use tracing::{debug, error};
use crate::pb::{ use crate::pb::{
encode_to_tcp_message, encode_to_udp_message, SdlEmpty, SdlStunProbe, SdlStunProbeReply, encode_to_tcp_message, encode_to_udp_message, SdlEmpty, SdlStunProbe, SdlStunProbeReply,
@ -203,6 +203,7 @@ impl Node {
pkt_id: Some(id), pkt_id: Some(id),
}) })
.await; .await;
debug!("start with feedback");
tokio::select! { tokio::select! {
rx_info = rx => { rx_info = rx => {

View File

@ -59,7 +59,7 @@ impl ReadWriteActor {
remote: remote.to_owned(), remote: remote.to_owned(),
from_tcp, from_tcp,
connecting_chan, connecting_chan,
ipv6_network_restarter ipv6_network_restarter,
} }
} }
@ -112,7 +112,7 @@ impl ReadWriteActor {
} }
} }
*/ */
debug!("start stop chan recv none"); debug!("start stop chan received: {}", started);
continue; continue;
} }
@ -136,6 +136,7 @@ impl ReadWriteActor {
return; return;
}; };
self.connected.store(true, Ordering::Relaxed); self.connected.store(true, Ordering::Relaxed);
debug!("connected");
on_connected(&mut stream, start_pkt_id.take()).await; on_connected(&mut stream, start_pkt_id.take()).await;
if let Some(ref connecting_chan) = self.connecting_chan { if let Some(ref connecting_chan) = self.connecting_chan {
@ -276,7 +277,15 @@ impl ReadWriterHandle {
let (from_tcp, mut data_from_tcp) = channel(20); let (from_tcp, mut data_from_tcp) = channel(20);
let connected: Arc<AtomicBool> = Arc::new(AtomicBool::new(false)); let connected: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
let actor = ReadWriteActor::new(cancel, addr, from_tcp, connected.clone(), pong_time, connecting_chan, ipv6_network_restarter); let actor = ReadWriteActor::new(
cancel,
addr,
from_tcp,
connected.clone(),
pong_time,
connecting_chan,
ipv6_network_restarter,
);
tokio::spawn(async move { tokio::spawn(async move {
actor actor
.run(true, to_tcp, on_connected, on_disconnected, start_stop_chan) .run(true, to_tcp, on_connected, on_disconnected, start_stop_chan)

View File

@ -2,6 +2,7 @@ mod command;
pub use command::*; pub use command::*;
mod socks; mod socks;
use rand::Rng;
use sdlan_sn_rs::utils::Mac; use sdlan_sn_rs::utils::Mac;
pub use socks::*; pub use socks::*;
@ -14,3 +15,16 @@ pub fn mac_to_string(mac: &Mac) -> String {
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
) )
} }
pub fn generate_mac_address() -> Mac {
let mut rng = rand::thread_rng();
let mut mac = [0; 6];
for i in 0..6 {
let number: u8 = rng.gen();
mac[i] = number;
}
mac[0] &= !0x01;
mac[0] |= 0x02;
mac
}