Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/impl plugins #37

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dap-bin/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod parse;
mod service;
mod plugins;
mod target_config;
mod toml;
mod utils_verifier;

pub use {
self::toml::ConfigToml,
parse::{build_settings, parse_opts},
service::ConfigTomlReloader,
parse::parse_opts,
target_config::{ConfigReloader, TargetConfig},
};
55 changes: 0 additions & 55 deletions dap-bin/src/config/parse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use super::toml::ConfigToml;
// use crate::error::{anyhow, ensure};
use clap::{Arg, ArgAction};
use doh_auth_proxy_lib::ProxyConfig;

/// Parsed options
pub struct Opts {
Expand Down Expand Up @@ -39,55 +36,3 @@ pub fn parse_opts() -> Result<Opts, anyhow::Error> {
watch,
})
}

pub fn build_settings(config: &ConfigToml) -> std::result::Result<ProxyConfig, anyhow::Error> {
///////////////////////////////////
// build proxy config
let proxy_config: ProxyConfig = config.try_into()?;

// ///////////////////////////////////
// // backend_apps
// let apps = config.apps.clone().ok_or(anyhow!("Missing application spec"))?;

// // assertions for all backend apps
// ensure!(!apps.0.is_empty(), "Wrong application spec.");
// // if only https_port is specified, tls must be configured for all apps
// if proxy_config.http_port.is_none() {
// ensure!(
// apps.0.iter().all(|(_, app)| app.tls.is_some()),
// "Some apps serves only plaintext HTTP"
// );
// }
// // https redirection can be configured if both ports are active
// if !(proxy_config.https_port.is_some() && proxy_config.http_port.is_some()) {
// ensure!(
// apps.0.iter().all(|(_, app)| {
// if let Some(tls) = app.tls.as_ref() {
// tls.https_redirection.is_none()
// } else {
// true
// }
// }),
// "https_redirection can be specified only when both http_port and https_port are specified"
// );
// }

// // build applications
// let mut app_config_list_inner = Vec::<AppConfig<CryptoFileSource>>::new();

// // let mut backends = Backends::new();
// for (app_name, app) in apps.0.iter() {
// let _server_name_string = app.server_name.as_ref().ok_or(anyhow!("No server name"))?;
// let registered_app_name = app_name.to_ascii_lowercase();
// let app_config = app.build_app_config(&registered_app_name)?;
// app_config_list_inner.push(app_config);
// }

// let app_config_list = AppConfigList {
// inner: app_config_list_inner,
// default_app: config.default_app.clone().map(|v| v.to_ascii_lowercase()), // default backend application for plaintext http requests
// };

// Ok((proxy_config, app_config_list))
Ok(proxy_config)
}
53 changes: 53 additions & 0 deletions dap-bin/src/config/plugins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use super::toml::ConfigToml;
use crate::error::*;
use doh_auth_proxy_lib::QueryManipulationConfig;
use std::{env, fs, path::PathBuf};

/// Read query manipulation settings from paths specified in config toml
impl TryFrom<&ConfigToml> for Option<QueryManipulationConfig> {
type Error = anyhow::Error;

fn try_from(value: &ConfigToml) -> Result<Self, Self::Error> {
if value.plugins.is_none() {
// debug!("Query manipulation plugins are disabled");
return Ok(None);
}
if value.plugins.as_ref().unwrap().domains_overridden_file.is_none()
&& value.plugins.as_ref().unwrap().domains_blocked_file.is_none()
{
// debug!("Query manipulation plugins are disabled");
return Ok(None);
}

// debug!("Query manipulation plugins are enabled");
let plugins = value.plugins.as_ref().unwrap();

let mut query_manipulation_config = QueryManipulationConfig::default();

// override
if let Some(override_path) = &plugins.domains_overridden_file {
// debug!("Read: Query override plugin");
let path = Some(env::current_dir()?.join(override_path)).ok_or(anyhow!("Invalid plugin file path"))?;
query_manipulation_config.domain_override = Some(read_plugin_file(&path)?);
}
// block
if let Some(block_path) = &plugins.domains_blocked_file {
// debug!("Read: Query block plugin");
let path = Some(env::current_dir()?.join(block_path)).ok_or(anyhow!("Invalid plugin file path"))?;
query_manipulation_config.domain_block = Some(read_plugin_file(&path)?);
}

Ok(Some(query_manipulation_config))
}
}

/// Read plugin files
fn read_plugin_file(path: &PathBuf) -> anyhow::Result<Vec<String>> {
let content = fs::read_to_string(path)?;
let truncate_vec: Vec<String> = content
.split('\n')
.filter(|c| !c.is_empty())
.map(|v| v.to_string())
.collect();
Ok(truncate_vec)
}
24 changes: 0 additions & 24 deletions dap-bin/src/config/service.rs

This file was deleted.

Loading