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

how to close connection immediately? #474

Open
lithbitren opened this issue Dec 6, 2022 · 1 comment
Open

how to close connection immediately? #474

lithbitren opened this issue Dec 6, 2022 · 1 comment

Comments

@lithbitren
Copy link

lithbitren commented Dec 6, 2022

if client's connection of actix-web is probably attacking, how can I close it immediately and insert the address into a dynamic blocklist?

when a addr is in the blocklist, i don't what to parse the uri or headers from this addr, just close the connection without sending or receiving any of the message.

    let listener = TcpListener::bind(...).await?;

    while let (socket, addr) = listener.accept().await? {

        if blocklist.contains(&addr) {
            continue;
        }

        tokio::spawn(async move {

            ...

            if connection_is_attacking() {
                blocklist.insert(addr);
                connection.close_immediately();
            }

            ...

        });
    }
@robjtede robjtede transferred this issue from actix/actix-web Dec 18, 2022
@lithbitren
Copy link
Author

lithbitren commented Jan 13, 2023

I found that tcpstream can be obtained by using the "on_connect" API, but still have no idea to shut it down.

type Blocklist = Arc<RwLock<HashMap<IpAddr, Instant>>>;
...
let blocklist = Blocklist::default();
...
.on_connect({
            let blocklist = blocklist.clone();
            move |conn, data| {
                if let Some(tcp_stream) = conn.downcast_ref::<TcpStream>() {
                    let peer = tcp_stream.peer_addr().unwrap();
                    if blocklist.read().unwrap().contains_key(&peer.ip()) {
                          // the ref of tcp_stream is immutable, how to shut it down? ...
                    }
                    // if possible, i need to send tcpstream object to handles and determine whether to close it
                    data.insert(ConnectionInfo {
                        bind: tcp_stream.local_addr().unwrap(),
                        peer,
                        ttl: tcp_stream.ttl().ok(),
                    });
                } else {
                    unreachable!("connection should only be plaintext since no TLS is set up");
                }
            }
        })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant