2020-10-26 09:25:59 +13:00
|
|
|
// Import Crates
|
2021-01-18 09:58:12 +13:00
|
|
|
use actix_web::{
|
|
|
|
dev::{Service, ServiceResponse},
|
|
|
|
middleware, web, App, HttpResponse, HttpServer,
|
|
|
|
};
|
2021-01-17 19:04:03 +13:00
|
|
|
use futures::future::FutureExt;
|
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
|
|
|
|
|
|
|
// Create Services
|
2020-11-19 13:31:46 +13:00
|
|
|
async fn style() -> HttpResponse {
|
2020-11-30 10:46:53 +13:00
|
|
|
HttpResponse::Ok().content_type("text/css").body(include_str!("../static/style.css"))
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2021-02-01 23:10:53 +13:00
|
|
|
// Required for creating a PWA
|
|
|
|
async fn manifest() -> HttpResponse {
|
|
|
|
HttpResponse::Ok().content_type("application/json").body(include_str!("../static/manifest.json"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Required for the manifest to be valid
|
|
|
|
async fn pwa_logo() -> HttpResponse {
|
|
|
|
HttpResponse::Ok().content_type("image/png").body(include_bytes!("../static/logo.png").as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Required for iOS App Icons
|
|
|
|
async fn iphone_logo() -> HttpResponse {
|
2021-02-08 14:33:54 +13:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("image/png")
|
|
|
|
.body(include_bytes!("../static/touch-icon-iphone.png").as_ref())
|
2021-02-01 23:10:53 +13:00
|
|
|
}
|
|
|
|
|
2020-11-21 16:33:38 +13:00
|
|
|
async fn robots() -> HttpResponse {
|
2021-01-07 11:19:10 +13:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
|
2021-01-14 16:53:52 +13:00
|
|
|
.body("User-agent: *\nAllow: /")
|
2020-11-21 16:33:38 +13:00
|
|
|
}
|
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
async fn favicon() -> HttpResponse {
|
2021-01-13 17:18:20 +13:00
|
|
|
HttpResponse::Ok()
|
2021-01-29 18:53:10 +13:00
|
|
|
.content_type("image/x-icon")
|
2021-01-13 17:18:20 +13:00
|
|
|
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
|
|
|
|
.body(include_bytes!("../static/favicon.ico").as_ref())
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2020-11-23 16:21:07 +13:00
|
|
|
let mut address = "0.0.0.0:8080".to_string();
|
2021-01-17 19:04:03 +13:00
|
|
|
let mut force_https = false;
|
2020-11-23 16:21:07 +13:00
|
|
|
|
2021-01-11 10:08:36 +13:00
|
|
|
for arg in std::env::args().collect::<Vec<String>>() {
|
|
|
|
match arg.split('=').collect::<Vec<&str>>()[0] {
|
|
|
|
"--address" | "-a" => address = arg.split('=').collect::<Vec<&str>>()[1].to_string(),
|
2021-01-17 19:04:03 +13:00
|
|
|
"--redirect-https" | "-r" => force_https = true,
|
2021-01-14 16:53:52 +13:00
|
|
|
_ => (),
|
2020-11-23 16:21:07 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:57:19 +13:00
|
|
|
// start http server
|
2021-01-02 09:55:09 +13:00
|
|
|
println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), &address);
|
2020-11-19 15:50:59 +13:00
|
|
|
|
2021-01-17 19:04:03 +13:00
|
|
|
HttpServer::new(move || {
|
2020-10-26 16:57:19 +13:00
|
|
|
App::new()
|
2021-01-17 19:04:03 +13:00
|
|
|
// Redirect to HTTPS if "--redirect-https" enabled
|
|
|
|
.wrap_fn(move |req, srv| {
|
|
|
|
let secure = req.connection_info().scheme() == "https";
|
|
|
|
let https_url = format!("https://{}{}", req.connection_info().host(), req.uri().to_string());
|
2021-01-31 17:18:57 +13:00
|
|
|
srv.call(req).map(move |res: Result<ServiceResponse, _>| {
|
2021-01-17 19:04:03 +13:00
|
|
|
if force_https && !secure {
|
2021-01-21 14:38:34 +13:00
|
|
|
Ok(ServiceResponse::new(
|
|
|
|
res.unwrap().request().to_owned(),
|
|
|
|
HttpResponse::Found().header("Location", https_url).finish(),
|
|
|
|
))
|
2021-01-17 19:04:03 +13:00
|
|
|
} else {
|
|
|
|
res
|
|
|
|
}
|
2021-01-31 17:18:57 +13:00
|
|
|
})
|
2021-01-17 19:04:03 +13:00
|
|
|
})
|
2021-01-14 16:53:52 +13:00
|
|
|
// Append trailing slash and remove double slashes
|
2021-01-11 10:20:47 +13:00
|
|
|
.wrap(middleware::NormalizePath::default())
|
2021-01-29 18:53:10 +13:00
|
|
|
// Apply default headers for security
|
2021-01-31 17:18:57 +13:00
|
|
|
.wrap(
|
|
|
|
middleware::DefaultHeaders::new()
|
|
|
|
.header("Referrer-Policy", "no-referrer")
|
|
|
|
.header("X-Content-Type-Options", "nosniff")
|
|
|
|
.header("X-Frame-Options", "DENY")
|
|
|
|
.header(
|
|
|
|
"Content-Security-Policy",
|
2021-02-01 23:10:53 +13:00
|
|
|
"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';",
|
2021-01-31 17:18:57 +13:00
|
|
|
),
|
|
|
|
)
|
2021-01-14 16:53:52 +13:00
|
|
|
// Default service in case no routes match
|
2021-01-15 06:53:54 +13:00
|
|
|
.default_service(web::get().to(|| utils::error("Nothing here".to_string())))
|
2021-01-14 16:53:52 +13:00
|
|
|
// Read static files
|
2020-12-15 13:35:04 +13:00
|
|
|
.route("/style.css/", web::get().to(style))
|
2021-01-13 17:18:20 +13:00
|
|
|
.route("/favicon.ico/", web::get().to(favicon))
|
2020-12-15 13:35:04 +13:00
|
|
|
.route("/robots.txt/", web::get().to(robots))
|
2021-02-01 23:10:53 +13:00
|
|
|
.route("/manifest.json/", web::get().to(manifest))
|
|
|
|
.route("/logo.png/", web::get().to(pwa_logo))
|
|
|
|
.route("/touch-icon-iphone.png/", web::get().to(iphone_logo))
|
2021-01-14 16:53:52 +13:00
|
|
|
// Proxy media through Libreddit
|
2020-12-15 13:35:04 +13:00
|
|
|
.route("/proxy/{url:.*}/", web::get().to(proxy::handler))
|
2021-01-14 16:53:52 +13:00
|
|
|
// Browse user profile
|
2021-01-15 07:57:50 +13:00
|
|
|
.service(
|
|
|
|
web::scope("/{scope:user|u}").service(
|
|
|
|
web::scope("/{username}").route("/", web::get().to(user::profile)).service(
|
|
|
|
web::scope("/comments/{id}/{title}")
|
|
|
|
.route("/", web::get().to(post::item))
|
|
|
|
.route("/{comment_id}/", web::get().to(post::item)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2021-01-14 16:53:52 +13:00
|
|
|
// Configure settings
|
|
|
|
.service(web::resource("/settings/").route(web::get().to(settings::get)).route(web::post().to(settings::set)))
|
|
|
|
// Subreddit services
|
|
|
|
.service(
|
|
|
|
web::scope("/r/{sub}")
|
|
|
|
// See posts and info about subreddit
|
|
|
|
.route("/", web::get().to(subreddit::page))
|
|
|
|
.route("/{sort:hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
|
2021-01-30 20:00:00 +13:00
|
|
|
// Handle subscribe/unsubscribe
|
2021-01-31 18:21:46 +13:00
|
|
|
.route("/{action:subscribe|unsubscribe}/", web::post().to(subreddit::subscriptions))
|
2021-01-14 16:53:52 +13:00
|
|
|
// View post on subreddit
|
|
|
|
.service(
|
|
|
|
web::scope("/comments/{id}/{title}")
|
|
|
|
.route("/", web::get().to(post::item))
|
|
|
|
.route("/{comment_id}/", web::get().to(post::item)),
|
|
|
|
)
|
|
|
|
// Search inside subreddit
|
|
|
|
.route("/search/", web::get().to(search::find))
|
|
|
|
// View wiki of subreddit
|
|
|
|
.service(
|
2021-02-03 05:59:50 +13:00
|
|
|
web::scope("/{scope:wiki|w}")
|
2021-01-14 16:53:52 +13:00
|
|
|
.route("/", web::get().to(subreddit::wiki))
|
|
|
|
.route("/{page}/", web::get().to(subreddit::wiki)),
|
|
|
|
),
|
|
|
|
)
|
2021-01-21 14:38:34 +13:00
|
|
|
// Front page
|
|
|
|
.route("/", web::get().to(subreddit::page))
|
|
|
|
.route("/{sort:best|hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
|
|
|
|
// View Reddit wiki
|
2021-01-14 16:53:52 +13:00
|
|
|
.service(
|
2021-01-21 14:38:34 +13:00
|
|
|
web::scope("/wiki")
|
|
|
|
.route("/", web::get().to(subreddit::wiki))
|
|
|
|
.route("/{page}/", web::get().to(subreddit::wiki)),
|
2021-01-14 16:53:52 +13:00
|
|
|
)
|
2021-01-21 14:38:34 +13:00
|
|
|
// Search all of Reddit
|
|
|
|
.route("/search/", web::get().to(search::find))
|
|
|
|
// Short link for post
|
|
|
|
.route("/{id:.{5,6}}/", web::get().to(post::item))
|
2020-10-26 16:57:19 +13:00
|
|
|
})
|
2021-01-02 09:55:09 +13:00
|
|
|
.bind(&address)
|
2021-01-11 10:08:36 +13:00
|
|
|
.unwrap_or_else(|e| panic!("Cannot bind to the address {}: {}", address, e))
|
2020-10-26 16:57:19 +13:00
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|