Compare commits

...

6 Commits

Author SHA1 Message Date
Cadey Ratio d10611ac35 more words
continuous-integration/drone/push Build encountered an error Details
2020-07-31 13:42:47 -04:00
Cadey Ratio 687bece5be better stuff :D
continuous-integration/drone/push Build is passing Details
2020-07-31 13:38:33 -04:00
Cadey Ratio 9e217953b9 input test and static file serving with majsite
continuous-integration/drone/push Build is passing Details
2020-07-31 13:13:51 -04:00
Cadey Ratio e84375a572 file serving
continuous-integration/drone/push Build is passing Details
2020-07-31 12:16:15 -04:00
Cadey Ratio 0a100b081c fix gitignore 2020-07-31 07:59:27 -04:00
Cadey Ratio 42319764c4 karnycukta experiment 2020-07-31 07:49:06 -04:00
17 changed files with 460 additions and 39 deletions

View File

@ -1,5 +1,12 @@
# Changelog # Changelog
## 0.5.0
### ADDED
- A few more helper methods for making text/gemini nodes and responses.
`majsite` now serves static files and tests input from the user.
## 0.4.1 ## 0.4.1
Oops Oops

View File

@ -1,6 +1,6 @@
[package] [package]
name = "maj" name = "maj"
version = "0.4.2" version = "0.5.0"
authors = ["Christine Dodrill <me@christine.website>"] authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018" edition = "2018"
license = "0BSD" license = "0BSD"
@ -10,22 +10,23 @@ repository = "https://tulpa.dev/cadey/maj"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
async-std = { version = "1.6", optional = true }
async-tls = { default-features = false, optional = true, version = "0" }
async-trait = { version = "0", optional = true } async-trait = { version = "0", optional = true }
log = "0.4"
mime_guess = "2.0"
num = "0.2" num = "0.2"
num-derive = "0.3" num-derive = "0.3"
num-traits = "0.2" num-traits = "0.2"
rustls = { version = "0.18", optional = true, features = ["dangerous_configuration"] }
tokio-rustls = { version = "0.14", features = ["dangerous_configuration"], optional = true }
webpki = { version = "0.21.0", optional = true }
webpki-roots = { version = "0.20", optional = true }
tokio = { version = "0.2", features = ["full"], optional = true }
async-tls = { default-features = false, optional = true, version = "0" }
async-std = { version = "1.6", optional = true }
log = "0.4"
url = "2"
thiserror = "1"
structopt = "0.3"
once_cell = "1.4" once_cell = "1.4"
rustls = { version = "0.18", optional = true, features = ["dangerous_configuration"] }
structopt = "0.3"
thiserror = "1"
tokio-rustls = { version = "0.14", features = ["dangerous_configuration"], optional = true }
tokio = { version = "0.2", features = ["full"], optional = true }
url = "2"
webpki-roots = { version = "0.20", optional = true }
webpki = { version = "0.21.0", optional = true }
[dev-dependencies] [dev-dependencies]
pretty_env_logger = "0.4" pretty_env_logger = "0.4"
@ -55,5 +56,6 @@ server = [
members = [ members = [
"./majc", "./majc",
"./majd", "./majd",
"./site" "./site",
"./pilno/karnycukta"
] ]

2
pilno/karnycukta/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.epub
*.mobi

View File

@ -0,0 +1,22 @@
[package]
name = "karnycukta"
version = "0.1.0"
authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1"
async-std = "1.5"
async-trait = "0"
atom_syndication = "0.9"
chrono = "*"
log = "0"
pretty_env_logger = "0.4"
webpki = "0.21.0"
rustls = { version = "0.18", features = ["dangerous_configuration"] }
structopt = "0.3"
tokio = { version = "0.2", features = ["full"] }
maj = { path = "../..", features = ["server", "client"], default-features = false }

View File

@ -0,0 +1,38 @@
use anyhow::Result;
use structopt::StructOpt;
mod selfu;
mod zbasu;
#[derive(StructOpt, Debug)]
#[structopt(about = "la .karnycukta. cu finti lo samcukta fo lo zo .gemlogs.")]
enum Cmd {
/// selfu la samse'u
Selfu {
#[structopt(flatten)]
opts: selfu::Options,
},
/// zbasu lo cukta
Zbasu {
#[structopt(long, short = "n")]
nuzyurli: Vec<String>,
/// How many days to look back
#[structopt(long, short = "d")]
seldei: usize,
},
}
#[tokio::main]
async fn main() -> Result<()> {
pretty_env_logger::init();
let cmd = Cmd::from_args();
log::debug!("{:?}", cmd);
match cmd {
Cmd::Selfu { opts } => selfu::run(opts).await?,
Cmd::Zbasu { nuzyurli, seldei } => zbasu::run(nuzyurli, seldei).await?,
}
Ok(())
}

View File

@ -0,0 +1,66 @@
use anyhow::Result;
use rustls::internal::pemfile::{certs, rsa_private_keys};
use rustls::{
AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig,
};
use std::fs::File;
use std::io::{self, BufReader};
use std::path::{Path, PathBuf};
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
pub struct Options {
/// host to listen on
#[structopt(short = "H", long, env = "HOST", default_value = "10.77.2.8")]
host: String,
/// port to listen on
#[structopt(short = "p", long, env = "PORT", default_value = "1965")]
port: u16,
/// cert file
#[structopt(short = "c", long = "cert", env = "CERT_FILE")]
cert: PathBuf,
/// key file
#[structopt(short = "k", long = "key", env = "KEY_FILE")]
key: PathBuf,
/// server hostname
#[structopt(
long = "hostname",
env = "SERVER_HOSTNAME",
default_value = "shachi.wg.akua"
)]
hostname: String,
}
fn load_certs(path: &Path) -> io::Result<Vec<Certificate>> {
certs(&mut BufReader::new(File::open(path)?))
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))
}
fn load_keys(path: &Path) -> io::Result<Vec<PrivateKey>> {
rsa_private_keys(&mut BufReader::new(File::open(path)?))
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
}
pub async fn run(opts: Options) -> Result<()> {
let certs = load_certs(&opts.cert)?;
let mut keys = load_keys(&opts.key)?;
log::info!(
"serving gemini://{} on {}:{}",
opts.hostname,
opts.host,
opts.port
);
let mut config = ServerConfig::new(AllowAnyAnonymousOrAuthenticatedClient::new(
RootCertStore::empty(),
));
config
.set_single_cert(certs, keys.remove(0))
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
Ok(())
}

