mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-05 15:39:07 +08:00
fix: 修复 chromium 系浏览器创建 WebSocket 失败的问题 (#395)
This commit is contained in:
@@ -39,21 +39,28 @@ pub fn router() -> Router {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 中间件:验证请求头中的 Authorization 是否与配置中的 auth_token 匹配
|
/// 中间件:使用 auth token 对请求进行身份验证
|
||||||
pub async fn auth(headers: HeaderMap, request: Request, next: Next) -> Result<Response, StatusCode> {
|
pub async fn auth(mut headers: HeaderMap, request: Request, next: Next) -> Result<Response, StatusCode> {
|
||||||
let config = VersionedConfig::get().load();
|
let config = VersionedConfig::get().load();
|
||||||
let token = config.auth_token.as_str();
|
let token = config.auth_token.as_str();
|
||||||
if headers
|
if headers
|
||||||
.get("Authorization")
|
.get("Authorization")
|
||||||
.and_then(|v| v.to_str().ok())
|
.and_then(|v| v.to_str().ok())
|
||||||
.is_some_and(|s| s == token)
|
.is_some_and(|s| s == token)
|
||||||
|| headers
|
{
|
||||||
.get("Sec-WebSocket-Protocol")
|
return Ok(next.run(request).await);
|
||||||
.and_then(|v| v.to_str().ok())
|
}
|
||||||
|
if let Some(protocol) = headers.remove("Sec-WebSocket-Protocol") {
|
||||||
|
if protocol
|
||||||
|
.to_str()
|
||||||
|
.ok()
|
||||||
.and_then(|s| BASE64_URL_SAFE_NO_PAD.decode(s).ok())
|
.and_then(|s| BASE64_URL_SAFE_NO_PAD.decode(s).ok())
|
||||||
.is_some_and(|s| s == token.as_bytes())
|
.is_some_and(|s| s == token.as_bytes())
|
||||||
{
|
{
|
||||||
return Ok(next.run(request).await);
|
let mut resp = next.run(request).await;
|
||||||
|
resp.headers_mut().insert("Sec-WebSocket-Protocol", protocol);
|
||||||
|
return Ok(resp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(ApiResponse::<()>::unauthorized("auth token does not match").into_response())
|
Ok(ApiResponse::<()>::unauthorized("auth token does not match").into_response())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user