added start_with(out)_feedback's hostname
This commit is contained in:
parent
43c2b01fca
commit
89f7db657d
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
// "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu"
|
"rust-analyzer.cargo.target": "x86_64-pc-windows-gnu"
|
||||||
// "rust-analyzer.cargo.features": ["tun"]
|
// "rust-analyzer.cargo.features": ["tun"]
|
||||||
}
|
}
|
||||||
14
Cargo.lock
generated
14
Cargo.lock
generated
@ -637,7 +637,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1324,9 +1324,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.177"
|
version = "0.2.178"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
|
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libloading"
|
name = "libloading"
|
||||||
@ -1895,6 +1895,7 @@ dependencies = [
|
|||||||
"dns-lookup",
|
"dns-lookup",
|
||||||
"etherparse",
|
"etherparse",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
|
"libc",
|
||||||
"local-ip-address",
|
"local-ip-address",
|
||||||
"num_enum",
|
"num_enum",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
@ -1910,6 +1911,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"tracing",
|
"tracing",
|
||||||
|
"winapi",
|
||||||
"wintun",
|
"wintun",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -2119,7 +2121,7 @@ dependencies = [
|
|||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys",
|
"linux-raw-sys",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -2185,7 +2187,7 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "sdlan-sn-rs"
|
name = "sdlan-sn-rs"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://gitea.s5s8.com/punchnet/sdlan-rs.git#b58b18d9c31dea900f82a5a8d5d8d29a5b1aa47b"
|
source = "git+https://gitea.s5s8.com/punchnet/sdlan-rs.git#8e93a4224be025b0a6557e2bfd885aeb467b37ab"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes",
|
"aes",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
@ -2750,7 +2752,7 @@ dependencies = [
|
|||||||
"getrandom 0.3.4",
|
"getrandom 0.3.4",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rustix",
|
"rustix",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@ -30,8 +30,12 @@ tokio-util = "0.7.11"
|
|||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
# rolling-file = { path = "../rolling-file" }
|
# rolling-file = { path = "../rolling-file" }
|
||||||
|
|
||||||
|
[target.'cfg(unix)'.dependencies]
|
||||||
|
libc = "0.2.178"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
wintun = "0.4.0"
|
wintun = "0.4.0"
|
||||||
|
winapi = "0.3.9"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
tun = []
|
tun = []
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
use punchnet::get_base_dir;
|
use punchnet::get_base_dir;
|
||||||
use punchnet::get_edge;
|
use punchnet::get_edge;
|
||||||
use punchnet::get_my_networks;
|
use punchnet::get_my_networks;
|
||||||
|
use punchnet::mod_hostname;
|
||||||
use punchnet::restore_dns;
|
use punchnet::restore_dns;
|
||||||
use punchnet::run_sdlan;
|
use punchnet::run_sdlan;
|
||||||
use punchnet::set_base_dir;
|
use punchnet::set_base_dir;
|
||||||
@ -43,6 +44,22 @@ async fn main() {
|
|||||||
|
|
||||||
println!("server is {}", server);
|
println!("server is {}", server);
|
||||||
|
|
||||||
|
mod_hostname::get_hostname();
|
||||||
|
/*
|
||||||
|
let hostname = if cmd.hostname.len() == 0 {
|
||||||
|
mod_hostname::get_hostname()
|
||||||
|
} else {
|
||||||
|
Some(cmd.hostname)
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
let hostname = if cmd.hostname.len() == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(cmd.hostname)
|
||||||
|
};
|
||||||
|
// let hostname = mod_hostname::get_hostname();
|
||||||
|
println!("hostname = {:?}", hostname);
|
||||||
|
|
||||||
let _ = run_sdlan(
|
let _ = run_sdlan(
|
||||||
CommandLine {
|
CommandLine {
|
||||||
sn: server.clone()+":1265",
|
sn: server.clone()+":1265",
|
||||||
@ -63,7 +80,7 @@ async fn main() {
|
|||||||
tx,
|
tx,
|
||||||
&punchnet::get_install_channel(),
|
&punchnet::get_install_channel(),
|
||||||
server,
|
server,
|
||||||
Some(format!("{:08x}", gen_uuid_u64() as u32)),
|
hostname,
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
@ -95,7 +112,7 @@ async fn main() {
|
|||||||
// exit(0);
|
// exit(0);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
if let Err(e) = edge.start_without_feedback(cmd.token, cmd.network_code).await {
|
if let Err(e) = edge.start_without_feedback(cmd.token, cmd.network_code, None).await {
|
||||||
error!("failed to start: {:?}", e);
|
error!("failed to start: {:?}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
src/lib.rs
13
src/lib.rs
@ -12,12 +12,13 @@ use std::net::{SocketAddr, ToSocketAddrs};
|
|||||||
pub use network::get_edge;
|
pub use network::get_edge;
|
||||||
pub use network::get_install_channel;
|
pub use network::get_install_channel;
|
||||||
pub use network::{async_main, init_arp, init_edge, NodeConfig, restore_dns};
|
pub use network::{async_main, init_arp, init_edge, NodeConfig, restore_dns};
|
||||||
|
use sdlan_sn_rs::utils::save_to_file;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::net::UdpSocket;
|
use tokio::net::UdpSocket;
|
||||||
use tokio::sync::mpsc::{channel, Sender};
|
use tokio::sync::mpsc::{channel, Sender};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
pub use utils::{CommandLine, CommandLineInput};
|
pub use utils::{CommandLine, CommandLineInput, mod_hostname};
|
||||||
|
|
||||||
pub use config::{get_base_dir, set_base_dir};
|
pub use config::{get_base_dir, set_base_dir};
|
||||||
|
|
||||||
@ -53,12 +54,16 @@ pub async fn run_sdlan(
|
|||||||
// start_stop_receiver: Receiver<String>,
|
// start_stop_receiver: Receiver<String>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let (start_stop_sender, start_stop_chan) = channel(20);
|
let (start_stop_sender, start_stop_chan) = channel(20);
|
||||||
let edge_uuid = create_or_load_uuid(&format!("{}/.id", get_base_dir()))?;
|
let edge_uuid = create_or_load_uuid(&format!("{}/.id", get_base_dir()), None)?;
|
||||||
let node_conf = parse_config(edge_uuid, &args).await?;
|
let node_conf = parse_config(edge_uuid, &args).await?;
|
||||||
|
|
||||||
|
let hostfile = format!("{}/.host", get_base_dir());
|
||||||
|
let host = create_or_load_uuid(&hostfile, Some(8))?;
|
||||||
|
|
||||||
init_arp();
|
init_arp();
|
||||||
|
|
||||||
let hostname = hostname.unwrap_or("".to_owned());
|
let hostname = hostname.unwrap_or(host);
|
||||||
|
let _ = save_to_file(&hostfile, &hostname);
|
||||||
|
|
||||||
let sock = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
|
let sock = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
|
||||||
|
|
||||||
@ -217,7 +222,7 @@ pub struct NetworkResp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_my_networks(url: &str) -> Result<Vec<NetworkRespSingleNetwork>>{
|
pub async fn get_my_networks(url: &str) -> Result<Vec<NetworkRespSingleNetwork>>{
|
||||||
let edge_uuid = create_or_load_uuid(&format!("{}/.id", get_base_dir()))?;
|
let edge_uuid = create_or_load_uuid(&format!("{}/.id", get_base_dir()), None)?;
|
||||||
|
|
||||||
let req_data = NetworkReq {
|
let req_data = NetworkReq {
|
||||||
client_id: &edge_uuid,
|
client_id: &edge_uuid,
|
||||||
|
|||||||
@ -64,7 +64,8 @@ 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);
|
||||||
println!("network is: {}.{}", edge.hostname, dev.network_domain);
|
let hostname = edge.hostname.read().unwrap().clone();
|
||||||
|
println!("network is: {}.{}", hostname, dev.network_domain);
|
||||||
edge.device_config
|
edge.device_config
|
||||||
.ip
|
.ip
|
||||||
.net_addr
|
.net_addr
|
||||||
@ -324,7 +325,7 @@ pub async fn async_main(
|
|||||||
pub_key: edge.rsa_pubkey.clone(),
|
pub_key: edge.rsa_pubkey.clone(),
|
||||||
token,
|
token,
|
||||||
network_code: code,
|
network_code: code,
|
||||||
hostname: edge.hostname.clone(),
|
hostname: edge.hostname.read().unwrap().clone(),
|
||||||
};
|
};
|
||||||
// debug!("send register super: {:?}", register_super);
|
// debug!("send register super: {:?}", register_super);
|
||||||
let packet_id = match pkt_id {
|
let packet_id = match pkt_id {
|
||||||
|
|||||||
@ -26,7 +26,7 @@ use super::device::{DeviceConfig, Mode};
|
|||||||
use super::tun::{new_iface, Iface};
|
use super::tun::{new_iface, Iface};
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
|
|
||||||
use sdlan_sn_rs::utils::{gen_rsa_keys, load_private_key_file, Mac};
|
use sdlan_sn_rs::utils::{Mac, gen_rsa_keys, load_private_key_file, save_to_file};
|
||||||
use sdlan_sn_rs::utils::{Result, SDLanError};
|
use sdlan_sn_rs::utils::{Result, SDLanError};
|
||||||
|
|
||||||
static EDGE: OnceCell<Node> = OnceCell::new();
|
static EDGE: OnceCell<Node> = OnceCell::new();
|
||||||
@ -126,7 +126,7 @@ pub struct Node {
|
|||||||
|
|
||||||
pub network_id: AtomicU32,
|
pub network_id: AtomicU32,
|
||||||
|
|
||||||
pub hostname: String,
|
pub hostname: RwLock<String>,
|
||||||
|
|
||||||
pub udp_sock_for_dns: Arc<UdpSocket>,
|
pub udp_sock_for_dns: Arc<UdpSocket>,
|
||||||
pub server_ip: String,
|
pub server_ip: String,
|
||||||
@ -201,7 +201,12 @@ impl Node {
|
|||||||
self.nat_type.lock().unwrap().clone()
|
self.nat_type.lock().unwrap().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn start_without_feedback(&self, token: String, network_code: String) -> Result<()> {
|
pub async fn start_without_feedback(&self, token: String, network_code: String, hostname: Option<String>) -> Result<()> {
|
||||||
|
if let Some(host) = hostname {
|
||||||
|
let idfile = format!("{}/.host", get_base_dir());
|
||||||
|
let _ = save_to_file(&idfile, &host);
|
||||||
|
*self.hostname.write().unwrap() = host;
|
||||||
|
}
|
||||||
*self._token.lock().unwrap() = token;
|
*self._token.lock().unwrap() = token;
|
||||||
*self.network_code.lock().unwrap() = network_code;
|
*self.network_code.lock().unwrap() = network_code;
|
||||||
let _ = self
|
let _ = self
|
||||||
@ -218,8 +223,14 @@ impl Node {
|
|||||||
&self,
|
&self,
|
||||||
token: String,
|
token: String,
|
||||||
network_code: String,
|
network_code: String,
|
||||||
|
hostname: Option<String>,
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
) -> Result<RegisterSuperFeedback> {
|
) -> Result<RegisterSuperFeedback> {
|
||||||
|
if let Some(host) = hostname {
|
||||||
|
let idfile = format!("{}/.host", get_base_dir());
|
||||||
|
let _ = save_to_file(&idfile, &host);
|
||||||
|
*self.hostname.write().unwrap() = host;
|
||||||
|
}
|
||||||
*self._token.lock().unwrap() = token;
|
*self._token.lock().unwrap() = token;
|
||||||
*self.network_code.lock().unwrap() = network_code;
|
*self.network_code.lock().unwrap() = network_code;
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
@ -286,7 +297,7 @@ impl Node {
|
|||||||
Self {
|
Self {
|
||||||
packet_id: AtomicU32::new(1),
|
packet_id: AtomicU32::new(1),
|
||||||
network_id: AtomicU32::new(0),
|
network_id: AtomicU32::new(0),
|
||||||
hostname,
|
hostname: RwLock::new(hostname),
|
||||||
|
|
||||||
udp_sock_for_dns: udpsock_for_dns,
|
udp_sock_for_dns: udpsock_for_dns,
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,9 @@ pub struct CommandLineInput {
|
|||||||
|
|
||||||
#[structopt(long = "code", default_value = "", help="specify a network code")]
|
#[structopt(long = "code", default_value = "", help="specify a network code")]
|
||||||
pub network_code: String,
|
pub network_code: String,
|
||||||
|
|
||||||
|
#[structopt(short= "h", long = "hostname", default_value="", help="specify the hostname")]
|
||||||
|
pub hostname: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(StructOpt, Debug)]
|
#[derive(StructOpt, Debug)]
|
||||||
|
|||||||
@ -36,3 +36,50 @@ pub fn generate_mac_address() -> Mac {
|
|||||||
|
|
||||||
mac
|
mac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub mod mod_hostname {
|
||||||
|
use std::{ffi::OsString, os::windows::ffi::OsStringExt};
|
||||||
|
|
||||||
|
use winapi::um::winbase::GetComputerNameW;
|
||||||
|
|
||||||
|
pub fn get_hostname() -> Option<String> {
|
||||||
|
unsafe {
|
||||||
|
let mut buffer: Vec<u16> = vec![0; 64];
|
||||||
|
let mut buffer_size = 64;
|
||||||
|
if GetComputerNameW(buffer.as_mut_ptr(), &mut buffer_size) != 0 {
|
||||||
|
let hostname = OsString::from_wide(&buffer[..buffer_size as usize]);
|
||||||
|
return hostname.into_string().ok();
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub mod mod_hostname {
|
||||||
|
use libc::{size_t, c_int, c_char};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
fn gethostname(name: *mut c_char, size: size_t) -> c_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_hostname() -> Option<String> {
|
||||||
|
let mut buffer = vec![0u8; 255];
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
if gethostname(buffer.as_mut_ptr() as *mut c_char, 255) == 0 {
|
||||||
|
let len = buffer.iter().position(|&b| b == 0).unwrap_or(255);
|
||||||
|
buffer.truncate(len);
|
||||||
|
return String::from_utf8(buffer).ok();
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub mod hostname {
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user