From 0efeffeaa3afcbe8766c6db377f02a9ae2cfb635 Mon Sep 17 00:00:00 2001 From: Geometrically Date: Thu, 31 Dec 2020 18:54:58 -0700 Subject: [PATCH] Bump ratelimit to 200 RPM, allow specified IPs to have lax ratelimit restrictions, and allow wildcard for CORS --- .env | 5 +- Cargo.lock | 313 +++++++++++++++++++++++++++++++++++++++------------- Cargo.toml | 6 +- src/main.rs | 54 ++++++--- 4 files changed, 274 insertions(+), 104 deletions(-) diff --git a/.env b/.env index 283ffba46..381839e55 100644 --- a/.env +++ b/.env @@ -1,7 +1,6 @@ DEBUG=true RUST_LOG=info,sqlx::query=warn -CORS_ORIGINS='["http://localhost:3000","https://modrinth.com"]' CDN_URL=https://cdn.modrinth.com DATABASE_URL=postgresql://labrinth:labrinth@localhost/labrinth @@ -31,4 +30,6 @@ LOCAL_INDEX_INTERVAL=3600 VERSION_INDEX_INTERVAL=1800 GITHUB_CLIENT_ID=3acffb2e808d16d4b226 -GITHUB_CLIENT_SECRET=none \ No newline at end of file +GITHUB_CLIENT_SECRET=none + +RATE_LIMIT_IGNORE_IPS='[]' \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 423012a72..faa0b7f50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,10 +17,10 @@ dependencies = [ "log", "once_cell", "parking_lot", - "pin-project", + "pin-project 0.4.27", "smallvec", "tokio", - "tokio-util", + "tokio-util 0.3.1", "trust-dns-proto", "trust-dns-resolver", ] @@ -36,9 +36,9 @@ dependencies = [ "futures-core", "futures-sink", "log", - "pin-project", + "pin-project 0.4.27", "tokio", - "tokio-util", + "tokio-util 0.3.1", ] [[package]] @@ -94,9 +94,9 @@ dependencies = [ [[package]] name = "actix-http" -version = "2.0.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05dd80ba8f27c4a34357c07e338c8f5c38f8520e6d626ca1727d8fecc41b0cab" +checksum = "452299e87817ae5673910e53c243484ca38be3828db819b6011736fc6982e874" dependencies = [ "actix", "actix-codec", @@ -105,7 +105,7 @@ dependencies = [ "actix-service", "actix-threadpool", "actix-utils", - "base64 0.12.3", + "base64 0.13.0", "bitflags", "brotli2", "bytes", @@ -129,12 +129,12 @@ dependencies = [ "log", "mime", "percent-encoding", - "pin-project", + "pin-project 1.0.2", "rand", "regex", "serde", "serde_json", - "serde_urlencoded", + "serde_urlencoded 0.7.0", "sha-1", "slab", "time 0.2.22", @@ -170,16 +170,20 @@ dependencies = [ [[package]] name = "actix-ratelimit" -version = "0.2.1" -source = "git+https://github.com/TerminalWitchcraft/actix-ratelimit?rev=870822067dfeae7cc0304352d81c4cb79ee27f5a#870822067dfeae7cc0304352d81c4cb79ee27f5a" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed99841cc349bc8d23b03ea62de0301946b740d1d08582ba332d0c5fd408eed" dependencies = [ "actix", "actix-http", "actix-web", + "backoff", "dashmap", "failure", "futures", "log", + "r2d2-memcache", + "redis", ] [[package]] @@ -237,7 +241,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0052435d581b5be835d11f4eb3bce417c8af18d87ddf8ace99f8e67e595882bb" dependencies = [ "futures-util", - "pin-project", + "pin-project 0.4.27", ] [[package]] @@ -297,15 +301,15 @@ dependencies = [ "futures-sink", "futures-util", "log", - "pin-project", + "pin-project 0.4.27", "slab", ] [[package]] name = "actix-web" -version = "3.1.0" +version = "3.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1b12fe25e11cd9ed2ef2e428427eb6178a1b363f3f7f0dab8278572f11b2da1" +checksum = "e641d4a172e7faa0862241a20ff4f1f5ab0ab7c279f00c2d4587b77483477b86" dependencies = [ "actix-codec", "actix-http", @@ -329,11 +333,11 @@ dependencies = [ "fxhash", "log", "mime", - "pin-project", + "pin-project 1.0.2", "regex", "serde", "serde_json", - "serde_urlencoded", + "serde_urlencoded 0.7.0", "socket2", "time 0.2.22", "tinyvec 1.0.1", @@ -342,9 +346,9 @@ dependencies = [ [[package]] name = "actix-web-codegen" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "750ca8fb60bbdc79491991650ba5d2ae7cd75f3fc00ead51390cfe9efda0d4d8" +checksum = "ad26f77093333e0e7c6ffe54ebe3582d908a104e448723eec6d43d08b07143fb" dependencies = [ "proc-macro2", "quote", @@ -382,9 +386,6 @@ name = "ahash" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217" -dependencies = [ - "const-random", -] [[package]] name = "ahash" @@ -423,6 +424,12 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" +[[package]] +name = "ascii" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" + [[package]] name = "async-channel" version = "1.5.1" @@ -583,16 +590,17 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "awc" -version = "2.0.0" +version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "150e00c06683ab44c5f97d033950e5d87a7a042d06d77f5eecb443cbd23d0575" +checksum = "b381e490e7b0cfc37ebc54079b0413d8093ef43d14a4e4747083f7fa47a9e691" dependencies = [ "actix-codec", "actix-http", "actix-rt", "actix-service", - "base64 0.12.3", + "base64 0.13.0", "bytes", + "cfg-if 1.0.0", "derive_more", "futures-core", "log", @@ -601,7 +609,7 @@ dependencies = [ "rand", "serde", "serde_json", - "serde_urlencoded", + "serde_urlencoded 0.7.0", ] [[package]] @@ -629,6 +637,16 @@ dependencies = [ "simpl", ] +[[package]] +name = "backoff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "721c249ab59cbc483ad4294c9ee2671835c1e43e9ffc277e6b4ecfef733cfdc5" +dependencies = [ + "instant", + "rand", +] + [[package]] name = "backtrace" version = "0.3.53" @@ -810,6 +828,19 @@ dependencies = [ "bitflags", ] +[[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 = "concurrent-queue" version = "1.2.2" @@ -819,26 +850,6 @@ dependencies = [ "cache-padded", ] -[[package]] -name = "const-random" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02dc82c12dc2ee6e1ded861cf7d582b46f66f796d1b6c93fa28b911ead95da02" -dependencies = [ - "const-random-macro", - "proc-macro-hack", -] - -[[package]] -name = "const-random-macro" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc757bbb9544aa296c2ae00c679e81f886b37e28e59097defe0cf524306f6685" -dependencies = [ - "getrandom 0.2.0", - "proc-macro-hack", -] - [[package]] name = "const_fn" version = "0.4.2" @@ -987,12 +998,11 @@ dependencies = [ [[package]] name = "dashmap" -version = "3.11.10" +version = "4.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f260e2fc850179ef410018660006951c1b55b79e8087e87111a2c388994b9b5" +checksum = "4b937cd1fbd1f194ac842196bd2529f21618088ee6d8bff6a46ece611451c96b" dependencies = [ - "ahash 0.3.8", - "cfg-if 0.1.10", + "cfg-if 1.0.0", "num_cpus", ] @@ -1093,6 +1103,18 @@ dependencies = [ "syn", ] +[[package]] +name = "enum_dispatch" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "733967190e01b0dcb64f2f42687a78af0e418e064489e993e16445643d088560" +dependencies = [ + "once_cell", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "env_logger" version = "0.8.1" @@ -1176,6 +1198,16 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "form_urlencoded" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ece68d15c92e84fa4f19d3780f1294e5ca82a78a6d515f1efaabcc144688be00" +dependencies = [ + "matches", + "percent-encoding", +] + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -1194,9 +1226,9 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" [[package]] name = "futures" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d8e3078b7b2a8a671cb7a3d17b4760e4181ea243227776ba83fd043b4ca034e" +checksum = "9b3b0c040a1fe6529d30b3c5944b280c7f0dcb2930d2c3062bca967b602583d0" dependencies = [ "futures-channel", "futures-core", @@ -1209,9 +1241,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a4d35f7401e948629c9c3d6638fb9bf94e0b2121e96c3b428cc4e631f3eb74" +checksum = "4b7109687aa4e177ef6fe84553af6280ef2778bdb7783ba44c9dc3399110fe64" dependencies = [ "futures-core", "futures-sink", @@ -1219,15 +1251,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d674eaa0056896d5ada519900dbf97ead2e46a7b6621e8160d79e2f2e1e2784b" +checksum = "847ce131b72ffb13b6109a221da9ad97a64cbe48feb1028356b836b47b8f1748" [[package]] name = "futures-executor" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc709ca1da6f66143b8c9bec8e6260181869893714e9b5a490b169b0414144ab" +checksum = "4caa2b2b68b880003057c1dd49f1ed937e38f22fcf6c212188a121f08cf40a65" dependencies = [ "futures-core", "futures-task", @@ -1236,9 +1268,9 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fc94b64bb39543b4e432f1790b6bf18e3ee3b74653c5449f63310e9a74b123c" +checksum = "611834ce18aaa1bd13c4b374f5d653e1027cf99b6b502584ff8c9a64413b30bb" [[package]] name = "futures-lite" @@ -1257,9 +1289,9 @@ dependencies = [ [[package]] name = "futures-macro" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f57ed14da4603b2554682e9f2ff3c65d7567b53188db96cb71538217fc64581b" +checksum = "77408a692f1f97bcc61dc001d752e00643408fbc922e4d634c655df50d595556" dependencies = [ "proc-macro-hack", "proc-macro2", @@ -1269,15 +1301,15 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d8764258ed64ebc5d9ed185cf86a95db5cac810269c5d20ececb32e0088abbd" +checksum = "f878195a49cee50e006b02b93cf7e0a95a38ac7b776b4c4d9cc1207cd20fcb3d" [[package]] name = "futures-task" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dd26820a9f3637f1302da8bceba3ff33adbe53464b54ca24d4e2d4f1db30f94" +checksum = "7c554eb5bf48b2426c4771ab68c6b14468b6e76cc90996f528c3338d761a4d0d" dependencies = [ "once_cell", ] @@ -1290,9 +1322,9 @@ checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" [[package]] name = "futures-util" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a894a0acddba51a2d49a6f4263b1e64b8c579ece8af50fa86503d52cd1eea34" +checksum = "d304cff4a7b99cfb7986f7d43fbe93d175e72e704a8860787cc95e9ffd85cbd2" dependencies = [ "futures-channel", "futures-core", @@ -1301,7 +1333,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project", + "pin-project 1.0.2", "pin-utils", "proc-macro-hack", "proc-macro-nested", @@ -1403,7 +1435,7 @@ dependencies = [ "indexmap", "slab", "tokio", - "tokio-util", + "tokio-util 0.3.1", "tracing", ] @@ -1523,7 +1555,7 @@ dependencies = [ "httparse", "httpdate", "itoa", - "pin-project", + "pin-project 0.4.27", "socket2", "tokio", "tower-service", @@ -1782,6 +1814,19 @@ dependencies = [ "web-sys", ] +[[package]] +name = "memcache" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c71b11d4a87166e8ecab53cdc444e54383bde8be8e3ed810c7dc152b8ba69c07" +dependencies = [ + "byteorder", + "enum_dispatch", + "openssl", + "rand", + "url", +] + [[package]] name = "memchr" version = "2.3.3" @@ -2039,7 +2084,16 @@ version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ffbc8e94b38ea3d2d8ba92aea2983b503cd75d0888d75b86bb37970b5698e15" dependencies = [ - "pin-project-internal", + "pin-project-internal 0.4.27", +] + +[[package]] +name = "pin-project" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ccc2237c2c489783abd8c4c80e5450fc0e98644555b1364da68cc29aa151ca7" +dependencies = [ + "pin-project-internal 1.0.2", ] [[package]] @@ -2053,6 +2107,17 @@ dependencies = [ "syn", ] +[[package]] +name = "pin-project-internal" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.1.10" @@ -2092,9 +2157,9 @@ checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20" [[package]] name = "proc-macro-hack" -version = "0.5.18" +version = "0.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro-nested" @@ -2126,6 +2191,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.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5114344cebd85a7f39c3825f97ff2eee685af8abae8560250a28d30d7740f99f" +dependencies = [ + "memcache", + "r2d2", +] + [[package]] name = "rand" version = "0.7.3" @@ -2167,6 +2253,26 @@ dependencies = [ "rand_core", ] +[[package]] +name = "redis" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eeb1fe3fc011cde97315f370bc88e4db3c23b08709a04915921e02b1d363b20" +dependencies = [ + "bytes", + "combine", + "dtoa", + "futures-executor", + "futures-util", + "itoa", + "percent-encoding", + "pin-project-lite", + "sha1", + "tokio", + "tokio-util 0.2.0", + "url", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -2237,7 +2343,7 @@ dependencies = [ "pin-project-lite", "serde", "serde_json", - "serde_urlencoded", + "serde_urlencoded 0.6.1", "tokio", "tokio-tls", "url", @@ -2339,6 +2445,15 @@ dependencies = [ "winapi 0.3.9", ] +[[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" @@ -2439,6 +2554,18 @@ dependencies = [ "url", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_with" version = "1.5.1" @@ -2523,13 +2650,12 @@ checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" [[package]] name = "socket2" -version = "0.3.15" +version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1fa70dc5c8104ec096f4fe7ede7a221d35ae13dcd19ba1ad9a81d2cab9a1c44" +checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" dependencies = [ - "cfg-if 0.1.10", + "cfg-if 1.0.0", "libc", - "redox_syscall", "winapi 0.3.9", ] @@ -2919,6 +3045,20 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-util" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "571da51182ec208780505a32528fc5512a8fe1443ab960b3f2f3ef093cd16930" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-util" version = "0.3.1" @@ -3068,6 +3208,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.1.1" @@ -3135,6 +3284,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 = "waker-fn" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 3018d79cc..ab50795ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ actix-rt = "1.1.1" actix-files = "0.4.0" actix-multipart = "0.3.0" actix-cors = "0.4.1" +actix-ratelimit = "0.3.0" meilisearch-sdk = "0.4.0" reqwest = { version = "0.10.8", features = ["json"] } @@ -53,8 +54,3 @@ git = "https://github.com/launchbadge/sqlx/" branch = "master" default-features = false features = ["runtime-actix", "postgres", "chrono", "offline"] - -[dependencies.actix-ratelimit] -# Temp until actix-ratelimit bumps version on cargo -git = "https://github.com/TerminalWitchcraft/actix-ratelimit" -rev = "870822067dfeae7cc0304352d81c4cb79ee27f5a" diff --git a/src/main.rs b/src/main.rs index 53918c7e9..09a248c46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use log::{error, info, warn}; use search::indexing::index_mods; use search::indexing::IndexingSettings; use std::sync::Arc; +use actix_ratelimit::errors::ARError; +use rand::Rng; mod auth; mod database; @@ -234,32 +236,48 @@ async fn main() -> std::io::Result<()> { pepper: crate::models::ids::Base62Id(crate::models::ids::random_base62(11)).to_string(), }; - let allowed_origins = dotenv::var("CORS_ORIGINS") - .ok() - .and_then(|s| serde_json::from_str::>(&s).ok()) - .unwrap_or_else(|| vec![String::from("http://localhost")]); - let store = MemoryStore::new(); info!("Starting Actix HTTP server!"); // Init App HttpServer::new(move || { - let mut cors = Cors::new() - .allowed_methods(vec!["GET", "POST", "DELETE", "PATCH", "PUT"]) - .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT]) - .allowed_header(http::header::CONTENT_TYPE) - .max_age(3600); - for allowed_origin in &allowed_origins { - cors = cors.allowed_origin(allowed_origin); - } - App::new() - .wrap(cors.finish()) + .wrap(Cors::new() + .allowed_methods(vec!["GET", "POST", "DELETE", "PATCH", "PUT"]) + .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT]) + .allowed_header(http::header::CONTENT_TYPE) + .send_wildcard() + .max_age(3600) + .finish()) .wrap( + // This is a hacky workaround to allowing the frontend server-side renderer to have + // an unlimited rate limit, since there is no current way with this library to + // have dynamic rate-limit max requests RateLimiter::new(MemoryStoreActor::from(store.clone()).start()) + .with_identifier(|req| { + let connection_info = req.connection_info(); + let ip = String::from(connection_info + .remote_addr() + .ok_or(ARError::IdentificationError)?); + + let ignore_ips = dotenv::var("RATE_LIMIT_IGNORE_IPS") + .ok() + .and_then(|s| serde_json::from_str::>(&s).ok()) + .unwrap_or_else(|| vec![]); + + if ignore_ips.contains(&ip) { + // At an even distribution of numbers, this will allow at the most + // 3000 requests per minute from the frontend, which is reasonable + // (50 requests per second) + let random = rand::thread_rng().gen_range(1, 15); + return Ok(format!("{}-{}", ip, random)) + } + + Ok(ip) + }) .with_interval(std::time::Duration::from_secs(60)) - .with_max_requests(100), + .with_max_requests(200), ) .data(pool.clone()) .data(file_host.clone()) @@ -305,12 +323,12 @@ fn check_env_vars() -> bool { } } - if dotenv::var("CORS_ORIGINS") + if dotenv::var("RATE_LIMIT_IGNORE_IPS") .ok() .and_then(|s| serde_json::from_str::>(&s).ok()) .is_none() { - warn!("Variable `CORS_ORIGINS` missing in dotenv or not a json array of strings"); + warn!("Variable `RATE_LIMIT_IGNORE_IPS` missing in dotenv or not a json array of strings"); failed |= true; }