View File

@ -0,0 +1,89 @@
use anyhow::{anyhow, Result};
use atom_syndication as atom;
use maj::gemini::Node;
use rustls::ClientConfig;
use std::io::{self, BufReader, Cursor, Write};
use std::ops::Sub;
use std::str;
use chrono::{Duration, prelude::*};
mod tls;
fn gem_to_md(tcana: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
use Node::*;
for tcan in tcana {
match tcan {
Text(body) => {
if body == "---" {
break;
}
write!(out, "{}\n", body)?;
}
Link { to, name } => match name {
Some(name) => write!(out, "[{}]({})\n\n", name, to)?,
None => write!(out, "[{0}]({0})", to)?,
},
Preformatted(body) => write!(out, "```\n{}\n```\n\n", body)?,
Heading { level, body } => {
write!(out, "##{} {}\n\n", "#".repeat(level as usize), body)?
}
ListItem(body) => write!(out, "* {}\n", body)?,
Quote(body) => write!(out, "> {}\n\n", body)?,
}
}
Ok(())
}
async fn read_feed(gurl: String, cfg: ClientConfig) -> Result<atom::Feed> {
let resp = maj::get(gurl, cfg).await?;
if resp.status != maj::StatusCode::Success {
Err(anyhow!(
"expected success, got: {} {}",
resp.status as u8,
resp.meta
))?;
}
let body = Cursor::new(resp.body);
let feed = atom::Feed::read_from(BufReader::new(body))?;
Ok(feed)
}
pub async fn run(nuzyurli: Vec<String>, seldei: usize) -> Result<()> {
let cfg = tls::config();
let ca: atom::FixedDateTime = Utc::now().into();
for urli in nuzyurli {
let feed = read_feed(urli.clone(), cfg.clone()).await?;
log::info!("reading entries for {}: {}", urli, feed.title);
println!("## {}\nBy {}\n\n", feed.title, feed.authors[0].name);
println!("[Site link]({})\n\n", feed.id);
for entry in feed.entries {
if ca.sub(entry.updated) > Duration::days(seldei as i64) {
continue;
}
let href: String = entry.links()[0].href.clone();
let resp = maj::get(href, cfg.clone()).await?;
if resp.status != maj::StatusCode::Success {
return Err(anyhow!(
"expected success, got: {} {}",
resp.status as u8,
resp.meta
));
}
let body = str::from_utf8(&resp.body)?;
let body = maj::gemini::parse(body);
let mut buf: Vec<u8> = vec![];
gem_to_md(body, &mut buf)?;
println!("{}", str::from_utf8(&buf)?);
}
}
Ok(())
}

