|
1 | 1 | use rocket::http::Method; |
2 | 2 | use rocket_cors::{AllowedHeaders, AllowedOrigins}; |
| 3 | +use aw_datastore::Datastore; |
| 4 | +use std::sync::Mutex; |
3 | 5 |
|
4 | 6 | use crate::config::AWConfig; |
5 | 7 |
|
6 | | -pub fn cors(config: &AWConfig) -> rocket_cors::Cors { |
| 8 | +pub fn cors(config: &AWConfig, datastore_mutex: &Mutex<Datastore>) -> rocket_cors::Cors { |
7 | 9 | let root_url = format!("http://127.0.0.1:{}", config.port); |
8 | 10 | let root_url_localhost = format!("http://localhost:{}", config.port); |
9 | 11 | let mut allowed_exact_origins = vec![root_url, root_url_localhost]; |
10 | 12 | allowed_exact_origins.extend(config.cors.clone()); |
11 | 13 |
|
| 14 | + let db = datastore_mutex.lock().unwrap(); |
| 15 | + if let Ok(cors_origins_str) = db.get_key_value("settings.cors_origins") { |
| 16 | + |
| 17 | + let cors_origins: Vec<String> = cors_origins_str |
| 18 | + .trim_matches('"') |
| 19 | + .split(',') |
| 20 | + .map(|s| s.trim().to_string()) |
| 21 | + .filter(|s| !s.is_empty()) |
| 22 | + .filter(|s| { |
| 23 | + let is_valid = s.starts_with("http://") |
| 24 | + || s.starts_with("https://") |
| 25 | + || s.starts_with("chrome-extension://") |
| 26 | + || s.starts_with("moz-extension://"); |
| 27 | + if !is_valid { |
| 28 | + log::warn!("Ignoring invalid CORS origin: '{}'", s); |
| 29 | + } |
| 30 | + is_valid |
| 31 | + }) |
| 32 | + .collect(); |
| 33 | + info!("Parsed cors_origins from settings: {:?}", cors_origins); |
| 34 | + allowed_exact_origins.extend(cors_origins); |
| 35 | + } |
| 36 | + drop(db); |
| 37 | + |
12 | 38 | if config.testing { |
13 | 39 | allowed_exact_origins.push("http://127.0.0.1:27180".to_string()); |
14 | 40 | allowed_exact_origins.push("http://localhost:27180".to_string()); |
|
0 commit comments