diff --git a/etc/upstreams.yaml b/etc/upstreams.yaml index aed20f1..c222acd 100644 --- a/etc/upstreams.yaml +++ b/etc/upstreams.yaml @@ -7,13 +7,13 @@ globals: - "Access-Control-Allow-Methods:POST, GET, OPTIONS" - "Access-Control-Max-Age:86400" - "X-Custom-Header:Something Special" - authorization: # Optional, only one of auth methods below can be active at a time - # - "basic" - # - "zangag:Anhnazand1234" - # - "apikey" - # - "5a28cc4c-ce10-4ff1-824e-743c38835f5c" - - "jwt" - - "910517d9-f9a1-48de-8826-dbadacbd84af-cb6f830e-ab16-47ec-9d8f-0090de732774" +# authorization: # Optional, only one of auth methods below can be active at a time +# - "basic" +# - "zangag:Anhnazand1234" +# - "apikey" +# - "5a28cc4c-ce10-4ff1-824e-743c38835f5c" +# - "jwt" +# - "910517d9-f9a1-48de-8826-dbadacbd84af-cb6f830e-ab16-47ec-9d8f-0090de732774" consul: # If the provider is consul. Otherwise ignored servers: - "http://master1:8500" diff --git a/src/utils.rs b/src/utils.rs index a409697..75ef53b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,4 +5,5 @@ mod filewatch; pub mod healthcheck; pub mod jwt; pub mod parceyaml; +pub mod structs; pub mod tools; diff --git a/src/utils/consul.rs b/src/utils/consul.rs index 91bdbc8..30d5001 100644 --- a/src/utils/consul.rs +++ b/src/utils/consul.rs @@ -1,16 +1,12 @@ -use crate::utils::parceyaml::{load_configuration, Configuration, ServiceMapping}; -use crate::utils::tools::{clone_dashmap_into, compare_dashmaps, UpstreamsDashMap}; +use crate::utils::parceyaml::load_configuration; +use crate::utils::structs::{Configuration, ServiceMapping, UpstreamsDashMap}; +use crate::utils::tools::{clone_dashmap_into, compare_dashmaps}; use dashmap::DashMap; use futures::channel::mpsc::Sender; use futures::SinkExt; -// use hickory_client::client::{Client, ClientHandle}; -// use hickory_client::proto::rr::{DNSClass, Name, RecordType}; -// use hickory_client::proto::runtime::TokioRuntimeProvider; -// use hickory_client::proto::tcp::TcpClientStream; use log::{info, warn}; use pingora::prelude::sleep; use rand::Rng; -// use std::str::FromStr; use reqwest::header::{HeaderMap, HeaderValue}; use serde::Deserialize; use std::collections::HashMap; @@ -19,8 +15,6 @@ use std::time::Duration; #[derive(Debug, Deserialize)] struct Service { - // #[serde(rename = "ServiceName")] - // service_name: String, #[serde(rename = "ServiceTaggedAddresses")] tagged_addresses: HashMap, } @@ -148,20 +142,3 @@ async fn get_by_http(url: String, token: Option) -> Option {:?} : {:?}", y.target().to_utf8(), y.port()); -// } -// } -// } diff --git a/src/utils/discovery.rs b/src/utils/discovery.rs index 9739b1c..1696869 100644 --- a/src/utils/discovery.rs +++ b/src/utils/discovery.rs @@ -1,6 +1,6 @@ use crate::utils::consul; use crate::utils::filewatch; -use crate::utils::parceyaml::Configuration; +use crate::utils::structs::Configuration; use crate::web::webserver; use async_trait::async_trait; use futures::channel::mpsc::Sender; diff --git a/src/utils/filewatch.rs b/src/utils/filewatch.rs index 52257bb..595933b 100644 --- a/src/utils/filewatch.rs +++ b/src/utils/filewatch.rs @@ -1,4 +1,5 @@ -use crate::utils::parceyaml::{load_configuration, Configuration}; +use crate::utils::parceyaml::load_configuration; +use crate::utils::structs::Configuration; use futures::channel::mpsc::Sender; use futures::SinkExt; use log::{error, info}; diff --git a/src/utils/healthcheck.rs b/src/utils/healthcheck.rs index 98c5f0f..102c30b 100644 --- a/src/utils/healthcheck.rs +++ b/src/utils/healthcheck.rs @@ -1,3 +1,4 @@ +use crate::utils::structs::{UpstreamsDashMap, UpstreamsIdMap}; use crate::utils::tools::*; use dashmap::DashMap; use log::{error, info, warn}; diff --git a/src/utils/parceyaml.rs b/src/utils/parceyaml.rs index 36d77ae..f49f52f 100644 --- a/src/utils/parceyaml.rs +++ b/src/utils/parceyaml.rs @@ -1,59 +1,11 @@ -use crate::utils::tools::*; +use crate::utils::structs::*; use dashmap::DashMap; use log::{error, info, warn}; -use serde::{Deserialize, Serialize}; use serde_yaml::Error; use std::collections::HashMap; use std::fs; use std::sync::atomic::AtomicUsize; -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct ServiceMapping { - pub proxy: String, - pub real: String, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Extraparams { - pub stickysessions: bool, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Consul { - pub servers: Option>, - pub services: Option>, - pub token: Option, -} -#[derive(Debug, Serialize, Deserialize)] -struct Config { - provider: String, - stickysessions: bool, - upstreams: Option>, - globals: Option>>, - consul: Option, -} - -#[derive(Debug, Serialize, Deserialize)] -struct HostConfig { - paths: HashMap, -} - -#[derive(Debug, Serialize, Deserialize)] -struct PathConfig { - ssl: bool, - servers: Vec, - headers: Option>, -} -pub struct Configuration { - pub upstreams: UpstreamsDashMap, - pub headers: Headers, - pub consul: Option, - pub typecfg: String, - pub extraparams: Extraparams, - pub globals: Option>>, -} - -// pub fn load_configuration(d: &str, kind: &str) -> Option<(UpstreamsDashMap, Headers, String)> { pub fn load_configuration(d: &str, kind: &str) -> Option { let mut toreturn: Configuration = Configuration { upstreams: Default::default(), @@ -66,9 +18,6 @@ pub fn load_configuration(d: &str, kind: &str) -> Option { toreturn.upstreams = UpstreamsDashMap::new(); toreturn.headers = Headers::new(); - // let upstreamsmap = UpstreamsDashMap::new(); - // let headersmap = DashMap::new(); - let mut yaml_data = d.to_string(); match kind { "filepath" => { @@ -172,21 +121,6 @@ pub fn load_configuration(d: &str, kind: &str) -> Option { } } -#[derive(Debug, Deserialize)] -pub struct AppConfig { - pub hc_interval: u16, - pub hc_method: String, - pub upstreams_conf: String, - pub log_level: String, - pub config_address: String, - pub proxy_address_http: String, - pub master_key: String, - pub proxy_address_tls: Option, - pub tls_certificate: Option, - pub tls_key_file: Option, -} - -// pub fn parce_main_config(path: &str) -> DashMap { pub fn parce_main_config(path: &str) -> AppConfig { info!("Parsing configuration"); let data = fs::read_to_string(path).unwrap(); diff --git a/src/utils/structs.rs b/src/utils/structs.rs new file mode 100644 index 0000000..1085847 --- /dev/null +++ b/src/utils/structs.rs @@ -0,0 +1,68 @@ +use dashmap::DashMap; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::sync::atomic::AtomicUsize; + +pub type UpstreamsDashMap = DashMap, AtomicUsize)>>; +pub type Headers = DashMap>>; +pub type UpstreamsIdMap = DashMap; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ServiceMapping { + pub proxy: String, + pub real: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Extraparams { + pub stickysessions: bool, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Consul { + pub servers: Option>, + pub services: Option>, + pub token: Option, +} +#[derive(Debug, Serialize, Deserialize)] +pub struct Config { + pub provider: String, + pub stickysessions: bool, + pub upstreams: Option>, + pub globals: Option>>, + pub consul: Option, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct HostConfig { + pub paths: HashMap, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct PathConfig { + pub ssl: bool, + pub servers: Vec, + pub headers: Option>, +} +pub struct Configuration { + pub upstreams: UpstreamsDashMap, + pub headers: Headers, + pub consul: Option, + pub typecfg: String, + pub extraparams: Extraparams, + pub globals: Option>>, +} + +#[derive(Debug, Deserialize)] +pub struct AppConfig { + pub hc_interval: u16, + pub hc_method: String, + pub upstreams_conf: String, + pub log_level: String, + pub config_address: String, + pub proxy_address_http: String, + pub master_key: String, + pub proxy_address_tls: Option, + pub tls_certificate: Option, + pub tls_key_file: Option, +} diff --git a/src/utils/tools.rs b/src/utils/tools.rs index 411fa76..8ad4e41 100644 --- a/src/utils/tools.rs +++ b/src/utils/tools.rs @@ -1,3 +1,4 @@ +use crate::utils::structs::{UpstreamsDashMap, UpstreamsIdMap}; use dashmap::DashMap; use sha2::{Digest, Sha256}; use std::any::type_name; @@ -22,10 +23,6 @@ pub fn print_upstreams(upstreams: &UpstreamsDashMap) { } } -pub type UpstreamsDashMap = DashMap, AtomicUsize)>>; -pub type Headers = DashMap>>; -pub type UpstreamsIdMap = DashMap; - #[allow(dead_code)] pub fn typeoff(_: T) { let to = type_name::(); @@ -44,7 +41,6 @@ pub fn string_to_bool(val: Option<&str>) -> Option { } } -#[allow(dead_code)] pub fn clone_dashmap(original: &UpstreamsDashMap) -> UpstreamsDashMap { let new_map: UpstreamsDashMap = DashMap::new(); @@ -117,7 +113,6 @@ pub fn compare_dashmaps(map1: &UpstreamsDashMap, map2: &UpstreamsDashMap) -> boo true } -#[allow(dead_code)] pub fn merge_headers(target: &DashMap>, source: &DashMap>) { for entry in source.iter() { let global_key = entry.key().clone(); @@ -137,10 +132,8 @@ pub fn clone_idmap_into(original: &UpstreamsDashMap, cloned: &UpstreamsIdMap) { let (vec, _) = inner_entry.value(); let new_vec = vec.clone(); for x in vec.iter() { - // let id = format!("{}:{}:{}", x.0.to_string(), x.1.to_string(), x.2.to_string()); let mut id = String::new(); write!(&mut id, "{}:{}:{}", x.0, x.1, x.2).unwrap(); - let mut hasher = Sha256::new(); hasher.update(id.clone().into_bytes()); let hash = hasher.finalize(); diff --git a/src/web/bgservice.rs b/src/web/bgservice.rs index 3365d7e..e1dc107 100644 --- a/src/web/bgservice.rs +++ b/src/web/bgservice.rs @@ -1,5 +1,5 @@ use crate::utils::discovery::{APIUpstreamProvider, ConsulProvider, Discovery, FromFileProvider}; -use crate::utils::parceyaml::Configuration; +use crate::utils::structs::Configuration; use crate::utils::tools::*; use crate::utils::*; use crate::web::proxyhttp::LB; diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index e5a2fd8..b49b208 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -1,6 +1,5 @@ use crate::utils::auth::authenticate; -use crate::utils::parceyaml::{AppConfig, Extraparams}; -use crate::utils::tools::*; +use crate::utils::structs::{AppConfig, Extraparams, Headers, UpstreamsDashMap, UpstreamsIdMap}; use crate::web::gethosts::GetHost; use async_trait::async_trait; use dashmap::DashMap; diff --git a/src/web/start.rs b/src/web/start.rs index ab11a10..cba1764 100644 --- a/src/web/start.rs +++ b/src/web/start.rs @@ -1,5 +1,4 @@ -use crate::utils::parceyaml::Extraparams; -use crate::utils::tools::*; +use crate::utils::structs::{Extraparams, Headers, UpstreamsDashMap, UpstreamsIdMap}; use crate::web::proxyhttp::LB; use dashmap::DashMap; use log::info; diff --git a/src/web/webserver.rs b/src/web/webserver.rs index c298d67..8561fdb 100644 --- a/src/web/webserver.rs +++ b/src/web/webserver.rs @@ -1,4 +1,4 @@ -use crate::utils::parceyaml::Configuration; +use crate::utils::structs::Configuration; use axum::body::Body; use axum::extract::State; use axum::http::{Response, StatusCode};