-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.rs
executable file
·64 lines (56 loc) · 1.48 KB
/
conf.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::{fs::File, process::exit};
use std::io::prelude::*;
use serde::Deserialize;
use lazy_static::lazy_static;
use serde_yaml::from_str;
use crate::data::{HOST_MAP, generate_host_map};
lazy_static! {
pub static ref CONFIG: Arc<Mutex<RootConf>> = Arc::new(Mutex::new(load()));
}
#[derive(Debug, Deserialize, Clone)]
pub struct RootConf {
pub listen: SocketAddr,
pub proxy: HashMap<String, ProxyConf>
}
#[derive(Debug, Deserialize, Clone)]
pub struct ProxyConf {
pub hosts: Vec<String>,
pub target: String,
#[serde(default)]
pub socket: bool,
pub spawn: Option<SpawnConf>,
pub timeout: Option<u64>
}
#[derive(Debug, Deserialize, Clone)]
pub struct SpawnConf {
pub command: String,
#[serde(default)]
pub args: Vec<String>,
#[serde(default)]
pub envs: Vec<(String, String)>
}
fn load() -> RootConf {
let file = File::open("config.yml");
if file.is_err() {
println!("[!] Config file was not found!"); exit(-1);
}
let mut contents = String::new();
if file.unwrap().read_to_string(&mut contents).is_err() {
println!("[!] Unable to read config file!"); exit(-1);
}
match from_str(&contents) {
Ok(conf) => conf,
Err(_) => {println!("[!] Unable to parse config!"); exit(0);}
}
}
pub fn reload() {
let conf: RootConf = load();
*CONFIG.lock().unwrap() = conf;
*HOST_MAP.lock().unwrap() = generate_host_map();
}
pub fn get() -> RootConf {
return CONFIG.lock().unwrap().clone();
}