Compare commits
2 Commits
de8bf7cd31
...
5249a271c5
| Author | SHA1 | Date | |
|---|---|---|---|
| 5249a271c5 | |||
| 814ab9181a |
66
Cargo.lock
generated
66
Cargo.lock
generated
@ -2,6 +2,16 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aead"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0"
|
||||
dependencies = [
|
||||
"crypto-common",
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aes"
|
||||
version = "0.8.4"
|
||||
@ -347,6 +357,30 @@ version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
|
||||
|
||||
[[package]]
|
||||
name = "chacha20"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cipher",
|
||||
"cpufeatures",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chacha20poly1305"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35"
|
||||
dependencies = [
|
||||
"aead",
|
||||
"chacha20",
|
||||
"cipher",
|
||||
"poly1305",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.43"
|
||||
@ -368,6 +402,7 @@ checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
|
||||
dependencies = [
|
||||
"crypto-common",
|
||||
"inout",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -555,6 +590,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"rand_core 0.6.4",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
@ -1757,6 +1793,12 @@ version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "opaque-debug"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
|
||||
|
||||
[[package]]
|
||||
name = "openssl-probe"
|
||||
version = "0.2.1"
|
||||
@ -1856,6 +1898,17 @@ version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
|
||||
|
||||
[[package]]
|
||||
name = "poly1305"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf"
|
||||
dependencies = [
|
||||
"cpufeatures",
|
||||
"opaque-debug",
|
||||
"universal-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "potential_utf"
|
||||
version = "0.1.4"
|
||||
@ -2009,10 +2062,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "punchnet"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"cargo-deb",
|
||||
"chacha20poly1305",
|
||||
"clap 4.5.60",
|
||||
"crc",
|
||||
"crc32fast",
|
||||
@ -3443,6 +3497,16 @@ version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"
|
||||
|
||||
[[package]]
|
||||
name = "universal-hash"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea"
|
||||
dependencies = [
|
||||
"crypto-common",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "punchnet"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
@ -35,6 +35,7 @@ rustls-pemfile = "2.2.0"
|
||||
clap = { version = "4.5.60", features = ["derive", "env"] }
|
||||
rpassword = "7.4.0"
|
||||
serde_json = "1.0.149"
|
||||
chacha20poly1305 = "0.10.1"
|
||||
# rolling-file = { path = "../rolling-file" }
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
|
||||
2
Makefile
2
Makefile
@ -16,7 +16,7 @@ win:
|
||||
|
||||
pb:
|
||||
cargo run --bin build_pb
|
||||
mv src/pb/_.rs src/pb/message.rs
|
||||
# mv src/pb/_.rs src/pb/message.rs
|
||||
|
||||
libtun-so-clang:
|
||||
cd src/network && clang -fPIC -shared -o libtuntap.so tuntap.c && cd -
|
||||
|
||||
@ -68,9 +68,9 @@ where T: Serialize,
|
||||
return Err(SDLanError::IOError("failed to do request".to_owned()));
|
||||
};
|
||||
|
||||
println!("status: {}", response.status());
|
||||
// println!("status: {}", response.status());
|
||||
let text = response.text().await.unwrap();
|
||||
println!("text = {}", text);
|
||||
// println!("text = {}", text);
|
||||
|
||||
let data = serde_json::from_str(&text).unwrap();
|
||||
|
||||
|
||||
167
src/bin/punchnet/local_udp_info.rs
Normal file
167
src/bin/punchnet/local_udp_info.rs
Normal file
@ -0,0 +1,167 @@
|
||||
use std::{net::SocketAddr, sync::atomic::Ordering, time::Duration};
|
||||
|
||||
use num_enum::TryFromPrimitive;
|
||||
use punchnet::get_edge;
|
||||
use sdlan_sn_rs::utils::Mac;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::{net::UdpSocket, time::sleep};
|
||||
|
||||
|
||||
#[derive(TryFromPrimitive)]
|
||||
#[repr(u8)]
|
||||
pub enum InfoFuncCode {
|
||||
Info = 0x00,
|
||||
|
||||
InfoFeedback = 0x80,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct InfoFeedback {
|
||||
pub mac: Mac,
|
||||
pub ip: u32,
|
||||
pub tx_p2p: u64,
|
||||
pub rx_p2p: u64,
|
||||
|
||||
pub tx_sup: u64,
|
||||
pub rx_sup: u64,
|
||||
}
|
||||
|
||||
pub async fn query_for_info() {
|
||||
let Ok(udp) = UdpSocket::bind("127.0.0.1:0").await else {
|
||||
eprintln!("failed to create");
|
||||
return;
|
||||
};
|
||||
|
||||
let remote = format!("127.0.0.1:{}", LOCAL_INFO_UDP_PORT).parse::<SocketAddr>().unwrap();
|
||||
|
||||
let buf = vec![InfoFuncCode::Info as u8, 0, 0];
|
||||
|
||||
if let Err(e) = udp.send_to(buf.as_slice(), remote).await {
|
||||
eprintln!("failed to send query info");
|
||||
return;
|
||||
}
|
||||
|
||||
let mut buf = vec![0;1024];
|
||||
tokio::select! {
|
||||
data = udp.recv_from(&mut buf) => {
|
||||
if let Ok((size, from)) = data {
|
||||
if size < 3 {
|
||||
eprintln!("no byte received");
|
||||
return;
|
||||
}
|
||||
buf.truncate(size);
|
||||
|
||||
let Ok(typecode) = InfoFuncCode::try_from_primitive(buf[0]) else {
|
||||
eprintln!("invalid type: {}", buf[0]);
|
||||
return;
|
||||
};
|
||||
|
||||
let size = u16::from_be_bytes([buf[1], buf[2]]);
|
||||
if buf.len() as u16 != size + 3 {
|
||||
eprintln!("info length error: buf.len={}, size={}", buf.len(), size);
|
||||
return;
|
||||
}
|
||||
|
||||
match typecode {
|
||||
InfoFuncCode::InfoFeedback => {
|
||||
let Ok(data) = serde_json::from_slice::<InfoFeedback>(&buf[3..]) else {
|
||||
eprintln!("failed to marshal to json");
|
||||
return;
|
||||
};
|
||||
println!("punchnet info:");
|
||||
let ip = data.ip;
|
||||
println!(" ip: {}.{}.{}.{}",
|
||||
((ip>>24) & 0xff) as u8,
|
||||
((ip>>16) & 0xff) as u8,
|
||||
((ip>>8) & 0xff) as u8,
|
||||
((ip) & 0xff) as u8,
|
||||
);
|
||||
let mac = data.mac;
|
||||
println!(" mac: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
|
||||
);
|
||||
|
||||
println!(" p2p tx: {} bytes", data.tx_p2p);
|
||||
println!(" p2p rx: {} bytes", data.rx_p2p);
|
||||
|
||||
println!(" super tx: {} bytes", data.tx_sup);
|
||||
println!(" super rx: {} bytes", data.rx_sup);
|
||||
}
|
||||
_other => {
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
eprintln!("failed to recv from punchnet");
|
||||
}
|
||||
return;
|
||||
}
|
||||
_ = sleep(Duration::from_secs(3)) => {
|
||||
eprintln!("query timed out, is punchnet running?");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const LOCAL_INFO_UDP_PORT: u16 = 1789;
|
||||
|
||||
pub async fn handle_query_for_info_info() {
|
||||
let Ok(udp) = UdpSocket::bind(format!("127.0.0.1:{}", LOCAL_INFO_UDP_PORT)).await else {
|
||||
eprintln!("failed to create");
|
||||
return;
|
||||
};
|
||||
|
||||
let mut buf = vec![0u8; 1024];
|
||||
loop {
|
||||
if let Ok((size, from)) = udp.recv_from(&mut buf).await {
|
||||
let current_data = &buf[..size];
|
||||
handle_query_info(&udp, &buf[..size], from).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_query_info(udp: &UdpSocket, buf: &[u8], from: SocketAddr) {
|
||||
if buf.len() < 1 {
|
||||
return;
|
||||
}
|
||||
let tp = buf[0];
|
||||
|
||||
let Ok(typecode) = InfoFuncCode::try_from_primitive(tp) else {
|
||||
eprintln!("invalid type: {}", tp);
|
||||
return;
|
||||
};
|
||||
|
||||
match typecode {
|
||||
InfoFuncCode::Info => {
|
||||
send_info_back(udp, from).await;
|
||||
}
|
||||
_other => {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_info_back(udp: &UdpSocket, from: SocketAddr) {
|
||||
let edge = get_edge();
|
||||
let ip = edge.device_config.get_ip();
|
||||
let mac = edge.device_config.get_mac();
|
||||
let feedback = InfoFeedback {
|
||||
ip,
|
||||
mac,
|
||||
tx_p2p: edge.stats.tx_p2p.load(Ordering::Relaxed),
|
||||
rx_p2p: edge.stats.rx_p2p.load(Ordering::Relaxed),
|
||||
|
||||
tx_sup: edge.stats.tx_sup.load(Ordering::Relaxed),
|
||||
rx_sup: edge.stats.rx_sup.load(Ordering::Relaxed),
|
||||
};
|
||||
let value = serde_json::to_string(&feedback).unwrap();
|
||||
|
||||
let mut content = Vec::with_capacity(value.len() + 3);
|
||||
content.push(InfoFuncCode::InfoFeedback as u8);
|
||||
|
||||
let size = value.len() as u16;
|
||||
let size_buf = size.to_be_bytes();
|
||||
|
||||
content.extend_from_slice(&size_buf);
|
||||
content.extend_from_slice(value.as_bytes());
|
||||
udp.send_to(content.as_slice(), from).await;
|
||||
}
|
||||
@ -1,13 +1,19 @@
|
||||
mod api;
|
||||
mod local_udp_info;
|
||||
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::stdout;
|
||||
use std::process;
|
||||
|
||||
use std::env;
|
||||
use std::time::Duration;
|
||||
use clap::Parser;
|
||||
use daemonize::Daemonize;
|
||||
use futures_util::io;
|
||||
use libc::SIGTERM;
|
||||
use libc::kill;
|
||||
use punchnet::CachedLoginInfo;
|
||||
use punchnet::CommandLineInput2;
|
||||
use punchnet::Commands;
|
||||
@ -29,7 +35,8 @@ use sdlan_sn_rs::utils::Mac;
|
||||
use sdlan_sn_rs::utils::Result;
|
||||
use sdlan_sn_rs::utils::create_or_load_uuid;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::io::stdout;
|
||||
use tokio::net::UdpSocket;
|
||||
use tokio::runtime::Runtime;
|
||||
use tokio::time::sleep;
|
||||
use tracing::error;
|
||||
|
||||
@ -44,6 +51,8 @@ use crate::api::TEST_PREFIX;
|
||||
use crate::api::connect;
|
||||
use crate::api::login_with_token;
|
||||
use crate::api::login_with_user_pass;
|
||||
use crate::local_udp_info::handle_query_for_info_info;
|
||||
use crate::local_udp_info::query_for_info;
|
||||
|
||||
|
||||
const APP_USER_ENV_NAME: &str = "PUNCH_USER";
|
||||
@ -107,7 +116,7 @@ async fn daemonize_me(
|
||||
client_id: String,
|
||||
mac: Mac,
|
||||
) {
|
||||
println!("identity_id = {}", connect_info.identity_id);
|
||||
let _guard = log::init_log(&format!("{}/.output", get_base_dir()));
|
||||
|
||||
let self_host_name = connect_info.hostname;
|
||||
|
||||
@ -127,8 +136,6 @@ async fn daemonize_me(
|
||||
return;
|
||||
}
|
||||
|
||||
println!("server is {}", server);
|
||||
|
||||
// mod_hostname::get_hostname();
|
||||
/*
|
||||
let hostname = if cmd.hostname.len() == 0 {
|
||||
@ -177,6 +184,10 @@ async fn daemonize_me(
|
||||
|
||||
let _ = rx.recv();
|
||||
|
||||
tokio::spawn(async {
|
||||
handle_query_for_info_info().await;
|
||||
});
|
||||
|
||||
let edge = get_edge();
|
||||
// let res = edge.start_without_feedback(cmd.token).await;
|
||||
/*
|
||||
@ -234,6 +245,9 @@ async fn daemonize_me(
|
||||
|
||||
match tokio::signal::ctrl_c().await {
|
||||
Ok(()) => {
|
||||
edge.quic_endpoint.close(0u32.into(), "bye".as_bytes());
|
||||
println!("quic is quitting");
|
||||
delete_pid_file();
|
||||
let _ = restore_dns();
|
||||
}
|
||||
Err(err) => {
|
||||
@ -258,17 +272,13 @@ async fn daemonize_me(
|
||||
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
fn main() {
|
||||
set_base_dir("/usr/local/punchnet");
|
||||
let _guard = log::init_log(&format!("{}/.output", get_base_dir()));
|
||||
// let _guard = log::init_log(&format!("{}/.output", get_base_dir()));
|
||||
|
||||
let client_id = create_or_load_uuid(&format!("{}/.id", get_base_dir()), None).unwrap();
|
||||
|
||||
let test_token = "49722584273728716817720074439183";
|
||||
|
||||
let mac = create_or_load_mac();
|
||||
|
||||
let system = "linux";
|
||||
let version = "1.0.0";
|
||||
|
||||
@ -276,26 +286,107 @@ async fn main() {
|
||||
let cmd = CommandLineInput2::parse();
|
||||
// println!("port is {}", cmd.port);
|
||||
|
||||
let connect_info: ConnectData;
|
||||
let remembered: CachedLoginInfo;
|
||||
|
||||
let should_daemonize: bool;
|
||||
|
||||
match cmd.cmd {
|
||||
match &cmd.cmd {
|
||||
Commands::Login(user) => {
|
||||
// TODO: do login with user
|
||||
let rt = Runtime::new().unwrap();
|
||||
rt.block_on( async move {
|
||||
let _ = parse_login_result(
|
||||
login_with_user_pass(TEST_PREFIX, &client_id, &user.username, &user.password, mac, system, version).await
|
||||
);
|
||||
});
|
||||
process::exit(0);
|
||||
// TODO: do login with user
|
||||
}
|
||||
Commands::TokenLogin(tk) => {
|
||||
let rt = Runtime::new().unwrap();
|
||||
rt.block_on(async move {
|
||||
let _ = parse_login_result(
|
||||
login_with_token(TEST_PREFIX, &client_id, &tk.token, mac, system, version).await
|
||||
);
|
||||
});
|
||||
process::exit(0);
|
||||
}
|
||||
Commands::Stop => {
|
||||
match fs::read_to_string("/tmp/punchnet.pid") {
|
||||
Ok(content) => {
|
||||
let pid: i32 = match content.trim().parse() {
|
||||
Ok(value) => value,
|
||||
Err(_e) => {
|
||||
eprintln!("failed to parse value: {}", content);
|
||||
process::exit(-4);
|
||||
}
|
||||
};
|
||||
let result = unsafe {
|
||||
kill(pid, SIGTERM)
|
||||
};
|
||||
if result != 0 {
|
||||
let err = io::Error::last_os_error();
|
||||
eprintln!("failed to kill: {}", err);
|
||||
process::exit(-5);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("failed to read pid: {}", e);
|
||||
process::exit(-6);
|
||||
}
|
||||
}
|
||||
process::exit(0);
|
||||
}
|
||||
Commands::Info => {
|
||||
|
||||
let rt = Runtime::new().unwrap();
|
||||
rt.block_on(async move {
|
||||
query_for_info().await;
|
||||
});
|
||||
process::exit(0);
|
||||
}
|
||||
_other => {
|
||||
// just fall through to next code
|
||||
}
|
||||
}
|
||||
|
||||
let out = OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.write(true)
|
||||
.open("/tmp/punchnet.out").unwrap();
|
||||
let err = OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.write(true)
|
||||
.open("/tmp/punchnet.err").unwrap();
|
||||
|
||||
let daemonize = Daemonize::new()
|
||||
.pid_file("/tmp/punchnet.pid")
|
||||
.chown_pid_file(true)
|
||||
.working_directory(get_base_dir())
|
||||
.stdout(out)
|
||||
.stderr(err)
|
||||
.privileged_action(|| {
|
||||
});
|
||||
|
||||
match daemonize.start() {
|
||||
Ok(_) => {
|
||||
let rt = Runtime::new().unwrap();
|
||||
match &cmd.cmd {
|
||||
Commands::Start => {
|
||||
rt.block_on(async move {
|
||||
let remembered_token = get_access_token();
|
||||
if remembered_token.is_none() {
|
||||
eprintln!("not logged in, should login with user/pass or token first");
|
||||
process::exit(-2);
|
||||
}
|
||||
|
||||
let remembered = remembered_token.unwrap();
|
||||
|
||||
let connect_info = parse_connect_result(
|
||||
connect(TEST_PREFIX, &client_id, &remembered.access_token).await
|
||||
);
|
||||
daemonize_me(connect_info, remembered, client_id, mac).await;
|
||||
})
|
||||
}
|
||||
Commands::AutoRun(tk) => {
|
||||
rt.block_on(async move {
|
||||
let mut remembered_token = get_access_token();
|
||||
if remembered_token.is_none() {
|
||||
let data = parse_login_result(
|
||||
@ -311,68 +402,28 @@ async fn main() {
|
||||
});
|
||||
}
|
||||
|
||||
remembered = remembered_token.unwrap();
|
||||
let remembered = remembered_token.unwrap();
|
||||
|
||||
connect_info = parse_connect_result(
|
||||
let connect_info = parse_connect_result(
|
||||
connect(TEST_PREFIX, &client_id, &remembered.access_token).await
|
||||
);
|
||||
should_daemonize = false;
|
||||
}
|
||||
Commands::Start => {
|
||||
let remembered_token = get_access_token();
|
||||
if remembered_token.is_none() {
|
||||
eprintln!("not logged in, should login with user/pass or token first");
|
||||
process::exit(-2);
|
||||
}
|
||||
|
||||
remembered = remembered_token.unwrap();
|
||||
|
||||
connect_info = parse_connect_result(
|
||||
connect(TEST_PREFIX, &client_id, &remembered.access_token).await
|
||||
);
|
||||
should_daemonize = false;
|
||||
}
|
||||
Commands::Stop => {
|
||||
process::exit(-4);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if should_daemonize {
|
||||
let stdout = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.write(true)
|
||||
.open("/tmp/punchnet.out").unwrap();
|
||||
let stderr = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.write(true)
|
||||
.open("/tmp/punchnet.err").unwrap();
|
||||
|
||||
let daemonize = Daemonize::new()
|
||||
.pid_file("/tmp/punchnet.pid")
|
||||
.chown_pid_file(true)
|
||||
.working_directory(get_base_dir())
|
||||
.stdout(stdout)
|
||||
.stderr(stderr)
|
||||
.privileged_action(|| {
|
||||
|
||||
});
|
||||
match daemonize.start() {
|
||||
Ok(_) => {
|
||||
loop {
|
||||
println!("guard is {:?}", _guard);
|
||||
sleep(Duration::from_secs(3)).await;
|
||||
}
|
||||
daemonize_me(connect_info, remembered, client_id, mac).await;
|
||||
})
|
||||
|
||||
}
|
||||
other => {
|
||||
eprintln!("should not comes here");
|
||||
process::exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("failed to daemonize: {}", e);
|
||||
eprintln!("failed to daemonize");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
daemonize_me(connect_info, remembered, client_id, mac).await;
|
||||
}
|
||||
|
||||
pub fn delete_pid_file() {
|
||||
fs::remove_file("/tmp/punchnet.pid");
|
||||
}
|
||||
|
||||
@ -903,6 +903,11 @@ async fn handle_tun_packet(
|
||||
return;
|
||||
};
|
||||
|
||||
if _from_sn {
|
||||
eee.stats.rx_sup.fetch_add(data.len() as u64, Ordering::Relaxed);
|
||||
} else {
|
||||
eee.stats.rx_p2p.fetch_add(data.len() as u64, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
if let Some(ip) = headers.net {
|
||||
match ip {
|
||||
|
||||
@ -307,10 +307,10 @@ impl TunTapPacketHandler for Iface {
|
||||
|
||||
send_packet_to_net(edge, target, &msg, size as u64).await;
|
||||
} else {
|
||||
println!("erro 2");
|
||||
error!("erro 2");
|
||||
}
|
||||
} else {
|
||||
println!("erro 1");
|
||||
error!("erro 1");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@ -122,11 +122,11 @@ impl Iface {
|
||||
}
|
||||
|
||||
let gw = ip_to_string(&default_gw);
|
||||
println!("gw = {}", gw);
|
||||
debug!("gw = {}", gw);
|
||||
if let Err(e) = set_dns(&self.name, network_domain, &gw, self.if_idx) {
|
||||
println!("failed to set dns: {:?}", e);
|
||||
error!("failed to set dns: {:?}", e);
|
||||
} else {
|
||||
println!("set dns ok");
|
||||
error!("set dns ok");
|
||||
}
|
||||
|
||||
}
|
||||
@ -420,7 +420,7 @@ fn create_wintun(path: &str, name: &str) -> Iface {
|
||||
.expect("failed to create tun adapter"),
|
||||
};
|
||||
let idx = adapter.get_adapter_index().expect("failed to get adapter index");
|
||||
println!("idx = {}", idx);
|
||||
// println!("idx = {}", idx);
|
||||
let session = Arc::new(adapter.start_session(wintun::MAX_RING_CAPACITY).unwrap());
|
||||
Iface {
|
||||
if_idx: idx,
|
||||
@ -451,7 +451,7 @@ pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> std::
|
||||
.creation_flags(0x08000000)
|
||||
.output()?;
|
||||
|
||||
println!("res1: {}", res.status.success());
|
||||
//println!("res1: {}", res.status.success());
|
||||
|
||||
println!("route set ok");
|
||||
let res = Command::new("netsh")
|
||||
@ -464,7 +464,7 @@ pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> std::
|
||||
.arg("validate=no")
|
||||
.creation_flags(0x08000000)
|
||||
.output()?;
|
||||
println!("res2: {}", res.status.success());
|
||||
// println!("res2: {}", res.status.success());
|
||||
|
||||
println!("netsh set ok");
|
||||
|
||||
|
||||
@ -4,10 +4,10 @@ use futures_util::pin_mut;
|
||||
use prost::Message;
|
||||
use quinn::SendStream;
|
||||
use sdlan_sn_rs::{config::AF_INET, peer::{SdlanSock, V6Info}, utils::{Result, SDLanError, get_current_timestamp, ip_to_string, rsa_decrypt}};
|
||||
use tokio::{sync::mpsc::{Receiver, Sender, channel}};
|
||||
use tokio::{sync::mpsc::{Receiver, Sender, channel}, time::sleep};
|
||||
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::{debug, error};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
use crate::{ConnectionInfo, ConnectionState, config::{NULL_MAC, TCP_PING_TIME}, get_edge, network::{ARP_REPLY, ArpHdr, EthHdr, Node, RegisterSuperFeedback, StartStopInfo, check_peer_registration_needed, handle_packet_peer_info}, pb::{SdlArpResponse, SdlPolicyResponse, SdlRegisterSuper, SdlRegisterSuperAck, SdlRegisterSuperNak, SdlSendRegisterEvent, encode_to_tcp_message}, tcp::{EventType, NakMsgCode, NatType, PacketType, RuleInfo, SdlanTcp, read_a_packet, send_stun_request, set_identity_cache}};
|
||||
|
||||
@ -185,7 +185,7 @@ async fn handle_tcp_message(msg: SdlanTcp) {
|
||||
};
|
||||
if resp.target_mac.len() != 6 {
|
||||
// invalid target_mac
|
||||
error!("invalid target_mac");
|
||||
error!("invalid target_mac: {:?}, ip={}", resp.target_mac, ip_to_string(&resp.target_ip));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -534,7 +534,7 @@ impl ReadWriteActor {
|
||||
Ok(conn) => conn,
|
||||
Err(e) => {
|
||||
error!("failed to connect: {}", e);
|
||||
println!("failed to connect: {}", e);
|
||||
// println!("failed to connect: {}", e);
|
||||
|
||||
self.connected.store(false, Ordering::Relaxed);
|
||||
if keep_reconnect {
|
||||
@ -548,7 +548,7 @@ impl ReadWriteActor {
|
||||
|
||||
let conn = match conn.await {
|
||||
Err(e) => {
|
||||
println!("failed to connect await: {}", e);
|
||||
// println!("failed to connect await: {}", e);
|
||||
error!("failed to connect await: {}", e);
|
||||
self.connected.store(false, Ordering::Relaxed);
|
||||
if keep_reconnect {
|
||||
@ -574,6 +574,8 @@ impl ReadWriteActor {
|
||||
|
||||
self.connected.store(true, Ordering::Relaxed);
|
||||
debug!("connected");
|
||||
|
||||
sleep(Duration::from_millis(200)).await;
|
||||
on_connected_callback(local_ip, &mut send, start_pkt_id.take()).await;
|
||||
|
||||
if let Some(ref connecting_chan) = self.connecting_chan {
|
||||
@ -591,7 +593,7 @@ impl ReadWriteActor {
|
||||
loop {
|
||||
match read_a_packet(&mut recv).await {
|
||||
Ok(packet) => {
|
||||
debug!("got packet: {:?}", packet);
|
||||
warn!("got packet: {:?}", packet);
|
||||
if let Err(_e) = self.from_tcp.send(packet).await {
|
||||
error!("failed to receive a packet: {:?}", _e);
|
||||
}
|
||||
|
||||
@ -24,6 +24,8 @@ pub enum Commands {
|
||||
/// connect to the remote
|
||||
Start,
|
||||
|
||||
Info,
|
||||
|
||||
/// exits the
|
||||
Stop,
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user