Files
tuliprox/src/model/api_proxy.rs
T

67 lines
1.8 KiB
Rust
Raw Normal View History

use std::collections::{HashSet};
use crate::exit;
use log::{error};
2023-10-07 16:10:50 +02:00
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub(crate) struct UserCredentials {
pub username: String,
pub password: String,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub(crate) struct TargetUser {
2023-10-08 19:54:59 +02:00
pub target: String,
2023-10-07 16:10:50 +02:00
pub credentials: Vec<UserCredentials>,
}
impl TargetUser {
2023-10-07 16:10:50 +02:00
pub fn get_target_name(&self, username: &str, password: &str) -> Option<String> {
2023-10-11 14:33:17 +02:00
if self.credentials.iter().any(|c| c.username.eq_ignore_ascii_case(username) && c.password.eq(password)) {
2023-10-08 19:54:59 +02:00
return Some(self.target.clone());
2023-10-07 16:10:50 +02:00
}
None
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub(crate) struct ServerInfo {
pub protocol: String,
pub ip: String,
pub http_port: u16,
pub https_port: u16,
pub rtmp_port: u16,
pub timezone: String,
pub message: String,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub(crate) struct ApiProxyConfig {
pub server: ServerInfo,
2023-10-08 19:54:59 +02:00
pub user: Vec<TargetUser>,
2023-10-07 16:10:50 +02:00
}
impl ApiProxyConfig {
2023-10-08 19:54:59 +02:00
pub fn prepare(&self) {
let mut usernames = HashSet::new();
for target_user in &self.user {
for user in &target_user.credentials {
if usernames.contains(&user.username) {
exit!("Non unique username found {}", &user.username);
} else {
usernames.insert(user.username.to_string());
}
}
}
2023-10-07 16:10:50 +02:00
}
pub fn get_target_name(&self, username: &str, password: &str) -> Option<String> {
2023-10-08 19:54:59 +02:00
for target_user in &self.user {
if let Some(target_name) = target_user.get_target_name(username, password) {
return Some(target_name);
};
}
None
2023-10-07 16:10:50 +02:00
}
}