Try being multiplatform with our config files.

This commit is contained in:
2022-11-23 19:49:58 -08:00
parent 1d182a150f
commit 65e79b4237
3 changed files with 8 additions and 7 deletions

View File

@@ -14,6 +14,7 @@ path="server/main.rs"
[dependencies]
anyhow = "^1.0.57"
clap = { version = "^3.1.18", features = ["derive"] }
etcetera = "^0.4.0"
futures = "0.3.21"
if-addrs = "0.7.0"
lazy_static = "1.4.0"
@@ -25,7 +26,6 @@ tokio = { version = "^1", features = ["full"] }
toml = "^0.5.9"
tracing = "^0.1.34"
tracing-subscriber = { version = "^0.3.11", features = ["env-filter"] }
xdg = "2.4.1"
[dev-dependencies]
proptest = "1.0.0"

View File

@@ -10,14 +10,11 @@ use std::net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
use std::str::FromStr;
use thiserror::Error;
use tracing::metadata::LevelFilter;
use xdg::BaseDirectoriesError;
#[derive(Debug, Error)]
pub enum ConfigError {
#[error(transparent)]
CommandLineError(#[from] clap::Error),
#[error("Error querying XDG base directories: {0}")]
XdgError(#[from] BaseDirectoriesError),
#[error(transparent)]
IOError(#[from] io::Error),
#[error("TOML processing error: {0}")]
@@ -26,6 +23,8 @@ pub enum ConfigError {
NoAddressForInterface(String, String),
#[error("Server '{0}' specifies an address we couldn't parse: {1}")]
AddressParseError(String, AddrParseError),
#[error("Host directory error {0}")]
HostDirectoryError(String),
}
#[derive(Debug)]

View File

@@ -1,10 +1,10 @@
use super::ConfigError;
use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
use serde::{Deserialize, Deserializer};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use tracing::metadata::LevelFilter;
use xdg::BaseDirectories;
#[derive(serde_derive::Deserialize, Default)]
pub struct ConfigFile {
@@ -42,8 +42,10 @@ where
impl ConfigFile {
pub fn read(mut config_file_path: Option<PathBuf>) -> Result<ConfigFile, ConfigError> {
if config_file_path.is_none() {
let base_dirs = BaseDirectories::with_prefix("socks5")?;
let proposed_path = base_dirs.get_config_home();
let base_dirs = choose_base_strategy()
.map_err(|e| ConfigError::HostDirectoryError(e.to_string()))?;
let mut proposed_path = base_dirs.config_dir();
proposed_path.push("socks5");
if let Ok(attributes) = fs::metadata(proposed_path.clone()) {
if attributes.is_file() {
config_file_path = Some(proposed_path);