Commit 1d451f3e authored by nanamicat's avatar nanamicat

ddns

parent 8d2e070a
Pipeline #42098 passed with stages
in 2 minutes and 42 seconds
This diff is collapsed.
......@@ -12,6 +12,6 @@ serde_json = "1.0.145"
serde = { version = "1.0.228", features = ["derive"] }
serde_derive = "1.0"
tokio = { version = "1.48", features = ["full"] }
rtnetlink = "0.19.0"
anyhow = "1.0.100"
bincode = { version = "2.0.1", features = ["derive"] }
hickory-resolver = "0.26.0-alpha.1"
......@@ -19,6 +19,7 @@ mod route_writer;
mod router;
mod server;
mod settings;
use hickory_resolver::Resolver;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
......@@ -45,13 +46,15 @@ async fn main() -> anyhow::Result<()> {
let mut syn: bool = true;
let resolver = Resolver::builder_tokio()?.build();
loop {
tokio::select! {
biased; // 优先处理上面的
result = socket.recv_from(&mut buf) => {
let (len, src) = result?;
if src == config.server {
if src.port() == config.server.port {
// from server
let (message, _): (Change, usize) = bincode::decode_from_slice(&buf[..len], bincode::config::standard())?;
server.on_message(&message);
......@@ -62,7 +65,7 @@ async fn main() -> anyhow::Result<()> {
peers: Vec::new()
};
let message = bincode::encode_to_vec(&report, bincode::config::standard())?;
let _ = socket.send_to(message.as_slice(), config.server).await;
let _ = socket.send_to(message.as_slice(), config.server.to_socket_addrs(&resolver).await?).await;
syn = false;
} else if let Some(peer) = Router::get(&mut routers, src){
// from client
......@@ -92,7 +95,7 @@ async fn main() -> anyhow::Result<()> {
.collect(),
};
let len = bincode::encode_into_slice(&report, &mut buf, bincode::config::standard())?;
let _ = socket.send_to(&buf[..len], config.server).await;
let _ = socket.send_to(&buf[..len], config.server.to_socket_addrs(&resolver).await?).await;
}
}
}
......
use hickory_resolver::name_server::GenericConnector;
use hickory_resolver::proto::runtime::TokioRuntimeProvider;
use hickory_resolver::Resolver;
use serde::Deserialize;
use std::net::SocketAddr;
use std::time::Duration;
......@@ -5,7 +8,7 @@ use std::time::Duration;
#[derive(Deserialize)]
pub struct Settings {
pub id: u8,
pub server: SocketAddr,
pub server: Endpoint,
pub bind: SocketAddr,
// pub TIMEOUT: u32,
// pub history: usize,
......@@ -14,15 +17,37 @@ pub struct Settings {
// pub proto: u16,
}
#[derive(Deserialize)]
#[serde(try_from = "String")]
pub struct Endpoint {
pub host: String,
pub port: u16,
}
impl TryFrom<String> for Endpoint {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
let parts: Vec<&str> = value.rsplitn(2, ':').collect();
if parts.len() != 2 {
return Err(format!("Invalid endpoint format: {}", value));
}
let port = parts[0].parse::<u16>().map_err(|e| format!("Invalid port: {}", e))?;
let host = parts[1].to_string();
Ok(Endpoint { host, port })
}
}
impl Endpoint {
pub async fn to_socket_addrs(&self, resolver: &Resolver<GenericConnector<TokioRuntimeProvider>>) -> anyhow::Result<SocketAddr> {
let lookup = resolver.lookup_ip(&self.host).await?;
let ip = lookup.into_iter().next().ok_or_else(|| anyhow::anyhow!("No IP address found for host"))?;
Ok(SocketAddr::new(ip, self.port))
}
}
pub const INTERVAL: Duration = Duration::from_secs(1);
pub const TIMEOUT: Duration = Duration::from_secs(60);
pub const HISTORY: u8 = 100;
// pub static ref CONFIG: Settings = Config::builder()
// .add_source(config::File::with_name("config/config.json"))
// .add_source(config::Environment::with_prefix("RAILGUN"))
// .build()
// .unwrap()
// .try_deserialize()
// .unwrap();
//
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment