|
| 1 | +use std::{ |
| 2 | + net::{Ipv4Addr, SocketAddr}, |
| 3 | + path::PathBuf, |
| 4 | + time::Duration, |
| 5 | +}; |
| 6 | + |
| 7 | +use educe::Educe; |
| 8 | +use figment::{ |
| 9 | + Figment, |
| 10 | + providers::{Env, Format, Toml, Yaml}, |
| 11 | +}; |
| 12 | +use serde::{Deserialize, Serialize}; |
| 13 | +use wind_core::types::TargetAddr; |
| 14 | +use wind_socks::inbound::AuthMode; |
| 15 | + |
| 16 | +#[derive(Debug, Deserialize, Serialize, Educe)] |
| 17 | +#[educe(Default)] |
| 18 | +pub struct PersistentConfig { |
| 19 | + pub socks_opt: SocksOpt, |
| 20 | + pub tuic_opt: TuicOpt, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Deserialize, Serialize, Educe)] |
| 24 | +#[educe(Default)] |
| 25 | +pub struct SocksOpt { |
| 26 | + #[educe(Default(expression = "127.0.0.1:6666".parse().unwrap()))] |
| 27 | + pub listen_addr: SocketAddr, |
| 28 | + |
| 29 | + #[educe(Default = None)] |
| 30 | + pub public_addr: Option<std::net::IpAddr>, |
| 31 | + |
| 32 | + #[educe(Default = AuthModeConfig::NoAuth)] |
| 33 | + pub auth: AuthModeConfig, |
| 34 | + |
| 35 | + #[educe(Default = false)] |
| 36 | + pub skip_auth: bool, |
| 37 | + |
| 38 | + #[educe(Default = true)] |
| 39 | + pub allow_udp: bool, |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Debug, Deserialize, Serialize, Clone, Educe)] |
| 43 | +#[educe(Default)] |
| 44 | +pub enum AuthModeConfig { |
| 45 | + #[educe(Default)] |
| 46 | + NoAuth, |
| 47 | + Password { |
| 48 | + username: String, |
| 49 | + password: String, |
| 50 | + }, |
| 51 | +} |
| 52 | + |
| 53 | +impl From<AuthModeConfig> for AuthMode { |
| 54 | + fn from(config: AuthModeConfig) -> Self { |
| 55 | + match config { |
| 56 | + AuthModeConfig::NoAuth => AuthMode::NoAuth, |
| 57 | + AuthModeConfig::Password { username, password } => AuthMode::Password { username, password }, |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Debug, Deserialize, Serialize, Educe)] |
| 63 | +#[educe(Default)] |
| 64 | +pub struct TuicOpt { |
| 65 | + #[educe(Default = TargetAddr::IPv4(Ipv4Addr::new(127, 0, 0, 1), 9443))] |
| 66 | + pub server_addr: TargetAddr, |
| 67 | + |
| 68 | + #[educe(Default = "localhost")] |
| 69 | + pub sni: String, |
| 70 | + |
| 71 | + #[educe(Default = "c1e6dbe2-f417-4890-994c-9ee15b926597".parse().unwrap())] |
| 72 | + pub uuid: uuid::Uuid, |
| 73 | + |
| 74 | + #[educe(Default = "test_passwd")] |
| 75 | + pub password: String, |
| 76 | + |
| 77 | + #[educe(Default = false)] |
| 78 | + pub zero_rtt_handshake: bool, |
| 79 | + |
| 80 | + #[serde(with = "humantime_serde")] |
| 81 | + #[educe(Default(expression = Duration::from_secs(10)))] |
| 82 | + pub heartbeat: Duration, |
| 83 | + |
| 84 | + #[serde(with = "humantime_serde")] |
| 85 | + #[educe(Default(expression = Duration::from_secs(20)))] |
| 86 | + pub gc_interval: Duration, |
| 87 | + |
| 88 | + #[serde(with = "humantime_serde")] |
| 89 | + #[educe(Default(expression = Duration::from_secs(20)))] |
| 90 | + pub gc_lifetime: Duration, |
| 91 | + |
| 92 | + #[educe(Default = true)] |
| 93 | + pub skip_cert_verify: bool, |
| 94 | + |
| 95 | + #[educe(Default(expression = vec![String::from("h3")]))] |
| 96 | + pub alpn: Vec<String>, |
| 97 | +} |
| 98 | + |
| 99 | +impl PersistentConfig { |
| 100 | + pub fn export_to_file(&self, file_path: &PathBuf, format: &str) -> eyre::Result<()> { |
| 101 | + use std::{fs, io::Write}; |
| 102 | + |
| 103 | + match format.to_lowercase().as_str() { |
| 104 | + "yaml" => { |
| 105 | + let yaml_content = serde_yaml::to_string(&self)?; |
| 106 | + let mut file = fs::File::create(file_path)?; |
| 107 | + file.write_all(yaml_content.as_bytes())?; |
| 108 | + } |
| 109 | + "toml" => { |
| 110 | + let toml_content = toml::to_string_pretty(&self)?; |
| 111 | + let mut file = fs::File::create(file_path)?; |
| 112 | + file.write_all(toml_content.as_bytes())?; |
| 113 | + } |
| 114 | + _ => return Err(eyre::eyre!("Unsupported file format: {}", format)), |
| 115 | + } |
| 116 | + |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | + |
| 120 | + pub fn load(config_path: Option<String>, config_dir: Option<PathBuf>) -> eyre::Result<Self> { |
| 121 | + // Start with empty figment (will use default values via serde) |
| 122 | + let mut figment = Figment::new(); |
| 123 | + |
| 124 | + // Load from default configuration location |
| 125 | + if let Some(config_dir) = config_dir { |
| 126 | + let config_file = config_dir.join("config.toml"); |
| 127 | + if config_file.exists() { |
| 128 | + figment = figment.merge(Toml::file(config_file)); |
| 129 | + } |
| 130 | + |
| 131 | + let config_file = config_dir.join("config.yaml"); |
| 132 | + if config_file.exists() { |
| 133 | + figment = figment.merge(Yaml::file(config_file)); |
| 134 | + } |
| 135 | + } else { |
| 136 | + // Try to load from default locations in current directory |
| 137 | + let config_toml = std::path::Path::new("config.toml"); |
| 138 | + if config_toml.exists() { |
| 139 | + figment = figment.merge(Toml::file(config_toml)); |
| 140 | + } |
| 141 | + |
| 142 | + let config_yaml = std::path::Path::new("config.yaml"); |
| 143 | + if config_yaml.exists() { |
| 144 | + figment = figment.merge(Yaml::file(config_yaml)); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // If specific config path is provided, use that |
| 149 | + if let Some(config_path) = config_path { |
| 150 | + if config_path.ends_with(".toml") { |
| 151 | + figment = figment.merge(Toml::file(config_path)); |
| 152 | + } else if config_path.ends_with(".yaml") || config_path.ends_with(".yml") { |
| 153 | + figment = figment.merge(Yaml::file(config_path)); |
| 154 | + } else { |
| 155 | + // Assume it's TOML format |
| 156 | + figment = figment.merge(Toml::file(config_path)); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Environment variables can override config files |
| 161 | + figment = figment.merge(Env::prefixed("WIND_")); |
| 162 | + |
| 163 | + // Extract the configuration |
| 164 | + let config: PersistentConfig = figment.extract()?; |
| 165 | + |
| 166 | + Ok(config) |
| 167 | + } |
| 168 | +} |
0 commit comments