Agathe_DVY
Agathe_DVY

Reputation: 25

Close connection without send response on Actix-web

I try to find a way not to answer to an unwanted request (assimilated to ddos) by closing the connection without let the client it be known. This way, the TIME-WAIT connection stack of the emitter device will grow until no new connection is possible anymore, while everything is fine on my side.

I did look in the actix.rs manual and the docs.rs documentation, but I did not find any way to close a connection without giving an answer, a body seems always required. I actually have this function as default entry:

async fn defaultentry(req: HttpRequest) -> impl Responder {
    HttpResponse::build(StatusCode::from_u16(503).unwrap())
        .force_close()
        .finish()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(move || {
        App::new()
            .default_service(
                web::get().to(defaultentry)
            )
    })
    .bind(("0.0.0.0", 8080))?
    .run()
    .await
}

Upvotes: 1

Views: 837

Answers (1)

cafce25
cafce25

Reputation: 27549

You can call register a on_connect handler on the HttpServer to shutdown the connection. As far as I know there is no way to do it from within a handler.

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(move || {
        App::new()
            .default_service(
                web::get().to(defaultentry)
            )
    })
    .on_connect(|c, _| {
        if true { // whatever you're looking for
            c.downcast_ref::<TcpStream>().unwrap().shutdown(Shutdown::Both).unwrap();
        }
    })
    .bind(("0.0.0.0", 8080))?
    .run()
    .await
}

Upvotes: 2

Related Questions