Skip to content
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

Update to axum=0.7.4 #137

Merged
merged 1 commit into from
Jun 29, 2024
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ path = "src/main.rs"

[dependencies]
tokio = { version = "1", features = ["full"] }
axum = "0.6.20"
axum-server = { version = "0.3", features = ["tls-rustls"] }
axum = "=0.7.4"
axum-server = { version = "=0.6", features = ["tls-rustls"] }
rand = { git = "https://github.com/rust-lang-nursery/rand" }
openssl = { version = "0.10", features = ["vendored"] }
hex = "0.4.3"
chrono = "0.4.31"
serde = {version="1.0", features=["derive"]}
serde_json = "1.0"
reqwest = { version = "0.11", features = ["json"] }
reqwest = { version = "0.12", features = ["json", "stream"] }
regex = "1.10.2"
semver = "1.0.20"
libflate = "2"
Expand Down
2 changes: 1 addition & 1 deletion src/content/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ContentTree
/// content is never refreshed within the router
fn route(&self, static_router: bool) -> Router
{
let mut router: Router<(), axum::body::Body> = Router::new();
let mut router = Router::new();
for (uri, content) in self.contents.clone()
{
router = router.route
Expand Down
24 changes: 10 additions & 14 deletions src/integrations/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ use regex::Regex;
use reqwest::StatusCode;
use tokio::sync::Mutex;

use crate::{config::{Config, CONFIG_PATH}, util::strip_control_characters};
use crate::{config::{Config, CONFIG_PATH}, util::{extract_bytes, strip_control_characters}};

use super::git::refresh::GitRefreshTask;

/// If user-agent is GitHub-Hookshot, check if
/// x-github-event is push. If so pull the repo if
/// [crate::config::GitConfig] is not None
pub async fn filter_github<B>
pub async fn filter_github
(
State(repo_lock): State<Arc<Mutex<SystemTime>>>,
headers: HeaderMap,
request: Request<B>,
next: Next<B>
request: Request<axum::body::Body>,
next: Next
) -> Result<Response, StatusCode>
where B: axum::body::HttpBody<Data = Bytes>
{
let config = Config::load_or_default(CONFIG_PATH);
let remote = match config.git
Expand All @@ -45,22 +44,19 @@ where B: axum::body::HttpBody<Data = Bytes>

/// Checks the Github webhook event is authentic and
/// matches remote. If so tries to pull
pub async fn handle_push<B>
pub async fn handle_push
(
repo_lock: Arc<Mutex<SystemTime>>,
headers: HeaderMap,
request: Request<B>,
request: Request<axum::body::Body>,
remote: String,
token: String
) -> StatusCode
where B: axum::body::HttpBody<Data = Bytes>
{
let body = request.into_body();
let bytes = match body.collect().await {
Ok(collected) => collected.to_bytes(),
Err(_) => {
return StatusCode::BAD_REQUEST;
}
let bytes = match extract_bytes(request).await
{
Ok(b) => b,
Err(_) => return StatusCode::BAD_REQUEST
};

if !is_watched_repo(&bytes, &remote)
Expand Down
9 changes: 4 additions & 5 deletions src/server/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ pub trait ApiRequest
/// 2. authenticate the request (HMAC)
/// 3. respond to it
/// 4. continue on to the next reqeust
async fn filter<B>
async fn filter
(
stats: State<Option<Arc<Mutex<HitStats>>>>,
headers: HeaderMap,
request: Request<B>,
next: Next<B>
) -> Result<Response, StatusCode>
where B: axum::body::HttpBody<Data = Bytes>;
request: Request<axum::body::Body>,
next: Next
) -> Result<Response, StatusCode>;

}
20 changes: 9 additions & 11 deletions src/server/api/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reqwest::StatusCode;
use serde::Deserialize;
use tokio::sync::Mutex;

use crate::{config::{read_config, CONFIG_PATH}, integrations::{discord::post::try_post, is_authentic}, server::stats::{digest::{digest_message, process_hits}, hits::HitStats}};
use crate::{config::{read_config, CONFIG_PATH}, integrations::{discord::post::try_post, is_authentic}, server::stats::{digest::{digest_message, process_hits}, hits::HitStats}, util::extract_bytes};

use super::ApiRequest;

Expand Down Expand Up @@ -159,14 +159,13 @@ impl ApiRequest for StatsDigest
(Some(msg), StatusCode::OK)
}

async fn filter<B>
async fn filter
(
State(stats): State<Option<Arc<Mutex<HitStats>>>>,
headers: HeaderMap,
request: Request<B>,
next: Next<B>
request: Request<axum::body::Body>,
next: Next
) -> Result<Response, StatusCode>
where B: axum::body::HttpBody<Data = Bytes>
{

if !headers.contains_key("api")
Expand All @@ -190,13 +189,12 @@ impl ApiRequest for StatsDigest
false => { return Ok(next.run(request).await) }
}

let body = request.into_body();
let bytes = match body.collect().await {
Ok(collected) => collected.to_bytes(),
Err(_) => {
return Err(StatusCode::BAD_REQUEST)
}
let bytes = match extract_bytes(request).await
{
Ok(b) => b,
Err(_) => return Err(StatusCode::BAD_REQUEST)
};


match StatsDigest::is_authentic(headers.clone(), bytes.clone())
{
Expand Down
6 changes: 2 additions & 4 deletions src/server/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ impl ServerHttp

pub async fn serve(self: ServerHttp)
{
axum::Server::bind(&self.addr)
.serve(self.router.into_make_service_with_connect_info::<SocketAddr>())
.await
.unwrap();
let listener = tokio::net::TcpListener::bind(&self.addr).await.unwrap();
axum::serve(listener, self.router).await.unwrap();
}

}
2 changes: 1 addition & 1 deletion src/server/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Server

let throttle_state = Arc::new(Mutex::new(requests));

let mut router: Router<(), axum::body::Body> = sitemap.into();
let mut router: Router = sitemap.into();

let stats = Arc::new(Mutex::new(
HitStats::new()
Expand Down
6 changes: 3 additions & 3 deletions src/server/stats/hits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ impl HitStats
}

/// Launches a thread to log statistics for this request
pub async fn log_stats<B>
pub async fn log_stats
(
ConnectInfo(addr): ConnectInfo<SocketAddr>,
State(state): State<Arc<Mutex<HitStats>>>,
request: Request<B>,
next: Next<B>
request: Request<axum::body::Body>,
next: Next
) -> Result<Response, StatusCode>
{

Expand Down
6 changes: 3 additions & 3 deletions src/server/throttle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ impl IpThrottler

/// Reflects any [Request]s in timeout (see [IpThrottler::is_limited]) as
/// [StatusCode::TOO_MANY_REQUESTS].
pub async fn handle_throttle<B>
pub async fn handle_throttle
(
ConnectInfo(addr): ConnectInfo<SocketAddr>,
State(state): State<Arc<Mutex<IpThrottler>>>,
request: http::Request<B>,
next: Next<B>
request: http::Request<axum::body::Body>,
next: Next
) -> Result<Response, StatusCode>
{
let serve_start = Instant::now();
Expand Down
24 changes: 24 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use core::fmt;
use std::{collections::HashSet, fmt::Write, io::{Read, Write as ioWrite}, time::Instant};
use axum::{body::{to_bytes, Bytes}, http::Request};
use chrono::{DateTime, Datelike, FixedOffset};
use git2::Status;
use libflate::deflate::{Encoder, Decoder};
use openssl::sha::Sha256;
use regex::Regex;
use reqwest::StatusCode;

use crate::BLAZING;

Expand Down Expand Up @@ -201,4 +204,25 @@ pub fn formatted_differences(new: Vec<String>, old: Vec<String>) -> String
}

diffs
}

#[derive(Debug, Clone)]
pub struct BodyError
{
pub why: String
}

impl fmt::Display for BodyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.why)
}
}

pub async fn extract_bytes(request: Request<axum::body::Body>,) -> Result<Bytes, StatusCode>
{
let body = request.into_body();
match to_bytes(body, usize::MAX).await {
Ok(collected) => Ok(collected),
Err(_) => Err(StatusCode::BAD_REQUEST)
}
}
Loading