use std::fmt;
use std::sync::Arc;
use laravel::config::Section;
use laravel::http::Request;
#[derive(Debug, Clone, Section)]
pub struct StudioConfig {
#[env("STUDIO_ENABLED", default = true)]
pub enabled: bool,
#[env("STUDIO_PATH", default = "studio")]
pub path: String,
#[env("STUDIO_ALLOW_IN_PRODUCTION", default = false)]
pub allow_in_production: bool,
#[env("STUDIO_MCP_ENABLED", default = true)]
pub mcp_enabled: bool,
#[env("STUDIO_MAX_TRACES", default = 500)]
pub max_traces: usize,
#[env("STUDIO_MAX_QUERIES", default = 2000)]
pub max_queries: usize,
#[env("STUDIO_MAX_LOGS", default = 2000)]
pub max_logs: usize,
#[env("STUDIO_MAX_EXCEPTIONS", default = 500)]
pub max_exceptions: usize,
#[env("STUDIO_MAX_MAILS", default = 200)]
pub max_mails: usize,
#[env("STUDIO_MAX_HTTP_CLIENT", default = 500)]
pub max_http_client: usize,
#[env("STUDIO_MAX_CACHE", default = 1000)]
pub max_cache: usize,
#[env("STUDIO_SLOW_QUERY_MS", default = 100)]
pub slow_query_ms: u64,
#[env("STUDIO_SLOW_REQUEST_MS", default = 500)]
pub slow_request_ms: u64,
#[env("STUDIO_IGNORE_PATHS", separator = ",")]
pub ignore_paths: Vec<String>,
#[env("STUDIO_JOURNAL", default = true)]
pub journal: bool,
#[env("STUDIO_JOURNAL_MAX_MB", default = 32)]
pub journal_max_mb: u64,
#[env("STUDIO_JOURNAL_HOURS", default = 24)]
pub journal_hours: u64,
#[env("STUDIO_ALLOWED_HOSTS", separator = ",")]
pub allowed_hosts: Vec<String>,
}
impl StudioConfig {
#[must_use]
pub fn mount_path(&self) -> String {
match self.path.trim().trim_matches('/') {
"" => "/studio".to_owned(),
path => format!("/{path}"),
}
}
#[must_use]
pub fn should_ignore_path(&self, path: &str) -> bool {
let under = |prefix: &str| {
path.strip_prefix(prefix)
.is_some_and(|rest| rest.is_empty() || rest.starts_with('/'))
};
under(&self.mount_path())
|| under("/vendor/studio")
|| self
.ignore_paths
.iter()
.map(|ignored| ignored.trim().trim_matches('/'))
.any(|ignored| !ignored.is_empty() && under(&format!("/{ignored}")))
}
}
pub type AuthorizeFn = Arc<dyn Fn(&Request) -> bool + Send + Sync + 'static>;
#[derive(Clone)]
pub struct StudioGate {
pub(crate) enabled: bool,
pub(crate) is_local_or_testing: bool,
pub(crate) allow_in_production: bool,
pub(crate) authorize: Option<AuthorizeFn>,
pub(crate) allowed_hosts: Vec<String>,
}
impl StudioGate {
#[must_use]
pub fn is_authorized(&self, request: &Request) -> bool {
if let Some(check) = &self.authorize {
return check(request);
}
if self.allow_in_production {
return true;
}
self.is_local_or_testing && self.is_local_host(&request.host())
}
#[must_use]
pub fn is_local_host(&self, host: &str) -> bool {
let host = host.trim().to_ascii_lowercase();
let name = strip_port(&host);
if name.is_empty() || name == "localhost" || name.parse::<std::net::IpAddr>().is_ok() {
return true;
}
if name.ends_with(".localhost") || name.ends_with(".test") {
return true;
}
self.allowed_hosts
.iter()
.any(|allowed| allowed == "*" || allowed == name || strip_port(allowed) == name)
}
}
fn strip_port(host: &str) -> &str {
if let Some(rest) = host.strip_prefix('[') {
return rest.split(']').next().unwrap_or(rest);
}
match host.rsplit_once(':') {
Some((name, port)) if !name.contains(':') && port.chars().all(|c| c.is_ascii_digit()) => name,
_ => host,
}
}
impl fmt::Debug for StudioGate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StudioGate")
.field("enabled", &self.enabled)
.field("is_local_or_testing", &self.is_local_or_testing)
.field("allow_in_production", &self.allow_in_production)
.field("has_custom_authorizer", &self.authorize.is_some())
.field("allowed_hosts", &self.allowed_hosts)
.finish()
}
}