use chrono::{DateTime, Utc};
use crate::middleware::env_request::EnvRequestInfo;
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Endorsement {
pub endorser: String,
pub approved: bool,
pub timestamp: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EnvType {
Localhost,
RemoteWeb,
Extension,
Desktop,
P2P,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TrustLevel {
Untrusted,
Low,
Medium,
High,
Full,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RouteInfo {
pub hit_count: u64,
pub last_seen: DateTime<Utc>,
pub metadata: HashMap<String, String>,
}
use rusqlite::{ToSql, Result as SqlResult};
use rusqlite::types::{FromSql, FromSqlError, ValueRef, ToSqlOutput, Value};
impl ToSql for EnvType {
fn to_sql(&self) -> SqlResult<ToSqlOutput<'_>> {
Ok(ToSqlOutput::Owned(Value::Text(match self {
EnvType::Localhost => "Localhost",
EnvType::RemoteWeb => "RemoteWeb",
EnvType::Extension => "Extension",
EnvType::Desktop => "Desktop",
EnvType::P2P => "P2P",
}.to_string())))
}
}
impl FromSql for EnvType {
fn column_result(value: ValueRef<'_>) -> Result<Self, FromSqlError> {
match value.as_str()? {
"Localhost" => Ok(EnvType::Localhost),
"RemoteWeb" => Ok(EnvType::RemoteWeb),
"Extension" => Ok(EnvType::Extension),
"Desktop" => Ok(EnvType::Desktop),
"P2P" => Ok(EnvType::P2P),
_ => Err(FromSqlError::InvalidType),
}
}
}
impl ToSql for TrustLevel {
fn to_sql(&self) -> SqlResult<ToSqlOutput<'_>> {
Ok(ToSqlOutput::Owned(Value::Text(match self {
TrustLevel::Untrusted => "Untrusted",
TrustLevel::Low => "Low",
TrustLevel::Medium => "Medium",
TrustLevel::High => "High",
TrustLevel::Full => "Full",
}.to_string())))
}
}
impl FromSql for TrustLevel {
fn column_result(value: ValueRef<'_>) -> Result<Self, FromSqlError> {
match value.as_str()? {
"Untrusted" => Ok(TrustLevel::Untrusted),
"Low" => Ok(TrustLevel::Low),
"Medium" => Ok(TrustLevel::Medium),
"High" => Ok(TrustLevel::High),
"Full" => Ok(TrustLevel::Full),
_ => Err(FromSqlError::InvalidType),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EnvStatus {
Approved {
env_request: EnvRequestInfo,
},
Blocked {
env_request: EnvRequestInfo,
reason: String,
},
PendingApproval {
env_request: EnvRequestInfo,
reason: String,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvRequestLog {
pub timestamp: String,
pub method: String,
pub path: String,
pub ip: Option<String>,
pub host: String,
pub headers: String,
pub decision: String,
pub reason: String,
pub domain: String,
}
use crate::middleware::env_request::EnvRequest;
impl From<EnvRequest> for EnvRequestLog {
fn from(req: EnvRequest) -> Self {
match req {
EnvRequest::Http(http) => Self {
timestamp: Utc::now().to_rfc3339(),
method: http.method,
path: http.path,
ip: http.ip,
host: http.host.clone(),
headers: serde_json::to_string(&http.headers).unwrap_or_default(),
decision: String::new(),
reason: String::new(),
domain: http.headers.get("domain").cloned().unwrap_or_else(|| http.host.clone()),
},
EnvRequest::Ws(ws) => Self {
timestamp: Utc::now().to_rfc3339(),
method: "WS".into(),
path: "".into(),
ip: ws.ip,
host: ws.host.clone(),
headers: serde_json::to_string(&ws.headers).unwrap_or_default(),
decision: String::new(),
reason: String::new(),
domain: ws.headers.get("domain").cloned().unwrap_or_else(|| ws.host.clone()),
},
EnvRequest::Cli(_) => Self {
timestamp: Utc::now().to_rfc3339(),
method: "CLI".into(),
path: "".into(),
ip: None,
host: "localhost".into(),
headers: "{}".into(),
decision: String::new(),
reason: String::new(),
domain: "localhost".into(),
},
}
}
}