2020-10-26 09:25:59 +13:00
|
|
|
// Reference local files
|
|
|
|
mod post;
|
2020-11-30 15:50:29 +13:00
|
|
|
mod proxy;
|
2021-01-01 12:54:13 +13:00
|
|
|
mod search;
|
2021-01-06 15:04:49 +13:00
|
|
|
mod settings;
|
2020-10-26 09:25:59 +13:00
|
|
|
mod subreddit;
|
2020-10-26 16:57:19 +13:00
|
|
|
mod user;
|
2020-11-26 10:53:30 +13:00
|
|
|
mod utils;
|
2020-10-26 09:25:59 +13:00
|
|
|
|
2021-02-14 12:02:38 +13:00
|
|
|
// Import Crates
|
2021-02-19 08:40:10 +13:00
|
|
|
use clap::{App, Arg};
|
2021-02-20 18:46:44 +13:00
|
|
|
use proxy::handler;
|
2021-02-14 12:02:38 +13:00
|
|
|
use tide::{
|
|
|
|
utils::{async_trait, After},
|
|
|
|
Middleware, Next, Request, Response,
|
|
|
|
};
|
|
|
|
use utils::{error, redirect};
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
// Build middleware
|
|
|
|
struct HttpsRedirect<HttpsOnly>(HttpsOnly);
|
|
|
|
struct NormalizePath;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl<State, HttpsOnly> Middleware<State> for HttpsRedirect<HttpsOnly>
|
|
|
|
where
|
|
|
|
State: Clone + Send + Sync + 'static,
|
|
|
|
HttpsOnly: Into<bool> + Copy + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
async fn handle(&self, request: Request<State>, next: Next<'_, State>) -> tide::Result {
|
|
|
|
let secure = request.url().scheme() == "https";
|
|
|
|
|
|
|
|
if self.0.into() && !secure {
|
|
|
|
let mut secured = request.url().to_owned();
|
|
|
|
secured.set_scheme("https").unwrap_or_default();
|
|
|
|
|
2021-02-14 12:02:38 +13:00
|
|
|
Ok(redirect(secured.to_string()))
|
2021-02-10 06:38:52 +13:00
|
|
|
} else {
|
|
|
|
Ok(next.run(request).await)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl<State: Clone + Send + Sync + 'static> Middleware<State> for NormalizePath {
|
|
|
|
async fn handle(&self, request: Request<State>, next: Next<'_, State>) -> tide::Result {
|
2021-02-28 10:34:02 +13:00
|
|
|
let path = request.url().path();
|
|
|
|
let query = request.url().query().unwrap_or_default();
|
|
|
|
if path.ends_with('/') {
|
2021-02-10 06:38:52 +13:00
|
|
|
Ok(next.run(request).await)
|
2021-02-28 10:34:02 +13:00
|
|
|
} else {
|
|
|
|
let normalized = if query != "" {
|
|
|
|
format!("{}/?{}", path.replace("//", "/"), query)
|
|
|
|
} else {
|
|
|
|
format!("{}/", path.replace("//", "/"))
|
|
|
|
};
|
|
|
|
Ok(redirect(normalized))
|
2021-02-10 06:38:52 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
// Create Services
|
2021-02-01 23:10:53 +13:00
|
|
|
|
|
|
|
// Required for the manifest to be valid
|
2021-02-10 06:38:52 +13:00
|
|
|
async fn pwa_logo(_req: Request<()>) -> tide::Result {
|
2021-02-14 12:02:38 +13:00
|
|
|
Ok(Response::builder(200).content_type("image/png").body(include_bytes!("../static/logo.png").as_ref()).build())
|
2021-02-01 23:10:53 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// Required for iOS App Icons
|
2021-02-10 06:38:52 +13:00
|
|
|
async fn iphone_logo(_req: Request<()>) -> tide::Result {
|
|
|
|
Ok(
|
|
|
|
Response::builder(200)
|
|
|
|
.content_type("image/png")
|
2021-02-26 06:07:45 +13:00
|
|
|
.body(include_bytes!("../static/apple-touch-icon.png").as_ref())
|
2021-02-10 06:38:52 +13:00
|
|
|
.build(),
|
|
|
|
)
|
2021-02-01 23:10:53 +13:00
|
|
|
}
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
async fn favicon(_req: Request<()>) -> tide::Result {
|
|
|
|
Ok(
|
|
|
|
Response::builder(200)
|
2021-02-27 09:04:11 +13:00
|
|
|
.content_type("image/vnd.microsoft.icon")
|
2021-02-10 06:38:52 +13:00
|
|
|
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
|
2021-02-27 09:04:11 +13:00
|
|
|
.body(include_bytes!("../static/favicon.ico").as_ref())
|
2021-02-10 06:38:52 +13:00
|
|
|
.build(),
|
|
|
|
)
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2021-02-25 06:26:01 +13:00
|
|
|
async fn resource(body: &str, content_type: &str, cache: bool) -> tide::Result {
|
|
|
|
let mut res = Response::new(200);
|
|
|
|
|
|
|
|
if cache {
|
|
|
|
res.insert_header("Cache-Control", "public, max-age=1209600, s-maxage=86400");
|
|
|
|
}
|
|
|
|
|
|
|
|
res.set_content_type(content_type);
|
|
|
|
res.set_body(body);
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
#[async_std::main]
|
|
|
|
async fn main() -> tide::Result<()> {
|
2021-02-19 08:40:10 +13:00
|
|
|
let matches = App::new("Libreddit")
|
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
|
|
.about("Private front-end for Reddit written in Rust ")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("address")
|
|
|
|
.short("a")
|
|
|
|
.long("address")
|
|
|
|
.value_name("ADDRESS")
|
|
|
|
.help("Sets address to listen on")
|
2021-02-19 08:49:50 +13:00
|
|
|
.default_value("0.0.0.0")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("port")
|
|
|
|
.short("p")
|
|
|
|
.long("port")
|
|
|
|
.value_name("PORT")
|
|
|
|
.help("Port to listen on")
|
|
|
|
.default_value("8080")
|
2021-02-19 08:40:10 +13:00
|
|
|
.takes_value(true),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("redirect-https")
|
|
|
|
.short("r")
|
|
|
|
.long("redirect-https")
|
|
|
|
.help("Redirect all HTTP requests to HTTPS")
|
|
|
|
.takes_value(false),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
2021-02-19 08:49:50 +13:00
|
|
|
let address = matches.value_of("address").unwrap_or("0.0.0.0");
|
|
|
|
let port = matches.value_of("port").unwrap_or("8080");
|
2021-02-19 08:40:10 +13:00
|
|
|
let force_https = matches.is_present("redirect-https");
|
|
|
|
|
2021-02-19 08:49:50 +13:00
|
|
|
let listener = format!("{}:{}", address, port);
|
2020-11-23 16:21:07 +13:00
|
|
|
|
2021-02-25 08:00:04 +13:00
|
|
|
println!("Starting Libreddit...");
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
// Start HTTP server
|
|
|
|
let mut app = tide::new();
|
|
|
|
|
|
|
|
// Redirect to HTTPS if "--redirect-https" enabled
|
|
|
|
app.with(HttpsRedirect(force_https));
|
|
|
|
|
|
|
|
// Append trailing slash and remove double slashes
|
|
|
|
app.with(NormalizePath);
|
|
|
|
|
|
|
|
// Apply default headers for security
|
|
|
|
app.with(After(|mut res: Response| async move {
|
|
|
|
res.insert_header("Referrer-Policy", "no-referrer");
|
|
|
|
res.insert_header("X-Content-Type-Options", "nosniff");
|
|
|
|
res.insert_header("X-Frame-Options", "DENY");
|
|
|
|
res.insert_header(
|
|
|
|
"Content-Security-Policy",
|
|
|
|
"default-src 'none'; manifest-src 'self'; media-src 'self'; style-src 'self' 'unsafe-inline'; base-uri 'none'; img-src 'self' data:; form-action 'self'; frame-ancestors 'none';",
|
|
|
|
);
|
|
|
|
Ok(res)
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Read static files
|
2021-02-25 06:26:01 +13:00
|
|
|
app.at("/style.css/").get(|_| resource(include_str!("../static/style.css"), "text/css", false));
|
2021-02-26 06:07:45 +13:00
|
|
|
app
|
|
|
|
.at("/manifest.json/")
|
|
|
|
.get(|_| resource(include_str!("../static/manifest.json"), "application/json", false));
|
2021-02-25 06:26:01 +13:00
|
|
|
app.at("/robots.txt/").get(|_| resource("User-agent: *\nAllow: /", "text/plain", true));
|
2021-02-27 09:04:11 +13:00
|
|
|
app.at("/favicon.ico/").get(favicon);
|
2021-02-10 06:38:52 +13:00
|
|
|
app.at("/logo.png/").get(pwa_logo);
|
|
|
|
app.at("/touch-icon-iphone.png/").get(iphone_logo);
|
2021-02-10 09:08:38 +13:00
|
|
|
app.at("/apple-touch-icon.png/").get(iphone_logo);
|
2021-02-10 06:38:52 +13:00
|
|
|
|
|
|
|
// Proxy media through Libreddit
|
2021-02-20 18:46:44 +13:00
|
|
|
app
|
|
|
|
.at("/vid/:id/:size/") /* */
|
|
|
|
.get(|req| handler(req, "https://v.redd.it/{}/DASH_{}", vec!["id", "size"]));
|
|
|
|
app
|
|
|
|
.at("/img/:id/") /* */
|
|
|
|
.get(|req| handler(req, "https://i.redd.it/{}", vec!["id"]));
|
|
|
|
app
|
|
|
|
.at("/thumb/:point/:id/") /* */
|
|
|
|
.get(|req| handler(req, "https://{}.thumbs.redditmedia.com/{}", vec!["point", "id"]));
|
|
|
|
app
|
|
|
|
.at("/emoji/:id/:name/") /* */
|
|
|
|
.get(|req| handler(req, "https://emoji.redditmedia.com/{}/{}", vec!["id", "name"]));
|
|
|
|
app
|
|
|
|
.at("/preview/:loc/:id/:query/")
|
2021-02-20 18:49:02 +13:00
|
|
|
.get(|req| handler(req, "https://{}view.redd.it/{}?{}", vec!["loc", "id", "query"]));
|
2021-02-20 18:46:44 +13:00
|
|
|
app
|
|
|
|
.at("/style/*path/") /* */
|
|
|
|
.get(|req| handler(req, "https://styles.redditmedia.com/{}", vec!["path"]));
|
|
|
|
app
|
|
|
|
.at("/static/*path/") /* */
|
|
|
|
.get(|req| handler(req, "https://www.redditstatic.com/{}", vec!["path"]));
|
2021-02-10 06:38:52 +13:00
|
|
|
|
|
|
|
// Browse user profile
|
|
|
|
app.at("/u/:name/").get(user::profile);
|
|
|
|
app.at("/u/:name/comments/:id/:title/").get(post::item);
|
2021-02-13 06:16:59 +13:00
|
|
|
app.at("/u/:name/comments/:id/:title/:comment_id/").get(post::item);
|
2021-02-10 06:38:52 +13:00
|
|
|
|
|
|
|
app.at("/user/:name/").get(user::profile);
|
2021-02-10 07:11:39 +13:00
|
|
|
app.at("/user/:name/comments/:id/").get(post::item);
|
2021-02-10 06:38:52 +13:00
|
|
|
app.at("/user/:name/comments/:id/:title/").get(post::item);
|
2021-02-13 06:16:59 +13:00
|
|
|
app.at("/user/:name/comments/:id/:title/:comment_id/").get(post::item);
|
2021-02-10 06:38:52 +13:00
|
|
|
|
|
|
|
// Configure settings
|
|
|
|
app.at("/settings/").get(settings::get).post(settings::set);
|
2021-02-14 09:55:23 +13:00
|
|
|
app.at("/settings/restore/").get(settings::restore);
|
2021-02-10 06:38:52 +13:00
|
|
|
|
|
|
|
// Subreddit services
|
|
|
|
app.at("/r/:sub/").get(subreddit::page);
|
2021-02-25 06:26:01 +13:00
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
app.at("/r/:sub/subscribe/").post(subreddit::subscriptions);
|
|
|
|
app.at("/r/:sub/unsubscribe/").post(subreddit::subscriptions);
|
2021-02-25 06:26:01 +13:00
|
|
|
|
2021-02-10 07:11:39 +13:00
|
|
|
app.at("/r/:sub/comments/:id/").get(post::item);
|
2021-02-10 06:38:52 +13:00
|
|
|
app.at("/r/:sub/comments/:id/:title/").get(post::item);
|
|
|
|
app.at("/r/:sub/comments/:id/:title/:comment_id/").get(post::item);
|
2021-02-25 06:26:01 +13:00
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
app.at("/r/:sub/search/").get(search::find);
|
2021-02-25 06:26:01 +13:00
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
app.at("/r/:sub/wiki/").get(subreddit::wiki);
|
|
|
|
app.at("/r/:sub/wiki/:page/").get(subreddit::wiki);
|
2021-02-25 06:26:01 +13:00
|
|
|
app.at("/r/:sub/w/").get(subreddit::wiki);
|
|
|
|
app.at("/r/:sub/w/:page/").get(subreddit::wiki);
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
app.at("/r/:sub/:sort/").get(subreddit::page);
|
|
|
|
|
2021-03-06 03:24:40 +13:00
|
|
|
// Comments handler
|
|
|
|
app.at("/comments/:id/").get(post::item);
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
// Front page
|
|
|
|
app.at("/").get(subreddit::page);
|
|
|
|
|
|
|
|
// View Reddit wiki
|
|
|
|
app.at("/w/").get(subreddit::wiki);
|
|
|
|
app.at("/w/:page/").get(subreddit::wiki);
|
|
|
|
app.at("/wiki/").get(subreddit::wiki);
|
|
|
|
app.at("/wiki/:page/").get(subreddit::wiki);
|
|
|
|
|
|
|
|
// Search all of Reddit
|
|
|
|
app.at("/search/").get(search::find);
|
|
|
|
|
2021-02-22 07:13:20 +13:00
|
|
|
// Handle about pages
|
|
|
|
app.at("/about/").get(|req| error(req, "About pages aren't here yet".to_string()));
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
app.at("/:id/").get(|req: Request<()>| async {
|
2021-02-10 09:08:38 +13:00
|
|
|
match req.param("id") {
|
|
|
|
// Sort front page
|
|
|
|
Ok("best") | Ok("hot") | Ok("new") | Ok("top") | Ok("rising") | Ok("controversial") => subreddit::page(req).await,
|
|
|
|
// Short link for post
|
|
|
|
Ok(id) if id.len() > 4 && id.len() < 7 => post::item(req).await,
|
2021-02-25 06:26:01 +13:00
|
|
|
// Error message for unknown pages
|
2021-02-21 15:36:30 +13:00
|
|
|
_ => error(req, "Nothing here".to_string()).await,
|
2021-02-10 06:38:52 +13:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Default service in case no routes match
|
2021-02-21 15:36:30 +13:00
|
|
|
app.at("*").get(|req| error(req, "Nothing here".to_string()));
|
2021-02-10 06:38:52 +13:00
|
|
|
|
2021-02-25 08:00:04 +13:00
|
|
|
println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), listener);
|
|
|
|
|
2021-02-25 06:26:01 +13:00
|
|
|
app.listen(&listener).await?;
|
|
|
|
|
2021-02-25 08:00:04 +13:00
|
|
|
Ok(())
|
2020-10-26 16:57:19 +13:00
|
|
|
}
|