A cloudflare backed DDNS service written in Rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.4 KiB

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ServerConfig {
pub host: Option<String>,
pub port: Option<i16>,
pub workers: Option<usize>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct CloudflareDomainConfig {
pub domain: String,
pub zone_id: String,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct CloudflareConfig {
pub domains: Vec<CloudflareDomainConfig>,
pub key: String,
pub email: String,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct DDNSDomain {
pub domain: String,
pub subdomains: Vec<String>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct DDNSConfig {
pub domains: Vec<DDNSDomain>
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct UserRootConfig {
pub domain: String,
pub root: String,
pub zones: Vec<String>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct UserConfig {
pub username: String,
pub token: String,
pub roots: Vec<UserRootConfig>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Config {
pub server: ServerConfig,
pub cloudflare: CloudflareConfig,
pub ddns: DDNSConfig,
pub users: Vec<UserConfig>,
}
impl Config {
pub fn get_user(&self, username: &str) -> Option<&UserConfig> {
for user in &self.users {
if user.username == username {
return Some(user);
}
}
None
}
pub fn get_users_for_root_and_zone(&self, root: &str, zone: &str) -> Option<Vec<&UserConfig>> {
let mut users: Vec<&UserConfig> = Vec::new();
for user in &self.users {
if user.has_root_and_zone(root, zone) {
users.push(user);
}
}
if users.len() > 0 { Some(users) } else { None }
}
pub fn is_valid_username_and_token(&self, username: &str, token: &str) -> bool {
for user in &self.users {
if user.username == username && user.token == token {
return true;
}
}
return false;
}
}
impl UserConfig {
pub fn has_root_and_zone(&self, search_root: &str, search_zone: &str) -> bool {
for root in &self.roots {
let zone_match = &search_zone.to_string();
if root.root == search_root && root.zones.contains(zone_match) {
return true;
}
}
false
}
}