From 9a72c29870209ee284ba04b6ef86d85e66dbb417 Mon Sep 17 00:00:00 2001 From: asxalex Date: Tue, 30 Sep 2025 11:31:05 +0800 Subject: [PATCH] punchnet renamed --- .cargo/config.toml | 3 ++ .gitignore | 1 + Cargo.lock | 50 ++++++++++++++--------------- Cargo.toml | 5 +-- Makefile | 5 ++- install.sh | 13 ++++++++ punchnet.service | 12 +++++++ src/bin/{sdlan => punchnet}/main.rs | 19 +++++------ src/network/async_main.rs | 6 ++-- src/network/packet.rs | 16 ++------- src/network/route.rs | 11 ++++--- src/network/tun_linux.rs | 17 +++++----- src/network/tun_win.rs | 17 +++++----- src/network/tuntap.rs | 4 ++- src/tcp/tcp_conn.rs | 3 +- src/utils/command.rs | 4 +-- src/utils/pid_recorder.rs | 7 ++-- 17 files changed, 112 insertions(+), 81 deletions(-) create mode 100644 install.sh create mode 100644 punchnet.service rename src/bin/{sdlan => punchnet}/main.rs (85%) diff --git a/.cargo/config.toml b/.cargo/config.toml index bf99b02..8e41c46 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -6,3 +6,6 @@ linker = "aarch64-linux-gnu-gcc" [target.x86_64-pc-windows-gnu] linker = "x86_64-w64-mingw32-gcc" + +[build] +target = "x86_64-unknown-linux-gnu" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0422212..b53981d 100755 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ sdlan.exe *.tar *.tar.gz *.tgz +/punchnet diff --git a/Cargo.lock b/Cargo.lock index 9eb5645..e8c23db 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1297,6 +1297,31 @@ dependencies = [ "prost", ] +[[package]] +name = "punchnet" +version = "1.0.0" +dependencies = [ + "crc", + "crc32fast", + "dashmap 6.1.0", + "dns-lookup", + "etherparse", + "futures-util", + "local-ip-address", + "num_enum", + "once_cell", + "prost", + "prost-build", + "rand", + "rsa", + "sdlan-sn-rs", + "structopt", + "tokio", + "tokio-util", + "tracing", + "wintun", +] + [[package]] name = "quote" version = "1.0.37" @@ -1438,31 +1463,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" -[[package]] -name = "sdlan-rs" -version = "0.1.0" -dependencies = [ - "crc", - "crc32fast", - "dashmap 6.1.0", - "dns-lookup", - "etherparse", - "futures-util", - "local-ip-address", - "num_enum", - "once_cell", - "prost", - "prost-build", - "rand", - "rsa", - "sdlan-sn-rs", - "structopt", - "tokio", - "tokio-util", - "tracing", - "wintun", -] - [[package]] name = "sdlan-sn-rs" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index dfe3d7d..f304833 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "sdlan-rs" -version = "0.1.0" +name = "punchnet" +version = "1.0.0" edition = "2021" [dependencies] @@ -18,6 +18,7 @@ prost-build = "0.12.6" rand = "0.8.5" rsa = "0.9.6" sdlan-sn-rs = { git = "ssh://git@git.asxalex.pw/sdlan-v2/sdlan-rs.git" } +# sdlan-sn-rs = {path = "../sdlan-rs"} # sdlan-sn-rs = { git = "https://git.asxalex.pw/sdlan-v2/sdlan-rs.git" } structopt = "0.3.26" tokio = { version = "1.38.0", features = ["full"] } diff --git a/Makefile b/Makefile index 228a941..efe8315 100755 --- a/Makefile +++ b/Makefile @@ -25,4 +25,7 @@ libtun-so: cd src/network && gcc -fPIC -shared -o libtuntap.so tuntap.c && cd - libtun-a: - cd src/network && gcc -c tuntap.c && ar rcs libtuntap.a tuntap.o && cd - \ No newline at end of file + cd src/network && gcc -c tuntap.c && ar rcs libtuntap.a tuntap.o && cd - + +pack: + tar -czvf punchnet.tar.gz punchnet punchnet.service libtuntap.so install.sh \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..4d1179a --- /dev/null +++ b/install.sh @@ -0,0 +1,13 @@ +uid=`id -u` + +if [ "$uid" != "0" ]; then + echo "need to be root" + exit -1 +fi + +mkdir -p /usr/local/punchnet +cp punchnet /usr/local/punchnet +cp libtuntap.so /usr/lib/ +cp punchnet.service /etc/systemd/system +systemctl enable punchnet +systemctl start punchnet diff --git a/punchnet.service b/punchnet.service new file mode 100644 index 0000000..4570f26 --- /dev/null +++ b/punchnet.service @@ -0,0 +1,12 @@ +[Unit] +Description=punchnet client +Before=network.target network.service +Wants=network.target + +[Service] +Type=simple +WorkingDirectory=/usr/local/punchnet +ExecStart=/usr/local/punchnet/punchnet + +[Install] +WantedBy=multi-user.target diff --git a/src/bin/sdlan/main.rs b/src/bin/punchnet/main.rs similarity index 85% rename from src/bin/sdlan/main.rs rename to src/bin/punchnet/main.rs index 053a637..982c899 100755 --- a/src/bin/sdlan/main.rs +++ b/src/bin/punchnet/main.rs @@ -1,7 +1,7 @@ -use sdlan_rs::get_edge; -use sdlan_rs::run_sdlan; -use sdlan_rs::CommandLine; -use sdlan_rs::CommandLineInput; +use punchnet::get_edge; +use punchnet::run_sdlan; +use punchnet::CommandLine; +use punchnet::CommandLineInput; use sdlan_sn_rs::log; use tracing::error; @@ -23,14 +23,15 @@ async fn main() { let cmd = CommandLineInput::from_args(); - println!("port is {}", cmd.port); + // println!("port is {}", cmd.port); let (tx, rx) = std::sync::mpsc::channel(); + let server = "punchnet.aioe.tech".to_owned(); let _ = run_sdlan( CommandLine { - sn: "39.98.184.67:1265".to_owned(), - tcp: "39.98.184.67:18083".to_owned(), - nat_server1: "39.98.184.67:1265".to_owned(), + sn: server.clone()+":1265", + tcp: server.clone()+":18083", + nat_server1: server.clone() +":1265", nat_server2: "47.98.178.3:1265".to_owned(), _allow_routing: true, _drop_multicast: true, @@ -43,7 +44,7 @@ async fn main() { allow_p2p: true, }, tx, - &sdlan_rs::get_install_channel(), + &punchnet::get_install_channel(), None, ) .await; diff --git a/src/network/async_main.rs b/src/network/async_main.rs index 56e5927..4573c32 100755 --- a/src/network/async_main.rs +++ b/src/network/async_main.rs @@ -18,7 +18,7 @@ use crate::ConnectionState; use sdlan_sn_rs::config::AF_INET; 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::{Mac, Result}; +use sdlan_sn_rs::utils::{Mac, Result, gen_rsa_keys}; use tokio::io::AsyncWriteExt; use tokio::sync::mpsc::{channel, Receiver, Sender}; use tokio_util::sync::CancellationToken; @@ -85,7 +85,7 @@ async fn handle_tcp_message(msg: SdlanTcp) { send_stun_request(edge).await; tokio::spawn(async { let nattype = edge.probe_nat_type().await; - println!("nat type is: {:?}", nattype); + // println!("nat type is: {:?}", nattype); }); } PacketType::RegisterSuperNAK => { @@ -251,7 +251,7 @@ pub async fn async_main( // let _ = PidRecorder::new(".pid"); // // gen public key - // gen_rsa_keys(".client"); + gen_rsa_keys(".client"); // let mut pubkey = String::new(); // File::open(".client/id_rsa.pub")?.read_to_string(&mut pubkey)?; // let privatekey = load_private_key_file(".client/id_rsa")?; diff --git a/src/network/packet.rs b/src/network/packet.rs index 324021e..248147a 100755 --- a/src/network/packet.rs +++ b/src/network/packet.rs @@ -143,7 +143,7 @@ pub async fn handle_packet(eee: &'static Node, addr: SocketAddr, buf: &[u8]) -> error!("failed to decode SdlStunReply"); return Ok(()); }; - println!("got stun probe reply: {:?}", reply); + // println!("got stun probe reply: {:?}", reply); eee.send_nat_probe_reply(reply.cookie, reply).await; } PacketType::StunReply => { @@ -779,7 +779,7 @@ async fn send_register( } if need_guess_port { - println!("need guess port"); + // println!("need guess port"); } /* @@ -817,16 +817,6 @@ pub fn printHex(key: &[u8]) { println!("[{}]", value.join(" ")) } -fn test_aes(key: &[u8]) { - let content = (1..=10).map(|f| f as u8).collect::>(); - print!("test aes origin: "); - printHex(&content); - if let Ok(encrypted) = aes_encrypt(key, &content) { - print!("test aes: "); - printHex(&encrypted); - } -} - async fn handle_tun_packet( eee: &Node, _from_sock: &SdlanSock, @@ -841,7 +831,7 @@ async fn handle_tun_packet( return; } - test_aes(key.as_slice()); + // test_aes(key.as_slice()); let origin = aes_decrypt(key.as_slice(), &payload); if let Err(_e) = origin { error!("failed to decrypt original data"); diff --git a/src/network/route.rs b/src/network/route.rs index 3cf7c3e..c4974c1 100755 --- a/src/network/route.rs +++ b/src/network/route.rs @@ -3,6 +3,8 @@ use std::{net::Ipv4Addr, sync::RwLock}; use once_cell::sync::OnceCell; use sdlan_sn_rs::utils::net_bit_len_to_mask; +use tracing::{debug, error}; + #[derive(Debug)] pub struct RouteTable { content: RwLock>, @@ -29,7 +31,7 @@ impl RouteTable { pub fn get_gateway_ip(&self, net_ip: u32) -> Option { let routes = self.content.read().unwrap(); for route in &*routes { - println!("route: {:?}", route.to_string()); + debug!("route: {:?}", route.to_string()); if (route.net_ip & route.net_mask) == (net_ip & route.net_mask) { // found return Some(route.gateway_ip); @@ -100,16 +102,17 @@ pub fn parse_route(route: String) -> Vec<(u32, u32, u32)> { let routes: Vec<_> = route.split(",").collect(); for route in routes { let route_info: Vec<_> = route.split(";").collect(); - println!("got route info: {:?}", route_info); + debug!("got route info: {:?}", route_info); if route_info.len() != 2 { - println!("route info format error"); + + error!("route info format error"); continue; } let cidr = route_info[0]; let gateway = route_info[1].parse::().unwrap(); let ip_and_mask: Vec<_> = cidr.split("/").collect(); if ip_and_mask.len() != 2 { - println!("route info ip/bit error"); + error!("route info ip/bit error"); continue; } let ip = ip_and_mask[0].parse::().unwrap(); diff --git a/src/network/tun_linux.rs b/src/network/tun_linux.rs index a6df79a..65fac0b 100755 --- a/src/network/tun_linux.rs +++ b/src/network/tun_linux.rs @@ -30,7 +30,7 @@ use crate::utils::{caculate_crc, mac_to_string}; use super::device::{DeviceConfig, Mode}; use super::TunTapPacketHandler; -#[link(name = "tuntap")] +#[link(name = "tuntap", kind="static")] extern "C" { fn tuntap_setup(fd: c_int, name: *mut u8, mode: c_int, packet_info: c_int) -> c_int; } @@ -117,7 +117,7 @@ impl Iface { let netbit = ip_to_string(&net_bit_len_to_mask(netbit)); if cfg!(not(feature = "tun")) { - println!("set tap device"); + info!("set tap device"); let mac = device_config.get_mac(); let res = Command::new("ifconfig") .arg(&self.name) @@ -143,7 +143,7 @@ impl Iface { } } } else { - println!("set tun device"); + info!("set tun device"); let res = Command::new("ifconfig") .arg(&self.name) .arg(ip) @@ -271,7 +271,7 @@ impl TunTapPacketHandler for Iface { // handle ARP REQUEST debug!("got ARP REQUEST"); if arp.ethhdr.dest != [0xff; 6] { - println!("ARP REQUEST not broadcast"); + debug!("ARP REQUEST not broadcast"); return Ok(()); } if dest_ip == self_ip { @@ -310,7 +310,7 @@ impl TunTapPacketHandler for Iface { let v = encode_to_udp_message(Some(data), PacketType::Data as u8) .unwrap(); - println!( + debug!( "xxxx send arp reply to [{}], selfmac=[{}]", mac_to_string(&arp.dhwaddr), mac_to_string(&self_mac) @@ -321,9 +321,8 @@ impl TunTapPacketHandler for Iface { } } ARP_REPLY => { - println!("got arp reply",); - println!("mac {:?} is at {:?}", arp.shwaddr, from_ip.to_be_bytes()); + debug!("mac {:?} is at {:?}", arp.shwaddr, from_ip.to_be_bytes()); if dest_ip == self_ip { send_arp_request(ArpRequestInfo::Set { ip: from_ip, @@ -334,7 +333,7 @@ impl TunTapPacketHandler for Iface { } } _other => { - println!("unknown arp type info"); + error!("unknown arp type info"); } } } else { @@ -415,7 +414,7 @@ impl TunTapPacketHandler for Iface { } => { if do_arp_request { add_to_arp_wait_list(dstip, data); - println!( + debug!( "find ip: {:?} => {:?}", src.to_be_bytes(), dstip.to_be_bytes() diff --git a/src/network/tun_win.rs b/src/network/tun_win.rs index e39af72..3f3368d 100755 --- a/src/network/tun_win.rs +++ b/src/network/tun_win.rs @@ -152,18 +152,18 @@ impl TunTapPacketHandler for Iface { let mut arp = ArpHdr::from_slice(&data); let self_ip = edge.device_config.get_ip(); - println!("self_ip: {:?}", self_ip.to_be_bytes()); + debug!("self_ip: {:?}", self_ip.to_be_bytes()); let from_ip = ((arp.sipaddr[0] as u32) << 16) + arp.sipaddr[1] as u32; - println!("from_ip: {:?}", from_ip.to_be_bytes()); + debug!("from_ip: {:?}", from_ip.to_be_bytes()); let dest_ip = ((arp.dipaddr[0] as u32) << 16) + arp.dipaddr[1] as u32; - println!("dest_ip: {:?}", dest_ip.to_be_bytes()); + debug!("dest_ip: {:?}", dest_ip.to_be_bytes()); match arp.opcode { ARP_REQUEST => { // handle ARP REQUEST debug!("got ARP REQUEST"); if arp.ethhdr.dest != [0xff; 6] { - println!("ARP REQUEST not broadcast"); + debug!("ARP REQUEST not broadcast"); return Ok(()); } if dest_ip == self_ip { @@ -201,7 +201,7 @@ impl TunTapPacketHandler for Iface { let v = encode_to_udp_message(Some(data), PacketType::Data as u8) .unwrap(); - println!( + debug!( "xxxx send arp reply to [{}], selfmac=[{}]", mac_to_string(&arp.dhwaddr), mac_to_string(&self_mac) @@ -212,9 +212,8 @@ impl TunTapPacketHandler for Iface { } } ARP_REPLY => { - println!("got arp reply",); - println!("mac {:?} is at {:?}", arp.shwaddr, from_ip.to_be_bytes()); + debug!("mac {:?} is at {:?}", arp.shwaddr, from_ip.to_be_bytes()); if dest_ip == self_ip { send_arp_request(ArpRequestInfo::Set { ip: from_ip, @@ -225,7 +224,7 @@ impl TunTapPacketHandler for Iface { } } _other => { - println!("unknown arp type info"); + error!("unknown arp type info"); } } } else { @@ -305,7 +304,7 @@ impl TunTapPacketHandler for Iface { if do_arp_request { add_to_arp_wait_list(dstip, data); - println!( + info!( "find ip: {:?} => {:?}", src.to_be_bytes(), dstip.to_be_bytes() diff --git a/src/network/tuntap.rs b/src/network/tuntap.rs index abc7978..085d554 100755 --- a/src/network/tuntap.rs +++ b/src/network/tuntap.rs @@ -8,6 +8,8 @@ use sdlan_sn_rs::{ utils::{aes_encrypt, get_current_timestamp, ip_to_string, Mac}, }; +use tracing::debug; + use tracing::error; use crate::{ @@ -59,7 +61,7 @@ impl ArpWaitList { } async fn arp_arrived(&self, ip: u32, mac: Mac) { - println!( + debug!( "arp for {} arrived: {}", ip_to_string(&ip), mac_to_string(&mac) diff --git a/src/tcp/tcp_conn.rs b/src/tcp/tcp_conn.rs index f63135e..ced48f3 100755 --- a/src/tcp/tcp_conn.rs +++ b/src/tcp/tcp_conn.rs @@ -297,7 +297,8 @@ impl ReadWriterHandle { if let Some(msg) = data_from_tcp.recv().await { on_message(msg).await; } else { - eprintln!("data from tcp exited"); + error!("data from tcp exited"); + // eprintln!("data from tcp exited"); return; } } diff --git a/src/utils/command.rs b/src/utils/command.rs index 5521eb4..d9f2bfd 100755 --- a/src/utils/command.rs +++ b/src/utils/command.rs @@ -2,10 +2,10 @@ use structopt::StructOpt; #[derive(StructOpt, Debug)] pub struct CommandLineInput { - #[structopt(long = "token", default_value = "")] + #[structopt(long = "token", default_value = "", help="specify a token")] pub token: String, - #[structopt(short = "p", long = "port", default_value = "0")] + #[structopt(short = "p", long = "port", default_value = "0", help="which port to use")] pub port: u16, } diff --git a/src/utils/pid_recorder.rs b/src/utils/pid_recorder.rs index 94afb93..8164b44 100755 --- a/src/utils/pid_recorder.rs +++ b/src/utils/pid_recorder.rs @@ -2,6 +2,7 @@ use std::{ fs::{self, OpenOptions}, io::Write, }; +use tracing::{debug, error}; pub struct PidRecorder(String); @@ -19,7 +20,8 @@ impl PidRecorder { .expect("failed to write"); } Err(e) => { - eprintln!("failed to open pid file: {}", e); + error!("failed to open pid file: {}", e); + // eprintln!("failed to open pid file: {}", e); } } @@ -30,7 +32,8 @@ impl PidRecorder { impl Drop for PidRecorder { fn drop(&mut self) { if let Err(e) = fs::remove_file(&self.0) { - eprintln!("failed to remove pid file: {}", e); + error!("failed to remove pid file: {}", e); + // eprintln!("failed to remove pid file: {}", e); } } }