2018-12-19 00:57:45 +01:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fs::{create_dir_all, remove_file, symlink_metadata, File};
|
2018-02-10 01:00:55 +01:00
|
|
|
use std::io::prelude::*;
|
2018-12-18 22:33:32 +01:00
|
|
|
use std::time::SystemTime;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
use rocket::http::ContentType;
|
2018-12-19 00:57:45 +01:00
|
|
|
use rocket::response::Content;
|
|
|
|
use rocket::Route;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
use reqwest;
|
|
|
|
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::CONFIG;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
pub fn routes() -> Vec<Route> {
|
|
|
|
routes![icon]
|
|
|
|
}
|
|
|
|
|
2018-12-19 00:57:45 +01:00
|
|
|
const FALLBACK_ICON: &[u8; 344] = include_bytes!("../static/fallback-icon.png");
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
#[get("/<domain>/icon.png")]
|
|
|
|
fn icon(domain: String) -> Content<Vec<u8>> {
|
2018-02-17 18:48:42 +01:00
|
|
|
let icon_type = ContentType::new("image", "x-icon");
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
// Validate the domain to avoid directory traversal attacks
|
2018-09-13 21:55:23 +02:00
|
|
|
if domain.contains('/') || domain.contains("..") {
|
2018-12-19 00:57:45 +01:00
|
|
|
return Content(icon_type, FALLBACK_ICON.to_vec());
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
let icon = get_icon(&domain);
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-02-17 18:48:42 +01:00
|
|
|
Content(icon_type, icon)
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 00:57:45 +01:00
|
|
|
fn get_icon(domain: &str) -> Vec<u8> {
|
2018-06-12 21:09:42 +02:00
|
|
|
let path = format!("{}/{}.png", CONFIG.icon_cache_folder, domain);
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
if let Some(icon) = get_cached_icon(&path) {
|
|
|
|
return icon;
|
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
let url = get_icon_url(&domain);
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
// Get the icon, or fallback in case of error
|
|
|
|
match download_icon(&url) {
|
|
|
|
Ok(icon) => {
|
|
|
|
save_icon(&path, &icon);
|
|
|
|
icon
|
2018-12-19 00:57:45 +01:00
|
|
|
}
|
2018-07-01 15:27:42 +02:00
|
|
|
Err(e) => {
|
2018-12-06 20:35:25 +01:00
|
|
|
error!("Error downloading icon: {:?}", e);
|
2018-12-18 22:33:32 +01:00
|
|
|
mark_negcache(&path);
|
2018-12-19 00:57:45 +01:00
|
|
|
FALLBACK_ICON.to_vec()
|
2018-07-01 15:27:42 +02:00
|
|
|
}
|
2018-06-12 21:09:42 +02:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
fn get_cached_icon(path: &str) -> Option<Vec<u8>> {
|
2018-12-18 22:33:32 +01:00
|
|
|
// Check for expiration of negatively cached copy
|
|
|
|
if icon_is_negcached(path) {
|
2018-12-19 00:57:45 +01:00
|
|
|
return Some(FALLBACK_ICON.to_vec());
|
2018-12-18 22:33:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for expiration of successfully cached copy
|
|
|
|
if icon_is_expired(path) {
|
2018-12-19 00:57:45 +01:00
|
|
|
return None;
|
2018-12-18 22:33:32 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 00:53:11 +01:00
|
|
|
// Try to read the cached icon, and return it if it exists
|
2018-06-11 15:44:37 +02:00
|
|
|
if let Ok(mut f) = File::open(path) {
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
|
|
|
|
if f.read_to_end(&mut buffer).is_ok() {
|
2018-06-12 21:09:42 +02:00
|
|
|
return Some(buffer);
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-12-18 22:33:32 +01:00
|
|
|
fn file_is_expired(path: &str, ttl: u64) -> Result<bool, Box<Error>> {
|
|
|
|
let meta = symlink_metadata(path)?;
|
|
|
|
let modified = meta.modified()?;
|
|
|
|
let age = SystemTime::now().duration_since(modified)?;
|
|
|
|
|
|
|
|
Ok(ttl > 0 && ttl <= age.as_secs())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn icon_is_negcached(path: &str) -> bool {
|
|
|
|
let miss_indicator = path.to_owned() + ".miss";
|
|
|
|
let expired = file_is_expired(&miss_indicator, CONFIG.icon_cache_negttl);
|
2018-12-19 00:57:45 +01:00
|
|
|
|
2018-12-18 22:33:32 +01:00
|
|
|
match expired {
|
|
|
|
// No longer negatively cached, drop the marker
|
|
|
|
Ok(true) => {
|
2018-12-19 00:57:45 +01:00
|
|
|
if let Err(e) = remove_file(&miss_indicator) {
|
|
|
|
error!("Could not remove negative cache indicator for icon {:?}: {:?}", path, e);
|
2018-12-18 22:33:32 +01:00
|
|
|
}
|
|
|
|
false
|
2018-12-19 00:57:45 +01:00
|
|
|
}
|
2018-12-18 22:33:32 +01:00
|
|
|
// The marker hasn't expired yet.
|
2018-12-19 00:57:45 +01:00
|
|
|
Ok(false) => true,
|
2018-12-18 22:33:32 +01:00
|
|
|
// The marker is missing or inaccessible in some way.
|
2018-12-19 00:57:45 +01:00
|
|
|
Err(_) => false,
|
2018-12-18 22:33:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mark_negcache(path: &str) {
|
|
|
|
let miss_indicator = path.to_owned() + ".miss";
|
|
|
|
File::create(&miss_indicator).expect("Error creating negative cache marker");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn icon_is_expired(path: &str) -> bool {
|
|
|
|
let expired = file_is_expired(path, CONFIG.icon_cache_ttl);
|
|
|
|
expired.unwrap_or(true)
|
|
|
|
}
|
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
fn get_icon_url(domain: &str) -> String {
|
|
|
|
if CONFIG.local_icon_extractor {
|
|
|
|
format!("http://{}/favicon.ico", domain)
|
|
|
|
} else {
|
|
|
|
format!("https://icons.bitwarden.com/{}/icon.png", domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn download_icon(url: &str) -> Result<Vec<u8>, reqwest::Error> {
|
2018-12-06 20:35:25 +01:00
|
|
|
info!("Downloading icon for {}...", url);
|
2018-06-12 21:09:42 +02:00
|
|
|
let mut res = reqwest::get(url)?;
|
|
|
|
|
|
|
|
res = res.error_for_status()?;
|
|
|
|
|
|
|
|
let mut buffer: Vec<u8> = vec![];
|
|
|
|
res.copy_to(&mut buffer)?;
|
|
|
|
|
|
|
|
Ok(buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_icon(path: &str, icon: &[u8]) {
|
|
|
|
create_dir_all(&CONFIG.icon_cache_folder).expect("Error creating icon cache");
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-11 15:44:37 +02:00
|
|
|
if let Ok(mut f) = File::create(path) {
|
2018-06-12 21:09:42 +02:00
|
|
|
f.write_all(icon).expect("Error writing icon file");
|
2018-02-10 01:00:55 +01:00
|
|
|
};
|
|
|
|
}
|