Skip to content

Commit

Permalink
Add max cache entries and options.
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Myers <[email protected]>
  • Loading branch information
Elizafox committed Mar 31, 2024
1 parent 7b49ab1 commit 6a078f0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
20 changes: 20 additions & 0 deletions env_example
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ REDIS_POOL_SIZE=3
# For production, it's recommended to keep this at warn or error.
RUST_LOG="warn"

# URL cache parameters.
# The URL cache is used to cache if a URL matches a banned regex.
# If it does, it is disallowed for shadifying.
# - URL_CACHE_TTL: maximum time a URL can live in the cache. (default: 3 days)
# - URL_CACHE_IDLE: maximum time a URL can be idle in the cache. (default: 1 day)
# - URL_CACHE_MAX_ENTRIES: maximum cache entries allowed (default: 5000)
URL_CACHE_TTL="3d"
URL_CACHE_IDLE="1d"
URL_CACHE_MAX_ENTRIES=5000

# Ban cache parameters.
# The ban cache is used to cache if a given IP is in a banned IP block.
# If it is, the user cannot shadify their URL.
# - BAN_CACHE_TTL: maximum time an IP can live in the cache. (default: 3 days)
# - BAN_CACHE_IDLE: maximum time an IP can be idle in the cache. (default: 1 day)
# - BAN_CACHE_MAX_ENTRIES: maximum cache entries allowed (default: 5000)
BAN_CACHE_TTL="3d"
BAN_CACHE_IDLE="1d"
BAN_CACHE_MAX_ENTRIES=5000

# Set the CSRF encryption key.
# If unset, a random one will be generated each application start (invalidating all previous sessions).
# This default MUST be changed. Generate a new one with:
Expand Down
2 changes: 1 addition & 1 deletion src/cli/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use proctitle::set_title;
use crate::env::{EnvError, Vars};

// Re-exported
pub(crate) use crate::cli::parser::UsernameArgument;
pub use crate::cli::parser::UsernameArgument;

#[async_trait::async_trait]
pub trait CliSubcommand {
Expand Down
13 changes: 11 additions & 2 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ mod defaults {
2
}

pub(super) const fn max_cache_entries() -> u64 {
5000
}

pub(super) fn sitename() -> String {
"ShadyURL".to_string()
}
Expand All @@ -50,11 +54,11 @@ mod defaults {
ret
}

pub(super) fn duration_1d() -> Duration {
pub(super) const fn duration_1d() -> Duration {
Duration::days(1)
}

pub(super) fn duration_3d() -> Duration {
pub(super) const fn duration_3d() -> Duration {
Duration::days(3)
}
}
Expand Down Expand Up @@ -147,6 +151,11 @@ pub struct Vars {
)]
pub(crate) url_cache_idle: Duration,

#[serde(default = "defaults::max_cache_entries")]
pub(crate) ban_cache_max_entries: u64,
#[serde(default = "defaults::max_cache_entries")]
pub(crate) url_cache_max_entries: u64,

// FIXME: encrypt entire session with this, but axum-login isn't ready
#[serde(
deserialize_with = "deserializers::csrf_key",
Expand Down
16 changes: 13 additions & 3 deletions src/web/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,19 @@ impl App {
let db = Arc::new(Database::get_with_connect_options(opt).await?);

// TODO: configurable
let bancache = BanCache::new(db.clone(), 5000, env.ban_cache_ttl, env.ban_cache_idle);
let urlcache =
UrlCache::new(db.clone(), 5000, env.url_cache_ttl, env.url_cache_idle).await?;
let bancache = BanCache::new(
db.clone(),
env.ban_cache_max_entries,
env.ban_cache_ttl,
env.ban_cache_idle,
);
let urlcache = UrlCache::new(
db.clone(),
env.url_cache_max_entries,
env.url_cache_ttl,
env.url_cache_idle,
)
.await?;

let csrf_crypto_engine = CryptoEngine::new(&env.csrf_key.into());

Expand Down

0 comments on commit 6a078f0

Please sign in to comment.