odproxy/data.rs

59 lines
1.4 KiB
Rust
Raw Permalink Normal View History

2023-06-15 18:09:58 +02:00
use std::{sync::{Arc, Mutex}, vec};
2023-04-21 00:04:58 +02:00
use lazy_static::lazy_static;
2023-05-12 00:10:22 +02:00
use tokio::process::Child;
2023-04-21 00:04:58 +02:00
use std::collections::HashMap;
use hyper::http::HeaderValue;
2023-06-15 18:09:58 +02:00
use crate::conf::{ProxyConf, self};
2023-04-21 00:04:58 +02:00
lazy_static! {
2023-06-23 01:32:17 +02:00
pub static ref HOST_MAP: Arc<Mutex<HashMap<String, String>>> = Arc::new(Mutex::new(generate_host_map()));
2023-06-15 18:09:58 +02:00
pub static ref SERVICES: Arc<Mutex<HashMap<String, ServiceData>>> = Arc::new(Mutex::new(HashMap::from_iter(vec![])));
2023-04-21 00:04:58 +02:00
}
pub struct ServiceData {
pub child: Option<Child>,
pub running: bool,
pub last_active: u64
2023-04-21 00:04:58 +02:00
}
impl ServiceData {
pub fn new() -> ServiceData {
2023-04-21 00:04:58 +02:00
ServiceData {
child: None,
running: false,
last_active: 0
2023-04-21 00:04:58 +02:00
}
}
}
2023-06-23 01:32:17 +02:00
pub fn get_proxy(name: Option<String>) -> Option<ProxyConf> {
2023-06-15 18:09:58 +02:00
let c = conf::get();
match name {
2023-06-23 01:32:17 +02:00
Some(name) => c.proxy.get(&name).cloned(),
2023-04-21 00:04:58 +02:00
None => None
}
}
2023-06-23 01:32:17 +02:00
pub fn get_proxy_name(host: Option<&HeaderValue>) -> Option<String> {
2023-04-21 00:04:58 +02:00
match host {
Some(host) => {
let host_parts: Vec<&str> = host.to_str().unwrap().split(":").collect();
let domain = host_parts.get(0);
2023-06-23 01:32:17 +02:00
let host_map = HOST_MAP.lock().ok()?;
host_map.get(&domain?.to_string()).cloned()
2023-04-21 00:04:58 +02:00
},
None => None
}
}
2023-06-15 18:09:58 +02:00
pub fn generate_host_map() -> HashMap<String, String> {
let mut hosts: Vec<(String, String)> = vec![];
for (name, proxy) in conf::get().proxy.iter() {
for host in proxy.hosts.iter() {
2023-06-15 18:09:58 +02:00
hosts.push((host.to_string(), name.to_string()));
2023-06-23 01:32:17 +02:00
}
2023-04-21 00:04:58 +02:00
}
HashMap::from_iter(hosts)
}