feat: config works

This commit is contained in:
2024-03-30 15:37:26 +08:00
parent f02f6bb7dd
commit 55c1aa3205
8 changed files with 944 additions and 182 deletions

46
src/config.rs Normal file
View File

@@ -0,0 +1,46 @@
use std::collections::HashMap;
use std::fs;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Serialize, Deserialize)]
pub struct ProxyConfig {
pub prometheus: Option<bool>,
pub groups: Vec<ProxyGroup>,
}
impl ProxyConfig {
pub fn load(file_name: &str) -> Result<Self, String> {
let file_content = match fs::read_to_string(file_name) {
Ok(file_content) => file_content,
Err(e) => return Err(format!("read file: {} failed: {}", file_name, e)),
};
let proxy_config: ProxyConfig = match serde_json::from_str(&file_content) {
Ok(proxy_config) => proxy_config,
Err(e) => return Err(format!("parse file: {} failed: {}", file_name, e)),
};
Ok(proxy_config)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ProxyGroup {
pub port: u16,
pub lookup_dns: Option<bool>,
pub tls: Option<ProxyTls>,
pub proxy_map: Option<HashMap<String, ProxyItem>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ProxyItem {
pub address: String,
pub tls: Option<bool>,
pub sni: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ProxyTls {
pub intermediate_cert: String,
pub intermediate_key: String,
}