Compare commits
10 Commits
51c323008a
...
6537049e71
| Author | SHA1 | Date | |
|---|---|---|---|
| 6537049e71 | |||
| ec68a54957 | |||
| 9a72c29870 | |||
| 7fa2ffa04d | |||
| 6e8f4583d6 | |||
| 3f6da7c65e | |||
| 7e5792e190 | |||
| 4aa33a4e26 | |||
| 4fdcfa5bee | |||
| c070d345ad |
13
.cargo/config.toml
Normal file → Executable file
13
.cargo/config.toml
Normal file → Executable file
@ -1,2 +1,11 @@
|
|||||||
[target.x86_64-unknown-linux-musl]
|
[target.aarch64-unknown-linux-gnu]
|
||||||
linker = "x86_64-linux-musl-gcc"
|
linker = "aarch64-linux-gnu-gcc"
|
||||||
|
ar = "aarch64-linux-gnu-gcc"
|
||||||
|
strip = { path = "aarch64-linux-gnu-strip" }
|
||||||
|
# ranlib = "aarch64-linux-gnu-ranlib"
|
||||||
|
|
||||||
|
[target.x86_64-pc-windows-gnu]
|
||||||
|
linker = "x86_64-w64-mingw32-gcc"
|
||||||
|
|
||||||
|
# [build]
|
||||||
|
# target = "x86_64-unknown-linux-gnu"
|
||||||
1
.gitignore
vendored
Normal file → Executable file
1
.gitignore
vendored
Normal file → Executable file
@ -11,3 +11,4 @@ sdlan.exe
|
|||||||
*.tar
|
*.tar
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
*.tgz
|
*.tgz
|
||||||
|
/punchnet
|
||||||
|
|||||||
1553
Cargo.lock
generated
Normal file → Executable file
1553
Cargo.lock
generated
Normal file → Executable file
File diff suppressed because it is too large
Load Diff
34
Cargo.toml
Normal file → Executable file
34
Cargo.toml
Normal file → Executable file
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "sdlan-rs"
|
name = "punchnet"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@ -17,15 +17,41 @@ prost = "0.12.6"
|
|||||||
prost-build = "0.12.6"
|
prost-build = "0.12.6"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
rsa = "0.9.6"
|
rsa = "0.9.6"
|
||||||
sdlan-sn-rs = { git = "ssh://git@git.asxalex.pw/sdlan-v2/sdlan-rs.git" }
|
sdlan-sn-rs = { git = "ssh://git@git2.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" }
|
# sdlan-sn-rs = { git = "https://git.asxalex.pw/sdlan-v2/sdlan-rs.git" }
|
||||||
structopt = "0.3.26"
|
structopt = "0.3.26"
|
||||||
tokio = { version = "1.38.0", features = ["full"] }
|
tokio = { version = "1.38.0", features = ["full"] }
|
||||||
tokio-util = "0.7.11"
|
tokio-util = "0.7.11"
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
|
# rolling-file = { path = "../rolling-file" }
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
wintun = "0.4.0"
|
wintun = "0.4.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
tap = []
|
tun = []
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
cargo-deb = "3.6.2"
|
||||||
|
|
||||||
|
[package.metadata.deb]
|
||||||
|
maintainer = "alex <asxalex@163.com>"
|
||||||
|
copyright = "2025, alex"
|
||||||
|
# license-file = ["MIT"]
|
||||||
|
depends = "$auto"
|
||||||
|
|
||||||
|
## assets
|
||||||
|
assets = [
|
||||||
|
## libs and exxcutables
|
||||||
|
["target/release/punchnet", "usr/local/", "755"],
|
||||||
|
["assets/yolov5", "usr/bin/dump-rtsp-jpg", "755"],
|
||||||
|
["assets/libswscale.so.5.7.100", "usr/lib/libswscale.so.5.7.100", "755"],
|
||||||
|
# ["usr/lib/libswscale.so.5", "usr/lib/libswscale.so.5.7.100", "symlink"]
|
||||||
|
# ["usr/lib/libswscale.so", "usr/lib/libswscale.so.5.7.100", "symlink"]
|
||||||
|
]
|
||||||
|
|
||||||
|
maintainer-scripts = "debian"
|
||||||
|
systemd-units = [
|
||||||
|
{ unit-name = "sys-op-api", enable = true }
|
||||||
|
]
|
||||||
33
Makefile
Normal file → Executable file
33
Makefile
Normal file → Executable file
@ -1,14 +1,18 @@
|
|||||||
self:
|
self:
|
||||||
RUSTFLAGS="-L ." cargo build --features "tap" --release
|
|
||||||
|
|
||||||
linux:
|
|
||||||
RUSTFLAGS="-L ." cargo build --features "tap" --release --target x86_64-unknown-linux-musl
|
|
||||||
|
|
||||||
linux-tun:
|
|
||||||
RUSTFLAGS="-L ." cargo build --release
|
RUSTFLAGS="-L ." cargo build --release
|
||||||
|
|
||||||
|
linux:
|
||||||
|
RUSTFLAGS="-L ." cargo build --release --target x86_64-unknown-linux-musl
|
||||||
|
|
||||||
|
aarch64:
|
||||||
|
RUSTFLAGS="-L ." cargo build --release --target aarch64-unknown-linux-gnu
|
||||||
|
|
||||||
|
|
||||||
|
linux-tun:
|
||||||
|
RUSTFLAGS="-L ." cargo build --features "tun" --release
|
||||||
|
|
||||||
win:
|
win:
|
||||||
cargo build --release
|
cargo build --release --target x86_64-pc-windows-gnu
|
||||||
|
|
||||||
pb:
|
pb:
|
||||||
cargo run --bin build_pb
|
cargo run --bin build_pb
|
||||||
@ -18,7 +22,16 @@ libtun-so-clang:
|
|||||||
cd src/network && clang -fPIC -shared -o libtuntap.so tuntap.c && cd -
|
cd src/network && clang -fPIC -shared -o libtuntap.so tuntap.c && cd -
|
||||||
|
|
||||||
libtun-so:
|
libtun-so:
|
||||||
cd src/network && gcc -fPIC -shared -o libtuntap.so tuntap.c && cd -
|
cd src/network && gcc -fPIC -shared -o libtuntap.so tuntap.c && cp libtuntap.so ../.. && cd -
|
||||||
|
|
||||||
libtun-a:
|
libtun-aarch64-so:
|
||||||
cd src/network && gcc -c tuntap.c && ar rcs libtuntap.a tuntap.o && cd -
|
cd src/network && aarch64-linux-gnu-gcc -fPIC -shared -o libtuntap_aarch64.so tuntap.c && cp libtuntap_aarch64.so ../../libtuntap.so && cd -
|
||||||
|
|
||||||
|
pack:
|
||||||
|
tar -czvf punchnet.tar.gz punchnet punchnet.service libtuntap.so install.sh
|
||||||
|
|
||||||
|
deb-aarch64: libtun-aarch64-so
|
||||||
|
cargo deb --target aarch64-unknown-linux-gnu
|
||||||
|
|
||||||
|
deb: libtun-so
|
||||||
|
cargo deb
|
||||||
13
install.sh
Normal file
13
install.sh
Normal file
@ -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
|
||||||
@ -15,7 +15,7 @@ message SDLV6Info {
|
|||||||
|
|
||||||
// 设备网络地址信息
|
// 设备网络地址信息
|
||||||
message SDLDevAddr {
|
message SDLDevAddr {
|
||||||
uint32 network_id = 1;
|
string network_id = 1;
|
||||||
bytes mac = 2;
|
bytes mac = 2;
|
||||||
uint32 net_addr = 3;
|
uint32 net_addr = 3;
|
||||||
uint32 net_bit_len = 4;
|
uint32 net_bit_len = 4;
|
||||||
@ -33,6 +33,7 @@ message SDLRegisterSuper {
|
|||||||
SDLDevAddr dev_addr = 4;
|
SDLDevAddr dev_addr = 4;
|
||||||
string pub_key = 5;
|
string pub_key = 5;
|
||||||
string token = 6;
|
string token = 6;
|
||||||
|
string network_id = 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
message SDLRegisterSuperAck {
|
message SDLRegisterSuperAck {
|
||||||
@ -106,7 +107,7 @@ message SDLFlows {
|
|||||||
message SDLStunRequest {
|
message SDLStunRequest {
|
||||||
uint32 cookie = 1;
|
uint32 cookie = 1;
|
||||||
string client_id = 2;
|
string client_id = 2;
|
||||||
uint32 network_id = 3;
|
string network_id = 3;
|
||||||
bytes mac = 4;
|
bytes mac = 4;
|
||||||
uint32 ip = 5;
|
uint32 ip = 5;
|
||||||
uint32 nat_type = 6;
|
uint32 nat_type = 6;
|
||||||
@ -118,7 +119,7 @@ message SDLStunReply {
|
|||||||
}
|
}
|
||||||
|
|
||||||
message SDLData {
|
message SDLData {
|
||||||
uint32 network_id = 1;
|
string network_id = 1;
|
||||||
bytes src_mac = 2;
|
bytes src_mac = 2;
|
||||||
bytes dst_mac = 3;
|
bytes dst_mac = 3;
|
||||||
bool is_p2p = 4;
|
bool is_p2p = 4;
|
||||||
@ -127,13 +128,13 @@ message SDLData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
message SDLRegister {
|
message SDLRegister {
|
||||||
uint32 network_id = 1;
|
string network_id = 1;
|
||||||
bytes src_mac = 2;
|
bytes src_mac = 2;
|
||||||
bytes dst_mac = 3;
|
bytes dst_mac = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message SDLRegisterAck {
|
message SDLRegisterAck {
|
||||||
uint32 network_id = 1;
|
string network_id = 1;
|
||||||
bytes src_mac = 2;
|
bytes src_mac = 2;
|
||||||
bytes dst_mac = 3;
|
bytes dst_mac = 3;
|
||||||
}
|
}
|
||||||
|
|||||||
0
nat-simulator.sh
Normal file → Executable file
0
nat-simulator.sh
Normal file → Executable file
12
punchnet.service
Normal file
12
punchnet.service
Normal file
@ -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
|
||||||
4
remote.record
Normal file
4
remote.record
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
origin git@git.asxalex.pw:rust/sdlan-lib-rs.git (fetch)
|
||||||
|
origin git@git.asxalex.pw:rust/sdlan-lib-rs.git (push)
|
||||||
|
origin2 http://101.43.184.190:3000/xlf/sdlan-lib-rs.git (fetch)
|
||||||
|
origin2 http://101.43.184.190:3000/xlf/sdlan-lib-rs.git (push)
|
||||||
0
src/bin/build_pb/main.rs
Normal file → Executable file
0
src/bin/build_pb/main.rs
Normal file → Executable file
37
src/bin/sdlan/main.rs → src/bin/punchnet/main.rs
Normal file → Executable file
37
src/bin/sdlan/main.rs → src/bin/punchnet/main.rs
Normal file → Executable file
@ -1,41 +1,58 @@
|
|||||||
use sdlan_rs::get_edge;
|
use punchnet::get_edge;
|
||||||
use sdlan_rs::run_sdlan;
|
use punchnet::run_sdlan;
|
||||||
use sdlan_rs::CommandLine;
|
use punchnet::CommandLine;
|
||||||
use sdlan_rs::CommandLineInput;
|
use punchnet::CommandLineInput;
|
||||||
use sdlan_sn_rs::log;
|
use sdlan_sn_rs::log;
|
||||||
|
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
|
use std::process::exit;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
// let _guard = rolling_file::new_log(
|
||||||
|
// "./.output",
|
||||||
|
// 7,
|
||||||
|
// rolling_file::PeriodGap::Daily,
|
||||||
|
// tracing::Level::DEBUG,
|
||||||
|
// true,
|
||||||
|
// true,
|
||||||
|
// );
|
||||||
let _guard = log::init_log();
|
let _guard = log::init_log();
|
||||||
|
|
||||||
let cmd = CommandLineInput::from_args();
|
let cmd = CommandLineInput::from_args();
|
||||||
|
if cmd.token.is_empty() && cmd.user_network_id.is_empty() {
|
||||||
|
eprintln!("[ERROR] no token or network_id is specifed. \n\ttry \"punchnet --help\" for detailed usage");
|
||||||
|
exit(-1);
|
||||||
|
// error!("no token or network_id is specified");
|
||||||
|
// exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
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 _ = run_sdlan(
|
let _ = run_sdlan(
|
||||||
CommandLine {
|
CommandLine {
|
||||||
sn: "39.98.184.67:1265".to_owned(),
|
sn: server.clone()+":1265",
|
||||||
tcp: "39.98.184.67:18083".to_owned(),
|
tcp: server.clone()+":18083",
|
||||||
nat_server1: "39.98.184.67:1265".to_owned(),
|
nat_server1: server.clone() +":1265",
|
||||||
nat_server2: "47.98.178.3:1265".to_owned(),
|
nat_server2: "47.98.178.3:1265".to_owned(),
|
||||||
_allow_routing: true,
|
_allow_routing: true,
|
||||||
_drop_multicast: true,
|
_drop_multicast: true,
|
||||||
register_ttl: 1,
|
register_ttl: 1,
|
||||||
mtu: 1290,
|
mtu: 1400,
|
||||||
name: "tau".to_owned(),
|
name: "tau".to_owned(),
|
||||||
tos: 0,
|
tos: 0,
|
||||||
local_port: cmd.port,
|
local_port: cmd.port,
|
||||||
token: "test-token".to_owned(),
|
token: "test-token".to_owned(),
|
||||||
|
user_network_id: cmd.user_network_id,
|
||||||
allow_p2p: true,
|
allow_p2p: true,
|
||||||
},
|
},
|
||||||
tx,
|
tx,
|
||||||
&sdlan_rs::get_install_channel(),
|
&punchnet::get_install_channel(),
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
0
src/config/mod.rs
Normal file → Executable file
0
src/config/mod.rs
Normal file → Executable file
11
src/lib.rs
Normal file → Executable file
11
src/lib.rs
Normal file → Executable file
@ -43,7 +43,16 @@ pub async fn run_sdlan(
|
|||||||
|
|
||||||
init_arp();
|
init_arp();
|
||||||
|
|
||||||
if let Err(e) = init_edge(&args.token, node_conf, args.tos, start_stop_sender).await {
|
if let Err(e) = init_edge(
|
||||||
|
&args.token,
|
||||||
|
&args.user_network_id,
|
||||||
|
node_conf,
|
||||||
|
args.tos,
|
||||||
|
start_stop_sender,
|
||||||
|
args.mtu,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
panic!("failed to init edge: {:?}", e);
|
panic!("failed to init edge: {:?}", e);
|
||||||
}
|
}
|
||||||
let _ = sender.send(true);
|
let _ = sender.send(true);
|
||||||
|
|||||||
0
src/network/arp.rs
Normal file → Executable file
0
src/network/arp.rs
Normal file → Executable file
33
src/network/async_main.rs
Normal file → Executable file
33
src/network/async_main.rs
Normal file → Executable file
@ -18,7 +18,7 @@ use crate::ConnectionState;
|
|||||||
use sdlan_sn_rs::config::AF_INET;
|
use sdlan_sn_rs::config::AF_INET;
|
||||||
use sdlan_sn_rs::peer::{SdlanSock, V6Info};
|
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, gen_rsa_keys};
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
@ -61,7 +61,7 @@ async fn handle_tcp_message(msg: SdlanTcp) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let ip = ip_to_string(&dev.net_addr);
|
let ip = ip_to_string(&dev.net_addr);
|
||||||
debug!("aes key is {:?}, ip is {}/{}", aes, ip, dev.net_bit_len,);
|
// debug!("aes key is {:?}, ip is {}/{}", aes, ip, dev.net_bit_len,);
|
||||||
println!("assigned ip: {}", ip);
|
println!("assigned ip: {}", ip);
|
||||||
edge.device_config
|
edge.device_config
|
||||||
.ip
|
.ip
|
||||||
@ -79,13 +79,14 @@ async fn handle_tcp_message(msg: SdlanTcp) {
|
|||||||
.net_bit_len
|
.net_bit_len
|
||||||
.store(dev.net_bit_len as u8, Ordering::Relaxed);
|
.store(dev.net_bit_len as u8, Ordering::Relaxed);
|
||||||
edge.device.reload_config(&edge.device_config);
|
edge.device.reload_config(&edge.device_config);
|
||||||
edge.network_id.store(dev.network_id, Ordering::Relaxed);
|
*edge.network_id.lock().unwrap() = dev.network_id;
|
||||||
|
// edge.network_id.store(dev.network_id, Ordering::Relaxed);
|
||||||
|
|
||||||
edge.set_authorized(true, aes);
|
edge.set_authorized(true, aes);
|
||||||
send_stun_request(edge).await;
|
send_stun_request(edge).await;
|
||||||
tokio::spawn(async {
|
tokio::spawn(async {
|
||||||
let nattype = edge.probe_nat_type().await;
|
let nattype = edge.probe_nat_type().await;
|
||||||
println!("nat type is: {:?}", nattype);
|
// println!("nat type is: {:?}", nattype);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
PacketType::RegisterSuperNAK => {
|
PacketType::RegisterSuperNAK => {
|
||||||
@ -251,7 +252,7 @@ pub async fn async_main(
|
|||||||
// let _ = PidRecorder::new(".pid");
|
// let _ = PidRecorder::new(".pid");
|
||||||
|
|
||||||
// // gen public key
|
// // gen public key
|
||||||
// gen_rsa_keys(".client");
|
gen_rsa_keys(".client");
|
||||||
// let mut pubkey = String::new();
|
// let mut pubkey = String::new();
|
||||||
// File::open(".client/id_rsa.pub")?.read_to_string(&mut pubkey)?;
|
// File::open(".client/id_rsa.pub")?.read_to_string(&mut pubkey)?;
|
||||||
// let privatekey = load_private_key_file(".client/id_rsa")?;
|
// let privatekey = load_private_key_file(".client/id_rsa")?;
|
||||||
@ -290,6 +291,7 @@ pub async fn async_main(
|
|||||||
let installed_channel = install_channel.to_owned();
|
let installed_channel = install_channel.to_owned();
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let token = edge._token.lock().unwrap().clone();
|
let token = edge._token.lock().unwrap().clone();
|
||||||
|
let networkid = edge.network_id.lock().unwrap().clone();
|
||||||
// let edge = get_edge();
|
// let edge = get_edge();
|
||||||
// let edge = get_edge();
|
// let edge = get_edge();
|
||||||
// let token = args.token.clone();
|
// let token = args.token.clone();
|
||||||
@ -310,11 +312,12 @@ pub async fn async_main(
|
|||||||
dev_addr: Some(SdlDevAddr {
|
dev_addr: Some(SdlDevAddr {
|
||||||
mac: Vec::from(edge.device_config.get_mac()),
|
mac: Vec::from(edge.device_config.get_mac()),
|
||||||
net_addr: 0,
|
net_addr: 0,
|
||||||
network_id: 0,
|
network_id: "".to_owned(),
|
||||||
net_bit_len: 0,
|
net_bit_len: 0,
|
||||||
}),
|
}),
|
||||||
pub_key: edge.rsa_pubkey.clone(),
|
pub_key: edge.rsa_pubkey.clone(),
|
||||||
token,
|
token,
|
||||||
|
network_id: networkid,
|
||||||
};
|
};
|
||||||
// debug!("send register super: {:?}", register_super);
|
// debug!("send register super: {:?}", register_super);
|
||||||
let packet_id = match pkt_id {
|
let packet_id = match pkt_id {
|
||||||
@ -448,7 +451,8 @@ async fn send_stun_request(eee: &Node) {
|
|||||||
let req = SdlStunRequest {
|
let req = SdlStunRequest {
|
||||||
cookie: 0,
|
cookie: 0,
|
||||||
client_id: eee.config.node_uuid.clone(),
|
client_id: eee.config.node_uuid.clone(),
|
||||||
network_id: eee.network_id.load(Ordering::Relaxed),
|
network_id: eee.network_id.lock().unwrap().clone(),
|
||||||
|
// network_id: eee.network_id.load(Ordering::Relaxed),
|
||||||
ip: eee.device_config.get_ip(),
|
ip: eee.device_config.get_ip(),
|
||||||
mac: Vec::from(eee.device_config.get_mac()),
|
mac: Vec::from(eee.device_config.get_mac()),
|
||||||
nat_type: eee.get_nat_type() as u32,
|
nat_type: eee.get_nat_type() as u32,
|
||||||
@ -555,7 +559,7 @@ async fn loop_tap(eee: &'static Node, cancel: CancellationToken) {
|
|||||||
async fn get_tun_flow(eee: &'static Node, tx: Sender<Vec<u8>>) {
|
async fn get_tun_flow(eee: &'static Node, tx: Sender<Vec<u8>>) {
|
||||||
loop {
|
loop {
|
||||||
let buf = tokio::task::spawn_blocking(|| {
|
let buf = tokio::task::spawn_blocking(|| {
|
||||||
let mut buf = vec![0; 1800];
|
let mut buf = vec![0; 1500];
|
||||||
let Ok(size) = eee.device.recv(&mut buf) else {
|
let Ok(size) = eee.device.recv(&mut buf) else {
|
||||||
return vec![];
|
return vec![];
|
||||||
};
|
};
|
||||||
@ -636,25 +640,32 @@ async fn find_peer_destination(eee: &'static Node, dst_mac: Mac) -> (SdlanSock,
|
|||||||
}
|
}
|
||||||
let mut is_p2p = false;
|
let mut is_p2p = false;
|
||||||
let result: SdlanSock;
|
let result: SdlanSock;
|
||||||
if let Some(dst) = eee.known_peers.get_peer(&dst_mac) {
|
let mut need_delete_from_known_peers = false;
|
||||||
|
if let Some(dst) = eee.known_peers.peers.get_mut(&dst_mac) {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
if now - dst.last_p2p.load(Ordering::Relaxed) >= ((dst.timeout / 2) as u64) {
|
if now - dst.last_p2p.load(Ordering::Relaxed) >= ((dst.timeout / 2) as u64) {
|
||||||
// too much time elapsed since we saw the peer, need to register again
|
// too much time elapsed since we saw the peer, need to register again
|
||||||
error!("last p2p is too old, deleting from known_peers");
|
error!("last p2p is too old, deleting from known_peers");
|
||||||
eee.known_peers.delete_peer_with_mac(&dst_mac);
|
need_delete_from_known_peers = true;
|
||||||
|
// eee.known_peers.delete_peer_with_mac(&dst_mac);
|
||||||
|
debug!("deleted from known");
|
||||||
result = eee.config.super_nodes
|
result = eee.config.super_nodes
|
||||||
[eee.config.super_node_index.load(Ordering::Relaxed) as usize]
|
[eee.config.super_node_index.load(Ordering::Relaxed) as usize]
|
||||||
.deepcopy();
|
.deepcopy();
|
||||||
} else {
|
} else {
|
||||||
// dst.last_seen.store(now, Ordering::Relaxed);
|
// dst.last_seen.store(now, Ordering::Relaxed);
|
||||||
is_p2p = true;
|
is_p2p = true;
|
||||||
result = dst.sock.read().unwrap().deepcopy();
|
result = dst.sock.deepcopy();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result = eee.config.super_nodes
|
result = eee.config.super_nodes
|
||||||
[eee.config.super_node_index.load(Ordering::Relaxed) as usize]
|
[eee.config.super_node_index.load(Ordering::Relaxed) as usize]
|
||||||
.deepcopy();
|
.deepcopy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if need_delete_from_known_peers {
|
||||||
|
eee.known_peers.delete_peer_with_mac(&dst_mac);
|
||||||
|
}
|
||||||
// println!("find peer_destination: {}", is_p2p);
|
// println!("find peer_destination: {}", is_p2p);
|
||||||
if !is_p2p {
|
if !is_p2p {
|
||||||
debug!("check_query_peer_info");
|
debug!("check_query_peer_info");
|
||||||
|
|||||||
4
src/network/device.rs
Normal file → Executable file
4
src/network/device.rs
Normal file → Executable file
@ -7,16 +7,18 @@ use tracing::debug;
|
|||||||
use crate::utils::generate_mac_address;
|
use crate::utils::generate_mac_address;
|
||||||
|
|
||||||
pub struct DeviceConfig {
|
pub struct DeviceConfig {
|
||||||
|
pub mtu: u32,
|
||||||
pub mac: RwLock<Mac>,
|
pub mac: RwLock<Mac>,
|
||||||
pub ip: IpSubnet,
|
pub ip: IpSubnet,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DeviceConfig {
|
impl DeviceConfig {
|
||||||
pub fn new() -> Self {
|
pub fn new(mtu: u32) -> Self {
|
||||||
let mac = generate_mac_address();
|
let 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,
|
||||||
mac: RwLock::new(mac),
|
mac: RwLock::new(mac),
|
||||||
ip: IpSubnet::new(0, 0),
|
ip: IpSubnet::new(0, 0),
|
||||||
}
|
}
|
||||||
|
|||||||
2
src/network/ipv6.rs
Normal file → Executable file
2
src/network/ipv6.rs
Normal file → Executable file
@ -76,7 +76,7 @@ pub fn get_current_ipv6() -> Option<Ipv6Addr> {
|
|||||||
}
|
}
|
||||||
IpAddr::V6(ipv6) => {
|
IpAddr::V6(ipv6) => {
|
||||||
if ipv6.octets()[0] & 0x70 == 0x20 {
|
if ipv6.octets()[0] & 0x70 == 0x20 {
|
||||||
println!("got global ip: {}", ipv6);
|
// println!("got global ip: {}", ipv6);
|
||||||
return Some(ipv6);
|
return Some(ipv6);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
0
src/network/mod.rs
Normal file → Executable file
0
src/network/mod.rs
Normal file → Executable file
87
src/network/node.rs
Normal file → Executable file
87
src/network/node.rs
Normal file → Executable file
@ -8,7 +8,6 @@ use std::time::Duration;
|
|||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
use tokio::time::error::Elapsed;
|
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
use crate::pb::{
|
use crate::pb::{
|
||||||
@ -17,7 +16,7 @@ use crate::pb::{
|
|||||||
use crate::tcp::{get_tcp_conn, NatType, PacketType, StunProbeAttr};
|
use crate::tcp::{get_tcp_conn, NatType, PacketType, StunProbeAttr};
|
||||||
use crate::utils::{PidRecorder, Socket};
|
use crate::utils::{PidRecorder, Socket};
|
||||||
|
|
||||||
use sdlan_sn_rs::peer::{is_sdlan_sock_equal, IpSubnet, V6Info};
|
use sdlan_sn_rs::peer::{IpSubnet, V6Info};
|
||||||
|
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
|
|
||||||
@ -32,9 +31,11 @@ static EDGE: OnceCell<Node> = OnceCell::new();
|
|||||||
|
|
||||||
pub async fn init_edge(
|
pub async fn init_edge(
|
||||||
token: &str,
|
token: &str,
|
||||||
|
user_network_id: &str,
|
||||||
node_conf: NodeConfig,
|
node_conf: NodeConfig,
|
||||||
tos: u32,
|
tos: u32,
|
||||||
start_stop: Sender<StartStopInfo>,
|
start_stop: Sender<StartStopInfo>,
|
||||||
|
mtu: u32,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let _ = PidRecorder::new(".pid");
|
let _ = PidRecorder::new(".pid");
|
||||||
|
|
||||||
@ -72,9 +73,11 @@ pub async fn init_edge(
|
|||||||
sock_v4,
|
sock_v4,
|
||||||
sock_multicast,
|
sock_multicast,
|
||||||
token,
|
token,
|
||||||
|
user_network_id,
|
||||||
privatekey,
|
privatekey,
|
||||||
tcp_pong.clone(),
|
tcp_pong.clone(),
|
||||||
start_stop,
|
start_stop,
|
||||||
|
mtu,
|
||||||
);
|
);
|
||||||
do_init_edge(edge)?;
|
do_init_edge(edge)?;
|
||||||
|
|
||||||
@ -111,7 +114,7 @@ pub struct StartStopInfo {
|
|||||||
pub struct Node {
|
pub struct Node {
|
||||||
packet_id: AtomicU32,
|
packet_id: AtomicU32,
|
||||||
|
|
||||||
pub network_id: AtomicU32,
|
pub network_id: Mutex<String>,
|
||||||
|
|
||||||
pub tcp_pong: Arc<AtomicU64>,
|
pub tcp_pong: Arc<AtomicU64>,
|
||||||
|
|
||||||
@ -119,6 +122,7 @@ pub struct Node {
|
|||||||
|
|
||||||
// user token info
|
// user token info
|
||||||
pub _token: Mutex<String>,
|
pub _token: Mutex<String>,
|
||||||
|
pub _user_network_id: String,
|
||||||
|
|
||||||
pub device_config: DeviceConfig,
|
pub device_config: DeviceConfig,
|
||||||
pub device: Iface,
|
pub device: Iface,
|
||||||
@ -244,11 +248,14 @@ impl Node {
|
|||||||
multicast_sock: Option<Socket>,
|
multicast_sock: Option<Socket>,
|
||||||
// tcpsock: TCPSocket,
|
// tcpsock: TCPSocket,
|
||||||
token: &str,
|
token: &str,
|
||||||
|
user_network_id: &str,
|
||||||
|
// network: &str,
|
||||||
private: RsaPrivateKey,
|
private: RsaPrivateKey,
|
||||||
tcp_pong: Arc<AtomicU64>,
|
tcp_pong: Arc<AtomicU64>,
|
||||||
start_stop: Sender<StartStopInfo>,
|
start_stop: Sender<StartStopInfo>,
|
||||||
|
mtu: u32,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mode = if cfg!(feature = "tap") {
|
let mode = if cfg!(not(feature = "tun")) {
|
||||||
Mode::Tap
|
Mode::Tap
|
||||||
} else {
|
} else {
|
||||||
Mode::Tun
|
Mode::Tun
|
||||||
@ -256,8 +263,10 @@ impl Node {
|
|||||||
|
|
||||||
Self {
|
Self {
|
||||||
packet_id: AtomicU32::new(1),
|
packet_id: AtomicU32::new(1),
|
||||||
network_id: AtomicU32::new(0),
|
network_id: Mutex::new("".to_owned()),
|
||||||
|
// network_id: AtomicU32::new(0),
|
||||||
_token: Mutex::new(token.to_owned()),
|
_token: Mutex::new(token.to_owned()),
|
||||||
|
_user_network_id: user_network_id.to_owned(),
|
||||||
|
|
||||||
start_stop_sender: start_stop,
|
start_stop_sender: start_stop,
|
||||||
|
|
||||||
@ -265,7 +274,7 @@ impl Node {
|
|||||||
|
|
||||||
nat_type: Mutex::new(NatType::Blocked),
|
nat_type: Mutex::new(NatType::Blocked),
|
||||||
|
|
||||||
device_config: DeviceConfig::new(),
|
device_config: DeviceConfig::new(mtu),
|
||||||
device: new_iface("dev", mode),
|
device: new_iface("dev", mode),
|
||||||
|
|
||||||
authorized: AtomicBool::new(false),
|
authorized: AtomicBool::new(false),
|
||||||
@ -400,28 +409,36 @@ impl Node {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if reply1.ip == self.outer_ip_v4.load(Ordering::Relaxed) {
|
if reply1.ip == self.outer_ip_v4.load(Ordering::Relaxed) {
|
||||||
let Ok(reply2) = self
|
let Ok(_reply2) = self
|
||||||
|
.send_and_wait_for_probe_reply(StunProbeAttr::Peer, &self.config.nat_server1)
|
||||||
|
.await
|
||||||
|
else {
|
||||||
|
*self.nat_type.lock().unwrap() = NatType::Symmetric;
|
||||||
|
return NatType::Symmetric;
|
||||||
|
};
|
||||||
|
*self.nat_type.lock().unwrap() = NatType::NoNat;
|
||||||
|
return NatType::NoNat;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(_reply2_2) = self
|
||||||
|
.send_and_wait_for_probe_reply(StunProbeAttr::Peer, &self.config.nat_server1)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
*self.nat_type.lock().unwrap() = NatType::FullCone;
|
||||||
|
return NatType::FullCone;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Ok(reply3) = self
|
||||||
.send_and_wait_for_probe_reply(StunProbeAttr::None, &self.config.nat_server2)
|
.send_and_wait_for_probe_reply(StunProbeAttr::None, &self.config.nat_server2)
|
||||||
.await
|
.await
|
||||||
else {
|
else {
|
||||||
*self.nat_type.lock().unwrap() = NatType::Blocked;
|
*self.nat_type.lock().unwrap() = NatType::Blocked;
|
||||||
return NatType::Blocked;
|
return NatType::Blocked;
|
||||||
};
|
};
|
||||||
if reply1.ip != reply2.ip || reply1.port != reply2.port {
|
if reply3.ip != reply1.ip || reply3.port != reply1.port {
|
||||||
*self.nat_type.lock().unwrap() = NatType::Symmetric;
|
*self.nat_type.lock().unwrap() = NatType::Symmetric;
|
||||||
return NatType::Symmetric;
|
return NatType::Symmetric;
|
||||||
}
|
}
|
||||||
*self.nat_type.lock().unwrap() = NatType::NoNat;
|
|
||||||
return NatType::NoNat;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(_reply3) = self
|
|
||||||
.send_and_wait_for_probe_reply(StunProbeAttr::Peer, &self.config.nat_server1)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
*self.nat_type.lock().unwrap() = NatType::FullCone;
|
|
||||||
return NatType::FullCone;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(_reply4) = self
|
if let Ok(_reply4) = self
|
||||||
.send_and_wait_for_probe_reply(StunProbeAttr::Port, &self.config.nat_server1)
|
.send_and_wait_for_probe_reply(StunProbeAttr::Port, &self.config.nat_server1)
|
||||||
@ -455,7 +472,7 @@ impl Node {
|
|||||||
attr: msgattr as u32,
|
attr: msgattr as u32,
|
||||||
cookie,
|
cookie,
|
||||||
};
|
};
|
||||||
println!("==> sending probe request: {:?}", probe);
|
// println!("==> sending probe request: {:?}", probe);
|
||||||
|
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
self.cookie_match.insert(cookie, tx);
|
self.cookie_match.insert(cookie, tx);
|
||||||
@ -486,7 +503,7 @@ impl Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct PeerMap {
|
pub struct PeerMap {
|
||||||
pub peers: DashMap<Mac, Arc<EdgePeer>>,
|
pub peers: DashMap<Mac, EdgePeer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
@ -497,33 +514,37 @@ impl PeerMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_peer(&self, ip: &Mac) -> Option<Arc<EdgePeer>> {
|
/*
|
||||||
if let Some(v) = self.peers.get(ip) {
|
pub fn get_peer(&self, mac: &Mac) -> Option<Arc<EdgePeer>> {
|
||||||
|
if let Some(v) = self.peers.get(mac) {
|
||||||
Some(v.clone())
|
Some(v.clone())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
pub fn clear(&self) {
|
pub fn clear(&self) {
|
||||||
self.peers.clear();
|
self.peers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_peer_by_sock(&self, sock: &SdlanSock) -> Option<Arc<EdgePeer>> {
|
pub fn get_peer_by_sock(&self, sock: &SdlanSock) -> Option<Arc<EdgePeer>> {
|
||||||
|
/*
|
||||||
for s in self.peers.iter() {
|
for s in self.peers.iter() {
|
||||||
let m = s.sock.read().unwrap();
|
let m = s.sock.read().unwrap();
|
||||||
if is_sdlan_sock_equal(&m, sock) {
|
if is_sdlan_sock_equal(&m, sock) {
|
||||||
return Some(s.value().clone());
|
return Some(s.value().clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_peer_with_mac(&self, mac: &Mac) -> Option<(Mac, Arc<EdgePeer>)> {
|
pub fn delete_peer_with_mac(&self, mac: &Mac) -> Option<(Mac, EdgePeer)> {
|
||||||
self.peers.remove(mac)
|
self.peers.remove(mac)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_peer(&self, mac: Mac, p: Arc<EdgePeer>) {
|
pub fn insert_peer(&self, mac: Mac, p: EdgePeer) {
|
||||||
self.peers.insert(mac, p);
|
self.peers.insert(mac, p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -558,7 +579,7 @@ impl NodeStats {
|
|||||||
|
|
||||||
use sdlan_sn_rs::peer::SdlanSock;
|
use sdlan_sn_rs::peer::SdlanSock;
|
||||||
|
|
||||||
use crate::config::{self, MULTICAST_PORT, REGISTER_INTERVAL, REGISTER_SUPER_INTERVAL};
|
use crate::config::{self, MULTICAST_PORT, REGISTER_SUPER_INTERVAL};
|
||||||
pub struct NodeConfig {
|
pub struct NodeConfig {
|
||||||
// node name
|
// node name
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@ -601,12 +622,12 @@ pub struct EdgePeer {
|
|||||||
// pub mac: Mac,
|
// pub mac: Mac,
|
||||||
pub dev_addr: IpSubnet,
|
pub dev_addr: IpSubnet,
|
||||||
|
|
||||||
pub nat_type: RwLock<NatType>,
|
pub nat_type: NatType,
|
||||||
|
|
||||||
// 对端对外开放的ip和端口信息
|
// 对端对外开放的ip和端口信息
|
||||||
pub sock: RwLock<SdlanSock>,
|
pub sock: SdlanSock,
|
||||||
// peer's ipv6 info
|
// peer's ipv6 info
|
||||||
pub _v6_info: RwLock<Option<SdlanSock>>,
|
pub _v6_info: Option<SdlanSock>,
|
||||||
|
|
||||||
pub timeout: u8,
|
pub timeout: u8,
|
||||||
|
|
||||||
@ -645,18 +666,18 @@ impl EdgePeer {
|
|||||||
Self {
|
Self {
|
||||||
// mac,
|
// mac,
|
||||||
dev_addr: IpSubnet::new(net_addr, net_bit_len),
|
dev_addr: IpSubnet::new(net_addr, net_bit_len),
|
||||||
sock: RwLock::new(sock.deepcopy()),
|
sock: sock.deepcopy(),
|
||||||
_v6_info: RwLock::new(v6_info),
|
_v6_info: v6_info,
|
||||||
timeout: REGISTER_SUPER_INTERVAL as u8,
|
timeout: REGISTER_SUPER_INTERVAL as u8,
|
||||||
last_p2p: AtomicU64::new(0),
|
last_p2p: AtomicU64::new(0),
|
||||||
last_seen: AtomicU64::new(0),
|
last_seen: AtomicU64::new(0),
|
||||||
_last_valid_timestamp: AtomicU64::new(now),
|
_last_valid_timestamp: AtomicU64::new(now),
|
||||||
last_sent_query: AtomicU64::new(0),
|
last_sent_query: AtomicU64::new(0),
|
||||||
nat_type: RwLock::new(NatType::Invalid),
|
nat_type: NatType::Invalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_nat_type(&self) -> NatType {
|
pub fn get_nat_type(&self) -> NatType {
|
||||||
*self.nat_type.read().unwrap()
|
self.nat_type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
222
src/network/packet.rs
Normal file → Executable file
222
src/network/packet.rs
Normal file → Executable file
@ -13,9 +13,8 @@ use crate::{
|
|||||||
utils::{send_to_sock, Socket},
|
utils::{send_to_sock, Socket},
|
||||||
};
|
};
|
||||||
use etherparse::Ethernet2Header;
|
use etherparse::Ethernet2Header;
|
||||||
use num_enum::TryFromPrimitive;
|
|
||||||
use prost::Message;
|
use prost::Message;
|
||||||
use sdlan_sn_rs::utils::BROADCAST_MAC;
|
use sdlan_sn_rs::utils::{aes_encrypt, BROADCAST_MAC};
|
||||||
use sdlan_sn_rs::{
|
use sdlan_sn_rs::{
|
||||||
config::{AF_INET, AF_INET6},
|
config::{AF_INET, AF_INET6},
|
||||||
peer::{is_sdlan_sock_equal, SdlanSock, V6Info},
|
peer::{is_sdlan_sock_equal, SdlanSock, V6Info},
|
||||||
@ -24,7 +23,6 @@ use sdlan_sn_rs::{
|
|||||||
Mac, Result, SDLanError,
|
Mac, Result, SDLanError,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use tracing::{debug, error, info};
|
use tracing::{debug, error, info};
|
||||||
|
|
||||||
@ -124,6 +122,8 @@ pub async fn handle_packet(eee: &'static Node, addr: SocketAddr, buf: &[u8]) ->
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if data.is_p2p {
|
if data.is_p2p {
|
||||||
|
let from_sock = from_sock.deepcopy();
|
||||||
|
tokio::spawn(async move {
|
||||||
check_peer_registration_needed(
|
check_peer_registration_needed(
|
||||||
eee,
|
eee,
|
||||||
!data.is_p2p,
|
!data.is_p2p,
|
||||||
@ -133,6 +133,7 @@ pub async fn handle_packet(eee: &'static Node, addr: SocketAddr, buf: &[u8]) ->
|
|||||||
&from_sock,
|
&from_sock,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
handle_tun_packet(eee, &from_sock, !data.is_p2p, data).await;
|
handle_tun_packet(eee, &from_sock, !data.is_p2p, data).await;
|
||||||
}
|
}
|
||||||
@ -142,7 +143,7 @@ pub async fn handle_packet(eee: &'static Node, addr: SocketAddr, buf: &[u8]) ->
|
|||||||
error!("failed to decode SdlStunReply");
|
error!("failed to decode SdlStunReply");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
println!("got stun probe reply: {:?}", reply);
|
// println!("got stun probe reply: {:?}", reply);
|
||||||
eee.send_nat_probe_reply(reply.cookie, reply).await;
|
eee.send_nat_probe_reply(reply.cookie, reply).await;
|
||||||
}
|
}
|
||||||
PacketType::StunReply => {
|
PacketType::StunReply => {
|
||||||
@ -284,16 +285,19 @@ pub async fn handle_packet_peer_info(
|
|||||||
if dst_mac == NULL_MAC {
|
if dst_mac == NULL_MAC {
|
||||||
// pong from sn
|
// pong from sn
|
||||||
} else {
|
} else {
|
||||||
match eee.pending_peers.get_peer(&dst_mac) {
|
let pending = eee.pending_peers.peers.get_mut(&dst_mac);
|
||||||
Some(edgeinfo) => {
|
match pending {
|
||||||
|
Some(mut v) => {
|
||||||
let sock = SdlanSock {
|
let sock = SdlanSock {
|
||||||
family: AF_INET,
|
family: AF_INET,
|
||||||
port: v4.port as u16,
|
port: v4.port as u16,
|
||||||
v4: v4_u32,
|
v4: v4_u32,
|
||||||
v6: [0; 16],
|
v6: [0; 16],
|
||||||
};
|
};
|
||||||
*(edgeinfo.nat_type.write().unwrap()) = remote_nat;
|
v.nat_type = remote_nat;
|
||||||
*(edgeinfo.sock.write().unwrap()) = sock.deepcopy();
|
v.sock = sock.deepcopy();
|
||||||
|
// v.sock = sock.deepcopy();
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"Rx PEERINFO for {}: is at {}",
|
"Rx PEERINFO for {}: is at {}",
|
||||||
mac_to_string(&dst_mac),
|
mac_to_string(&dst_mac),
|
||||||
@ -304,8 +308,6 @@ pub async fn handle_packet_peer_info(
|
|||||||
v6_info = Some(V6Info { port: v6_port, v6 })
|
v6_info = Some(V6Info { port: v6_port, v6 })
|
||||||
}
|
}
|
||||||
send_register(eee, remote_nat, &sock, dst_mac, &v6_info).await;
|
send_register(eee, remote_nat, &sock, dst_mac, &v6_info).await;
|
||||||
|
|
||||||
// register_with_local_peers(eee).await;
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
debug!("Rx PEERINFO unknown peer: {}", mac_to_string(&dst_mac));
|
debug!("Rx PEERINFO unknown peer: {}", mac_to_string(&dst_mac));
|
||||||
@ -544,26 +546,28 @@ pub async fn check_peer_registration_needed(
|
|||||||
_v6_info: &Option<V6Info>,
|
_v6_info: &Option<V6Info>,
|
||||||
peer_sock: &SdlanSock,
|
peer_sock: &SdlanSock,
|
||||||
) {
|
) {
|
||||||
let mut p = eee.known_peers.get_peer(&src_mac);
|
let p = eee.known_peers.peers.get(&src_mac);
|
||||||
if let None = p {
|
let last_seen;
|
||||||
p = eee.known_peers.get_peer_by_sock(peer_sock);
|
let now;
|
||||||
if let Some(ref k) = p {
|
|
||||||
eee.known_peers.insert_peer(src_mac, k.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match p {
|
match p {
|
||||||
None => {
|
None => {
|
||||||
debug!("check peer registration needed, not found in known peers");
|
debug!("check peer registration needed, not found in known peers");
|
||||||
let _ = register_with_new_peer(eee, from_sn, src_mac, _v6_info, remote_nat, peer_sock)
|
let _ = register_with_new_peer(eee, from_sn, src_mac, _v6_info, remote_nat, peer_sock)
|
||||||
.await;
|
.await;
|
||||||
|
return;
|
||||||
// unimplemented!();
|
// unimplemented!();
|
||||||
}
|
}
|
||||||
Some(k) => {
|
Some(k) => {
|
||||||
let mut ipv4_to_ipv6 = false;
|
// let mut ipv4_to_ipv6 = false;
|
||||||
let now = get_current_timestamp();
|
now = get_current_timestamp();
|
||||||
if !from_sn {
|
if !from_sn {
|
||||||
k.last_p2p.store(now, Ordering::Relaxed);
|
k.last_p2p.store(now, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
let origin_family = k.sock.family;
|
||||||
|
if origin_family != peer_sock.family {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/*
|
||||||
if peer_sock.family == AF_INET6 && k.sock.read().unwrap().family == AF_INET {
|
if peer_sock.family == AF_INET6 && k.sock.read().unwrap().family == AF_INET {
|
||||||
println!("changing to ipv6");
|
println!("changing to ipv6");
|
||||||
*k.sock.write().unwrap() = peer_sock.deepcopy();
|
*k.sock.write().unwrap() = peer_sock.deepcopy();
|
||||||
@ -571,13 +575,14 @@ pub async fn check_peer_registration_needed(
|
|||||||
} else {
|
} else {
|
||||||
println!("already is ipv6");
|
println!("already is ipv6");
|
||||||
}
|
}
|
||||||
let last_seen = k.last_seen.load(Ordering::Relaxed);
|
*/
|
||||||
|
last_seen = k.last_seen.load(Ordering::Relaxed);
|
||||||
// more than 3 seconds
|
// more than 3 seconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if now - last_seen > 3 {
|
if now - last_seen > 3 {
|
||||||
check_known_peer_sock_change(eee, from_sn, src_mac, peer_sock, now, ipv4_to_ipv6)
|
check_known_peer_sock_change(eee, from_sn, src_mac, peer_sock, now, false).await;
|
||||||
.await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -594,30 +599,38 @@ async fn check_known_peer_sock_change(
|
|||||||
if is_multi_broadcast(&mac) {
|
if is_multi_broadcast(&mac) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match eee.known_peers.get_peer(&mac) {
|
let delete_from_known_peers;
|
||||||
|
let remote_nat;
|
||||||
|
match eee.known_peers.peers.get_mut(&mac) {
|
||||||
Some(p) => {
|
Some(p) => {
|
||||||
if !ipv4_to_ipv6 && !is_sdlan_sock_equal(&p.sock.read().unwrap(), peersock) {
|
if !ipv4_to_ipv6 && !is_sdlan_sock_equal(&p.sock, peersock) {
|
||||||
if !from_sn {
|
if !from_sn {
|
||||||
info!(
|
info!(
|
||||||
"peer changed: {}: {} -> {}",
|
"peer changed: {}: {} -> {}",
|
||||||
mac_to_string(&mac),
|
mac_to_string(&mac),
|
||||||
&p.sock.read().unwrap().to_string(),
|
&p.sock.to_string(),
|
||||||
peersock.to_string()
|
peersock.to_string()
|
||||||
);
|
);
|
||||||
eee.known_peers.delete_peer_with_mac(&mac);
|
delete_from_known_peers = true;
|
||||||
error!("known peers is deleted");
|
remote_nat = p.get_nat_type();
|
||||||
let remote_nat = p.get_nat_type();
|
|
||||||
register_with_new_peer(eee, from_sn, mac, &None, remote_nat, peersock).await;
|
|
||||||
} else {
|
} else {
|
||||||
// from sn, sn could see a different sock with us, just ignore it
|
// from sn, sn could see a different sock with us, just ignore it
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
p.last_seen.store(when, Ordering::Relaxed);
|
p.last_seen.store(when, Ordering::Relaxed);
|
||||||
|
return;
|
||||||
// from sn, sn could see a different sock with us, just ignore it
|
// from sn, sn could see a different sock with us, just ignore it
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => return,
|
None => return,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if delete_from_known_peers {
|
||||||
|
eee.known_peers.delete_peer_with_mac(&mac);
|
||||||
|
error!("known peers is deleted");
|
||||||
|
register_with_new_peer(eee, from_sn, mac, &None, remote_nat, peersock).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn register_with_new_peer(
|
async fn register_with_new_peer(
|
||||||
@ -630,25 +643,21 @@ async fn register_with_new_peer(
|
|||||||
peersock: &SdlanSock,
|
peersock: &SdlanSock,
|
||||||
) {
|
) {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut scan = eee.pending_peers.get_peer(&mac);
|
let entry = eee.pending_peers.peers.entry(mac);
|
||||||
if let None = scan {
|
let mut new_created = false;
|
||||||
// such ip not found in pending
|
|
||||||
let temp = Arc::new(EdgePeer::new(
|
let mut scan = entry.or_insert_with(|| {
|
||||||
// mac,
|
new_created = true;
|
||||||
0,
|
let temp = EdgePeer::new(0, eee.device_config.get_net_bit(), peersock, &None, now);
|
||||||
eee.device_config.get_net_bit(),
|
temp
|
||||||
peersock,
|
});
|
||||||
&None,
|
|
||||||
now,
|
if new_created {
|
||||||
));
|
|
||||||
debug!(
|
debug!(
|
||||||
"===> new pending {} => {}",
|
"===> new pending {} => {}",
|
||||||
mac_to_string(&mac),
|
mac_to_string(&mac),
|
||||||
peersock.to_string(),
|
peersock.to_string(),
|
||||||
);
|
);
|
||||||
eee.pending_peers.insert_peer(mac, temp.clone());
|
|
||||||
scan = Some(temp);
|
|
||||||
debug!("Pending size: {}", eee.pending_peers.peers.len());
|
|
||||||
|
|
||||||
if from_sn {
|
if from_sn {
|
||||||
// should send register to peer
|
// should send register to peer
|
||||||
@ -690,14 +699,11 @@ async fn register_with_new_peer(
|
|||||||
}
|
}
|
||||||
register_with_local_peers(eee).await;
|
register_with_local_peers(eee).await;
|
||||||
} else {
|
} else {
|
||||||
if let Some(ref s) = scan {
|
scan.sock = peersock.deepcopy();
|
||||||
*(s.sock.write().unwrap()) = peersock.deepcopy();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if let Some(s) = scan {
|
scan.last_seen
|
||||||
s.last_seen
|
|
||||||
.store(get_current_timestamp(), Ordering::Relaxed);
|
.store(get_current_timestamp(), Ordering::Relaxed);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn register_with_local_peers(eee: &'static Node) {
|
async fn register_with_local_peers(eee: &'static Node) {
|
||||||
@ -724,8 +730,10 @@ async fn send_register(
|
|||||||
debug!("skipping register as p2p is disabled");
|
debug!("skipping register as p2p is disabled");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let network_id = eee.network_id.load(Ordering::Relaxed);
|
|
||||||
if network_id == 0 {
|
let network_id = eee.network_id.lock().unwrap().clone();
|
||||||
|
// let network_id = eee.network_id.load(Ordering::Relaxed);
|
||||||
|
if network_id.is_empty() {
|
||||||
error!("not authed");
|
error!("not authed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -773,7 +781,7 @@ async fn send_register(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if need_guess_port {
|
if need_guess_port {
|
||||||
println!("need guess port");
|
// println!("need guess port");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -803,6 +811,14 @@ async fn send_register(
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn printHex(key: &[u8]) {
|
||||||
|
let mut value = vec![];
|
||||||
|
for item in key {
|
||||||
|
value.push(format!("0x{:02x}", item))
|
||||||
|
}
|
||||||
|
println!("[{}]", value.join(" "))
|
||||||
|
}
|
||||||
|
|
||||||
async fn handle_tun_packet(
|
async fn handle_tun_packet(
|
||||||
eee: &Node,
|
eee: &Node,
|
||||||
_from_sock: &SdlanSock,
|
_from_sock: &SdlanSock,
|
||||||
@ -817,6 +833,7 @@ async fn handle_tun_packet(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test_aes(key.as_slice());
|
||||||
let origin = aes_decrypt(key.as_slice(), &payload);
|
let origin = aes_decrypt(key.as_slice(), &payload);
|
||||||
if let Err(_e) = origin {
|
if let Err(_e) = origin {
|
||||||
error!("failed to decrypt original data");
|
error!("failed to decrypt original data");
|
||||||
@ -1005,11 +1022,13 @@ async fn send_register_ack(eee: &Node, orig_sender: &SdlanSock, reg: &SdlRegiste
|
|||||||
debug!("Skipping REGISTER ACK as P2P is disallowed");
|
debug!("Skipping REGISTER ACK as P2P is disallowed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let network_id = eee.network_id.load(Ordering::Relaxed);
|
let network_id = eee.network_id.lock().unwrap().clone();
|
||||||
if network_id == 0 {
|
// let network_id = eee.network_id.load(Ordering::Relaxed);
|
||||||
|
if network_id.is_empty() {
|
||||||
error!("not authed");
|
error!("not authed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let src_mac = reg.src_mac.clone();
|
let src_mac = reg.src_mac.clone();
|
||||||
let ack = SdlRegisterAck {
|
let ack = SdlRegisterAck {
|
||||||
network_id,
|
network_id,
|
||||||
@ -1024,10 +1043,9 @@ async fn send_register_ack(eee: &Node, orig_sender: &SdlanSock, reg: &SdlRegiste
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn peer_set_p2p_confirmed(eee: &Node, src_mac: Mac, sender_sock: &SdlanSock) {
|
fn peer_set_p2p_confirmed(eee: &Node, src_mac: Mac, sender_sock: &SdlanSock) {
|
||||||
let mut scan = eee.pending_peers.get_peer(&src_mac);
|
// let mut scan = eee.pending_peers.get_peer(&src_mac);
|
||||||
if let None = scan {
|
let scan = eee.pending_peers.peers.remove(&src_mac);
|
||||||
scan = eee.pending_peers.get_peer_by_sock(sender_sock);
|
|
||||||
}
|
|
||||||
if let None = scan {
|
if let None = scan {
|
||||||
error!(
|
error!(
|
||||||
"failed to find sender in pending peer: {}",
|
"failed to find sender in pending peer: {}",
|
||||||
@ -1035,30 +1053,17 @@ fn peer_set_p2p_confirmed(eee: &Node, src_mac: Mac, sender_sock: &SdlanSock) {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let (_, scan) = scan.unwrap();
|
||||||
|
|
||||||
|
{
|
||||||
|
let entry = eee.known_peers.peers.entry(src_mac);
|
||||||
|
let mut scan2 = entry.or_insert(scan);
|
||||||
|
scan2.sock = sender_sock.deepcopy();
|
||||||
|
|
||||||
let mut scan = scan.unwrap();
|
|
||||||
eee.pending_peers.delete_peer_with_mac(&src_mac);
|
|
||||||
match eee.known_peers.get_peer(&src_mac) {
|
|
||||||
Some(scantmp) => {
|
|
||||||
eee.known_peers.delete_peer_with_mac(&src_mac);
|
|
||||||
scan = scantmp;
|
|
||||||
// set the remote peer sock
|
|
||||||
*scan.sock.write().unwrap() = sender_sock.deepcopy();
|
|
||||||
// scan.mac = src_mac;
|
|
||||||
/*
|
|
||||||
scan.dev_addr.net_addr.store(&src_mac, Ordering::Relaxed);
|
|
||||||
scan.dev_addr
|
|
||||||
.net_bit_len
|
|
||||||
.store(eee.device_config.get_net_bit(), Ordering::Relaxed);
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
*(scan.sock.write().unwrap()) = sender_sock.deepcopy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
scan.last_p2p.store(now, Ordering::Relaxed);
|
scan2.last_p2p.store(now, Ordering::Relaxed);
|
||||||
scan.last_seen.store(now, Ordering::Relaxed);
|
scan2.last_seen.store(now, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
let mac_string = mac_to_string(&src_mac);
|
let mac_string = mac_to_string(&src_mac);
|
||||||
let sock_string = sender_sock.to_string();
|
let sock_string = sender_sock.to_string();
|
||||||
@ -1066,45 +1071,47 @@ fn peer_set_p2p_confirmed(eee: &Node, src_mac: Mac, sender_sock: &SdlanSock) {
|
|||||||
"P2P connection established: {} [{}]",
|
"P2P connection established: {} [{}]",
|
||||||
&mac_string, &sock_string,
|
&mac_string, &sock_string,
|
||||||
);
|
);
|
||||||
eee.known_peers.insert_peer(src_mac, scan);
|
// eee.known_peers.insert_peer(src_mac, scan);
|
||||||
debug!("==> new peer: {} -> {}", &mac_string, &sock_string,);
|
debug!("==> new peer: {} -> {}", &mac_string, &sock_string,);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn check_query_peer_info(eee: &'static Node, mac: Mac) {
|
pub async fn check_query_peer_info(eee: &'static Node, mac: Mac) {
|
||||||
let scan: Arc<EdgePeer>;
|
// let scan: Arc<EdgePeer>;
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
match eee.pending_peers.get_peer(&mac) {
|
|
||||||
None => {
|
let last_sent_query;
|
||||||
|
let mut need_send_query = false;
|
||||||
|
{
|
||||||
|
let entry = eee.pending_peers.peers.entry(mac);
|
||||||
|
|
||||||
let sock = SdlanSock {
|
let sock = SdlanSock {
|
||||||
family: AF_INET,
|
family: AF_INET,
|
||||||
port: 0,
|
port: 0,
|
||||||
v4: [0; 4],
|
v4: [0; 4],
|
||||||
v6: [0; 16],
|
v6: [0; 16],
|
||||||
};
|
};
|
||||||
let peer = Arc::new(EdgePeer::new(
|
let peer = EdgePeer::new(
|
||||||
// mac,
|
// mac,
|
||||||
0,
|
0,
|
||||||
eee.device_config.get_net_bit(),
|
eee.device_config.get_net_bit(),
|
||||||
&sock,
|
&sock,
|
||||||
&None,
|
&None,
|
||||||
now,
|
now,
|
||||||
));
|
|
||||||
debug!("insert peer {} to pending", mac_to_string(&mac));
|
|
||||||
eee.pending_peers.insert_peer(mac, peer.clone());
|
|
||||||
scan = peer;
|
|
||||||
}
|
|
||||||
Some(s) => {
|
|
||||||
scan = s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
debug!(
|
|
||||||
"now={}, last_sent_query={}, REGISTER_INTERVAL={}, scan={:?}",
|
|
||||||
now,
|
|
||||||
scan.last_sent_query.load(Ordering::Relaxed),
|
|
||||||
REGISTER_INTERVAL,
|
|
||||||
scan,
|
|
||||||
);
|
);
|
||||||
if now - scan.last_sent_query.load(Ordering::Relaxed) > (REGISTER_INTERVAL as u64) {
|
debug!("insert peer {} to pending", mac_to_string(&mac));
|
||||||
|
let val = entry.or_insert(peer);
|
||||||
|
last_sent_query = val.last_sent_query.load(Ordering::Relaxed);
|
||||||
|
if now - last_sent_query > (REGISTER_INTERVAL as u64) {
|
||||||
|
need_send_query = true;
|
||||||
|
val.last_sent_query.store(now, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
"now={}, last_sent_query={}, REGISTER_INTERVAL={}",
|
||||||
|
now, last_sent_query, REGISTER_INTERVAL,
|
||||||
|
);
|
||||||
|
if need_send_query {
|
||||||
/*
|
/*
|
||||||
send_register(
|
send_register(
|
||||||
eee,
|
eee,
|
||||||
@ -1115,19 +1122,16 @@ pub async fn check_query_peer_info(eee: &'static Node, mac: Mac) {
|
|||||||
*/
|
*/
|
||||||
debug!("sending query for {}", mac_to_string(&mac));
|
debug!("sending query for {}", mac_to_string(&mac));
|
||||||
register_with_local_peers(eee).await;
|
register_with_local_peers(eee).await;
|
||||||
if let Ok(()) = send_query_peer(eee, mac).await {
|
let _ = send_query_peer(eee, mac).await;
|
||||||
scan.last_sent_query.store(now, Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_query_peer(eee: &Node, dst_mac: Mac) -> Result<()> {
|
async fn send_query_peer(eee: &Node, dst_mac: Mac) -> Result<()> {
|
||||||
let network_id = eee.network_id.load(Ordering::Relaxed);
|
if !eee.is_authorized() {
|
||||||
|
|
||||||
if network_id == 0 {
|
|
||||||
error!("not authed");
|
error!("not authed");
|
||||||
return Err(SDLanError::NormalError("not connected"));
|
return Err(SDLanError::NormalError("not connected"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let query = SdlQueryInfo {
|
let query = SdlQueryInfo {
|
||||||
dst_mac: Vec::from(dst_mac),
|
dst_mac: Vec::from(dst_mac),
|
||||||
};
|
};
|
||||||
|
|||||||
11
src/network/route.rs
Normal file → Executable file
11
src/network/route.rs
Normal file → Executable file
@ -3,6 +3,8 @@ use std::{net::Ipv4Addr, sync::RwLock};
|
|||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use sdlan_sn_rs::utils::net_bit_len_to_mask;
|
use sdlan_sn_rs::utils::net_bit_len_to_mask;
|
||||||
|
|
||||||
|
use tracing::{debug, error};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RouteTable {
|
pub struct RouteTable {
|
||||||
content: RwLock<Vec<RouteInfo>>,
|
content: RwLock<Vec<RouteInfo>>,
|
||||||
@ -29,7 +31,7 @@ impl RouteTable {
|
|||||||
pub fn get_gateway_ip(&self, net_ip: u32) -> Option<u32> {
|
pub fn get_gateway_ip(&self, net_ip: u32) -> Option<u32> {
|
||||||
let routes = self.content.read().unwrap();
|
let routes = self.content.read().unwrap();
|
||||||
for route in &*routes {
|
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) {
|
if (route.net_ip & route.net_mask) == (net_ip & route.net_mask) {
|
||||||
// found
|
// found
|
||||||
return Some(route.gateway_ip);
|
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();
|
let routes: Vec<_> = route.split(",").collect();
|
||||||
for route in routes {
|
for route in routes {
|
||||||
let route_info: Vec<_> = route.split(";").collect();
|
let route_info: Vec<_> = route.split(";").collect();
|
||||||
println!("got route info: {:?}", route_info);
|
debug!("got route info: {:?}", route_info);
|
||||||
if route_info.len() != 2 {
|
if route_info.len() != 2 {
|
||||||
println!("route info format error");
|
|
||||||
|
error!("route info format error");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let cidr = route_info[0];
|
let cidr = route_info[0];
|
||||||
let gateway = route_info[1].parse::<Ipv4Addr>().unwrap();
|
let gateway = route_info[1].parse::<Ipv4Addr>().unwrap();
|
||||||
let ip_and_mask: Vec<_> = cidr.split("/").collect();
|
let ip_and_mask: Vec<_> = cidr.split("/").collect();
|
||||||
if ip_and_mask.len() != 2 {
|
if ip_and_mask.len() != 2 {
|
||||||
println!("route info ip/bit error");
|
error!("route info ip/bit error");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let ip = ip_and_mask[0].parse::<Ipv4Addr>().unwrap();
|
let ip = ip_and_mask[0].parse::<Ipv4Addr>().unwrap();
|
||||||
|
|||||||
29
src/network/tun_linux.rs
Normal file → Executable file
29
src/network/tun_linux.rs
Normal file → Executable file
@ -30,6 +30,7 @@ use crate::utils::{caculate_crc, mac_to_string};
|
|||||||
use super::device::{DeviceConfig, Mode};
|
use super::device::{DeviceConfig, Mode};
|
||||||
use super::TunTapPacketHandler;
|
use super::TunTapPacketHandler;
|
||||||
|
|
||||||
|
// #[link(name = "tuntap", kind="static")]
|
||||||
#[link(name = "tuntap")]
|
#[link(name = "tuntap")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn tuntap_setup(fd: c_int, name: *mut u8, mode: c_int, packet_info: c_int) -> c_int;
|
fn tuntap_setup(fd: c_int, name: *mut u8, mode: c_int, packet_info: c_int) -> c_int;
|
||||||
@ -116,8 +117,8 @@ impl Iface {
|
|||||||
let ip = ip_to_string(&ip);
|
let ip = ip_to_string(&ip);
|
||||||
let netbit = ip_to_string(&net_bit_len_to_mask(netbit));
|
let netbit = ip_to_string(&net_bit_len_to_mask(netbit));
|
||||||
|
|
||||||
if cfg!(feature = "tap") {
|
if cfg!(not(feature = "tun")) {
|
||||||
println!("set tap device");
|
info!("set tap device");
|
||||||
let mac = device_config.get_mac();
|
let mac = device_config.get_mac();
|
||||||
let res = Command::new("ifconfig")
|
let res = Command::new("ifconfig")
|
||||||
.arg(&self.name)
|
.arg(&self.name)
|
||||||
@ -130,6 +131,8 @@ impl Iface {
|
|||||||
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
|
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
|
||||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
|
||||||
))
|
))
|
||||||
|
.arg("mtu")
|
||||||
|
.arg(format!("{}", device_config.mtu))
|
||||||
.arg("up")
|
.arg("up")
|
||||||
.output();
|
.output();
|
||||||
match res {
|
match res {
|
||||||
@ -141,12 +144,14 @@ impl Iface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!("set tun device");
|
info!("set tun device");
|
||||||
let res = Command::new("ifconfig")
|
let res = Command::new("ifconfig")
|
||||||
.arg(&self.name)
|
.arg(&self.name)
|
||||||
.arg(ip)
|
.arg(ip)
|
||||||
.arg("netmask")
|
.arg("netmask")
|
||||||
.arg(&netbit)
|
.arg(&netbit)
|
||||||
|
.arg("mtu")
|
||||||
|
.arg(format!("{}", device_config.mtu))
|
||||||
.arg("up")
|
.arg("up")
|
||||||
.output();
|
.output();
|
||||||
match res {
|
match res {
|
||||||
@ -169,7 +174,7 @@ impl Iface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "tap")]
|
#[cfg(not(feature = "tun"))]
|
||||||
impl TunTapPacketHandler for Iface {
|
impl TunTapPacketHandler for Iface {
|
||||||
async fn handle_packet_from_net(&self, data: &[u8], _: &[u8]) -> std::io::Result<()> {
|
async fn handle_packet_from_net(&self, data: &[u8], _: &[u8]) -> std::io::Result<()> {
|
||||||
// debug!("in tap mode, got data: {:?}", data);
|
// debug!("in tap mode, got data: {:?}", data);
|
||||||
@ -201,9 +206,10 @@ impl TunTapPacketHandler for Iface {
|
|||||||
error!("failed to encrypt packet request");
|
error!("failed to encrypt packet request");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
let network_id = edge.network_id.lock().unwrap().clone();
|
||||||
let data = SdlData {
|
let data = SdlData {
|
||||||
is_p2p: true,
|
is_p2p: true,
|
||||||
network_id: edge.network_id.load(Ordering::Relaxed),
|
network_id,
|
||||||
ttl: SDLAN_DEFAULT_TTL as u32,
|
ttl: SDLAN_DEFAULT_TTL as u32,
|
||||||
src_mac: Vec::from(edge.device_config.get_mac()),
|
src_mac: Vec::from(edge.device_config.get_mac()),
|
||||||
dst_mac: Vec::from(target),
|
dst_mac: Vec::from(target),
|
||||||
@ -221,7 +227,7 @@ impl TunTapPacketHandler for Iface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "tap"))]
|
#[cfg(feature = "tun")]
|
||||||
impl TunTapPacketHandler for Iface {
|
impl TunTapPacketHandler for Iface {
|
||||||
async fn handle_packet_from_net(&self, data: &[u8], key: &[u8]) -> std::io::Result<()> {
|
async fn handle_packet_from_net(&self, data: &[u8], key: &[u8]) -> std::io::Result<()> {
|
||||||
debug!("in tun mode");
|
debug!("in tun mode");
|
||||||
@ -267,7 +273,7 @@ impl TunTapPacketHandler for Iface {
|
|||||||
// handle ARP REQUEST
|
// handle ARP REQUEST
|
||||||
debug!("got ARP REQUEST");
|
debug!("got ARP REQUEST");
|
||||||
if arp.ethhdr.dest != [0xff; 6] {
|
if arp.ethhdr.dest != [0xff; 6] {
|
||||||
println!("ARP REQUEST not broadcast");
|
debug!("ARP REQUEST not broadcast");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if dest_ip == self_ip {
|
if dest_ip == self_ip {
|
||||||
@ -306,7 +312,7 @@ impl TunTapPacketHandler for Iface {
|
|||||||
|
|
||||||
let v = encode_to_udp_message(Some(data), PacketType::Data as u8)
|
let v = encode_to_udp_message(Some(data), PacketType::Data as u8)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
println!(
|
debug!(
|
||||||
"xxxx send arp reply to [{}], selfmac=[{}]",
|
"xxxx send arp reply to [{}], selfmac=[{}]",
|
||||||
mac_to_string(&arp.dhwaddr),
|
mac_to_string(&arp.dhwaddr),
|
||||||
mac_to_string(&self_mac)
|
mac_to_string(&self_mac)
|
||||||
@ -317,9 +323,8 @@ impl TunTapPacketHandler for Iface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ARP_REPLY => {
|
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 {
|
if dest_ip == self_ip {
|
||||||
send_arp_request(ArpRequestInfo::Set {
|
send_arp_request(ArpRequestInfo::Set {
|
||||||
ip: from_ip,
|
ip: from_ip,
|
||||||
@ -330,7 +335,7 @@ impl TunTapPacketHandler for Iface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_other => {
|
_other => {
|
||||||
println!("unknown arp type info");
|
error!("unknown arp type info");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -411,7 +416,7 @@ impl TunTapPacketHandler for Iface {
|
|||||||
} => {
|
} => {
|
||||||
if do_arp_request {
|
if do_arp_request {
|
||||||
add_to_arp_wait_list(dstip, data);
|
add_to_arp_wait_list(dstip, data);
|
||||||
println!(
|
debug!(
|
||||||
"find ip: {:?} => {:?}",
|
"find ip: {:?} => {:?}",
|
||||||
src.to_be_bytes(),
|
src.to_be_bytes(),
|
||||||
dstip.to_be_bytes()
|
dstip.to_be_bytes()
|
||||||
|
|||||||
39
src/network/tun_win.rs
Normal file → Executable file
39
src/network/tun_win.rs
Normal file → Executable file
@ -91,6 +91,28 @@ impl Iface {
|
|||||||
error!("failed to run netsh: {}", e.to_string());
|
error!("failed to run netsh: {}", e.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut cmd = Command::new("netsh");
|
||||||
|
let command = cmd
|
||||||
|
.creation_flags(0x08000000)
|
||||||
|
.arg("interface")
|
||||||
|
.arg("ipv4")
|
||||||
|
.arg("set")
|
||||||
|
.arg("subinterface")
|
||||||
|
.arg(&format!("\"{}\"", self.name))
|
||||||
|
.arg(format!("mtu={}", device_config.mtu))
|
||||||
|
.arg("store=persistent");
|
||||||
|
|
||||||
|
let res = command.output();
|
||||||
|
|
||||||
|
match res {
|
||||||
|
Ok(r) => {
|
||||||
|
debug!("netsh2 ok: [{:?}]", String::from_utf8_lossy(&r.stdout));
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("failed to run netsh2: {}", e.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,18 +152,18 @@ impl TunTapPacketHandler for Iface {
|
|||||||
let mut arp = ArpHdr::from_slice(&data);
|
let mut arp = ArpHdr::from_slice(&data);
|
||||||
let self_ip = edge.device_config.get_ip();
|
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;
|
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;
|
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 {
|
match arp.opcode {
|
||||||
ARP_REQUEST => {
|
ARP_REQUEST => {
|
||||||
// handle ARP REQUEST
|
// handle ARP REQUEST
|
||||||
debug!("got ARP REQUEST");
|
debug!("got ARP REQUEST");
|
||||||
if arp.ethhdr.dest != [0xff; 6] {
|
if arp.ethhdr.dest != [0xff; 6] {
|
||||||
println!("ARP REQUEST not broadcast");
|
debug!("ARP REQUEST not broadcast");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if dest_ip == self_ip {
|
if dest_ip == self_ip {
|
||||||
@ -179,7 +201,7 @@ impl TunTapPacketHandler for Iface {
|
|||||||
|
|
||||||
let v = encode_to_udp_message(Some(data), PacketType::Data as u8)
|
let v = encode_to_udp_message(Some(data), PacketType::Data as u8)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
println!(
|
debug!(
|
||||||
"xxxx send arp reply to [{}], selfmac=[{}]",
|
"xxxx send arp reply to [{}], selfmac=[{}]",
|
||||||
mac_to_string(&arp.dhwaddr),
|
mac_to_string(&arp.dhwaddr),
|
||||||
mac_to_string(&self_mac)
|
mac_to_string(&self_mac)
|
||||||
@ -190,9 +212,8 @@ impl TunTapPacketHandler for Iface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ARP_REPLY => {
|
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 {
|
if dest_ip == self_ip {
|
||||||
send_arp_request(ArpRequestInfo::Set {
|
send_arp_request(ArpRequestInfo::Set {
|
||||||
ip: from_ip,
|
ip: from_ip,
|
||||||
@ -203,7 +224,7 @@ impl TunTapPacketHandler for Iface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_other => {
|
_other => {
|
||||||
println!("unknown arp type info");
|
error!("unknown arp type info");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -283,7 +304,7 @@ impl TunTapPacketHandler for Iface {
|
|||||||
if do_arp_request {
|
if do_arp_request {
|
||||||
add_to_arp_wait_list(dstip, data);
|
add_to_arp_wait_list(dstip, data);
|
||||||
|
|
||||||
println!(
|
info!(
|
||||||
"find ip: {:?} => {:?}",
|
"find ip: {:?} => {:?}",
|
||||||
src.to_be_bytes(),
|
src.to_be_bytes(),
|
||||||
dstip.to_be_bytes()
|
dstip.to_be_bytes()
|
||||||
|
|||||||
0
src/network/tuntap.c
Normal file → Executable file
0
src/network/tuntap.c
Normal file → Executable file
8
src/network/tuntap.rs
Normal file → Executable file
8
src/network/tuntap.rs
Normal file → Executable file
@ -8,6 +8,8 @@ use sdlan_sn_rs::{
|
|||||||
utils::{aes_encrypt, get_current_timestamp, ip_to_string, Mac},
|
utils::{aes_encrypt, get_current_timestamp, ip_to_string, Mac},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use tracing::debug;
|
||||||
|
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -59,7 +61,7 @@ impl ArpWaitList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn arp_arrived(&self, ip: u32, mac: Mac) {
|
async fn arp_arrived(&self, ip: u32, mac: Mac) {
|
||||||
println!(
|
debug!(
|
||||||
"arp for {} arrived: {}",
|
"arp for {} arrived: {}",
|
||||||
ip_to_string(&ip),
|
ip_to_string(&ip),
|
||||||
mac_to_string(&mac)
|
mac_to_string(&mac)
|
||||||
@ -74,7 +76,7 @@ impl ArpWaitList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let encrypt_key = edge.get_encrypt_key();
|
let encrypt_key = edge.get_encrypt_key();
|
||||||
let network_id = edge.network_id.load(Ordering::Relaxed);
|
let network_id = edge.network_id.lock().unwrap().clone();
|
||||||
|
|
||||||
let src_mac = edge.device_config.get_mac();
|
let src_mac = edge.device_config.get_mac();
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
@ -92,7 +94,7 @@ impl ArpWaitList {
|
|||||||
};
|
};
|
||||||
let data = SdlData {
|
let data = SdlData {
|
||||||
is_p2p: true,
|
is_p2p: true,
|
||||||
network_id,
|
network_id: network_id.clone(),
|
||||||
ttl: SDLAN_DEFAULT_TTL as u32,
|
ttl: SDLAN_DEFAULT_TTL as u32,
|
||||||
src_mac: Vec::from(src_mac),
|
src_mac: Vec::from(src_mac),
|
||||||
dst_mac: Vec::from(mac),
|
dst_mac: Vec::from(mac),
|
||||||
|
|||||||
@ -21,8 +21,8 @@ pub struct Sdlv6Info {
|
|||||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct SdlDevAddr {
|
pub struct SdlDevAddr {
|
||||||
#[prost(uint32, tag = "1")]
|
#[prost(string, tag = "1")]
|
||||||
pub network_id: u32,
|
pub network_id: ::prost::alloc::string::String,
|
||||||
#[prost(bytes = "vec", tag = "2")]
|
#[prost(bytes = "vec", tag = "2")]
|
||||||
pub mac: ::prost::alloc::vec::Vec<u8>,
|
pub mac: ::prost::alloc::vec::Vec<u8>,
|
||||||
#[prost(uint32, tag = "3")]
|
#[prost(uint32, tag = "3")]
|
||||||
@ -49,6 +49,8 @@ pub struct SdlRegisterSuper {
|
|||||||
pub pub_key: ::prost::alloc::string::String,
|
pub pub_key: ::prost::alloc::string::String,
|
||||||
#[prost(string, tag = "6")]
|
#[prost(string, tag = "6")]
|
||||||
pub token: ::prost::alloc::string::String,
|
pub token: ::prost::alloc::string::String,
|
||||||
|
#[prost(string, tag = "7")]
|
||||||
|
pub network_id: ::prost::alloc::string::String,
|
||||||
}
|
}
|
||||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
@ -153,8 +155,8 @@ pub struct SdlStunRequest {
|
|||||||
pub cookie: u32,
|
pub cookie: u32,
|
||||||
#[prost(string, tag = "2")]
|
#[prost(string, tag = "2")]
|
||||||
pub client_id: ::prost::alloc::string::String,
|
pub client_id: ::prost::alloc::string::String,
|
||||||
#[prost(uint32, tag = "3")]
|
#[prost(string, tag = "3")]
|
||||||
pub network_id: u32,
|
pub network_id: ::prost::alloc::string::String,
|
||||||
#[prost(bytes = "vec", tag = "4")]
|
#[prost(bytes = "vec", tag = "4")]
|
||||||
pub mac: ::prost::alloc::vec::Vec<u8>,
|
pub mac: ::prost::alloc::vec::Vec<u8>,
|
||||||
#[prost(uint32, tag = "5")]
|
#[prost(uint32, tag = "5")]
|
||||||
@ -173,8 +175,8 @@ pub struct SdlStunReply {
|
|||||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct SdlData {
|
pub struct SdlData {
|
||||||
#[prost(uint32, tag = "1")]
|
#[prost(string, tag = "1")]
|
||||||
pub network_id: u32,
|
pub network_id: ::prost::alloc::string::String,
|
||||||
#[prost(bytes = "vec", tag = "2")]
|
#[prost(bytes = "vec", tag = "2")]
|
||||||
pub src_mac: ::prost::alloc::vec::Vec<u8>,
|
pub src_mac: ::prost::alloc::vec::Vec<u8>,
|
||||||
#[prost(bytes = "vec", tag = "3")]
|
#[prost(bytes = "vec", tag = "3")]
|
||||||
@ -189,8 +191,8 @@ pub struct SdlData {
|
|||||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct SdlRegister {
|
pub struct SdlRegister {
|
||||||
#[prost(uint32, tag = "1")]
|
#[prost(string, tag = "1")]
|
||||||
pub network_id: u32,
|
pub network_id: ::prost::alloc::string::String,
|
||||||
#[prost(bytes = "vec", tag = "2")]
|
#[prost(bytes = "vec", tag = "2")]
|
||||||
pub src_mac: ::prost::alloc::vec::Vec<u8>,
|
pub src_mac: ::prost::alloc::vec::Vec<u8>,
|
||||||
#[prost(bytes = "vec", tag = "3")]
|
#[prost(bytes = "vec", tag = "3")]
|
||||||
@ -199,8 +201,8 @@ pub struct SdlRegister {
|
|||||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct SdlRegisterAck {
|
pub struct SdlRegisterAck {
|
||||||
#[prost(uint32, tag = "1")]
|
#[prost(string, tag = "1")]
|
||||||
pub network_id: u32,
|
pub network_id: ::prost::alloc::string::String,
|
||||||
#[prost(bytes = "vec", tag = "2")]
|
#[prost(bytes = "vec", tag = "2")]
|
||||||
pub src_mac: ::prost::alloc::vec::Vec<u8>,
|
pub src_mac: ::prost::alloc::vec::Vec<u8>,
|
||||||
#[prost(bytes = "vec", tag = "3")]
|
#[prost(bytes = "vec", tag = "3")]
|
||||||
|
|||||||
0
src/pb/mod.rs
Normal file → Executable file
0
src/pb/mod.rs
Normal file → Executable file
0
src/tcp/mod.rs
Normal file → Executable file
0
src/tcp/mod.rs
Normal file → Executable file
0
src/tcp/tcp_codec.rs
Normal file → Executable file
0
src/tcp/tcp_codec.rs
Normal file → Executable file
3
src/tcp/tcp_conn.rs
Normal file → Executable file
3
src/tcp/tcp_conn.rs
Normal file → Executable file
@ -297,7 +297,8 @@ impl ReadWriterHandle {
|
|||||||
if let Some(msg) = data_from_tcp.recv().await {
|
if let Some(msg) = data_from_tcp.recv().await {
|
||||||
on_message(msg).await;
|
on_message(msg).await;
|
||||||
} else {
|
} else {
|
||||||
eprintln!("data from tcp exited");
|
error!("data from tcp exited");
|
||||||
|
// eprintln!("data from tcp exited");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/utils/command.rs
Normal file → Executable file
11
src/utils/command.rs
Normal file → Executable file
@ -2,11 +2,14 @@ use structopt::StructOpt;
|
|||||||
|
|
||||||
#[derive(StructOpt, Debug)]
|
#[derive(StructOpt, Debug)]
|
||||||
pub struct CommandLineInput {
|
pub struct CommandLineInput {
|
||||||
#[structopt(long = "token", default_value = "")]
|
#[structopt(long = "token", default_value = "", help="specify a token")]
|
||||||
pub token: String,
|
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,
|
pub port: u16,
|
||||||
|
|
||||||
|
#[structopt(long = "network_id", short = "n", default_value = "", help="specify a networkid")]
|
||||||
|
pub user_network_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(StructOpt, Debug)]
|
#[derive(StructOpt, Debug)]
|
||||||
@ -50,6 +53,9 @@ pub struct CommandLine {
|
|||||||
#[structopt(long = "token", default_value = "0")]
|
#[structopt(long = "token", default_value = "0")]
|
||||||
pub token: String,
|
pub token: String,
|
||||||
|
|
||||||
|
#[structopt(long = "network_id", default_value = "")]
|
||||||
|
pub user_network_id: String,
|
||||||
|
|
||||||
#[structopt(short = "p")]
|
#[structopt(short = "p")]
|
||||||
pub allow_p2p: bool,
|
pub allow_p2p: bool,
|
||||||
|
|
||||||
@ -70,6 +76,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(),
|
||||||
|
user_network_id: self.user_network_id.clone(),
|
||||||
allow_p2p: self.allow_p2p,
|
allow_p2p: self.allow_p2p,
|
||||||
nat_server1: self.nat_server1.clone(),
|
nat_server1: self.nat_server1.clone(),
|
||||||
nat_server2: self.nat_server2.clone(),
|
nat_server2: self.nat_server2.clone(),
|
||||||
|
|||||||
0
src/utils/mod.rs
Normal file → Executable file
0
src/utils/mod.rs
Normal file → Executable file
7
src/utils/pid_recorder.rs
Normal file → Executable file
7
src/utils/pid_recorder.rs
Normal file → Executable file
@ -2,6 +2,7 @@ use std::{
|
|||||||
fs::{self, OpenOptions},
|
fs::{self, OpenOptions},
|
||||||
io::Write,
|
io::Write,
|
||||||
};
|
};
|
||||||
|
use tracing::{debug, error};
|
||||||
|
|
||||||
pub struct PidRecorder(String);
|
pub struct PidRecorder(String);
|
||||||
|
|
||||||
@ -19,7 +20,8 @@ impl PidRecorder {
|
|||||||
.expect("failed to write");
|
.expect("failed to write");
|
||||||
}
|
}
|
||||||
Err(e) => {
|
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 {
|
impl Drop for PidRecorder {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Err(e) = fs::remove_file(&self.0) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
0
src/utils/socks.rs
Normal file → Executable file
0
src/utils/socks.rs
Normal file → Executable file
0
start_docker.sh
Normal file → Executable file
0
start_docker.sh
Normal file → Executable file
0
wintun.dll
Normal file → Executable file
0
wintun.dll
Normal file → Executable file
Loading…
x
Reference in New Issue
Block a user