fix stun request
This commit is contained in:
parent
408aedc0ab
commit
cf08d2d53c
3
Makefile
3
Makefile
@ -1,3 +1,6 @@
|
||||
linux:
|
||||
RUSTFLAGS="-L ." cargo build --release
|
||||
|
||||
pb:
|
||||
cargo run --bin build_pb
|
||||
mv src/pb/_.rs src/pb/message.rs
|
||||
|
||||
@ -4,11 +4,15 @@ use sdlan_rs::CommandLine;
|
||||
use sdlan_sn_rs::log;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc::Sender;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let _guard = log::init_log();
|
||||
tokio::spawn(run_sdlan(CommandLine {
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
tokio::spawn(run_sdlan(
|
||||
CommandLine {
|
||||
sn: "39.98.184.67:1265".to_owned(),
|
||||
tcp: "39.98.184.67:18083".to_owned(),
|
||||
_allow_routing: true,
|
||||
@ -17,9 +21,14 @@ async fn main() {
|
||||
name: "tau".to_owned(),
|
||||
tos: 0,
|
||||
token: "".to_owned(),
|
||||
}));
|
||||
allow_p2p: true,
|
||||
},
|
||||
tx,
|
||||
));
|
||||
|
||||
// waiting edge to be inited
|
||||
let _ = rx.await;
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(3)).await;
|
||||
let edge = get_edge();
|
||||
edge.start("0".to_owned()).await;
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@ use sdlan_sn_rs::{
|
||||
|
||||
pub async fn run_sdlan(
|
||||
args: CommandLine,
|
||||
sender: tokio::sync::oneshot::Sender<bool>,
|
||||
// start_stop_sender: Sender<String>,
|
||||
// start_stop_receiver: Receiver<String>,
|
||||
) -> Result<()> {
|
||||
@ -33,6 +34,7 @@ pub async fn run_sdlan(
|
||||
if let Err(e) = init_edge(&args.token, node_conf, args.tos, start_stop_sender).await {
|
||||
panic!("failed to init edge: {:?}", e);
|
||||
}
|
||||
let _ = sender.send(true);
|
||||
debug!("edge inited");
|
||||
|
||||
let cancel = CancellationToken::new();
|
||||
@ -50,6 +52,7 @@ async fn parse_config(uuid: String, args: &CommandLine) -> Result<NodeConfig> {
|
||||
return Err(SDLanError::NormalError("no sn is specified"));
|
||||
}
|
||||
let mut node_conf = NodeConfig::new();
|
||||
node_conf.allow_p2p = args.allow_p2p;
|
||||
|
||||
let sns: Vec<&str> = args.sn.split(",").collect();
|
||||
let mut correct_sns: Vec<_>;
|
||||
|
||||
@ -65,6 +65,7 @@ async fn handle_tcp_message(msg: SdlanTcp) {
|
||||
edge.network_id.store(dev.network_id, Ordering::Relaxed);
|
||||
|
||||
edge.set_authorized(true, aes);
|
||||
send_stun_request(edge).await;
|
||||
}
|
||||
PacketType::RegisterSuperNAK => {
|
||||
let Ok(_nak) = SdlRegisterSuperNak::decode(&msg.current_packet[..]) else {
|
||||
@ -223,6 +224,7 @@ pub async fn async_main(
|
||||
|
||||
debug!("waiting for authorization...");
|
||||
|
||||
/*
|
||||
loop {
|
||||
// let _ = edge.send_register_super().await;
|
||||
// let _ = read_and_parse_packet(edge, &edge.udp_sock_v4, Some(Duration::from_secs(3))).await;
|
||||
@ -239,6 +241,7 @@ pub async fn async_main(
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
{
|
||||
let cancel = cancel.clone();
|
||||
@ -303,6 +306,26 @@ async fn run_edge_loop(eee: &'static Node, cancel: CancellationToken) {
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_stun_request(eee: &Node) {
|
||||
let req = SdlStunRequest {
|
||||
cookie: 0,
|
||||
client_id: eee.config.node_uuid.clone(),
|
||||
network_id: eee.network_id.load(Ordering::Relaxed),
|
||||
ip: eee.device_config.get_ip(),
|
||||
nat_type: 0,
|
||||
};
|
||||
let msg = encode_to_udp_message(Some(req), PacketType::StunRequest as u8).unwrap();
|
||||
if let Err(e) = send_to_sock(
|
||||
eee,
|
||||
&msg,
|
||||
&eee.config.super_nodes[eee.config.super_node_index.load(Ordering::Relaxed) as usize],
|
||||
)
|
||||
.await
|
||||
{
|
||||
error!("failed to send to sock: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
async fn loop_socket_v4(eee: &Node, socket: &Socket, cancel: CancellationToken) {
|
||||
debug!("loop sock v4");
|
||||
loop {
|
||||
@ -312,6 +335,8 @@ async fn loop_socket_v4(eee: &Node, socket: &Socket, cancel: CancellationToken)
|
||||
}
|
||||
_ = read_and_parse_packet(eee, socket,Some(Duration::from_secs(10))) => { }
|
||||
_ = tokio::time::sleep(Duration::from_secs(10)) => {
|
||||
send_stun_request(eee).await;
|
||||
/*
|
||||
let req = SdlStunRequest {
|
||||
cookie: 0,
|
||||
client_id: eee.config.node_uuid.clone(),
|
||||
@ -322,7 +347,7 @@ async fn loop_socket_v4(eee: &Node, socket: &Socket, cancel: CancellationToken)
|
||||
let msg = encode_to_udp_message(Some(req), PacketType::StunRequest as u8).unwrap();
|
||||
if let Err(e) = send_to_sock(eee, &msg, &eee.config.super_nodes[eee.config.super_node_index.load(Ordering::Relaxed) as usize]).await {
|
||||
error!("failed to send to sock: {:?}", e);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,6 +100,8 @@ pub async fn handle_packet(eee: &Node, addr: SocketAddr, buf: &[u8]) -> Result<(
|
||||
let _ = handle_packet_register_ack(eee, &buf[1..], &from_sock).await;
|
||||
}
|
||||
PacketType::Data => {
|
||||
let from_sock = get_sdlan_sock_from_socketaddr(addr).unwrap();
|
||||
debug!("[PPP]Rx data from {}", from_sock.to_string());
|
||||
if !eee.is_authorized() {
|
||||
error!("dropping PACKET received before authorized");
|
||||
return Ok(());
|
||||
@ -109,7 +111,7 @@ pub async fn handle_packet(eee: &Node, addr: SocketAddr, buf: &[u8]) -> Result<(
|
||||
error!("failed to decode to SDLData");
|
||||
return Err(SDLanError::NormalError("failed to decode to SDLData"));
|
||||
};
|
||||
let from_sock = get_sdlan_sock_from_socketaddr(addr).unwrap();
|
||||
// let from_sock = get_sdlan_sock_from_socketaddr(addr).unwrap();
|
||||
if data.is_p2p {
|
||||
debug!("[P2P] Rx data from {}", from_sock.to_string());
|
||||
} else {
|
||||
|
||||
@ -29,6 +29,9 @@ pub struct CommandLine {
|
||||
|
||||
#[structopt(long = "token", default_value = "0")]
|
||||
pub token: String,
|
||||
|
||||
#[structopt(short = "p")]
|
||||
pub allow_p2p: bool,
|
||||
}
|
||||
|
||||
impl Clone for CommandLine {
|
||||
@ -42,6 +45,7 @@ impl Clone for CommandLine {
|
||||
name: self.name.clone(),
|
||||
tos: self.tos,
|
||||
token: self.token.clone(),
|
||||
allow_p2p: self.allow_p2p,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user