From a632c179678901d3d88a48919b8e3899f0ead981 Mon Sep 17 00:00:00 2001 From: Trevor Fitzgerald Date: Sat, 20 Jul 2024 09:35:55 -0400 Subject: [PATCH] Add rate limiting as setup option --- command/Cargo.lock | 106 ++++++++++++++++++++++---------------------- command/src/main.rs | 58 ++++++++++++++++++------ conf/lila.conf | 2 +- docker-compose.yml | 1 + 4 files changed, 100 insertions(+), 67 deletions(-) diff --git a/command/Cargo.lock b/command/Cargo.lock index 805c7a03..e3420d0f 100644 --- a/command/Cargo.lock +++ b/command/Cargo.lock @@ -53,9 +53,9 @@ dependencies = [ [[package]] name = "either" -version = "1.12.0" +version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "encode_unicode" @@ -139,9 +139,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "memchr" @@ -188,9 +188,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "portable-atomic" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" +checksum = "da544ee218f0d287a911e9c99a39a8c9bc8fcad3cb8db5959940044ecfc67265" [[package]] name = "proc-macro2" @@ -218,29 +218,29 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "serde" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", - "syn 2.0.67", + "syn 2.0.71", ] [[package]] name = "serde_json" -version = "1.0.117" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", @@ -275,9 +275,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.67" +version = "2.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff8655ed1d86f3af4ee3fd3263786bc14245ad17c4c7e85ba7187fb3ae028c90" +checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" dependencies = [ "proc-macro2", "quote", @@ -297,29 +297,29 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.61" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.61" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn 2.0.67", + "syn 2.0.71", ] [[package]] name = "toml" -version = "0.8.14" +version = "0.8.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" +checksum = "ac2caab0bf757388c6c0ae23b3293fdb463fee59434529014f85e3263b995c28" dependencies = [ "serde", "serde_spanned", @@ -338,9 +338,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.14" +version = "0.22.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" +checksum = "278f3d518e152219c994ce877758516bca5e118eaed6996192a774fb9fbf0788" dependencies = [ "indexmap", "serde", @@ -382,7 +382,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -402,18 +402,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.5", - "windows_aarch64_msvc 0.52.5", - "windows_i686_gnu 0.52.5", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.5", - "windows_x86_64_gnu 0.52.5", - "windows_x86_64_gnullvm 0.52.5", - "windows_x86_64_msvc 0.52.5", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -424,9 +424,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -436,9 +436,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -448,15 +448,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -466,9 +466,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -478,9 +478,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -490,9 +490,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -502,15 +502,15 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.6.13" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" +checksum = "374ec40a2d767a3c1b4972d9475ecd557356637be906f2cb3f7fe17a6eb5e22f" dependencies = [ "memchr", ] @@ -532,5 +532,5 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.67", + "syn 2.0.71", ] diff --git a/command/src/main.rs b/command/src/main.rs index eb4f0489..b80aaf05 100644 --- a/command/src/main.rs +++ b/command/src/main.rs @@ -31,6 +31,7 @@ struct Config { setup_database: Option, setup_bbppairings: Option, enable_monitoring: Option, + enable_rate_limiting: Option, su_password: Option, password: Option, setup_api_tokens: Option, @@ -80,6 +81,7 @@ impl Config { setup_database, setup_bbppairings, enable_monitoring, + enable_rate_limiting, su_password, password, setup_api_tokens, @@ -100,6 +102,7 @@ impl Config { to_env!(setup_database), to_env!(setup_bbppairings), to_env!(enable_monitoring), + to_env!(enable_rate_limiting), to_env!(su_password), to_env!(password), to_env!(setup_api_tokens), @@ -199,6 +202,12 @@ struct OptionalService<'a> { repositories: Option>, } +#[derive(Debug, Clone, Eq, PartialEq)] +enum Setting { + SetupDatabase, + EnableRateLimiting, +} + fn main() -> std::io::Result<()> { let args: Vec = std::env::args().collect(); assert!(args.len() > 1, "Missing command"); @@ -237,24 +246,18 @@ fn setup(mut config: Config, first_setup: bool) -> std::io::Result<()> { "NOTE: This will not remove any existing services that may be running.\nOnly the newly selected ones will be added." )?; } - let services = prompt_for_optional_services()?; + let services = prompt_for_services()?; - let setup_database = confirm(if first_setup { - "Do you want to seed the database with test users, games, etc? (Recommended)" - } else { - "Do you want to re-seed the database with test users, games, etc?" - }) - .initial_value(first_setup) - .interact()?; + let options = prompt_for_options(first_setup)?; - let (su_password, password) = if setup_database { + let (su_password, password) = if options.contains(&Setting::SetupDatabase) { (pwd_input("admin")?, pwd_input("regular")?) } else { (DEFAULT_PASSWORD.to_string(), DEFAULT_PASSWORD.to_string()) }; config.setup_api_tokens = Some( - setup_database + options.contains(&Setting::SetupDatabase) && if password != "password" || su_password != "password" { confirm("Do you want to setup default API tokens for the admin and regular users? Will be created with `lip_{username}` format") .interact()? @@ -263,7 +266,8 @@ fn setup(mut config: Config, first_setup: bool) -> std::io::Result<()> { }, ); - config.setup_database = Some(setup_database); + config.setup_database = Some(options.contains(&Setting::SetupDatabase)); + config.enable_rate_limiting = Some(options.contains(&Setting::EnableRateLimiting)); config.su_password = Some(su_password); config.password = Some(password); @@ -318,7 +322,7 @@ fn setup(mut config: Config, first_setup: bool) -> std::io::Result<()> { Repository::new("lichess-org", "lila-ws"), ]; - if setup_database { + if options.contains(&Setting::SetupDatabase) { repos_to_clone.push(Repository::new("lichess-org", "lila-db-seed")); } @@ -439,7 +443,7 @@ fn gitpod_checkout_pr() -> std::io::Result<()> { } #[allow(clippy::too_many_lines)] -fn prompt_for_optional_services() -> Result>, Error> { +fn prompt_for_services() -> Result>, Error> { multiselect( "Select which optional services to include:\n (Use arrows, to toggle, to continue)\n", ) @@ -591,6 +595,31 @@ fn prompt_for_optional_services() -> Result>, Error .interact() } +fn prompt_for_options(first_setup: bool) -> Result, Error> { + multiselect("Select options:\n") + .required(false) + .item( + Setting::SetupDatabase, + if first_setup { + "Seed the database with test users, games, etc. (Recommended)" + } else { + "Re-seed the database with test users, games, etc." + }, + "", + ) + .item( + Setting::EnableRateLimiting, + "Enable rate limiting", + "To be prod-like. Can be disabled for development/testing purposes", + ) + .initial_values(if first_setup { + vec![Setting::SetupDatabase, Setting::EnableRateLimiting] + } else { + vec![Setting::EnableRateLimiting] + }) + .interact() +} + fn hostname(mut config: Config) -> std::io::Result<()> { if Gitpod::is_host() { return error("Setting of hostname not available on Gitpod"); @@ -771,6 +800,7 @@ mod tests { setup_database: Some(true), setup_bbppairings: Some(false), enable_monitoring: Some(false), + enable_rate_limiting: Some(true), su_password: Some("foo".to_string()), password: Some("bar".to_string()), setup_api_tokens: Some(false), @@ -790,6 +820,7 @@ mod tests { "SETUP_DATABASE=true", "SETUP_BBPPAIRINGS=false", "ENABLE_MONITORING=false", + "ENABLE_RATE_LIMITING=true", "SU_PASSWORD=foo", "PASSWORD=bar", "SETUP_API_TOKENS=false", @@ -811,6 +842,7 @@ mod tests { setup_database: None, setup_bbppairings: None, enable_monitoring: None, + enable_rate_limiting: None, su_password: None, password: None, setup_api_tokens: None, diff --git a/conf/lila.conf b/conf/lila.conf index 85853027..6858f5f4 100644 --- a/conf/lila.conf +++ b/conf/lila.conf @@ -9,7 +9,7 @@ net.socket.domains = [ ${?LILA_DOMAIN} ] net.asset.base_url = ${?LILA_URL} net.asset.base_url_internal = "http://nginx" net.base_url = ${?LILA_URL} -# net.ratelimit = false +net.ratelimit = ${?ENABLE_RATE_LIMITING} mongodb.uri = "mongodb://mongodb?appName=lila" redis.uri = "redis://redis" diff --git a/docker-compose.yml b/docker-compose.yml index a54f306a..2303248d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -45,6 +45,7 @@ services: - LILA_DOMAIN=${LILA_DOMAIN:-localhost:8080} - LILA_URL=${LILA_URL:-http://localhost:8080} - ENABLE_MONITORING=${ENABLE_MONITORING:-false} + - ENABLE_RATE_LIMITING=${ENABLE_RATE_LIMITING:-true} volumes: - ./repos/lila:/lila - ./repos/chessground:/chessground