feat(config): use IpAddr instead of String, set defaults

This commit is contained in:
DarkCat09 2024-10-18 22:11:36 +04:00
parent b916a6371b
commit ab0934e976
Signed by: DarkCat09
GPG key ID: BD3CE9B65916CD82
2 changed files with 32 additions and 8 deletions

View file

@ -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)] #[derive(serde::Deserialize)]
pub struct Config { pub struct Config {
pub host: String, #[serde(default = "default_host")]
pub host: IpAddr,
#[serde(default = "default_port")]
pub port: u16, pub port: u16,
#[serde(default)]
pub local: HashSet<TargetAddr>, pub local: HashSet<TargetAddr>,
#[serde(default)]
pub remote: HashSet<TargetAddr>, pub remote: HashSet<TargetAddr>,
} }
@ -20,12 +31,25 @@ pub fn parse() -> AppResult<Config> {
Ok(toml::from_str(&buf)?) Ok(toml::from_str(&buf)?)
} else { } else {
let mut local = HashSet::new(); 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 { Ok(Config {
host: "0.0.0.0".to_owned(), host: default_host(),
port: 5233, port: default_port(),
local, 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
}

View file

@ -17,7 +17,7 @@ async fn main() -> AppResult<()> {
let config = Arc::new(config::parse()?); let config = Arc::new(config::parse()?);
let auth = Arc::new(socks::auth::NoAuth); 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); let server = socks::Server::new(listener, auth);
while let Ok((conn, _addr)) = server.accept().await { while let Ok((conn, _addr)) = server.accept().await {