brownpaper/src/main.rs
2020-12-18 20:51:36 +01:00

141 lines
5.0 KiB
Rust

#[macro_use]
extern crate lazy_static;
extern crate chrono;
extern crate iron;
extern crate rand;
extern crate sequoia_openpgp as openpgp;
extern crate snap;
mod chacha_io;
mod pgp;
mod snippet;
use crate::pgp::KnownKeys;
use crate::snippet::*;
use iron::method::Method;
use iron::modifiers::Redirect;
use iron::prelude::*;
use iron::url::Url;
use iron::mime::Mime;
use sha2::Digest;
use std::env::{self, args};
use std::io;
use std::io::prelude::*;
use std::iter::Iterator;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;
lazy_static! {
static ref STORAGE_DIR: String =
env::var("BROWNPAPER_STORAGE_DIR").unwrap_or("/snips".to_string());
static ref KNOWN_KEYS: Arc<Mutex<KnownKeys>> = Arc::new(Mutex::new(
KnownKeys::load_dir([&*STORAGE_DIR, "keys"].join("/")).expect("Failed to load pubkeys")
));
}
const VERSION: &str = env!("CARGO_PKG_VERSION");
fn handle(req: &mut Request) -> IronResult<Response> {
println!("{}", req.url);
let storage = SnippetStorage::new(&Path::new(&*STORAGE_DIR));
let segments: Vec<&str> = req.url.path();
match (&req.method, segments.first()) {
(Method::Get, Some(&"version")) => Ok(Response::with((iron::status::Ok, VERSION))),
(Method::Post, Some(path)) => {
if path == &"new" {
let snip = {
let pgp_text: String = {
let bytes = ((&mut req.body).bytes().take(1024 * 512).collect::<Result<
Vec<u8>,
io::Error,
>>(
))
.map_err(|err| IronError::new(err, ""))?;
String::from_utf8(bytes)
.map_err(|err| IronError::new(err, "Invalid utf8"))?
};
let b_text = KNOWN_KEYS
.lock()
.unwrap() //.map_err(|_| IronError::new(std::error::Error::from("Mutex Err"), "PGP Context unavailable"))?
.verify(pgp_text.as_bytes())
.map_err(|err| IronError::new(err, "Untrusted signature"))?;
let text = String::from_utf8(b_text).unwrap();
Snippet::random(&storage).write(&*text).map_err(|err| {
let msg = format!("Failed to save snippet: {:?}", &err);
IronError::new(err, msg)
})
};
snip.map(|snip| {
let mut snip_url: Url = req.url.clone().into();
snip_url.set_path(&*("/".to_string() + &*snip.id));
Response::with((
iron::status::TemporaryRedirect,
Redirect(iron::Url::from_generic_url(snip_url).unwrap()),
))
/*Response::with((
iron::status::Ok,
format!(
"<meta http-equiv=\"refresh\" content=\"0; url={}/\" />",
snip_url
),
))*/
})
} else {
Ok(Response::with((
iron::status::BadRequest,
"Post to /new or die",
)))
}
}
(Method::Get, Some(path)) => {
let (id, mime) = {
let mut parts = path.split(".");
(
parts.next().unwrap().to_string(),
Some(parts.collect::<Vec<_>>().join("/"))
.filter(|s| s.len() > 0)
.and_then(|format| format.parse::<Mime>().ok()),
)
};
let mime = mime.unwrap_or("text/plain".parse().unwrap());
let att = storage.open(&id).map(|snip| snip.contents()).map(|res| {
Response::with(
match res.map(|text| (iron::status::Ok, text)).map_err(|err| {
let msg = format!("Failed to load snippet: {:?}", &err);
msg
}) {
Ok(res) => res,
Err(e) => (iron::status::InternalServerError, e),
},
)
.set(mime)
});
Ok(att.unwrap_or(Response::with((iron::status::NotFound, "Not here sry"))))
}
(Method::Get, _) => Ok(Response::with((iron::status::NotFound, "Wrong path pal"))),
_ => Ok(Response::with((iron::status::BadRequest, "Give or take"))),
}
}
fn main() {
let chain = Chain::new(handle);
println!("Starting brownpaper: {}", &*STORAGE_DIR);
Iron::new(chain).http(
args()
.skip(1)
.next()
.map(|ip| {
ip.parse::<SocketAddr>()
.expect("can't parse socket address")
})
.unwrap_or("0.0.0.0:3000".parse::<SocketAddr>().unwrap())
.to_string()
.as_str(),
);
}