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:
|
pb:
|
||||||
cargo run --bin build_pb
|
cargo run --bin build_pb
|
||||||
mv src/pb/_.rs src/pb/message.rs
|
mv src/pb/_.rs src/pb/message.rs
|
||||||
|
|||||||
@ -4,22 +4,31 @@ use sdlan_rs::CommandLine;
|
|||||||
use sdlan_sn_rs::log;
|
use sdlan_sn_rs::log;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
|
use tokio::sync::oneshot;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let _guard = log::init_log();
|
let _guard = log::init_log();
|
||||||
tokio::spawn(run_sdlan(CommandLine {
|
|
||||||
sn: "39.98.184.67:1265".to_owned(),
|
|
||||||
tcp: "39.98.184.67:18083".to_owned(),
|
|
||||||
_allow_routing: true,
|
|
||||||
register_ttl: 1,
|
|
||||||
mtu: 1290,
|
|
||||||
name: "tau".to_owned(),
|
|
||||||
tos: 0,
|
|
||||||
token: "".to_owned(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
tokio::time::sleep(Duration::from_secs(3)).await;
|
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,
|
||||||
|
register_ttl: 1,
|
||||||
|
mtu: 1290,
|
||||||
|
name: "tau".to_owned(),
|
||||||
|
tos: 0,
|
||||||
|
token: "".to_owned(),
|
||||||
|
allow_p2p: true,
|
||||||
|
},
|
||||||
|
tx,
|
||||||
|
));
|
||||||
|
|
||||||
|
// waiting edge to be inited
|
||||||
|
let _ = rx.await;
|
||||||
|
|
||||||
let edge = get_edge();
|
let edge = get_edge();
|
||||||
edge.start("0".to_owned()).await;
|
edge.start("0".to_owned()).await;
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ use sdlan_sn_rs::{
|
|||||||
|
|
||||||
pub async fn run_sdlan(
|
pub async fn run_sdlan(
|
||||||
args: CommandLine,
|
args: CommandLine,
|
||||||
|
sender: tokio::sync::oneshot::Sender<bool>,
|
||||||
// start_stop_sender: Sender<String>,
|
// start_stop_sender: Sender<String>,
|
||||||
// start_stop_receiver: Receiver<String>,
|
// start_stop_receiver: Receiver<String>,
|
||||||
) -> Result<()> {
|
) -> 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 {
|
if let Err(e) = init_edge(&args.token, node_conf, args.tos, start_stop_sender).await {
|
||||||
panic!("failed to init edge: {:?}", e);
|
panic!("failed to init edge: {:?}", e);
|
||||||
}
|
}
|
||||||
|
let _ = sender.send(true);
|
||||||
debug!("edge inited");
|
debug!("edge inited");
|
||||||
|
|
||||||
let cancel = CancellationToken::new();
|
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"));
|
return Err(SDLanError::NormalError("no sn is specified"));
|
||||||
}
|
}
|
||||||
let mut node_conf = NodeConfig::new();
|
let mut node_conf = NodeConfig::new();
|
||||||
|
node_conf.allow_p2p = args.allow_p2p;
|
||||||
|
|
||||||
let sns: Vec<&str> = args.sn.split(",").collect();
|
let sns: Vec<&str> = args.sn.split(",").collect();
|
||||||
let mut correct_sns: Vec<_>;
|
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.network_id.store(dev.network_id, Ordering::Relaxed);
|
||||||
|
|
||||||
edge.set_authorized(true, aes);
|
edge.set_authorized(true, aes);
|
||||||
|
send_stun_request(edge).await;
|
||||||
}
|
}
|
||||||
PacketType::RegisterSuperNAK => {
|
PacketType::RegisterSuperNAK => {
|
||||||
let Ok(_nak) = SdlRegisterSuperNak::decode(&msg.current_packet[..]) else {
|
let Ok(_nak) = SdlRegisterSuperNak::decode(&msg.current_packet[..]) else {
|
||||||
@ -223,6 +224,7 @@ pub async fn async_main(
|
|||||||
|
|
||||||
debug!("waiting for authorization...");
|
debug!("waiting for authorization...");
|
||||||
|
|
||||||
|
/*
|
||||||
loop {
|
loop {
|
||||||
// let _ = edge.send_register_super().await;
|
// let _ = edge.send_register_super().await;
|
||||||
// let _ = read_and_parse_packet(edge, &edge.udp_sock_v4, Some(Duration::from_secs(3))).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();
|
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) {
|
async fn loop_socket_v4(eee: &Node, socket: &Socket, cancel: CancellationToken) {
|
||||||
debug!("loop sock v4");
|
debug!("loop sock v4");
|
||||||
loop {
|
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))) => { }
|
_ = read_and_parse_packet(eee, socket,Some(Duration::from_secs(10))) => { }
|
||||||
_ = tokio::time::sleep(Duration::from_secs(10)) => {
|
_ = tokio::time::sleep(Duration::from_secs(10)) => {
|
||||||
|
send_stun_request(eee).await;
|
||||||
|
/*
|
||||||
let req = SdlStunRequest {
|
let req = SdlStunRequest {
|
||||||
cookie: 0,
|
cookie: 0,
|
||||||
client_id: eee.config.node_uuid.clone(),
|
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();
|
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 {
|
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);
|
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;
|
let _ = handle_packet_register_ack(eee, &buf[1..], &from_sock).await;
|
||||||
}
|
}
|
||||||
PacketType::Data => {
|
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() {
|
if !eee.is_authorized() {
|
||||||
error!("dropping PACKET received before authorized");
|
error!("dropping PACKET received before authorized");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@ -109,7 +111,7 @@ pub async fn handle_packet(eee: &Node, addr: SocketAddr, buf: &[u8]) -> Result<(
|
|||||||
error!("failed to decode to SDLData");
|
error!("failed to decode to SDLData");
|
||||||
return Err(SDLanError::NormalError("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 {
|
if data.is_p2p {
|
||||||
debug!("[P2P] Rx data from {}", from_sock.to_string());
|
debug!("[P2P] Rx data from {}", from_sock.to_string());
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -29,6 +29,9 @@ pub struct CommandLine {
|
|||||||
|
|
||||||
#[structopt(long = "token", default_value = "0")]
|
#[structopt(long = "token", default_value = "0")]
|
||||||
pub token: String,
|
pub token: String,
|
||||||
|
|
||||||
|
#[structopt(short = "p")]
|
||||||
|
pub allow_p2p: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for CommandLine {
|
impl Clone for CommandLine {
|
||||||
@ -42,6 +45,7 @@ impl Clone for CommandLine {
|
|||||||
name: self.name.clone(),
|
name: self.name.clone(),
|
||||||
tos: self.tos,
|
tos: self.tos,
|
||||||
token: self.token.clone(),
|
token: self.token.clone(),
|
||||||
|
allow_p2p: self.allow_p2p,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user