10000 Add thread creation span management to ResCreationSpanManagementService by edginer · Pull Request #33 · edginer/eddist · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add thread creation span management to ResCreationSpanManage 8000 mentService #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ use redis::{aio::ConnectionManager, AsyncCommands};
pub struct ResCreationSpanManagementService {
redis_conn: ConnectionManager,
span: u64,
thread_span: u64,
}

impl ResCreationSpanManagementService {
pub fn new(redis_conn: ConnectionManager, span: u64) -> Self {
Self { redis_conn, span }
pub fn new(redis_conn: ConnectionManager, span: u64, thread_span: u64) -> Self {
Self {
redis_conn,
span,
thread_span,
}
}

/// Check if the timestamp is within the creation restriction span for the given authed token or ip address.
Expand Down Expand Up @@ -99,4 +104,97 @@ impl ResCreationSpanManagementService {
.await
.unwrap();
}

/// Check if the timestamp is within the thread creation restriction span for the given authed token or ip address.
pub async fn is_thread_within_creation_span(
&self,
authed_token: &str,
ip_adr: &str,
timestamp: u64,
) -> bool {
self.is_within_thread_creation_span_by_authed_token(authed_token, timestamp)
.await
|| self
.is_within_thread_creation_span_by_ip(ip_adr, timestamp)
.await
}

/// Check if the timestamp is within the thread creation restriction span for the given authed token.
async fn is_within_thread_creation_span_by_authed_token(
&self,
authed_token: &str,
timestamp: u64,
) -> bool {
if self.thread_span == 0 {
return false;
}

let mut redis_conn = self.redis_conn.clone();

let key = format!("thread_creation_span:{authed_token}");
let before_res_time = redis_conn.get::<_, u64>(&key).await.unwrap_or(0);

timestamp - before_res_time < self.thread_span
}

/// Check if the timestamp is within the thread creation restriction span for the given ip address.
async fn is_within_thread_creation_span_by_ip(&self, ip: &str, timestamp: u64) -> bool {
if self.thread_span == 0 {
return false;
}

let mut redis_conn = self.redis_conn.clone();

let key = format!("thread_creation_span_ip:{ip}");
let before_res_time = redis_conn.get::<_, u64>(&key).await.unwrap_or(0);

timestamp - before_res_time < self.thread_span
}

/// Update the last thread creation time for the given authed token and ip address.
pub async fn update_last_thread_creation_time(
&self,
authed_token: &str,
ip: &str,
timestamp: u64,
) {
self.update_last_thread_creation_time_authed_token(authed_token, timestamp)
.await;
self.update_last_thread_creation_time_by_ip(ip, timestamp)
.await;
}

/// Update the last thread creation time for the given authed token.
async fn update_last_thread_creation_time_authed_token(
&self,
authed_token: &str,
timestamp: u64,
) {
if self.thread_span == 0 {
return;
}

let mut redis_conn = self.redis_conn.clone();

let key = format!("res_creation_span:{authed_token}");
redis_conn
.set_ex::<_, _, ()>(key, timestamp, self.thread_span)
.await
.unwrap();
}

/// Update the last thread creation time for the given ip address.
async fn update_last_thread_creation_time_by_ip(&self, ip: &str, timestamp: u64) {
if self.thread_span == 0 {
return;
}

let mut redis_conn = self.redis_conn.clone();

let key = format!("res_creation_span_ip:{ip}");
redis_conn
.set_ex::<_, _, ()>(key, timestamp, self.thread_span)
.await
.unwrap();
}
}
5 changes: 5 additions & 0 deletions eddist-server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub enum BbsCgiError {
#[error("短期間にスレ立てすぎです (Lv{tinker_level}は{span_sec}秒以内に1回スレを立てることができます)")]
TooManyCreatingThread { tinker_level: u32, span_sec: i32 },

#[error("短期間にスレ立てすぎです")]
TooManyCreatingThreadWithoutTinker,

#[error("初回書き込み時にはスレッドを立てることができません")]
TmpCanNotCreateThread,

Expand All @@ -82,6 +85,7 @@ impl BbsCgiError {
BbsCgiError::ContentEmpty(_) => StatusCode::OK,
BbsCgiError::TooManyCreatingRes(_) => StatusCode::OK,
BbsCgiError::TooManyCreatingThread { .. } => StatusCode::OK,
BbsCgiError::TooManyCreatingThreadWithoutTinker => StatusCode::OK,
BbsCgiError::TmpCanNotCreateThread => StatusCode::OK,
BbsCgiError::ReadOnlyBoard => StatusCode::OK,
BbsCgiError::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
Expand All @@ -103,6 +107,7 @@ impl BbsCgiError {
BbsCgiError::ContentEmpty(_) => "ContentEmpty",
BbsCgiError::TooManyCreatingRes(_) => "TooManyCreatingRes",
BbsCgiError::TooManyCreatingThread { .. } => "TooManyCreatingThread",
BbsCgiError::TooManyCreatingThreadWithoutTinker => "TooManyCreatingThreadWithoutTinker",
BbsCgiError::TmpCanNotCreateThread => "TmpCanNotCreateThread",
BbsCgiError::ReadOnlyBoard => "ReadOnlyBoard",
BbsCgiError::Other(_) => "InternalError",
Expand Down
1 change: 1 addition & 0 deletions eddist-server/src/services/res_creation_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl<T: BbsRepository + Clone, P: PubRepository>
let res_span_svc = ResCreationSpanManagementService::new(
redis_conn.clone(),
board_info.base_response_creation_span_sec as u64,
board_info.base_thread_creation_span_sec as u64, // ignorable
);
if res_span_svc
.is_within_creation_span(
Expand Down
15 changes: 15 additions & 0 deletions eddist-server/src/services/thread_creation_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl<T: BbsRepository + Clone>
let res_span_svc = ResCreationSpanManagementService::new(
redis_conn.clone(),
board_info.base_response_creation_span_sec as u64,
board_info.base_thread_creation_span_sec as u64,
);
if res_span_svc
.is_within_creation_span(&authed_token.token, &input.ip_addr, unix_time as u64)
Expand All @@ -148,6 +149,13 @@ impl<T: BbsRepository + Clone>
board_info.base_response_creation_span_sec,
));
};
if res_span_svc
.is_thread_within_creation_span(&authed_token.token, &input.ip_addr, unix_time as u64)
.await
{
return Err(BbsCgiError::TooManyCreatingThreadWithoutTinker);
}

let ng_words = NgWordReadingService::new(self.0.clone(), redis_conn.clone())
.get_ng_words(&input.board_key)
.await?;
Expand Down Expand Up @@ -179,6 +187,13 @@ impl<T: BbsRepository + Clone>
unix_time as u64,
)
.await;
res_span_svc
.update_last_thread_creation_time(
&authed_token_clone,
&input.ip_addr,
unix_time as u64,
)
.await;
redis_conn
.send_packed_command(&Cmd::expire(
format!("thread:{}:{unix_time}", input.board_key),
Expand Down
Loading
0