View File

@ -0,0 +1,24 @@
use std::sync::Arc;
pub fn config() -> rustls::ClientConfig {
let mut config = rustls::ClientConfig::new();
config
.dangerous()
.set_certificate_verifier(Arc::new(NoCertificateVerification {}));
config
}
struct NoCertificateVerification {}
impl rustls::ServerCertVerifier for NoCertificateVerification {
fn verify_server_cert(
&self,
_roots: &rustls::RootCertStore,
_presented_certs: &[rustls::Certificate],
_dns_name: webpki::DNSNameRef<'_>,
_ocsp: &[u8],
) -> Result<rustls::ServerCertVerified, rustls::TLSError> {
Ok(rustls::ServerCertVerified::assertion())
}
}

View File

@ -1,10 +1,22 @@
{ pkgs ? import <nixpkgs> {} }: let
moz_overlay = import (builtins.fetchTarball
pkgs.mkShell { "https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz");
pkgs = import <nixpkgs> { overlays = [ moz_overlay ]; };
nur = import (builtins.fetchTarball
"https://github.com/nix-community/NUR/archive/master.tar.gz") {
inherit pkgs;
};
tex = with pkgs;
texlive.combine { inherit (texlive) scheme-medium bitter titlesec; };
in pkgs.mkShell {
buildInputs = with pkgs; [ buildInputs = with pkgs; [
rustc cargo rls rustfmt cargo-watch pkgs.latest.rustChannels.stable.rust
pkg-config pkg-config
ncurses ncurses
kindlegen
nur.repos.mic92.pandoc-bin
tex
]; ];
} }

View File

@ -11,7 +11,8 @@ anyhow = "1"
async-std = "1.5" async-std = "1.5"
async-trait = "0" async-trait = "0"
log = "0" log = "0"
pretty_env_logger = "0.4" env_logger = "0"
percent-encoding = "2"
rustls = { version = "0.18", features = ["dangerous_configuration"] } rustls = { version = "0.18", features = ["dangerous_configuration"] }
structopt = "0.3" structopt = "0.3"

View File

