Skip to content

Commit 7827e08

Browse files
committed
update tower-http to 0.6
1 parent af946b3 commit 7827e08

File tree

5 files changed

+44
-40
lines changed

5 files changed

+44
-40
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/auth/src/lib.rs

+22-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use std::{collections::HashSet, marker::PhantomData};
2-
31
use anyhow::{anyhow, Error};
42
use headers::authorization::{Bearer, Credentials};
53
use http::{header, Request, Response, StatusCode};
6-
use http_body::Body;
74
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation};
5+
use std::{collections::HashSet, marker::PhantomData};
86
use tower_http::validate_request::ValidateRequest;
9-
7+
// 添加这个导入
108
use crate::claims::Claims;
119

1210
pub mod access;
@@ -33,14 +31,11 @@ impl Keys {
3331
pub struct ManyValidate<ResBody> {
3432
tokens: HashSet<String>,
3533
decoding: DecodingKey,
36-
_ty: PhantomData<fn() -> ResBody>,
34+
_ty: PhantomData<ResBody>,
3735
}
3836

3937
impl<ResBody> ManyValidate<ResBody> {
40-
pub fn new(secret: String, tokens: Vec<String>) -> Self
41-
where
42-
ResBody: Body + Default,
43-
{
38+
pub fn new(secret: String, tokens: Vec<String>) -> Self {
4439
Self {
4540
tokens: tokens.into_iter().collect(),
4641
decoding: DecodingKey::from_secret(secret.as_bytes()),
@@ -59,11 +54,10 @@ impl<ResBody> Clone for ManyValidate<ResBody> {
5954
}
6055
}
6156

62-
impl<B, ResBody> ValidateRequest<B> for ManyValidate<ResBody>
63-
where
64-
ResBody: Body + Default,
65-
{
66-
type ResponseBody = ResBody;
57+
// 修改这个实现,添加必要的trait bounds
58+
impl<B: Default> ValidateRequest<B> for ManyValidate<B> {
59+
// 添加关联类型
60+
type ResponseBody = B;
6761

6862
fn validate(&mut self, request: &mut Request<B>) -> Result<(), Response<Self::ResponseBody>> {
6963
if self.tokens.is_empty() {
@@ -74,10 +68,10 @@ where
7468
});
7569
return Ok(());
7670
}
77-
(match request.headers().get(header::AUTHORIZATION) {
71+
72+
match request.headers().get(header::AUTHORIZATION) {
7873
Some(auth_header) => match Bearer::decode(auth_header) {
7974
Some(bearer) if self.tokens.contains(bearer.token()) => {
80-
// Static token is max permissions
8175
request.extensions_mut().insert(Claims {
8276
id: ANY_ID.to_string(),
8377
exp: 0,
@@ -91,18 +85,21 @@ where
9185
request.extensions_mut().insert(token_data.claims);
9286
Ok(())
9387
}
94-
_ => Err(()),
88+
_ => Err(Response::builder()
89+
.status(StatusCode::UNAUTHORIZED)
90+
.body(B::default())
91+
.unwrap()),
9592
}
9693
}
97-
_ => Err(()),
94+
_ => Err(Response::builder()
95+
.status(StatusCode::UNAUTHORIZED)
96+
.body(B::default())
97+
.unwrap()),
9898
},
99-
_ => Err(()),
100-
})
101-
.map_err(|_| {
102-
Response::builder()
99+
_ => Err(Response::builder()
103100
.status(StatusCode::UNAUTHORIZED)
104-
.body(ResBody::default())
105-
.unwrap()
106-
})
101+
.body(B::default())
102+
.unwrap()),
103+
}
107104
}
108105
}

libs/rtsp/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ pub fn filter_sdp(
371371
.retain(|fmt| fmt == &video_codec.payload_type.to_string());
372372
media.attributes.retain(|attr| {
373373
attr.key == "rtpmap"
374-
&& attr.value.as_ref().map_or(false, |v| {
374+
&& attr.value.as_ref().is_some_and(|v| {
375375
v.starts_with(&video_codec.payload_type.to_string())
376376
})
377377
});
@@ -391,7 +391,7 @@ pub fn filter_sdp(
391391
.retain(|fmt| fmt == &audio_codec.payload_type.to_string());
392392
media.attributes.retain(|attr| {
393393
attr.key == "rtpmap"
394-
&& attr.value.as_ref().map_or(false, |v| {
394+
&& attr.value.as_ref().is_some_and(|v| {
395395
v.starts_with(&audio_codec.payload_type.to_string())
396396
})
397397
});
@@ -402,10 +402,10 @@ pub fn filter_sdp(
402402
});
403403
}
404404
}
405-
405+
406406
true
407407
});
408-
408+
409409
session.attributes.retain(|attr| {
410410
!attr.key.starts_with("group")
411411
&& !attr.key.starts_with("fingerprint")

liveion/src/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use tracing::{error, info_span, Level};
1010

1111
use auth::{access::access_middleware, ManyValidate};
1212
use error::AppError;
13-
13+
// 在 use 声明中添加
1414
use crate::config::Config;
1515
use crate::route::{admin, session, whep, whip, AppState};
16+
use axum::body::Body;
1617

1718
use stream::manager::Manager;
1819

@@ -42,8 +43,11 @@ where
4243
stream_manager: Arc::new(Manager::new(cfg.clone()).await),
4344
config: cfg.clone(),
4445
};
45-
let auth_layer =
46-
ValidateRequestHeaderLayer::custom(ManyValidate::new(cfg.auth.secret, cfg.auth.tokens));
46+
// 修改 auth_layer 的创建
47+
let auth_layer = ValidateRequestHeaderLayer::custom(ManyValidate::<Body>::new(
48+
cfg.auth.secret,
49+
cfg.auth.tokens,
50+
));
4751
let app = Router::new()
4852
.merge(
4953
whip::route()

liveman/src/lib.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use std::{future::Future, time::Duration};
22

3+
use crate::admin::{authorize, token};
4+
use crate::config::Config;
5+
use crate::store::{Node, NodeKind, Storage};
6+
use auth::{access::access_middleware, ManyValidate};
7+
use axum::body::Body;
38
use axum::{extract::Request, middleware, response::IntoResponse, routing::post, Router};
49
use http::{StatusCode, Uri};
510
use tokio::net::TcpListener;
@@ -8,11 +13,6 @@ use tower_http::{
813
};
914
use tracing::{error, info, info_span};
1015

11-
use crate::admin::{authorize, token};
12-
use crate::config::Config;
13-
use crate::store::{Node, NodeKind, Storage};
14-
use auth::{access::access_middleware, ManyValidate};
15-
1616
#[cfg(feature = "webui")]
1717
#[derive(rust_embed::RustEmbed)]
1818
#[folder = "../assets/liveman/"]
@@ -145,8 +145,11 @@ where
145145
storage: store,
146146
};
147147

148-
let auth_layer =
149-
ValidateRequestHeaderLayer::custom(ManyValidate::new(cfg.auth.secret, cfg.auth.tokens));
148+
// 修改 auth_layer 的创建
149+
let auth_layer = ValidateRequestHeaderLayer::custom(ManyValidate::<Body>::new(
150+
cfg.auth.secret,
151+
cfg.auth.tokens,
152+
));
150153
let app = Router::new()
151154
.merge(
152155
route::proxy::route()

0 commit comments

Comments
 (0)