|
@ -1,36 +1,50 @@ |
|
|
use yaml_rust::{Yaml, YamlLoader};
|
|
|
|
|
|
|
|
|
use serde_yaml;
|
|
|
use std::error::Error;
|
|
|
use std::error::Error;
|
|
|
use std::fmt;
|
|
|
use std::fmt;
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
pub struct ServerConfig {
|
|
|
pub struct ServerConfig {
|
|
|
pub host: String,
|
|
|
pub host: String,
|
|
|
pub port: i64,
|
|
|
pub port: i64,
|
|
|
pub workers: i64,
|
|
|
|
|
|
|
|
|
pub workers: i64 |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
pub struct CloudflareDomainConfig {
|
|
|
pub struct CloudflareDomainConfig {
|
|
|
pub domain: String,
|
|
|
pub domain: String,
|
|
|
pub zone_id: String,
|
|
|
|
|
|
|
|
|
pub zone_id: String |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
pub struct CloudflareConfig {
|
|
|
pub struct CloudflareConfig {
|
|
|
pub domains: Vec<CloudflareDomainConfig>,
|
|
|
pub domains: Vec<CloudflareDomainConfig>,
|
|
|
pub key: String,
|
|
|
pub key: String,
|
|
|
|
|
|
pub email: String |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
pub struct DDNSDomain {
|
|
|
pub struct DDNSDomain {
|
|
|
pub domain: String,
|
|
|
pub domain: String,
|
|
|
pub subdomains: Vec<String>,
|
|
|
|
|
|
|
|
|
pub subdomains: Vec<String>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
pub struct DDNSConfig {
|
|
|
pub struct DDNSConfig {
|
|
|
pub domains: Vec<DDNSDomain>
|
|
|
pub domains: Vec<DDNSDomain>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
pub struct Config {
|
|
|
pub struct Config {
|
|
|
pub server: ServerConfig,
|
|
|
pub server: ServerConfig,
|
|
|
pub cloudflare: CloudflareConfig,
|
|
|
pub cloudflare: CloudflareConfig,
|
|
|
pub ddns: DDNSConfig,
|
|
|
|
|
|
|
|
|
pub ddns: DDNSConfig
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_default_config() -> Config {
|
|
|
fn get_default_config() -> Config {
|
|
@ -41,6 +55,7 @@ fn get_default_config() -> Config { |
|
|
workers: 4
|
|
|
workers: 4
|
|
|
},
|
|
|
},
|
|
|
cloudflare: CloudflareConfig {
|
|
|
cloudflare: CloudflareConfig {
|
|
|
|
|
|
email: String::from("something@something.com"),
|
|
|
key: String::from("IAmNotAKey"),
|
|
|
key: String::from("IAmNotAKey"),
|
|
|
domains: vec![
|
|
|
domains: vec![
|
|
|
CloudflareDomainConfig {
|
|
|
CloudflareDomainConfig {
|
|
@ -73,66 +88,28 @@ impl fmt::Display for ConfigError { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn create_server_config_from_yaml(yaml: &Yaml) -> Result<ServerConfig, ConfigError> {
|
|
|
|
|
|
Result::Ok(ServerConfig {
|
|
|
|
|
|
host: String::from(yaml["host"].as_str().unwrap()),
|
|
|
|
|
|
port: yaml["port"].as_i64().unwrap(),
|
|
|
|
|
|
workers: yaml["workers"].as_i64().unwrap(),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn create_cloudflare_domain_config_from_yaml(yaml: &Yaml) -> Result<CloudflareDomainConfig, ConfigError> {
|
|
|
|
|
|
Result::Ok(CloudflareDomainConfig {
|
|
|
|
|
|
domain: String::from(yaml["domain"].as_str().unwrap()),
|
|
|
|
|
|
zone_id: String::from(yaml["zone_id"].as_str().unwrap()),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn create_cloudflare_config_from_yaml(yaml: &Yaml) -> Result<CloudflareConfig, ConfigError> {
|
|
|
|
|
|
Result::Ok(CloudflareConfig {
|
|
|
|
|
|
key: String::from(yaml["key"].as_str().unwrap()),
|
|
|
|
|
|
domains: yaml["domains"].as_vec().unwrap().into_iter().map(|y| create_cloudflare_domain_config_from_yaml(&y).unwrap()).collect(),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn create_ddns_domain_config_from_yaml(yaml: &Yaml) -> Result<DDNSDomain, ConfigError> {
|
|
|
|
|
|
Result::Ok(DDNSDomain {
|
|
|
|
|
|
domain: String::from(yaml["domain"].as_str().unwrap()),
|
|
|
|
|
|
subdomains: yaml["subdomains"].as_vec().unwrap().into_iter().map(|y| String::from(y.as_str().unwrap())).collect(),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn create_ddns_config_from_yaml(yaml: &Yaml) -> Result<DDNSConfig, ConfigError> {
|
|
|
|
|
|
Result::Ok(DDNSConfig {
|
|
|
|
|
|
domains: yaml["domains"].as_vec().unwrap().into_iter().map(|y| create_ddns_domain_config_from_yaml(&y).unwrap()).collect()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn create_config_from_yaml(config_yaml: Vec<Yaml>) -> Result<Config, ConfigError> {
|
|
|
|
|
|
let config = &config_yaml[0];
|
|
|
|
|
|
let server_config = match create_server_config_from_yaml(&config["server"]) {
|
|
|
|
|
|
Ok(v) => v,
|
|
|
|
|
|
Err(e) => return Result::Err(e)
|
|
|
|
|
|
};
|
|
|
|
|
|
let cloudflare_config = match create_cloudflare_config_from_yaml(&config["cloudflare"]) {
|
|
|
|
|
|
Ok(v) => v,
|
|
|
|
|
|
Err(e) => return Result::Err(e)
|
|
|
|
|
|
};
|
|
|
|
|
|
let ddns_config = match create_ddns_config_from_yaml(&config["ddns"]) {
|
|
|
|
|
|
Ok(v) => v,
|
|
|
|
|
|
Err(e) => return Result::Err(e)
|
|
|
|
|
|
};
|
|
|
|
|
|
Result::Ok(Config {
|
|
|
|
|
|
server: server_config,
|
|
|
|
|
|
cloudflare: cloudflare_config,
|
|
|
|
|
|
ddns: ddns_config,
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
fn read_config(yaml_str: &str) -> Result<Config, ConfigError> {
|
|
|
|
|
|
match serde_yaml::from_str(yaml_str) {
|
|
|
|
|
|
Ok(v) => Result::Ok(v),
|
|
|
|
|
|
Err(_e) => Result::Err(ConfigError{})
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn load_config(path: &str) -> Result<Config, ConfigError> {
|
|
|
pub fn load_config(path: &str) -> Result<Config, ConfigError> {
|
|
|
let yaml_load = YamlLoader::load_from_str(path);
|
|
|
|
|
|
match yaml_load {
|
|
|
|
|
|
Ok(config_yaml) => create_config_from_yaml(config_yaml),
|
|
|
|
|
|
|
|
|
match File::open(path) {
|
|
|
|
|
|
Ok(mut file) => {
|
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
|
|
match file.read_to_string(&mut contents) {
|
|
|
|
|
|
Ok(c) => {
|
|
|
|
|
|
if c > 0 {
|
|
|
|
|
|
read_config(&contents)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Result::Err(ConfigError{})
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
Err(_e) => Result::Err(ConfigError{})
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
Err(_e) => Result::Err(ConfigError{})
|
|
|
Err(_e) => Result::Err(ConfigError{})
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|