Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
9960febb4a
|
|||
0d80322526
|
|||
c112dd7407
|
|||
aaf2bce03c
|
|||
d4b2f1ffbc
|
|||
faef1469f8
|
|||
c2f93e5aea
|
|||
acfe506f51
|
|||
3ea08410bb
|
@@ -3,12 +3,11 @@ name: default
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: build
|
- name: build
|
||||||
image: rust:1.37.0-buster
|
image: rust:1.41.0-buster
|
||||||
commands:
|
commands:
|
||||||
- apt update && apt install git clang make pkg-config nettle-dev libssl-dev capnproto libsqlite3-dev -y
|
- apt update && apt install git clang make pkg-config nettle-dev libssl-dev capnproto libsqlite3-dev -y
|
||||||
- cargo test
|
- cargo test
|
||||||
- rustup target add x86_64-unknown-linux-musl
|
- cargo install --path . --root .
|
||||||
- cargo build --release --target x86_64-unknown-linux-musl
|
|
||||||
- name: dockerize
|
- name: dockerize
|
||||||
image: plugins/docker
|
image: plugins/docker
|
||||||
settings:
|
settings:
|
||||||
|
645
Cargo.lock
generated
645
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "brownpaper"
|
name = "brownpaper"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
authors = ["shim_ <shimun@shimun.net>"]
|
authors = ["shimun <shimun@shimun.net>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
@@ -4,6 +4,6 @@ VOLUME /snips
|
|||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
COPY target/release/brownpaper /bin/
|
COPY bin/brownpaper /bin/
|
||||||
|
|
||||||
ENTRYPOINT [ "/bin/brownpaper" ]
|
ENTRYPOINT [ "/bin/brownpaper" ]
|
||||||
|
176
src/main.rs
176
src/main.rs
@@ -8,167 +8,26 @@ extern crate snap;
|
|||||||
|
|
||||||
mod chacha_io;
|
mod chacha_io;
|
||||||
mod pgp;
|
mod pgp;
|
||||||
|
mod snippet;
|
||||||
|
|
||||||
use crate::pgp::KnownKeys;
|
use crate::pgp::KnownKeys;
|
||||||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
use crate::snippet::*;
|
||||||
use chacha_io::{ChaChaReader, ChaChaWriter};
|
|
||||||
use chrono::*;
|
|
||||||
use core::cell::RefCell;
|
|
||||||
use iron::method::Method;
|
use iron::method::Method;
|
||||||
use iron::modifiers::Redirect;
|
use iron::modifiers::Redirect;
|
||||||
use iron::prelude::*;
|
use iron::prelude::*;
|
||||||
use iron::url::Url;
|
use iron::url::Url;
|
||||||
use rand::Rng;
|
|
||||||
use sha2::{Digest, Sha256};
|
use iron::mime::Mime;
|
||||||
use std::borrow::BorrowMut;
|
use sha2::Digest;
|
||||||
use std::convert::TryInto;
|
|
||||||
use std::fs;
|
|
||||||
use std::fs::File;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::Path;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
struct Snippet<'a> {
|
|
||||||
id: String,
|
|
||||||
storage: &'a SnippetStorage<'a>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
struct SnippetMeta {
|
|
||||||
created: DateTime<Utc>,
|
|
||||||
compression: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SnippetStorage<'a> {
|
|
||||||
directory: &'a Path,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Snippet<'a> {
|
|
||||||
pub fn random(storage: &'a SnippetStorage) -> Snippet<'a> {
|
|
||||||
Snippet::new(
|
|
||||||
&rand::thread_rng()
|
|
||||||
.gen_ascii_chars()
|
|
||||||
.take(8)
|
|
||||||
.collect::<String>(),
|
|
||||||
storage,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(id: &str, storage: &'a SnippetStorage) -> Snippet<'a> {
|
|
||||||
Snippet {
|
|
||||||
id: id.to_string(),
|
|
||||||
storage,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn file_id(&self) -> String {
|
|
||||||
SnippetStorage::file_id(&self.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn passphrase(&self) -> ([u8; 8], [u8; 32]) {
|
|
||||||
let mut hasher = Sha256::new();
|
|
||||||
hasher.input(self.id.as_bytes());
|
|
||||||
let res = hasher.result();
|
|
||||||
let nonce: [u8; 8] = res[0..8].try_into().unwrap();
|
|
||||||
let mut hasher = Sha256::new();
|
|
||||||
hasher.input(self.id.as_bytes());
|
|
||||||
hasher.input(b"pass");
|
|
||||||
let pass: [u8; 32] = res[0..32].try_into().unwrap();
|
|
||||||
(nonce, pass)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn path(&self) -> PathBuf {
|
|
||||||
self.storage.directory.join(&self.file_id())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn metadata(&self) -> Result<SnippetMeta, io::Error> {
|
|
||||||
let mut file = File::open(self.path())?;
|
|
||||||
let (nonce, key) = self.passphrase();
|
|
||||||
self.metadata_via_handle(&mut ChaChaReader::new(&key, &nonce, &mut file))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn metadata_via_handle(&self, hdl: &mut impl Read) -> Result<SnippetMeta, io::Error> {
|
|
||||||
let timestamp = hdl.read_i64::<BigEndian>()?;
|
|
||||||
let comp_len = hdl.read_u16::<BigEndian>()? as usize;
|
|
||||||
let mut comp = Vec::with_capacity(comp_len);
|
|
||||||
comp.resize(comp_len, 0u8);
|
|
||||||
hdl.read_exact(&mut comp)?;
|
|
||||||
let comp = String::from_utf8(comp).unwrap();
|
|
||||||
Ok(SnippetMeta {
|
|
||||||
created: Utc.timestamp_millis(timestamp),
|
|
||||||
compression: Some(comp).filter(|_| comp_len > 0),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn contents(&self) -> Result<String, io::Error> {
|
|
||||||
let mut file = File::open(self.path())?;
|
|
||||||
let (nonce, key) = self.passphrase();
|
|
||||||
let mut reader = ChaChaReader::new(&key, &nonce, &mut file);
|
|
||||||
let meta = self.metadata_via_handle(&mut reader)?;
|
|
||||||
fn read_string(r: &mut impl Read) -> Result<String, io::Error> {
|
|
||||||
let mut text = String::new();
|
|
||||||
r.read_to_string(&mut text)?;
|
|
||||||
Ok(text)
|
|
||||||
}
|
|
||||||
dbg!((&meta.compression, &meta.created, self.file_id()));
|
|
||||||
match meta.compression {
|
|
||||||
Some(ref comp) if comp == "snap" => {
|
|
||||||
let mut r = snap::Reader::new(&mut reader);
|
|
||||||
read_string(&mut r)
|
|
||||||
}
|
|
||||||
_ => read_string(&mut reader),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write(self, content: &str) -> Result<Snippet<'a>, io::Error> {
|
|
||||||
let mut file = File::create(self.path())?;
|
|
||||||
let (nonce, key) = self.passphrase();
|
|
||||||
let mut writer = ChaChaWriter::new(&key, &nonce, &mut file);
|
|
||||||
writer.write_i64::<BigEndian>(Utc::now().timestamp())?;
|
|
||||||
let comp = if content.len() > 2048 {
|
|
||||||
Some("snap")
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
writer.write_u16::<BigEndian>(comp.map(|s| s.len() as u16).unwrap_or(0u16))?;
|
|
||||||
writer.write(comp.map(|s| s.as_bytes()).unwrap_or(&[0u8; 0]))?;
|
|
||||||
match comp {
|
|
||||||
Some(ref comp) if comp == &"snap" => {
|
|
||||||
let mut w = snap::Writer::new(&mut writer);
|
|
||||||
w.write_all(content.as_bytes())?
|
|
||||||
}
|
|
||||||
_ => writer.write_all(content.as_bytes())?,
|
|
||||||
};
|
|
||||||
Ok(Snippet::new(&self.id, self.storage))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> SnippetStorage<'a> {
|
|
||||||
pub fn new(directory: &'a Path) -> SnippetStorage<'a> {
|
|
||||||
SnippetStorage {
|
|
||||||
directory: directory,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn file_id(id: &str) -> String {
|
|
||||||
let mut hasher = Sha256::new();
|
|
||||||
hasher.input(id.as_bytes());
|
|
||||||
hex::encode(&hasher.result()[0..12])
|
|
||||||
}
|
|
||||||
fn has(&self, id: &str) -> bool {
|
|
||||||
self.directory.join(Self::file_id(id)).exists()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn open(&self, id: &str) -> Option<Snippet> {
|
|
||||||
if self.has(id) {
|
|
||||||
Some(Snippet::new(id, self))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
const STORAGE_DIR: &str = "/snips";
|
const STORAGE_DIR: &str = "/snips";
|
||||||
|
|
||||||
@@ -176,9 +35,9 @@ const STORAGE_DIR: &str = "/snips";
|
|||||||
const STORAGE_DIR: &str = "/tmp";
|
const STORAGE_DIR: &str = "/tmp";
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref KNOWN_KEYS: Mutex<KnownKeys> = Mutex::new(
|
static ref KNOWN_KEYS: Arc<Mutex<KnownKeys>> = Arc::new(Mutex::new(
|
||||||
KnownKeys::load_dir([STORAGE_DIR, "keys"].join("/")).expect("Failed to load pubkeys")
|
KnownKeys::load_dir([STORAGE_DIR, "keys"].join("/")).expect("Failed to load pubkeys")
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
@@ -235,7 +94,17 @@ fn handle(req: &mut Request) -> IronResult<Response> {
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(Method::Get, Some(id)) => {
|
(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| {
|
let att = storage.open(&id).map(|snip| snip.contents()).map(|res| {
|
||||||
Response::with(
|
Response::with(
|
||||||
match res.map(|text| (iron::status::Ok, text)).map_err(|err| {
|
match res.map(|text| (iron::status::Ok, text)).map_err(|err| {
|
||||||
@@ -246,6 +115,7 @@ fn handle(req: &mut Request) -> IronResult<Response> {
|
|||||||
Err(e) => (iron::status::InternalServerError, e),
|
Err(e) => (iron::status::InternalServerError, e),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
.set(mime)
|
||||||
});
|
});
|
||||||
Ok(att.unwrap_or(Response::with((iron::status::NotFound, "Not here sry"))))
|
Ok(att.unwrap_or(Response::with((iron::status::NotFound, "Not here sry"))))
|
||||||
}
|
}
|
||||||
|
@@ -38,8 +38,7 @@ impl KnownKeys {
|
|||||||
let mut content = Vec::with_capacity(2048);
|
let mut content = Vec::with_capacity(2048);
|
||||||
let helper = &*self;
|
let helper = &*self;
|
||||||
let mut v = Verifier::<&KnownKeys>::from_reader(r, helper, None)
|
let mut v = Verifier::<&KnownKeys>::from_reader(r, helper, None)
|
||||||
.map_err(|e| dbg!(e))
|
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, "Failed to verify signature"))?;
|
||||||
.unwrap();
|
|
||||||
if v.read_to_end(&mut content).is_err() {
|
if v.read_to_end(&mut content).is_err() {
|
||||||
return Err(io::Error::new(
|
return Err(io::Error::new(
|
||||||
io::ErrorKind::InvalidData,
|
io::ErrorKind::InvalidData,
|
||||||
|
153
src/snippet.rs
Normal file
153
src/snippet.rs
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
use crate::chacha_io::{ChaChaReader, ChaChaWriter};
|
||||||
|
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||||
|
use chrono::*;
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use std::iter::Iterator;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
pub struct Snippet<'a> {
|
||||||
|
pub id: String,
|
||||||
|
pub storage: &'a SnippetStorage<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub struct SnippetMeta {
|
||||||
|
created: DateTime<Utc>,
|
||||||
|
compression: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SnippetStorage<'a> {
|
||||||
|
directory: &'a Path,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Snippet<'a> {
|
||||||
|
pub fn random(storage: &'a SnippetStorage) -> Snippet<'a> {
|
||||||
|
Snippet::new(
|
||||||
|
&rand::thread_rng()
|
||||||
|
.gen_ascii_chars()
|
||||||
|
.take(8)
|
||||||
|
.collect::<String>(),
|
||||||
|
storage,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(id: &str, storage: &'a SnippetStorage) -> Snippet<'a> {
|
||||||
|
Snippet {
|
||||||
|
id: id.to_string(),
|
||||||
|
storage,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn file_id(&self) -> String {
|
||||||
|
SnippetStorage::file_id(&self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn passphrase(&self) -> ([u8; 8], [u8; 32]) {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.input(self.id.as_bytes());
|
||||||
|
let res = hasher.result();
|
||||||
|
let nonce: [u8; 8] = res[0..8].try_into().unwrap();
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.input(self.id.as_bytes());
|
||||||
|
hasher.input(b"pass");
|
||||||
|
let pass: [u8; 32] = res[0..32].try_into().unwrap();
|
||||||
|
(nonce, pass)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn path(&self) -> PathBuf {
|
||||||
|
self.storage.directory.join(&self.file_id())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn metadata(&self) -> Result<SnippetMeta, io::Error> {
|
||||||
|
let mut file = File::open(self.path())?;
|
||||||
|
let (nonce, key) = self.passphrase();
|
||||||
|
self.metadata_via_handle(&mut ChaChaReader::new(&key, &nonce, &mut file))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn metadata_via_handle(&self, hdl: &mut impl Read) -> Result<SnippetMeta, io::Error> {
|
||||||
|
let timestamp = hdl.read_i64::<BigEndian>()?;
|
||||||
|
let comp_len = hdl.read_u16::<BigEndian>()? as usize;
|
||||||
|
let mut comp = Vec::with_capacity(comp_len);
|
||||||
|
comp.resize(comp_len, 0u8);
|
||||||
|
hdl.read_exact(&mut comp)?;
|
||||||
|
let comp = String::from_utf8(comp).unwrap();
|
||||||
|
Ok(SnippetMeta {
|
||||||
|
created: Utc.timestamp(timestamp, 0),
|
||||||
|
compression: Some(comp).filter(|_| comp_len > 0),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn contents(&self) -> Result<String, io::Error> {
|
||||||
|
let mut file = File::open(self.path())?;
|
||||||
|
let (nonce, key) = self.passphrase();
|
||||||
|
let mut reader = ChaChaReader::new(&key, &nonce, &mut file);
|
||||||
|
let meta = self.metadata_via_handle(&mut reader)?;
|
||||||
|
fn read_string(r: &mut impl Read) -> Result<String, io::Error> {
|
||||||
|
let mut text = String::new();
|
||||||
|
r.read_to_string(&mut text)?;
|
||||||
|
Ok(text)
|
||||||
|
}
|
||||||
|
dbg!((&meta.compression, &meta.created, self.file_id()));
|
||||||
|
match meta.compression {
|
||||||
|
Some(ref comp) if comp == "snap" => {
|
||||||
|
let mut r = snap::Reader::new(&mut reader);
|
||||||
|
read_string(&mut r)
|
||||||
|
}
|
||||||
|
_ => read_string(&mut reader),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(self, content: &str) -> Result<Snippet<'a>, io::Error> {
|
||||||
|
let mut file = File::create(self.path())?;
|
||||||
|
let (nonce, key) = self.passphrase();
|
||||||
|
let mut writer = ChaChaWriter::new(&key, &nonce, &mut file);
|
||||||
|
writer.write_i64::<BigEndian>(Utc::now().timestamp())?;
|
||||||
|
let comp = if content.len() > 2048 {
|
||||||
|
Some("snap")
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
writer.write_u16::<BigEndian>(comp.map(|s| s.len() as u16).unwrap_or(0u16))?;
|
||||||
|
writer.write(comp.map(|s| s.as_bytes()).unwrap_or(&[0u8; 0]))?;
|
||||||
|
match comp {
|
||||||
|
Some(ref comp) if comp == &"snap" => {
|
||||||
|
let mut w = snap::Writer::new(&mut writer);
|
||||||
|
w.write_all(content.as_bytes())?
|
||||||
|
}
|
||||||
|
_ => writer.write_all(content.as_bytes())?,
|
||||||
|
};
|
||||||
|
Ok(Snippet::new(&self.id, self.storage))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> SnippetStorage<'a> {
|
||||||
|
pub fn new(directory: &'a Path) -> SnippetStorage<'a> {
|
||||||
|
SnippetStorage {
|
||||||
|
directory: directory,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn file_id(id: &str) -> String {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.input(id.as_bytes());
|
||||||
|
hex::encode(&hasher.result()[0..12])
|
||||||
|
}
|
||||||
|
fn has(&self, id: &str) -> bool {
|
||||||
|
self.directory.join(Self::file_id(id)).exists()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open(&self, id: &str) -> Option<Snippet> {
|
||||||
|
if self.has(id) {
|
||||||
|
Some(Snippet::new(id, self))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user