This commit is contained in:
asxalex 2024-10-24 18:46:21 +08:00
parent 220cee4a89
commit 06d7819fd1
9 changed files with 44 additions and 23 deletions

10
Cargo.lock generated
View File

@ -315,6 +315,15 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
[[package]]
name = "crc32fast"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.13"
@ -1434,6 +1443,7 @@ name = "sdlan-rs"
version = "0.1.0"
dependencies = [
"crc",
"crc32fast",
"dashmap 6.1.0",
"dns-lookup",
"etherparse",

View File

@ -5,6 +5,7 @@ edition = "2021"
[dependencies]
crc = "3.2.1"
crc32fast = "1.4.2"
dashmap = "6.0.1"
dns-lookup = "2.0.4"
etherparse = "0.15.0"

View File

@ -2,7 +2,7 @@ linux:
RUSTFLAGS="-L ." cargo build --features "tap" --release
linux-tun:
RUSTFLAGS="-L ." cargo build --features "tap" --release
RUSTFLAGS="-L ." cargo build --release
pb:
cargo run --bin build_pb

View File

@ -13,8 +13,6 @@ use tokio::sync::{
oneshot,
};
use crate::utils::CRC_HASH;
use super::{get_edge, get_route_table};
static GLOBAL_ARP: OnceCell<ArpActor> = OnceCell::new();

View File

@ -15,7 +15,7 @@ use crate::pb::{
SdlRegisterSuperAck, SdlRegisterSuperNak, SdlSendRegisterEvent, SdlStunRequest, Sdlv6Info,
};
use crate::tcp::{init_tcp_conn, EventType, NakMsgCode, PacketType, SdlanTcp};
use crate::utils::{send_to_sock, CommandLine, CRC_HASH};
use crate::utils::{send_to_sock, CommandLine};
use crate::ConnectionState;
use etherparse::ether_type::ARP;
use etherparse::{Ethernet2Header, IpHeaders};

View File

@ -6,7 +6,7 @@ use std::{
use crate::{
network::{send_packet_to_net, TunTapPacketHandler},
utils::{mac_to_string, CRC_HASH},
utils::mac_to_string,
};
use crate::{

View File

@ -2,7 +2,8 @@ use etherparse::ether_type::ARP;
use etherparse::{Ethernet2Header, IpHeaders};
use sdlan_sn_rs::config::SDLAN_DEFAULT_TTL;
use sdlan_sn_rs::utils::{
aes_encrypt, ip_to_string, is_multi_broadcast, net_bit_len_to_mask, SDLanError, BROADCAST_MAC,
aes_encrypt, ip_to_string, is_ipv6_multicast, is_multi_broadcast, net_bit_len_to_mask,
SDLanError, BROADCAST_MAC,
};
use std::error::Error;
use std::ffi::CStr;
@ -25,7 +26,7 @@ use crate::network::{
};
use crate::pb::{encode_to_udp_message, SdlData};
use crate::tcp::PacketType;
use crate::utils::{mac_to_string, CRC_HASH};
use crate::utils::{caculate_crc, mac_to_string};
use super::device::{DeviceConfig, Mode};
use super::TunTapPacketHandler;
@ -172,7 +173,7 @@ impl Iface {
#[cfg(feature = "tap")]
impl TunTapPacketHandler for Iface {
async fn handle_packet_from_net(&self, data: &[u8], _: &[u8]) -> std::io::Result<()> {
debug!("in tap mode");
debug!("in tap mode, got data: {:?}", data);
match self.send(data) {
Err(e) => {
error!("failed to write to tap: {}", e.to_string());
@ -188,9 +189,13 @@ impl TunTapPacketHandler for Iface {
) -> std::io::Result<()> {
debug!("in tap mode2");
let edge = get_edge();
match Ethernet2Header::from_slice(data) {
Ok((hdr, _)) => {
let target = hdr.destination;
if is_ipv6_multicast(&target) {
return Ok(());
}
let size = data.len();
let Ok(encrypted) = aes_encrypt(encrypt_key, data) else {
@ -209,7 +214,9 @@ impl TunTapPacketHandler for Iface {
send_packet_to_net(edge, target, &msg, size as u64).await;
}
Err(e) => {}
Err(e) => {
error!("failed to parse packet from device");
}
};
Ok(())
}
@ -226,27 +233,25 @@ impl TunTapPacketHandler for Iface {
error!("payload length error");
return Ok(());
}
let crc_code = &rest[(rest.len() - 4)..rest.len()];
let rest = &rest[..(rest.len() - 4)];
// let crc_code = &rest[(rest.len() - 4)..rest.len()];
// let rest = &rest[..(rest.len() - 4)];
let crc_hash: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_CKSUM);
let ck = CRC_HASH.checksum(&data[..(data.len()) - 4]);
let sent_ck = u32::from_be_bytes(crc_code.try_into().unwrap());
debug!("ck = {}, sent_ck = {}", ck, sent_ck);
// let crc_hash: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_CKSUM);
// let ck = caculate_crc(&data[..(data.len() - 4)]);
// let sent_ck = u32::from_be_bytes(crc_code.try_into().unwrap());
// debug!("ck = {}, sent_ck = {}", ck, sent_ck);
debug!("ip size is {}", rest.len());
let edge = get_edge();
let self_mac = edge.device_config.get_mac();
/*
if hdr.destination != self_mac && hdr.destination != BROADCAST_MAC {
if hdr.destination != self_mac && !is_multi_broadcast(&hdr.destination) {
error!(
"packet to [{:?}] not direct to us",
mac_to_string(&hdr.destination)
);
return;
return Ok(());
}
*/
if hdr.ether_type == ARP {
let mut arp = ArpHdr::from_slice(&data);
@ -323,7 +328,7 @@ impl TunTapPacketHandler for Iface {
.await;
}
}
other => {
_other => {
println!("unknown arp type info");
}
}
@ -443,7 +448,7 @@ impl TunTapPacketHandler for Iface {
let mut packet = Vec::with_capacity(14 + data.len() + 4);
packet.extend_from_slice(&etherheader.to_bytes()[..]);
packet.extend_from_slice(&data);
let crc = CRC_HASH.checksum(&packet);
let crc = caculate_crc(&packet);
packet.extend_from_slice(&crc.to_be_bytes());
let pkt_size = packet.len();

View File

@ -19,7 +19,7 @@ use crate::network::{
};
use crate::pb::{encode_to_udp_message, SdlData};
use crate::tcp::PacketType;
use crate::utils::{mac_to_string, CRC_HASH};
use crate::utils::mac_to_string;
use super::device::{DeviceConfig, Mode};
use super::TunTapPacketHandler;

View File

@ -1,4 +1,6 @@
mod command;
use std::hash::Hasher;
pub use command::*;
mod socks;
@ -9,7 +11,12 @@ pub use socks::*;
mod pid_recorder;
pub use pid_recorder::PidRecorder;
pub const CRC_HASH: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_CKSUM);
// pub const CRC_HASH: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_XFER);
pub fn caculate_crc(data: &[u8]) -> u32 {
let res = crc32fast::hash(data);
res
}
pub fn mac_to_string(mac: &Mac) -> String {
format!(