Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve public files from root instead of /pj and /pj/static #55

Merged
merged 2 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
tests/compose/nginx/ssl/localhost-key.pem
tests/compose/nginx/ssl/localhost.pem
static/qr_codes/*.png
public/qr_codes/*.png
Binary file added public/.DS_Store
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
21 changes: 10 additions & 11 deletions static/index.html → public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
<link rel="stylesheet" href="https://rsms.me/inter/inter.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/selekkt-skelet@latest/css/skelet.min.css">
<!-- having trouble getting the right path on both umbrel and local.. -->
<link rel="stylesheet" href="/pj/static/style.css">
<link rel="stylesheet" href="style.css">
<link rel="apple-touch-icon" sizes="180x180" href="/pj/static/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/pj/static/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/pj/static/favicons/favicon-16x16.png">
<link rel="manifest" href="/pj/static/favicons/site.webmanifest">
<link rel="mask-icon" href="/pj/static/favicons/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="/pj/static/favicons/favicon.ico">
<link rel="apple-touch-icon" sizes="180x180" href="favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicons/favicon-16x16.png">
<link rel="manifest" href="favicons/site.webmanifest">
<link rel="mask-icon" href="favicons/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="favicon.ico">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-config" content="/pj/static/favicons/browserconfig.xml">
<meta name="msapplication-config" content="favicons/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<title>nolooking // Lightning PayJoin</title>
</head>
Expand All @@ -27,7 +26,7 @@ <h3 style="color: orange;">🎃 Halloween Alpha [experimental] | Avoid sp👀ks<
<h2>Queue batches of lightning channels to open in a single transaction</h2>
</header>
<main class="center-axyz">
<form action="/pj/schedule" method="post" enctype="application/x-www-form-urlencoded"
<form action="/schedule" method="post" enctype="application/x-www-form-urlencoded"
x-flex direction="column">
<fieldset>
<legend>Config</legend>
Expand All @@ -47,7 +46,7 @@ <h2>Queue batches of lightning channels to open in a single transaction</h2>
</div>
<div>
<label for="fee_rate">Maximum fee rate (sats/vB)</label>
<input type="number" name="fee_rate" id="feerate" value="1" min="1"><!-- /pj/schedule api accepts u64 -->
<input type="number" name="fee_rate" id="feerate" value="1" min="1"><!-- /schedule api accepts u64 -->
</div>
</fieldset>
<fieldset>
Expand Down Expand Up @@ -125,7 +124,7 @@ <h2>PayJoin here to open these channels</h2>
link.innerHTML = bip21;

var address = bip21.split("bitcoin:")[1].split("?")[0];
document.getElementById("qrcode").innerHTML = `<img src="pj/static/qr_codes/${address}.png" width="256px" />`;
document.getElementById("qrcode").innerHTML = `<img src="/qr_codes/${address}.png" width="256px" />`;
document.getElementById("queue").classList.add("invisible");
document.getElementById("queued").classList.remove("invisible");
})
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
26 changes: 14 additions & 12 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use qrcode_generator::QrCodeEcc;
use crate::scheduler::{self, ScheduledPayJoin, Scheduler, SchedulerError};

#[cfg(not(feature = "test_paths"))]
const STATIC_DIR: &str = "/usr/share/nolooking/static";
const PUBLIC_DIR: &str = "/usr/share/nolooking/public";

#[cfg(feature = "test_paths")]
const STATIC_DIR: &str = "static";
const PUBLIC_DIR: &str = "public";

/// Create QR code and save to `STATIC_DIR/qr_codes/<name>.png`
/// Create QR code and save to `PUBLIC_DIR/qr_codes/<name>.png`
fn create_qr_code(qr_string: &str, name: &str) {
let filename = format!("{}/qr_codes/{}.png", STATIC_DIR, name);
let filename = format!("{}/qr_codes/{}.png", PUBLIC_DIR, name);
qrcode_generator::to_png_to_file(qr_string, QrCodeEcc::Low, 512, filename.clone())
.expect(&format!("Saved QR code: {}", filename));
}
Expand Down Expand Up @@ -47,10 +47,10 @@ async fn handle_web_req(
endpoint: url::Url,
) -> Result<Response<Body>, hyper::Error> {
let result = match (req.method(), req.uri().path()) {
(&Method::GET, "/pj") => handle_index().await,
(&Method::GET, path) if path.starts_with("/pj/static/") => handle_static(path).await,
(&Method::GET, "/") => handle_index().await,
(&Method::POST, "/pj") => handle_pj(scheduler, req).await,
(&Method::POST, "/pj/schedule") => handle_pj_schedule(scheduler, endpoint, req).await,
(&Method::POST, "/schedule") => handle_schedule(scheduler, endpoint, req).await,
(&Method::GET, path) => serve_public_file(path).await,
_ => handle_404().await,
};

Expand All @@ -67,13 +67,15 @@ async fn handle_404() -> Result<Response<Body>, HttpError> {
}

async fn handle_index() -> Result<Response<Body>, HttpError> {
let index = std::fs::read(Path::new(STATIC_DIR).join("index.html")).expect("can't open index");
let index = std::fs::read(Path::new(PUBLIC_DIR).join("index.html")).expect("can't open index");
Ok(Response::new(Body::from(index)))
}

async fn handle_static(path: &str) -> Result<Response<Body>, HttpError> {
let directory_traversal_vulnerable_path = &path[("/pj/static/".len())..];
match std::fs::read(Path::new(STATIC_DIR).join(directory_traversal_vulnerable_path)) {
async fn serve_public_file(path: &str) -> Result<Response<Body>, HttpError> {
// A path argument to PathBuf::join(&self, path) with a leading slash
// is treated as an absolute path, so we strip it in preparation.
let directory_traversal_vulnerable_path = &path[("/".len())..];
match std::fs::read(Path::new(PUBLIC_DIR).join(directory_traversal_vulnerable_path)) {
Ok(file) => Response::builder()
.status(200)
.header("Cache-Control", "max-age=604800")
Expand Down Expand Up @@ -104,7 +106,7 @@ async fn handle_pj(scheduler: Scheduler, req: Request<Body>) -> Result<Response<
Ok(Response::new(Body::from(proposal_psbt)))
}

async fn handle_pj_schedule(
async fn handle_schedule(
scheduler: Scheduler,
endpoint: url::Url,
req: Request<Body>,
Expand Down