From ab0934e976b91d4b6d2847a2cd24f94d93c0c375 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Fri, 18 Oct 2024 22:11:36 +0400 Subject: [PATCH] feat(config): use IpAddr instead of String, set defaults --- src/config.rs | 38 +++++++++++++++++++++++++++++++------- src/main.rs | 2 +- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index aa831a9..6dd15ce 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,12 +1,23 @@ -use std::{collections::HashSet, io::Read, net::Ipv4Addr}; +use std::{ + collections::HashSet, + io::Read, + net::{IpAddr, Ipv4Addr}, +}; -use crate::{error::AppResult, serde_addr::TargetAddr}; +use crate::{ + error::AppResult, + serde_addr::{TargetAddr, LOCALHOST_V4}, +}; #[derive(serde::Deserialize)] pub struct Config { - pub host: String, + #[serde(default = "default_host")] + pub host: IpAddr, + #[serde(default = "default_port")] pub port: u16, + #[serde(default)] pub local: HashSet, + #[serde(default)] pub remote: HashSet, } @@ -20,12 +31,25 @@ pub fn parse() -> AppResult { Ok(toml::from_str(&buf)?) } else { let mut local = HashSet::new(); - local.insert((Ipv4Addr::new(127, 0, 0, 1).into(), 5232).into()); + local.insert((LOCALHOST_V4, 5232).into()); + + let remote = HashSet::new(); + Ok(Config { - host: "0.0.0.0".to_owned(), - port: 5233, + host: default_host(), + port: default_port(), local, - remote: HashSet::new(), + remote, }) } } + +#[inline] +const fn default_host() -> IpAddr { + IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)) +} + +#[inline] +const fn default_port() -> u16 { + 5233 +} diff --git a/src/main.rs b/src/main.rs index a5710d7..831e7b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ async fn main() -> AppResult<()> { let config = Arc::new(config::parse()?); let auth = Arc::new(socks::auth::NoAuth); - let listener = TcpListener::bind((config.host.as_str(), config.port)).await?; + let listener = TcpListener::bind((config.host, config.port)).await?; let server = socks::Server::new(listener, auth); while let Ok((conn, _addr)) = server.accept().await {