odproxy/conf.rs

64 lines
1.5 KiB
Rust
Raw Normal View History

2023-06-15 18:09:58 +02:00
use std::collections::HashMap;
use std::net::SocketAddr;
2023-06-15 18:09:58 +02:00
use std::sync::{Arc, Mutex};
2023-04-21 00:04:58 +02:00
use std::{fs::File, process::exit};
use std::io::prelude::*;
use serde::Deserialize;
use lazy_static::lazy_static;
use serde_yaml::from_str;
2023-04-21 00:04:58 +02:00
2023-06-23 01:32:17 +02:00
use crate::data::{HOST_MAP, generate_host_map};
2023-04-21 00:04:58 +02:00
lazy_static! {
2023-06-15 18:09:58 +02:00
pub static ref CONFIG: Arc<Mutex<RootConf>> = Arc::new(Mutex::new(load()));
2023-04-21 00:04:58 +02:00
}
2023-06-15 18:09:58 +02:00
#[derive(Debug, Deserialize, Clone)]
2023-04-21 00:04:58 +02:00
pub struct RootConf {
pub listen: SocketAddr,
2023-06-15 18:09:58 +02:00
pub proxy: HashMap<String, ProxyConf>
2023-04-21 00:04:58 +02:00
}
2023-06-15 18:09:58 +02:00
#[derive(Debug, Deserialize, Clone)]
2023-04-21 00:04:58 +02:00
pub struct ProxyConf {
pub hosts: Vec<String>,
2023-04-21 00:04:58 +02:00
pub target: String,
#[serde(default)]
pub socket: bool,
pub spawn: Option<SpawnConf>,
pub timeout: Option<u64>
2023-04-21 00:04:58 +02:00
}
2023-06-15 18:09:58 +02:00
#[derive(Debug, Deserialize, Clone)]
2023-04-21 00:04:58 +02:00
pub struct SpawnConf {
pub command: String,
#[serde(default)]
pub args: Vec<String>,
#[serde(default)]
pub envs: Vec<(String, String)>
2023-04-21 00:04:58 +02:00
}
2023-06-15 18:09:58 +02:00
fn load() -> RootConf {
let file = File::open("config.yml");
2023-04-21 00:04:58 +02:00
if file.is_err() {
println!("[!] Config file was not found!"); exit(-1);
2023-04-21 00:04:58 +02:00
}
let mut contents = String::new();
if file.unwrap().read_to_string(&mut contents).is_err() {
println!("[!] Unable to read config file!"); exit(-1);
2023-04-21 00:04:58 +02:00
}
match from_str(&contents) {
Ok(conf) => conf,
Err(_) => {println!("[!] Unable to parse config!"); exit(0);}
2023-04-21 00:04:58 +02:00
}
2023-06-15 18:09:58 +02:00
}
pub fn reload() {
let conf: RootConf = load();
*CONFIG.lock().unwrap() = conf;
2023-06-23 01:32:17 +02:00
*HOST_MAP.lock().unwrap() = generate_host_map();
2023-06-15 18:09:58 +02:00
}
pub fn get() -> RootConf {
return CONFIG.lock().unwrap().clone();
2023-04-21 00:04:58 +02:00
}