forked from 0x676e67/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.rs
342 lines (298 loc) · 10.3 KB
/
frontend.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use super::{
auth::{token, CHECK_AUTH, EXP},
error::AppError,
ext::RequestExt,
ConfigExt,
};
use crate::{constant, InstallConfig, Running, ServeConfig};
use anyhow::Context;
use axum::{
body::{Body, StreamBody},
extract::State,
http::{header, HeaderName, HeaderValue, StatusCode},
response::{Html, IntoResponse, Redirect, Response},
routing::{any, get, post},
Form, Json, Router,
};
use axum_server::{tls_rustls::RustlsConfig, AddrIncomingConfig, Handle, HttpConfig};
use serde::Deserialize;
use std::{
io::{BufRead, Read},
process::Stdio,
str::FromStr,
sync::Arc,
time::Duration,
};
use tokio::io::BufReader;
use tokio_util::io::ReaderStream;
use tower_http::trace;
use tracing::Level;
// Access cookie
const ACCESS_COOKIE: &'static str = "access_token";
// Login html
const LOGIN_HTML: &str = include_str!("../static/login.html");
#[derive(Deserialize)]
struct User {
password: String,
}
pub(super) struct FrontendServer(ServeConfig, InstallConfig, tokio::sync::mpsc::Receiver<()>);
impl Running for FrontendServer {
fn run(self) -> anyhow::Result<()> {
self.start_server()
}
}
impl FrontendServer {
pub(super) fn new(
serve_config: ServeConfig,
install_config: InstallConfig,
graceful_shutdown: tokio::sync::mpsc::Receiver<()>,
) -> Self {
Self(serve_config, install_config, graceful_shutdown)
}
#[tokio::main]
async fn start_server(self) -> anyhow::Result<()> {
log::info!("Starting frontend server: {}", self.0.bind);
// Set check auth
CHECK_AUTH.set(self.0.auth_password.clone())?;
// router
let router = Router::new()
.route("/webman/login.cgi", get(get_webman_login))
.route("/", any(get_pan_thunder_com))
.route("/*path", any(get_pan_thunder_com))
// Need to auth middleware
.route_layer(axum::middleware::from_fn(auth_middleware))
.route("/login", get(get_login))
.route("/login", post(post_login))
.layer(
tower_http::trace::TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO))
.on_request(trace::DefaultOnRequest::new().level(Level::INFO))
.on_failure(trace::DefaultOnFailure::new().level(Level::WARN)),
)
.with_state(Arc::new((self.0.clone(), self.1.clone())));
// http server config
let http_config = HttpConfig::new()
.http1_title_case_headers(true)
.http1_preserve_header_case(true)
.http2_keep_alive_interval(Duration::from_secs(60))
.build();
// http server incoming config
let incoming_config = AddrIncomingConfig::new()
.tcp_sleep_on_accept_errors(true)
.tcp_keepalive(Some(Duration::from_secs(60)))
.build();
// Signal the server to shutdown using Handle.
let handle = Handle::new();
// Wait for the server to shutdown gracefully
tokio::spawn(graceful_shutdown_signal(handle.clone(), self.2));
// If tls_cert and tls_key is not None, use https
let result = match (self.0.tls_cert, self.0.tls_key) {
(Some(cert), Some(key)) => {
// Load tls config
let tls_config = RustlsConfig::from_pem_file(cert, key).await?;
axum_server::bind_rustls(self.0.bind, tls_config)
.handle(handle)
.addr_incoming_config(incoming_config)
.http_config(http_config)
.serve(router.into_make_service())
.await
}
_ => {
axum_server::bind(self.0.bind)
.handle(handle)
.addr_incoming_config(incoming_config)
.http_config(http_config)
.serve(router.into_make_service())
.await
}
};
if let Some(err) = result.err() {
log::warn!("Http Server error: {}", err);
}
Ok(())
}
}
/// Authentication
fn authentication(auth_password: &str) -> bool {
match CHECK_AUTH.get() {
Some(Some(p)) => auth_password.eq(p),
_ => true,
}
}
/// GET /login handler
async fn get_login() -> Html<&'static str> {
Html(LOGIN_HTML)
}
/// POST Login handler
async fn post_login(user: Form<User>) -> Result<impl IntoResponse, Redirect> {
if authentication(user.password.as_str()) {
if let Ok(token) = token::generate_token() {
let resp = Response::builder()
.header(header::LOCATION, constant::SYNOPKG_WEB_UI_HOME)
.header(
header::SET_COOKIE,
format!("{ACCESS_COOKIE}={token}; Max-Age={EXP}; Path=/; HttpOnly"),
)
.status(StatusCode::SEE_OTHER)
.body(Body::empty())
.expect("Failed to build response");
return Ok(resp.into_response());
}
}
Err(Redirect::to("/login"))
}
/// GET "/webman/login.cgi" handler
async fn get_webman_login() -> Json<&'static str> {
Json(r#"{"SynoToken", ""}"#)
}
/// Any "/webman/3rdparty/pan-thunder-com/index.cgi/" handler
async fn get_pan_thunder_com(
State(conf): State<Arc<(ServeConfig, InstallConfig)>>,
req: RequestExt,
) -> Result<impl IntoResponse, AppError> {
if !req.uri.to_string().contains(constant::SYNOPKG_WEB_UI_HOME) {
return Ok(Redirect::temporary(constant::SYNOPKG_WEB_UI_HOME).into_response());
}
// environment variables
let envs = (&conf.0, &conf.1).envs()?;
// My Server real host
let remove_host = extract_real_host(&req);
let mut cmd = tokio::process::Command::new(constant::SYNOPKG_CLI_WEB);
cmd.current_dir(constant::SYNOPKG_PKGDEST)
.envs(envs)
.env("SERVER_SOFTWARE", "rust")
.env("SERVER_PROTOCOL", "HTTP/1.1")
.env("HTTP_HOST", remove_host)
.env("GATEWAY_INTERFACE", "CGI/1.1")
.env("REQUEST_METHOD", req.method.as_str())
.env("QUERY_STRING", req.uri.query().unwrap_or_default())
.env(
"REQUEST_URI",
req.uri
.path_and_query()
.context("Failed to get path_and_query")?
.as_str(),
)
.env("PATH_INFO", req.uri.path())
.env("SCRIPT_NAME", ".")
.env("SCRIPT_FILENAME", req.uri.path())
.env("SERVER_PORT", conf.0.bind.port().to_string())
.env("REMOTE_ADDR", remove_host)
.env("SERVER_NAME", remove_host)
.uid(conf.1.uid)
.gid(conf.1.gid)
.stdout(Stdio::piped())
.stdin(Stdio::piped());
// If debug is false, hide stderr
if !conf.0.debug {
cmd.stderr(Stdio::null());
}
for ele in req.headers.iter() {
let k = ele.0.as_str().to_ascii_lowercase();
let v = ele.1;
if k == "PROXY" {
continue;
}
if !v.is_empty() {
cmd.env(format!("HTTP_{k}"), v.to_str().unwrap_or_default());
}
}
req.headers.get(header::CONTENT_TYPE).map(|h| {
cmd.env("CONTENT_TYPE", h.to_str().unwrap_or_default());
});
req.headers.get(header::CONTENT_LENGTH).map(|h| {
cmd.env("CONTENT_LENGTH", h.to_str().unwrap_or_default());
});
let mut child = cmd.spawn()?;
if let Some(body) = req.body {
if let Some(w) = child.stdin.as_mut() {
let mut r = BufReader::new(&body[..]);
tokio::io::copy(&mut r, w).await?;
}
}
// Wait for the child to exit
let output = child.wait_with_output().await?;
// Get status code
let mut status_code = 200;
// Response builder
let mut builder = Response::builder();
// Extract headers
let mut cursor = std::io::Cursor::new(output.stdout);
for header_res in cursor.by_ref().lines() {
let header = header_res?;
if header.is_empty() {
break;
}
if header.starts_with("getEnvs ") {
continue;
}
let (header, val) = header
.split_once(':')
.context("Failed to split_once header")?;
let val = &val[1..];
if header == "Status" {
status_code = val[0..3]
.parse()
.context("Status returned by CGI program is invalid")?;
} else {
builder = builder.header(HeaderName::from_str(header)?, HeaderValue::from_str(val)?);
}
}
Ok(builder
.status(status_code)
.body(StreamBody::from(ReaderStream::new(cursor)))?
.into_response())
}
/// Extract real request host (bind, port)
fn extract_real_host(req: &RequestExt) -> &str {
req.headers
.get(header::HOST)
.map(|h| h.to_str().unwrap_or_default())
.unwrap_or_default()
}
use axum::{http::Request, middleware::Next};
/// Auth middleware
pub(crate) async fn auth_middleware<B>(
request: Request<B>,
next: Next<B>,
) -> Result<Response, Redirect> {
// If CHECK_AUTH is None, return true
if let Some(None) = CHECK_AUTH.get() {
return Ok(next.run(request).await);
}
// extract access_token from cookie
if let Some(h) = request.headers().get(header::COOKIE) {
let cookie = h.to_str().unwrap_or_default();
let cookie = cookie
.split(';')
.filter(|c| !c.is_empty())
.collect::<Vec<&str>>();
for c in cookie {
let c = c.trim();
if c.starts_with(ACCESS_COOKIE) {
let token = c.split('=').collect::<Vec<&str>>();
if token.len() == 2 {
// Verify token
if token::verifier(token[1]).is_ok() {
return Ok(next.run(request).await);
}
}
}
}
}
Err(Redirect::to("/login"))
}
/// Graceful shutdown signal
async fn graceful_shutdown_signal(
handle: Handle,
mut graceful_shutdown: tokio::sync::mpsc::Receiver<()>,
) {
tokio::select! {
_ = graceful_shutdown.recv() => {
println!("Received signal to shutdown");
handle.shutdown();
return ;
}
}
}