15353's parse is ok, should restore /etc/resolv.conf while exitting
This commit is contained in:
parent
41a21188bf
commit
fad345becb
@ -10,6 +10,7 @@ use sdlan_sn_rs::log;
|
|||||||
use sdlan_sn_rs::utils::gen_uuid_u64;
|
use sdlan_sn_rs::utils::gen_uuid_u64;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
|
use std::net::ToSocketAddrs;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
@ -31,7 +32,23 @@ async fn main() {
|
|||||||
// println!("port is {}", cmd.port);
|
// println!("port is {}", cmd.port);
|
||||||
|
|
||||||
let (tx, rx) = std::sync::mpsc::channel();
|
let (tx, rx) = std::sync::mpsc::channel();
|
||||||
let server = "punchnet.aioe.tech".to_owned();
|
|
||||||
|
let hostname = "punchnet.aioe.tech".to_owned();
|
||||||
|
let host = format!("{}:80", hostname);
|
||||||
|
let mut server = String::new();
|
||||||
|
if let Ok(addrs) = host.to_socket_addrs() {
|
||||||
|
for addr in addrs {
|
||||||
|
let h = addr.to_string().split(":").take(1).collect::<Vec<_>>()[0].to_owned();
|
||||||
|
server = h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if server.is_empty() {
|
||||||
|
println!("failed to resolv host ip");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("server is {}", server);
|
||||||
|
|
||||||
let _ = run_sdlan(
|
let _ = run_sdlan(
|
||||||
CommandLine {
|
CommandLine {
|
||||||
sn: server.clone()+":1265",
|
sn: server.clone()+":1265",
|
||||||
@ -51,6 +68,7 @@ async fn main() {
|
|||||||
},
|
},
|
||||||
tx,
|
tx,
|
||||||
&punchnet::get_install_channel(),
|
&punchnet::get_install_channel(),
|
||||||
|
server,
|
||||||
Some(format!("{:08x}", gen_uuid_u64() as u32)),
|
Some(format!("{:08x}", gen_uuid_u64() as u32)),
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
|||||||
@ -4,6 +4,7 @@ mod pb;
|
|||||||
mod tcp;
|
mod tcp;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
use std::{sync::atomic::AtomicU8, time::Duration};
|
use std::{sync::atomic::AtomicU8, time::Duration};
|
||||||
|
|
||||||
use std::net::{SocketAddr, ToSocketAddrs};
|
use std::net::{SocketAddr, ToSocketAddrs};
|
||||||
@ -12,6 +13,7 @@ pub use network::get_edge;
|
|||||||
pub use network::get_install_channel;
|
pub use network::get_install_channel;
|
||||||
use network::{async_main, init_arp, init_edge, NodeConfig};
|
use network::{async_main, init_arp, init_edge, NodeConfig};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
use tokio::sync::mpsc::{channel, Sender};
|
use tokio::sync::mpsc::{channel, Sender};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
@ -44,6 +46,8 @@ pub async fn run_sdlan(
|
|||||||
sender: std::sync::mpsc::Sender<bool>,
|
sender: std::sync::mpsc::Sender<bool>,
|
||||||
install_channel: &str,
|
install_channel: &str,
|
||||||
|
|
||||||
|
server_ip: String,
|
||||||
|
|
||||||
hostname: Option<String>,
|
hostname: Option<String>,
|
||||||
connecting_chan: Option<Sender<ConnectionInfo>>, // start_stop_sender: Sender<String>,
|
connecting_chan: Option<Sender<ConnectionInfo>>, // start_stop_sender: Sender<String>,
|
||||||
// start_stop_receiver: Receiver<String>,
|
// start_stop_receiver: Receiver<String>,
|
||||||
@ -56,6 +60,8 @@ pub async fn run_sdlan(
|
|||||||
|
|
||||||
let hostname = hostname.unwrap_or("".to_owned());
|
let hostname = hostname.unwrap_or("".to_owned());
|
||||||
|
|
||||||
|
let sock = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
|
||||||
|
|
||||||
if let Err(e) = init_edge(
|
if let Err(e) = init_edge(
|
||||||
&args.token,
|
&args.token,
|
||||||
&args.network_code,
|
&args.network_code,
|
||||||
@ -64,7 +70,9 @@ pub async fn run_sdlan(
|
|||||||
start_stop_sender,
|
start_stop_sender,
|
||||||
args.mtu,
|
args.mtu,
|
||||||
connecting_chan.clone(),
|
connecting_chan.clone(),
|
||||||
|
sock,
|
||||||
hostname,
|
hostname,
|
||||||
|
server_ip,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
|
|||||||
@ -20,6 +20,7 @@ use sdlan_sn_rs::peer::{SdlanSock, V6Info};
|
|||||||
use sdlan_sn_rs::utils::{get_current_timestamp, ip_to_string, is_multi_broadcast, rsa_decrypt};
|
use sdlan_sn_rs::utils::{get_current_timestamp, ip_to_string, is_multi_broadcast, rsa_decrypt};
|
||||||
use sdlan_sn_rs::utils::{Mac, Result};
|
use sdlan_sn_rs::utils::{Mac, Result};
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
@ -537,6 +538,19 @@ pub async fn loop_socket_v4(
|
|||||||
debug!("loop_socket_v4 exited");
|
debug!("loop_socket_v4 exited");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn receive_dns_reply(sock: &Arc<UdpSocket>) -> Option<Vec<u8>> {
|
||||||
|
let mut reply = vec![0;1024];
|
||||||
|
if let Ok((size, _from)) = sock.recv_from(&mut reply).await {
|
||||||
|
if size == 0 {
|
||||||
|
// closed
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
reply.truncate(size);
|
||||||
|
return Some(reply);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
async fn loop_tap(eee: &'static Node, cancel: CancellationToken) {
|
async fn loop_tap(eee: &'static Node, cancel: CancellationToken) {
|
||||||
debug!("loop tap");
|
debug!("loop tap");
|
||||||
let (tx, mut rx) = channel(10);
|
let (tx, mut rx) = channel(10);
|
||||||
@ -550,6 +564,23 @@ async fn loop_tap(eee: &'static Node, cancel: CancellationToken) {
|
|||||||
drop(rx);
|
drop(rx);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
reply = receive_dns_reply(&eee.udp_sock_for_dns) => {
|
||||||
|
if reply.is_none() {
|
||||||
|
drop(rx);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let reply = reply.unwrap();
|
||||||
|
let dstmac = eee.device_config.get_mac();
|
||||||
|
let srcmac = eee.device_config.dns_mac;
|
||||||
|
let mut packet = Vec::with_capacity(14+reply.len());
|
||||||
|
packet.extend_from_slice(&dstmac);
|
||||||
|
packet.extend_from_slice(&srcmac);
|
||||||
|
packet.push(0x08);
|
||||||
|
packet.push(0x00);
|
||||||
|
packet.extend_from_slice(&reply);
|
||||||
|
eee.device.handle_packet_from_net(&packet, &Vec::new()).await;
|
||||||
|
println!("got 15353's reply");
|
||||||
|
}
|
||||||
buf = rx.recv() => {
|
buf = rx.recv() => {
|
||||||
if buf.is_none() {
|
if buf.is_none() {
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -10,17 +10,21 @@ pub struct DeviceConfig {
|
|||||||
pub mtu: u32,
|
pub mtu: u32,
|
||||||
pub mac: RwLock<Mac>,
|
pub mac: RwLock<Mac>,
|
||||||
pub ip: IpSubnet,
|
pub ip: IpSubnet,
|
||||||
|
|
||||||
|
pub dns_mac: Mac,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DeviceConfig {
|
impl DeviceConfig {
|
||||||
pub fn new(mtu: u32) -> Self {
|
pub fn new(mtu: u32) -> Self {
|
||||||
let mac = generate_mac_address();
|
let mac = generate_mac_address();
|
||||||
|
let dns_mac = generate_mac_address();
|
||||||
println!("self mac: {}", mac_to_string(&mac));
|
println!("self mac: {}", mac_to_string(&mac));
|
||||||
debug!("self mac: {}", mac_to_string(&mac));
|
debug!("self mac: {}", mac_to_string(&mac));
|
||||||
DeviceConfig {
|
DeviceConfig {
|
||||||
mtu,
|
mtu,
|
||||||
mac: RwLock::new(mac),
|
mac: RwLock::new(mac),
|
||||||
ip: IpSubnet::new(0, 0),
|
ip: IpSubnet::new(0, 0),
|
||||||
|
dns_mac,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use rsa::RsaPrivateKey;
|
use rsa::RsaPrivateKey;
|
||||||
use sdlan_sn_rs::config::{AF_INET, AF_INET6};
|
use sdlan_sn_rs::config::{AF_INET, AF_INET6};
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicU8, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicU8, Ordering};
|
||||||
use std::sync::{Arc, Mutex, RwLock};
|
use std::sync::{Arc, Mutex, RwLock};
|
||||||
@ -38,7 +39,9 @@ pub async fn init_edge(
|
|||||||
start_stop: Sender<StartStopInfo>,
|
start_stop: Sender<StartStopInfo>,
|
||||||
mtu: u32,
|
mtu: u32,
|
||||||
connecting_chan: Option<Sender<ConnectionInfo>>,
|
connecting_chan: Option<Sender<ConnectionInfo>>,
|
||||||
|
udpsock_for_dns: Arc<UdpSocket>,
|
||||||
hostname: String,
|
hostname: String,
|
||||||
|
server_ip: String,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// gen public key
|
// gen public key
|
||||||
let rsa_path = format!("{}/.client", get_base_dir());
|
let rsa_path = format!("{}/.client", get_base_dir());
|
||||||
@ -83,6 +86,8 @@ pub async fn init_edge(
|
|||||||
mtu,
|
mtu,
|
||||||
connecting_chan,
|
connecting_chan,
|
||||||
hostname,
|
hostname,
|
||||||
|
udpsock_for_dns,
|
||||||
|
server_ip,
|
||||||
);
|
);
|
||||||
do_init_edge(edge)?;
|
do_init_edge(edge)?;
|
||||||
|
|
||||||
@ -123,6 +128,9 @@ pub struct Node {
|
|||||||
|
|
||||||
pub hostname: String,
|
pub hostname: String,
|
||||||
|
|
||||||
|
pub udp_sock_for_dns: Arc<UdpSocket>,
|
||||||
|
pub server_ip: String,
|
||||||
|
|
||||||
pub tcp_pong: Arc<AtomicU64>,
|
pub tcp_pong: Arc<AtomicU64>,
|
||||||
|
|
||||||
start_stop_sender: Sender<StartStopInfo>,
|
start_stop_sender: Sender<StartStopInfo>,
|
||||||
@ -266,6 +274,8 @@ impl Node {
|
|||||||
mtu: u32,
|
mtu: u32,
|
||||||
connecting_chan: Option<Sender<ConnectionInfo>>,
|
connecting_chan: Option<Sender<ConnectionInfo>>,
|
||||||
hostname: String,
|
hostname: String,
|
||||||
|
udpsock_for_dns: Arc<UdpSocket>,
|
||||||
|
server_ip: String,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mode = if cfg!(not(feature = "tun")) {
|
let mode = if cfg!(not(feature = "tun")) {
|
||||||
Mode::Tap
|
Mode::Tap
|
||||||
@ -277,6 +287,9 @@ impl Node {
|
|||||||
packet_id: AtomicU32::new(1),
|
packet_id: AtomicU32::new(1),
|
||||||
network_id: AtomicU32::new(0),
|
network_id: AtomicU32::new(0),
|
||||||
hostname,
|
hostname,
|
||||||
|
|
||||||
|
udp_sock_for_dns: udpsock_for_dns,
|
||||||
|
|
||||||
_token: Mutex::new(token.to_owned()),
|
_token: Mutex::new(token.to_owned()),
|
||||||
network_code: Mutex::new(network_code.to_owned()),
|
network_code: Mutex::new(network_code.to_owned()),
|
||||||
|
|
||||||
@ -322,6 +335,7 @@ impl Node {
|
|||||||
packet_id_match: DashMap::new(),
|
packet_id_match: DashMap::new(),
|
||||||
nat_cookie: AtomicU32::new(1),
|
nat_cookie: AtomicU32::new(1),
|
||||||
cookie_match: DashMap::new(),
|
cookie_match: DashMap::new(),
|
||||||
|
server_ip,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -25,6 +25,8 @@ use crate::tcp::PacketType;
|
|||||||
use super::device::{DeviceConfig, Mode};
|
use super::device::{DeviceConfig, Mode};
|
||||||
use super::TunTapPacketHandler;
|
use super::TunTapPacketHandler;
|
||||||
|
|
||||||
|
const DNS_IP: u32 = (100<<24) + (100<<16) + (100<<8) + 100;
|
||||||
|
|
||||||
// #[link(name = "tuntap", kind="static")]
|
// #[link(name = "tuntap", kind="static")]
|
||||||
#[link(name = "tuntap")]
|
#[link(name = "tuntap")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -186,11 +188,39 @@ impl TunTapPacketHandler for Iface {
|
|||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
encrypt_key: &[u8],
|
encrypt_key: &[u8],
|
||||||
) -> std::io::Result<()> {
|
) -> std::io::Result<()> {
|
||||||
|
use etherparse::PacketHeaders;
|
||||||
|
|
||||||
debug!("in tap mode2");
|
debug!("in tap mode2");
|
||||||
let edge = get_edge();
|
let edge = get_edge();
|
||||||
|
|
||||||
match Ethernet2Header::from_slice(&data) {
|
let Ok(headers) = PacketHeaders::from_ethernet_slice(&data) else {
|
||||||
Ok((hdr, _)) => {
|
error!("failed to parse packet");
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(eth) = headers.link {
|
||||||
|
if let Some(hdr) = eth.ethernet2() {
|
||||||
|
if let Some(ip) = headers.net {
|
||||||
|
match ip {
|
||||||
|
etherparse::NetHeaders::Ipv4(ipv4, _) => {
|
||||||
|
println!("3, target = {}.{}.{}.{}", ipv4.destination[0], ipv4.destination[1], ipv4.destination[2], ipv4.destination[3]);
|
||||||
|
if u32::from_be_bytes(ipv4.destination) == DNS_IP {
|
||||||
|
// should send to dns
|
||||||
|
println!("got dns request");
|
||||||
|
if let Err(e) = edge.udp_sock_for_dns.send_to(&data[14..], format!("{}:15353", edge.server_ip)).await {
|
||||||
|
println!("failed to send request to 15353: {}", e);
|
||||||
|
}
|
||||||
|
// edge.udp_sock_for_dns.send_to()
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_other => {
|
||||||
|
// just ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
let target = hdr.destination;
|
let target = hdr.destination;
|
||||||
if is_ipv6_multicast(&target) {
|
if is_ipv6_multicast(&target) {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@ -212,11 +242,13 @@ impl TunTapPacketHandler for Iface {
|
|||||||
let msg = encode_to_udp_message(Some(data), PacketType::Data as u8).unwrap();
|
let msg = encode_to_udp_message(Some(data), PacketType::Data as u8).unwrap();
|
||||||
|
|
||||||
send_packet_to_net(edge, target, &msg, size as u64).await;
|
send_packet_to_net(edge, target, &msg, size as u64).await;
|
||||||
|
} else {
|
||||||
|
println!("erro 2");
|
||||||
}
|
}
|
||||||
Err(_) => {
|
} else {
|
||||||
error!("failed to parse packet from device");
|
println!("erro 1");
|
||||||
}
|
}
|
||||||
};
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -365,7 +397,7 @@ impl TunTapPacketHandler for Iface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("failed to parse tap packet: {}", e);
|
error!("failed to parse tun packet: {}", e);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user