mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-09-13 11:04:25 +08:00
feat: 深入适配 RK628D CSI 采集卡的设备识别、参数读取、自恢复和音频采集
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
pub mod middleware;
|
||||
mod password;
|
||||
mod rfc3339;
|
||||
mod session;
|
||||
mod user;
|
||||
|
||||
|
||||
13
src/auth/rfc3339.rs
Normal file
13
src/auth/rfc3339.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
//! RFC3339 strings in SQLite; structs use `time::serde::rfc3339`.
|
||||
|
||||
use time::format_description::well_known::Rfc3339;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
/// Parse DB text; bad input → `now_utc()`.
|
||||
pub fn parse(s: &str) -> OffsetDateTime {
|
||||
OffsetDateTime::parse(s, &Rfc3339).unwrap_or_else(|_| OffsetDateTime::now_utc())
|
||||
}
|
||||
|
||||
pub fn format(dt: OffsetDateTime) -> String {
|
||||
dt.format(&Rfc3339).expect("RFC3339 format")
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
use chrono::{DateTime, Duration, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use time::{Duration, OffsetDateTime};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::rfc3339;
|
||||
use crate::error::Result;
|
||||
|
||||
/// Session data
|
||||
@@ -10,15 +11,17 @@ use crate::error::Result;
|
||||
pub struct Session {
|
||||
pub id: String,
|
||||
pub user_id: String,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub expires_at: DateTime<Utc>,
|
||||
#[serde(with = "time::serde::rfc3339")]
|
||||
pub created_at: OffsetDateTime,
|
||||
#[serde(with = "time::serde::rfc3339")]
|
||||
pub expires_at: OffsetDateTime,
|
||||
pub data: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// Check if session is expired
|
||||
pub fn is_expired(&self) -> bool {
|
||||
Utc::now() > self.expires_at
|
||||
OffsetDateTime::now_utc() > self.expires_at
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,11 +43,12 @@ impl SessionStore {
|
||||
|
||||
/// Create a new session
|
||||
pub async fn create(&self, user_id: &str) -> Result<Session> {
|
||||
let now = OffsetDateTime::now_utc();
|
||||
let session = Session {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
user_id: user_id.to_string(),
|
||||
created_at: Utc::now(),
|
||||
expires_at: Utc::now() + self.default_ttl,
|
||||
created_at: now,
|
||||
expires_at: now + self.default_ttl,
|
||||
data: None,
|
||||
};
|
||||
|
||||
@@ -56,8 +60,8 @@ impl SessionStore {
|
||||
)
|
||||
.bind(&session.id)
|
||||
.bind(&session.user_id)
|
||||
.bind(session.created_at.to_rfc3339())
|
||||
.bind(session.expires_at.to_rfc3339())
|
||||
.bind(rfc3339::format(session.created_at))
|
||||
.bind(rfc3339::format(session.expires_at))
|
||||
.bind(session.data.as_ref().map(|d| d.to_string()))
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
@@ -79,12 +83,8 @@ impl SessionStore {
|
||||
let session = Session {
|
||||
id,
|
||||
user_id,
|
||||
created_at: DateTime::parse_from_rfc3339(&created_at)
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.unwrap_or_else(|_| Utc::now()),
|
||||
expires_at: DateTime::parse_from_rfc3339(&expires_at)
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.unwrap_or_else(|_| Utc::now()),
|
||||
created_at: rfc3339::parse(&created_at),
|
||||
expires_at: rfc3339::parse(&expires_at),
|
||||
data: data.and_then(|d| serde_json::from_str(&d).ok()),
|
||||
};
|
||||
|
||||
@@ -110,7 +110,7 @@ impl SessionStore {
|
||||
|
||||
/// Delete all expired sessions
|
||||
pub async fn cleanup_expired(&self) -> Result<u64> {
|
||||
let now = Utc::now().to_rfc3339();
|
||||
let now = rfc3339::format(OffsetDateTime::now_utc());
|
||||
let result = sqlx::query("DELETE FROM sessions WHERE expires_at < ?1")
|
||||
.bind(now)
|
||||
.execute(&self.pool)
|
||||
@@ -145,9 +145,9 @@ impl SessionStore {
|
||||
|
||||
/// Extend session expiration
|
||||
pub async fn extend(&self, session_id: &str) -> Result<()> {
|
||||
let new_expires = Utc::now() + self.default_ttl;
|
||||
let new_expires = OffsetDateTime::now_utc() + self.default_ttl;
|
||||
sqlx::query("UPDATE sessions SET expires_at = ?1 WHERE id = ?2")
|
||||
.bind(new_expires.to_rfc3339())
|
||||
.bind(rfc3339::format(new_expires))
|
||||
.bind(session_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use time::OffsetDateTime;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::password::{hash_password, verify_password};
|
||||
use super::rfc3339;
|
||||
use crate::error::{AppError, Result};
|
||||
|
||||
/// User row type from database
|
||||
@@ -16,8 +17,10 @@ pub struct User {
|
||||
pub username: String,
|
||||
#[serde(skip_serializing)]
|
||||
pub password_hash: String,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
#[serde(with = "time::serde::rfc3339")]
|
||||
pub created_at: OffsetDateTime,
|
||||
#[serde(with = "time::serde::rfc3339")]
|
||||
pub updated_at: OffsetDateTime,
|
||||
}
|
||||
|
||||
impl User {
|
||||
@@ -28,12 +31,8 @@ impl User {
|
||||
id,
|
||||
username,
|
||||
password_hash,
|
||||
created_at: DateTime::parse_from_rfc3339(&created_at)
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.unwrap_or_else(|_| Utc::now()),
|
||||
updated_at: DateTime::parse_from_rfc3339(&updated_at)
|
||||
.map(|dt| dt.with_timezone(&Utc))
|
||||
.unwrap_or_else(|_| Utc::now()),
|
||||
created_at: rfc3339::parse(&created_at),
|
||||
updated_at: rfc3339::parse(&updated_at),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,7 +60,7 @@ impl UserStore {
|
||||
}
|
||||
|
||||
let password_hash = hash_password(password)?;
|
||||
let now = Utc::now();
|
||||
let now = OffsetDateTime::now_utc();
|
||||
let user = User {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
username: username.to_string(),
|
||||
@@ -79,8 +78,8 @@ impl UserStore {
|
||||
.bind(&user.id)
|
||||
.bind(&user.username)
|
||||
.bind(&user.password_hash)
|
||||
.bind(user.created_at.to_rfc3339())
|
||||
.bind(user.updated_at.to_rfc3339())
|
||||
.bind(rfc3339::format(user.created_at))
|
||||
.bind(rfc3339::format(user.updated_at))
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
@@ -128,12 +127,12 @@ impl UserStore {
|
||||
/// Update user password
|
||||
pub async fn update_password(&self, user_id: &str, new_password: &str) -> Result<()> {
|
||||
let password_hash = hash_password(new_password)?;
|
||||
let now = Utc::now();
|
||||
let now = OffsetDateTime::now_utc();
|
||||
|
||||
let result =
|
||||
sqlx::query("UPDATE users SET password_hash = ?1, updated_at = ?2 WHERE id = ?3")
|
||||
.bind(&password_hash)
|
||||
.bind(now.to_rfc3339())
|
||||
.bind(rfc3339::format(now))
|
||||
.bind(user_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
@@ -156,10 +155,10 @@ impl UserStore {
|
||||
}
|
||||
}
|
||||
|
||||
let now = Utc::now();
|
||||
let now = OffsetDateTime::now_utc();
|
||||
let result = sqlx::query("UPDATE users SET username = ?1, updated_at = ?2 WHERE id = ?3")
|
||||
.bind(new_username)
|
||||
.bind(now.to_rfc3339())
|
||||
.bind(rfc3339::format(now))
|
||||
.bind(user_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Reference in New Issue
Block a user