Working settings file! (partially)

This commit is contained in:
Slatian
2023-02-18 22:16:09 +01:00
parent 2394d90087
commit 645a0eed69
8 changed files with 478 additions and 39 deletions

View File

@ -1,45 +1,44 @@
use std::path::Path;
use axum_client_ip::SecureClientIpSource;
use std::net::SocketAddr;
#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Default)]
pub struct EchoIpServiceConfig {
server: ServerConfig,
dns: DnsConfig,
geoip: GeoIpConfig,
template: TemplateConfig,
pub server: ServerConfig,
pub dns: DnsConfig,
pub geoip: GeoIpConfig,
pub template: TemplateConfig,
}
#[derive(serde::Deserialize)]
pub struct ServerConfig {
listen_on: SocketAddr,
ip_header: SecureClientIpSource,
pub listen_on: SocketAddr,
pub ip_header: SecureClientIpSource,
}
#[derive(serde::Deserialize)]
pub struct DnsConfig {
allow_forward_lookup: bool,
allow_reverse_lookup: bool,
hide_private_range_ips: bool,
hidden_suffixes: Vec<String>,
pub allow_forward_lookup: bool,
pub allow_reverse_lookup: bool,
pub hide_private_range_ips: bool,
pub hidden_suffixes: Vec<String>,
//Future Idea: allow custom resolver
}
#[derive(serde::Deserialize)]
pub struct GeoIpConfig {
asn_database: Option<String>,
location_database: Option<String>,
pub asn_database: Option<String>,
pub location_database: Option<String>,
}
#[derive(serde::Deserialize)]
pub struct TemplateConfig {
template_location: String,
pub template_location: String,
}
impl Default for ServerConfig {
fn default() -> Self {
ServerConfig {
listen_on: "127.0.0.1:3000",
listen_on: "127.0.0.1:3000".parse().unwrap(),
ip_header: SecureClientIpSource::RightmostXForwardedFor,
}
}
@ -48,10 +47,27 @@ impl Default for ServerConfig {
impl Default for DnsConfig {
fn default() -> Self {
DnsConfig {
allow_forward_lookup: true;
allow_reverse_lookup: false;
hide_private_range_ips: true;
hidden_suffixes: Vec::new();
allow_forward_lookup: true,
allow_reverse_lookup: false,
hide_private_range_ips: true,
hidden_suffixes: Vec::new(),
}
}
}
impl Default for GeoIpConfig {
fn default() -> Self {
GeoIpConfig {
asn_database: Some("mmdb/GeoLite2-ASN.mmdb".to_string()),
location_database: Some("mmdb/GeoLite2-City.mmdb".to_string()),
}
}
}
impl Default for TemplateConfig {
fn default() -> Self {
TemplateConfig {
template_location: "templates/".to_string(),
}
}
}

View File

@ -7,7 +7,7 @@ use axum::{
routing::get,
};
use axum_client_ip::{SecureClientIp, SecureClientIpSource};
use clap::Parser;
use tera::Tera;
use trust_dns_resolver::{
TokioAsyncResolver,
@ -15,6 +15,7 @@ use trust_dns_resolver::{
config::ResolverConfig,
};
use std::fs;
use std::net::IpAddr;
use std::sync::Arc;
use std::path::Path;
@ -64,15 +65,50 @@ struct ServiceSharedState {
dns_resolver: TokioAsyncResolver,
asn_db: geoip::MMDBCarrier,
location_db: geoip::MMDBCarrier,
config: config::EchoIpServiceConfig,
}
#[derive(Parser)]
#[command(author, version, long_about="None")]
struct CliArgs {
#[arg(short, long)]
config: Option<String>,
#[arg(short, long)]
listen_on: Option<String>,
#[arg(short, long)]
templates: Option<String>,
}
#[tokio::main]
async fn main() {
// Parse Command line arguments
let cli_args = CliArgs::parse();
// Read configuration file
let config: config::EchoIpServiceConfig = match cli_args.config {
Some(config_path) => {
let config_text = fs::read_to_string(config_path)
.expect("Can't read configuration file!");
match toml::from_str(&config_text) {
Ok(c) => c,
Err(e) => {
panic!("Unable to parse configuration file: {e}");
}
}
},
None => Default::default(),
};
// Initalize Tera templates
// TODO: don't hardcode template directory
println!("Parsing Templates ...");
let res = Tera::new("templates/*.html");
let mut template_base_dir = (&config.template.template_location).to_owned();
if !template_base_dir.ends_with("/") {
template_base_dir = template_base_dir + "/";
}
let template_glob = template_base_dir+"*.html";
println!("Parsing Templates from '{}' ...", &template_glob);
let res = Tera::new((template_glob).as_str());
let tera = match res {
Ok(t) => t,
Err(e) => {
@ -87,13 +123,19 @@ async fn main() {
mmdb: None,
name: "GeoIP ASN Database".to_string(),
};
asn_db.load_database_from_path(Path::new("mmdb/GeoLite2-ASN.mmdb")).ok();
match &config.geoip.asn_database {
Some(path) => { asn_db.load_database_from_path(Path::new(&path)).ok(); },
None => {},
}
let mut location_db = geoip::MMDBCarrier {
mmdb: None,
name: "GeoIP Location Database".to_string(),
};
location_db.load_database_from_path(Path::new("mmdb/GeoLite2-City.mmdb")).ok();
match &config.geoip.location_database {
Some(path) => { location_db.load_database_from_path(Path::new(&path)).ok(); },
None => {},
}
// Initalize DNS resolver with os defaults
println!("Initalizing dns resolver ...");
@ -105,16 +147,19 @@ async fn main() {
::std::process::exit(1);
}
};
let listen_on = config.server.listen_on;
// Initialize shared state
let shared_state = Arc::new(
ServiceSharedState{
ServiceSharedState {
templating_engine: templating_engine::Engine{
tera: tera,
},
dns_resolver: dns_resolver,
asn_db: asn_db,
location_db: location_db,
config: config,
});
// Initalize axum server
@ -131,7 +176,7 @@ async fn main() {
println!("Starting Server ...");
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
axum::Server::bind(&listen_on)
.serve(app.into_make_service())
.await
.unwrap();