fix: crash with invalid cookie (#131)

This commit is contained in:
Xinrea
2025-07-01 00:24:11 +08:00
committed by GitHub
parent 151e1bdb8a
commit 6cad3d6afb
3 changed files with 51 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ use crate::recorder::bilibili::client::{QrInfo, QrStatus};
use crate::state::State;
use crate::state_type;
use hyper::header::HeaderValue;
#[cfg(feature = "gui")]
use tauri::State as TauriState;
@@ -20,6 +21,10 @@ pub async fn add_account(
platform: String,
cookies: &str,
) -> Result<AccountRow, String> {
// check if cookies is valid
if let Err(e) = cookies.parse::<HeaderValue>() {
return Err(format!("Invalid cookies: {}", e));
}
let account = state.db.add_account(&platform, cookies).await?;
if platform == "bilibili" {
let account_info = state.client.get_user_info(&account, account.uid).await?;

View File

@@ -214,7 +214,11 @@ impl BiliClient {
pub async fn logout(&self, account: &AccountRow) -> Result<(), BiliClientError> {
let url = "https://passport.bilibili.com/login/exit/v2";
let mut headers = self.headers.clone();
headers.insert("cookie", account.cookies.parse().unwrap());
if let Ok(cookies) = account.cookies.parse() {
headers.insert("cookie", cookies);
} else {
return Err(BiliClientError::InvalidCookie);
}
let params = [("csrf", account.csrf.clone())];
let _ = self
.client
@@ -241,7 +245,11 @@ impl BiliClient {
});
let params = self.get_sign(params).await?;
let mut headers = self.headers.clone();
headers.insert("cookie", account.cookies.parse().unwrap());
if let Ok(cookies) = account.cookies.parse() {
headers.insert("cookie", cookies);
} else {
return Err(BiliClientError::InvalidCookie);
}
let resp = self
.client
.get(format!(
@@ -283,7 +291,11 @@ impl BiliClient {
room_id: u64,
) -> Result<RoomInfo, BiliClientError> {
let mut headers = self.headers.clone();
headers.insert("cookie", account.cookies.parse().unwrap());
if let Ok(cookies) = account.cookies.parse() {
headers.insert("cookie", cookies);
} else {
return Err(BiliClientError::InvalidCookie);
}
let response = self
.client
.get(format!(
@@ -359,7 +371,11 @@ impl BiliClient {
url: &String,
) -> Result<String, BiliClientError> {
let mut headers = self.headers.clone();
headers.insert("cookie", account.cookies.parse().unwrap());
if let Ok(cookies) = account.cookies.parse() {
headers.insert("cookie", cookies);
} else {
return Err(BiliClientError::InvalidCookie);
}
let response = self
.client
.get(url.to_owned())
@@ -476,7 +492,11 @@ impl BiliClient {
video_file: &Path,
) -> Result<PreuploadResponse, BiliClientError> {
let mut headers = self.headers.clone();
headers.insert("cookie", account.cookies.parse().unwrap());
if let Ok(cookies) = account.cookies.parse() {
headers.insert("cookie", cookies);
} else {
return Err(BiliClientError::InvalidCookie);
}
let url = format!(
"https://member.bilibili.com/preupload?name={}&r=upos&profile=ugcfx/bup",
video_file.file_name().unwrap().to_str().unwrap()
@@ -715,7 +735,11 @@ impl BiliClient {
video: &profile::Video,
) -> Result<VideoSubmitData, BiliClientError> {
let mut headers = self.headers.clone();
headers.insert("cookie", account.cookies.parse().unwrap());
if let Ok(cookies) = account.cookies.parse() {
headers.insert("cookie", cookies);
} else {
return Err(BiliClientError::InvalidCookie);
}
let url = format!(
"https://member.bilibili.com/x/vu/web/add/v3?ts={}&csrf={}",
chrono::Local::now().timestamp(),
@@ -761,7 +785,11 @@ impl BiliClient {
chrono::Local::now().timestamp(),
);
let mut headers = self.headers.clone();
headers.insert("cookie", account.cookies.parse().unwrap());
if let Ok(cookies) = account.cookies.parse() {
headers.insert("cookie", cookies);
} else {
return Err(BiliClientError::InvalidCookie);
}
let params = [("csrf", account.csrf.clone()), ("cover", cover.to_string())];
match self
.client
@@ -799,7 +827,11 @@ impl BiliClient {
) -> Result<(), BiliClientError> {
let url = "https://api.live.bilibili.com/msg/send".to_string();
let mut headers = self.headers.clone();
headers.insert("cookie", account.cookies.parse().unwrap());
if let Ok(cookies) = account.cookies.parse() {
headers.insert("cookie", cookies);
} else {
return Err(BiliClientError::InvalidCookie);
}
let params = [
("bubble", "0"),
("msg", message),
@@ -829,7 +861,11 @@ impl BiliClient {
) -> Result<Vec<response::Typelist>, BiliClientError> {
let url = "https://member.bilibili.com/x/vupre/web/archive/pre?lang=cn";
let mut headers = self.headers.clone();
headers.insert("cookie", account.cookies.parse().unwrap());
if let Ok(cookies) = account.cookies.parse() {
headers.insert("cookie", cookies);
} else {
return Err(BiliClientError::InvalidCookie);
}
let resp: GeneralResponse = self
.client
.get(url)

View File

@@ -10,6 +10,7 @@ custom_error! {pub BiliClientError
InvalidUrl = "Invalid url",
InvalidFormat = "Invalid stream format",
InvalidStream = "Invalid stream",
InvalidCookie = "Invalid cookie",
UploadError{err: String} = "Upload error: {err}",
UploadCancelled = "Upload was cancelled by user",
EmptyCache = "Empty cache",