Try being multiplatform with our config files.
This commit is contained in:
@@ -14,6 +14,7 @@ path="server/main.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "^1.0.57"
|
anyhow = "^1.0.57"
|
||||||
clap = { version = "^3.1.18", features = ["derive"] }
|
clap = { version = "^3.1.18", features = ["derive"] }
|
||||||
|
etcetera = "^0.4.0"
|
||||||
futures = "0.3.21"
|
futures = "0.3.21"
|
||||||
if-addrs = "0.7.0"
|
if-addrs = "0.7.0"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
@@ -25,7 +26,6 @@ tokio = { version = "^1", features = ["full"] }
|
|||||||
toml = "^0.5.9"
|
toml = "^0.5.9"
|
||||||
tracing = "^0.1.34"
|
tracing = "^0.1.34"
|
||||||
tracing-subscriber = { version = "^0.3.11", features = ["env-filter"] }
|
tracing-subscriber = { version = "^0.3.11", features = ["env-filter"] }
|
||||||
xdg = "2.4.1"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
proptest = "1.0.0"
|
proptest = "1.0.0"
|
||||||
|
|||||||
@@ -10,14 +10,11 @@ use std::net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use tracing::metadata::LevelFilter;
|
use tracing::metadata::LevelFilter;
|
||||||
use xdg::BaseDirectoriesError;
|
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum ConfigError {
|
pub enum ConfigError {
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
CommandLineError(#[from] clap::Error),
|
CommandLineError(#[from] clap::Error),
|
||||||
#[error("Error querying XDG base directories: {0}")]
|
|
||||||
XdgError(#[from] BaseDirectoriesError),
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
IOError(#[from] io::Error),
|
IOError(#[from] io::Error),
|
||||||
#[error("TOML processing error: {0}")]
|
#[error("TOML processing error: {0}")]
|
||||||
@@ -26,6 +23,8 @@ pub enum ConfigError {
|
|||||||
NoAddressForInterface(String, String),
|
NoAddressForInterface(String, String),
|
||||||
#[error("Server '{0}' specifies an address we couldn't parse: {1}")]
|
#[error("Server '{0}' specifies an address we couldn't parse: {1}")]
|
||||||
AddressParseError(String, AddrParseError),
|
AddressParseError(String, AddrParseError),
|
||||||
|
#[error("Host directory error {0}")]
|
||||||
|
HostDirectoryError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use super::ConfigError;
|
use super::ConfigError;
|
||||||
|
use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
|
||||||
use serde::{Deserialize, Deserializer};
|
use serde::{Deserialize, Deserializer};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tracing::metadata::LevelFilter;
|
use tracing::metadata::LevelFilter;
|
||||||
use xdg::BaseDirectories;
|
|
||||||
|
|
||||||
#[derive(serde_derive::Deserialize, Default)]
|
#[derive(serde_derive::Deserialize, Default)]
|
||||||
pub struct ConfigFile {
|
pub struct ConfigFile {
|
||||||
@@ -42,8 +42,10 @@ where
|
|||||||
impl ConfigFile {
|
impl ConfigFile {
|
||||||
pub fn read(mut config_file_path: Option<PathBuf>) -> Result<ConfigFile, ConfigError> {
|
pub fn read(mut config_file_path: Option<PathBuf>) -> Result<ConfigFile, ConfigError> {
|
||||||
if config_file_path.is_none() {
|
if config_file_path.is_none() {
|
||||||
let base_dirs = BaseDirectories::with_prefix("socks5")?;
|
let base_dirs = choose_base_strategy()
|
||||||
let proposed_path = base_dirs.get_config_home();
|
.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 let Ok(attributes) = fs::metadata(proposed_path.clone()) {
|
||||||
if attributes.is_file() {
|
if attributes.is_file() {
|
||||||
config_file_path = Some(proposed_path);
|
config_file_path = Some(proposed_path);
|
||||||
|
|||||||
Reference in New Issue
Block a user