@@ -20,7 +20,7 @@ use models::r#type::ServerType;
2020use routes:: { ApiError , GetState } ;
2121use sentry_tower:: SentryHttpLayer ;
2222use sha1:: Digest ;
23- use std:: { sync:: Arc , time:: Instant } ;
23+ use std:: { net :: IpAddr , sync:: Arc , time:: Instant } ;
2424use tower_cookies:: CookieManagerLayer ;
2525use tower_http:: { catch_panic:: CatchPanicLayer , cors:: CorsLayer , trace:: TraceLayer } ;
2626use utoipa:: openapi:: security:: { ApiKey , ApiKeyValue , SecurityScheme } ;
@@ -47,12 +47,9 @@ fn handle_panic(_err: Box<dyn std::any::Any + Send + 'static>) -> Response<Body>
4747}
4848
4949fn handle_request ( req : & Request < Body > , _span : & tracing:: Span ) {
50- let ip = req
51- . headers ( )
52- . get ( "x-real-ip" )
53- . or_else ( || req. headers ( ) . get ( "x-forwarded-for" ) )
54- . map ( |ip| ip. to_str ( ) . unwrap_or_default ( ) )
55- . unwrap_or_default ( ) ;
50+ let ip = extract_ip ( req. headers ( ) )
51+ . map ( |ip| ip. to_string ( ) )
52+ . unwrap_or_else ( || "unknown" . to_string ( ) ) ;
5653
5754 logger:: log (
5855 logger:: LoggerLevel :: Info ,
@@ -76,7 +73,7 @@ async fn handle_postprocessing(req: Request, next: Next) -> Result<Response, Sta
7673
7774 let mut response = next. run ( req) . await ;
7875
79- if let Some ( content_type) = response. headers ( ) . get ( "Content-Type" ) . cloned ( ) {
76+ if let Some ( content_type) = response. headers ( ) . get ( "Content-Type" ) {
8077 if content_type
8178 . to_str ( )
8279 . map ( |c| c. starts_with ( "text/plain" ) )
@@ -131,6 +128,26 @@ async fn handle_postprocessing(req: Request, next: Next) -> Result<Response, Sta
131128 Ok ( Response :: from_parts ( parts, Body :: from ( body_bytes) ) )
132129}
133130
131+ pub fn extract_ip ( headers : & HeaderMap ) -> Option < IpAddr > {
132+ let ip = headers
133+ . get ( "x-real-ip" )
134+ . or_else ( || headers. get ( "x-forwarded-for" ) )
135+ . map ( |ip| ip. to_str ( ) . unwrap_or_default ( ) )
136+ . unwrap_or_default ( ) ;
137+
138+ if ip. is_empty ( ) {
139+ return None ;
140+ }
141+
142+ let ip = if ip. contains ( ',' ) {
143+ ip. split ( ',' ) . next ( ) . unwrap_or_default ( ) . trim ( ) . to_string ( )
144+ } else {
145+ ip. to_string ( )
146+ } ;
147+
148+ ip. parse ( ) . ok ( )
149+ }
150+
134151#[ tokio:: main]
135152async fn main ( ) {
136153 let env = env:: Env :: parse ( ) ;
@@ -230,8 +247,7 @@ async fn main() {
230247 )
231248 . as_str ( ) ,
232249 )
233- . await
234- . unwrap ( ) ,
250+ . await ,
235251 "legacy-fabric" => reqwest:: get (
236252 format ! (
237253 "https://meta.legacyfabric.net/v2/versions/loader/{}/{}/{}/server/jar" ,
@@ -241,15 +257,25 @@ async fn main() {
241257 )
242258 . as_str ( ) ,
243259 )
244- . await
245- . unwrap ( ) ,
260+ . await ,
246261 _ => return (
247262 StatusCode :: NOT_FOUND ,
248263 headers,
249264 Body :: from ( b"project not supported" . to_vec ( ) ) ,
250265 ) ,
251266 } ;
252267
268+ let response = match response {
269+ Ok ( response) => response,
270+ Err ( _) => {
271+ return (
272+ StatusCode :: NOT_FOUND ,
273+ headers,
274+ Body :: from ( b"error fetching build" . to_vec ( ) ) ,
275+ ) ;
276+ }
277+ } ;
278+
253279 if !response. status ( ) . is_success ( ) {
254280 return (
255281 StatusCode :: NOT_FOUND ,
0 commit comments