@ -1,12 +1,13 @@
use async_std::task; use async_std::task;
use maj::{ use maj::{
gemini::Builder, gemini::{Builder, Node},
route, seg, route, seg,
server::{Error, Handler as MajHandler, Request}, server::{Error, Handler as MajHandler, Request},
split, Response, split, Response,
}; };
use rustls::internal::pemfile::{certs, rsa_private_keys}; use percent_encoding::percent_decode_str;
use rustls::{ use rustls::{
internal::pemfile::{certs, rsa_private_keys},
AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig, AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig,
}; };
use std::fs::File; use std::fs::File;
@ -33,6 +34,10 @@ struct Options {
#[structopt(short = "k", long = "key", env = "KEY_FILE")] #[structopt(short = "k", long = "key", env = "KEY_FILE")]
key: PathBuf, key: PathBuf,
/// static path
#[structopt(short = "s", long, env = "STATIC_PATH", default_value = "./static")]
static_path: PathBuf,
/// server hostname /// server hostname
#[structopt( #[structopt(
long = "hostname", long = "hostname",
@ -53,7 +58,7 @@ fn load_keys(path: &Path) -> io::Result<Vec<PrivateKey>> {
} }
fn main() -> Result<(), maj::server::Error> { fn main() -> Result<(), maj::server::Error> {
pretty_env_logger::init(); env_logger::init();
let opts = Options::from_args(); let opts = Options::from_args();
let certs = load_certs(&opts.cert)?; let certs = load_certs(&opts.cert)?;
let mut keys = load_keys(&opts.key)?; let mut keys = load_keys(&opts.key)?;
@ -75,6 +80,7 @@ fn main() -> Result<(), maj::server::Error> {
task::block_on(maj::server::serve( task::block_on(maj::server::serve(
Arc::new(Handler { Arc::new(Handler {
hostname: opts.hostname, hostname: opts.hostname,
files: maj::server::files::Handler::new(opts.static_path),
}), }),
config, config,
opts.host, opts.host,
@ -86,6 +92,7 @@ fn main() -> Result<(), maj::server::Error> {
struct Handler { struct Handler {
hostname: String, hostname: String,
files: maj::server::files::Handler,
} }
async fn index() -> Result<maj::Response, maj::server::Error> { async fn index() -> Result<maj::Response, maj::server::Error> {
@ -110,6 +117,41 @@ async fn need_cert(req: Request) -> Result<Response, Error> {
} }
} }
async fn input(req: Request) -> Result<Response, Error> {
match req.url.query() {
None => Ok(Response::input("test")),
Some(q) => Ok({
use Node::*;
let result = vec![
Heading {
level: 1,
body: "Input test".to_string(),
},
Node::blank(),
Text("You gave me:".to_string()),
Preformatted(format!("{}", percent_decode_str(q).decode_utf8()?)),
];
Response::render(result)
}),
}
}
async fn user(name: String) -> Result<Response, Error> {
Ok(Response::render({
use Node::*;
vec![
Heading {
level: 1,
body: format!("{}'s user page", name),
},
Node::blank(),
Text(format!("this is a test page for {}", name)),
]
}))
}
#[async_trait::async_trait] #[async_trait::async_trait]
impl MajHandler for Handler { impl MajHandler for Handler {
async fn handle(&self, req: Request) -> Result<Response, Error> { async fn handle(&self, req: Request) -> Result<Response, Error> {
@ -127,9 +169,11 @@ impl MajHandler for Handler {
route!(req.url.path(), { route!(req.url.path(), {
(/) => index().await; (/) => index().await;
(/"cert") => need_cert(req).await; (/"cert") => need_cert(req).await;
(/"input") => input(req).await;
(/"majc") => majc().await; (/"majc") => majc().await;
(/"~"/[name: String][/rest..]) => user(name).await;
}); });
Ok(Response::not_found()) self.files.handle(req).await
} }
} }

3
site/static/foo.gmi Normal file
View File

@ -0,0 +1,3 @@
# foo
Hahaha static serving works

3
site/static/index.gmi Normal file
View File

@ -0,0 +1,3 @@
# test
Hi there

View File

@ -156,6 +156,12 @@ pub enum Node {
Quote(String), Quote(String),
} }
impl Node {
pub fn blank() -> Node {
Node::Text("".to_string())
}
}
pub fn parse(doc: &str) -> Vec<Node> { pub fn parse(doc: &str) -> Vec<Node> {
let mut result: Vec<Node> = vec![]; let mut result: Vec<Node> = vec![];
let mut collect_preformatted: bool = false; let mut collect_preformatted: bool = false;

View File

@ -1,5 +1,6 @@
use crate::{gemini, StatusCode}; use crate::{gemini, StatusCode};
use num::FromPrimitive; use num::FromPrimitive;
use std::fmt;
use std::io::{self, prelude::*, ErrorKind}; use std::io::{self, prelude::*, ErrorKind};
/// A Gemini response as specified in [the spec](https://gemini.circumlunar.space/docs/specification.html). /// A Gemini response as specified in [the spec](https://gemini.circumlunar.space/docs/specification.html).
@ -10,7 +11,31 @@ pub struct Response {
pub body: Vec<u8>, pub body: Vec<u8>,
} }
#[derive(thiserror::Error, Debug)]
pub struct ResponseStatusError {
status: StatusCode,
meta: String,
}
impl fmt::Display for ResponseStatusError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{:?} ({}): {}",
self.status, self.status as u8, self.meta
)
}
}
impl Response { impl Response {
pub fn with_body(meta: String, body: Vec<u8>) -> Response {
Response {
status: StatusCode::Success,
meta: meta,
body: body,
}
}
pub fn gemini(body: Vec<u8>) -> Response { pub fn gemini(body: Vec<u8>) -> Response {
Response { Response {
status: StatusCode::Success, status: StatusCode::Success,

62
src/server/files.rs Normal file
View File

@ -0,0 +1,62 @@
/// A simple handler for disk based files. Will optionally chop off a prefix.
use super::{Handler as MajHandler, Request, Result};
use crate::Response;
use async_trait::async_trait;
use std::ffi::OsStr;
use std::path::PathBuf;
pub struct Handler {
base_dir: PathBuf,
}
impl Handler {
/// Serves static files from an OS directory with a given prefix chopped off.
pub fn new(base_dir: PathBuf) -> Self {
Handler {
base_dir: base_dir,
}
}
}
#[async_trait]
impl MajHandler for Handler {
async fn handle(&self, r: Request) -> Result<Response> {
let mut path = std::path::PathBuf::from(&self.base_dir);
if let Some(segments) = r.url.path_segments() {
path.extend(segments);
}
log::debug!("opening file {:?}", path);
match async_std::fs::metadata(&path).await {
Ok(stat) => {
if stat.is_dir() {
if r.url.as_str().ends_with('/') {
path.push("index.gmi");
} else {
// Send a redirect when the URL for a directory has no trailing slash.
return Ok(Response::perm_redirect(format!("{}/", r.url)));
}
}
}
Err(why) => {
log::error!("file {} not found: {}", path.to_str().unwrap(), why);
return Ok(Response::not_found());
}
}
let mut file = async_std::fs::File::open(&path).await?;
let mut buf: Vec<u8> = Vec::new();
async_std::io::copy(&mut file, &mut buf).await?;
// Send header.
if path.extension() == Some(OsStr::new("gmi"))
|| path.extension() == Some(OsStr::new("gemini"))
{
return Ok(Response::gemini(buf));
}
let mime = mime_guess::from_path(&path).first_or_octet_stream();
Ok(Response::with_body(mime.essence_str().to_string(), buf))
}
}

View File

@ -34,6 +34,8 @@ enum RequestParsingError {
mod routes; mod routes;
pub use routes::*; pub use routes::*;
pub mod files;
#[async_trait] #[async_trait]
pub trait Handler { pub trait Handler {
async fn handle(&self, r: Request) -> Result<Response>; async fn handle(&self, r: Request) -> Result<Response>;
@ -78,13 +80,23 @@ async fn handle_request(
Ok(url) => { Ok(url) => {
if let Some(u_port) = url.port() { if let Some(u_port) = url.port() {
if port != u_port { if port != u_port {
let _ = respond(&mut stream, "53", &["Cannot proxy"]).await; let _ = write_header(
&mut stream,
StatusCode::ProxyRequestRefused,
"Cannot proxy to that URL",
)
.await;
return Ok(()); return Ok(());
} }
} }
if url.scheme() != "gemini" { if url.scheme() != "gemini" {
let _ = respond(&mut stream, "53", &["Cannot proxy outside geminispace"]).await; let _ = write_header(
&mut stream,
StatusCode::ProxyRequestRefused,
"Cannot proxy to that URL",
)
.await;
Err(RequestParsingError::InvalidScheme(url.scheme().to_string()))? Err(RequestParsingError::InvalidScheme(url.scheme().to_string()))?
} }
@ -95,20 +107,19 @@ async fn handle_request(
handle(h, req, &mut stream, addr).await; handle(h, req, &mut stream, addr).await;
} }
Err(e) => { Err(e) => {
respond(&mut stream, "59", &["Invalid request."]).await?; let _ = write_header(&mut stream, StatusCode::BadRequest, "Invalid request").await;
log::error!("error from {}: {:?}", addr, e); log::error!("error from {}: {}", addr, e);
} }
} }
Ok(()) Ok(())
} }
async fn respond<W: Write + Unpin>(mut stream: W, status: &str, meta: &[&str]) -> Result { pub async fn write_header<W: Write + Unpin>(
stream.write_all(status.as_bytes()).await?; mut stream: W,
stream.write_all(b" ").await?; status: StatusCode,
for m in meta { meta: &str,
stream.write_all(m.as_bytes()).await?; ) -> Result {
} stream.write(format!("{} {}\r\n", status as u8, meta).as_bytes()).await?;
stream.write_all(b"\r\n").await?;
Ok(()) Ok(())
} }
@ -145,8 +156,12 @@ async fn parse_request<R: Read + Unpin>(mut stream: R) -> Result<Url> {
Ok(url) Ok(url)
} }
async fn handle<T>(h: Arc<(dyn Handler + Send + Sync)>, req: Request, stream: &mut T, addr: SocketAddr) async fn handle<T>(
where h: Arc<(dyn Handler + Send + Sync)>,
req: Request,
stream: &mut T,
addr: SocketAddr,
) where
T: Write + Unpin, T: Write + Unpin,
{ {
let u = req.url.clone(); let u = req.url.clone();
@ -156,11 +171,11 @@ where
.write(format!("{} {}\r\n", resp.status as u8, resp.meta).as_bytes()) .write(format!("{} {}\r\n", resp.status as u8, resp.meta).as_bytes())
.await; .await;
let _ = stream.write(&resp.body).await; let _ = stream.write(&resp.body).await;
log::info!("{}: {} {} {:?}", addr, u, resp.meta, resp.status); log::info!("{}: {} {:?} {}", addr, u, resp.status, resp.meta);
} }
Err(why) => { Err(why) => {
let _ = stream let _ = stream
.write(format!("{} {:?}\r\n", StatusCode::PermanentFailure as u8, why).as_bytes()) .write(format!("{} {}\r\n", StatusCode::PermanentFailure as u8, why.to_string()).as_bytes())
.await; .await;
log::error!("{}: {}: {:?}", addr, u, why); log::error!("{}: {}: {:?}", addr, u, why);
} }