From ce5ee19c9e137fce75355970213b75ac846921d4 Mon Sep 17 00:00:00 2001 From: Piyush Mishra Date: Wed, 12 May 2021 14:01:36 +0530 Subject: [PATCH] rate limit --- Cargo.lock | 264 ++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/main.rs | 9 +- static/css/style.css | 4 + 4 files changed, 274 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c169b5e..1fab63e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,7 +20,7 @@ dependencies = [ "pin-project 0.4.27", "smallvec", "tokio", - "tokio-util", + "tokio-util 0.3.1", "trust-dns-proto", "trust-dns-resolver", ] @@ -49,7 +49,7 @@ dependencies = [ "log", "pin-project 0.4.27", "tokio", - "tokio-util", + "tokio-util 0.3.1", ] [[package]] @@ -97,6 +97,7 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "452299e87817ae5673910e53c243484ca38be3828db819b6011736fc6982e874" dependencies = [ + "actix", "actix-codec", "actix-connect", "actix-rt", @@ -148,6 +149,24 @@ dependencies = [ "syn", ] +[[package]] +name = "actix-ratelimit" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cab6838f2c71885e4985a61ab96ea9968121c2a41ef416b76875f61091f7d73" +dependencies = [ + "actix", + "actix-http", + "actix-web", + "backoff", + "dashmap", + "failure", + "futures", + "log", + "r2d2-memcache", + "redis", +] + [[package]] name = "actix-router" version = "0.2.6" @@ -377,6 +396,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "ascii" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" + [[package]] name = "async-trait" version = "0.1.42" @@ -429,6 +454,16 @@ dependencies = [ "serde_urlencoded", ] +[[package]] +name = "backoff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "721c249ab59cbc483ad4294c9ee2671835c1e43e9ffc277e6b4ecfef733cfdc5" +dependencies = [ + "instant", + "rand 0.7.3", +] + [[package]] name = "backtrace" version = "0.3.56" @@ -565,6 +600,19 @@ dependencies = [ "vec_map", ] +[[package]] +name = "combine" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3da6baa321ec19e1cc41d31bf599f00c783d0517095cdaf0332e3fe8d20680" +dependencies = [ + "ascii", + "byteorder", + "either", + "memchr", + "unreachable", +] + [[package]] name = "const_fn" version = "0.4.5" @@ -624,6 +672,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "dashmap" +version = "4.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e77a43b28d0668df09411cb0bc9a8c2adc40f9a048afe863e05fd43251e8e39c" +dependencies = [ + "cfg-if 1.0.0", + "num_cpus", +] + [[package]] name = "derive_more" version = "0.99.11" @@ -650,6 +708,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" +[[package]] +name = "dtoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" + [[package]] name = "either" version = "1.6.1" @@ -677,6 +741,18 @@ dependencies = [ "syn", ] +[[package]] +name = "enum_dispatch" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44bead1f97ab8e54cd4f9205571b5c2c6f18abaa9193f9fdcf7fe09ddd4be313" +dependencies = [ + "once_cell", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "env_logger" version = "0.8.3" @@ -690,6 +766,28 @@ dependencies = [ "termcolor", ] +[[package]] +name = "failure" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" +dependencies = [ + "backtrace", + "failure_derive", +] + +[[package]] +name = "failure_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "flate2" version = "1.0.20" @@ -708,6 +806,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.0.0" @@ -891,7 +1004,7 @@ dependencies = [ "indexmap", "slab", "tokio", - "tokio-util", + "tokio-util 0.3.1", "tracing", "tracing-futures", ] @@ -1079,6 +1192,7 @@ dependencies = [ "actix", "actix-broker", "actix-files", + "actix-ratelimit", "actix-web", "actix-web-actors", "base64", @@ -1109,6 +1223,20 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" +[[package]] +name = "memcache" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c85d68ede7335fcbd6908253f90314ddfc4afb6bf79ec24833fe30f94cd48a1" +dependencies = [ + "byteorder", + "enum_dispatch", + "openssl", + "r2d2", + "rand 0.7.3", + "url", +] + [[package]] name = "memchr" version = "2.3.4" @@ -1232,6 +1360,33 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "openssl" +version = "0.10.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d7830286ad6a3973c0f1d9b73738f69c76b739301d0229c4b96501695cbe4c8" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "foreign-types", + "libc", + "once_cell", + "openssl-sys", +] + +[[package]] +name = "openssl-sys" +version = "0.9.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b0d6fb7d80f877617dfcb014e605e2b5ab2fb0afdf27935219bb6bd984cb98" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "parking_lot" version = "0.11.1" @@ -1321,6 +1476,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + [[package]] name = "ppv-lite86" version = "0.2.10" @@ -1363,6 +1524,27 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r2d2" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "545c5bc2b880973c9c10e4067418407a0ccaa3091781d1671d46eb35107cb26f" +dependencies = [ + "log", + "parking_lot", + "scheduled-thread-pool", +] + +[[package]] +name = "r2d2-memcache" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74b9d7bc44a11909bffe6bf1e4bddae23215ecd6186695e4766d6b57d819f640" +dependencies = [ + "memcache", + "r2d2", +] + [[package]] name = "rand" version = "0.7.3" @@ -1444,6 +1626,26 @@ dependencies = [ "rand_core 0.6.1", ] +[[package]] +name = "redis" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eeb1fe3fc011cde97315f370bc88e4db3c23b08709a04915921e02b1d363b20" +dependencies = [ + "bytes 0.5.6", + "combine", + "dtoa", + "futures-executor", + "futures-util", + "itoa", + "percent-encoding", + "pin-project-lite 0.1.11", + "sha1", + "tokio", + "tokio-util 0.2.0", + "url", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -1499,6 +1701,15 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "scheduled-thread-pool" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f74fd1204073fa02d5d5d68bec8021be4c38690b61264b2fdb48083d0e7d7" +dependencies = [ + "parking_lot", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -1689,6 +1900,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + [[package]] name = "termcolor" version = "1.1.2" @@ -1819,6 +2042,20 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "tokio-util" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "571da51182ec208780505a32528fc5512a8fe1443ab960b3f2f3ef093cd16930" +dependencies = [ + "bytes 0.5.6", + "futures-core", + "futures-sink", + "log", + "pin-project-lite 0.1.11", + "tokio", +] + [[package]] name = "tokio-util" version = "0.3.1" @@ -1956,6 +2193,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +dependencies = [ + "void", +] + [[package]] name = "url" version = "2.2.0" @@ -2000,6 +2246,12 @@ dependencies = [ "v_escape", ] +[[package]] +name = "vcpkg" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbdbff6266a24120518560b5dc983096efb98462e51d0d68169895b237be3e5d" + [[package]] name = "vec_map" version = "0.8.2" @@ -2018,6 +2270,12 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 10d20c5..88fa857 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ actix-web = "3" actix-web-actors = "3" actix-broker = "0.3.1" actix-files = "0.5.0" +actix-ratelimit = "0.3.1" env_logger = "0.8.3" clap = "2.33.3" diff --git a/src/main.rs b/src/main.rs index 7c9dd80..978f542 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use actix_web::{App, Error, HttpRequest, HttpResponse, HttpServer, middleware::Logger, web}; use actix_files as fs; use actix_web_actors::ws; +use actix_ratelimit::{RateLimiter, MemoryStore, MemoryStoreActor}; use ws_sansad::WsSansad; mod config; @@ -24,11 +25,17 @@ mod validator; async fn main() -> std::io::Result<()> { std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); - + let store = MemoryStore::new(); let config = config::Config::new(); let static_path = config.static_path; HttpServer::new(move || { App::new() + .wrap( + RateLimiter::new( + MemoryStoreActor::from(store.clone()).start()) + .with_interval(std::time::Duration::from_secs(60)) + .with_max_requests(200) + ) .wrap(Logger::new("%t [%{x-forwarded-for}i] %s %{User-Agent}i %r")) .service(web::resource("/ws/").route(web::get().to(ws_index))) .service(fs::Files::new("/", &static_path).index_file("index.html")) diff --git a/static/css/style.css b/static/css/style.css index 9cb552e..cf907f3 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -126,6 +126,10 @@ input { background-size: 30px; } +.tabs > a { + cursor: pointer; +} + .chatpanel-top { width: 100%; height: 50px;