-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1047 from NordSecurity/LLT-5882_refactor_teliod_c…
…gi_structure LLT-5882: Refactor teliod cgi structure
- Loading branch information
Showing
15 changed files
with
230 additions
and
99 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use const_format::concatcp; | ||
|
||
#[cfg(feature = "qnap")] | ||
pub const APP_DIR: &str = "/share/CACHEDEV1_DATA/.qpkg/NordSecurityMeshnet"; | ||
#[cfg(not(feature = "qnap"))] | ||
pub const APP_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/target/debug"); | ||
|
||
pub const TELIOD_BIN: &str = concatcp!(APP_DIR, "/teliod"); | ||
pub const MESHNET_LOG: &str = concatcp!(APP_DIR, "/meshnet.log"); | ||
pub const TELIOD_LOG: &str = "/var/log/teliod.log"; | ||
|
||
#[cfg(not(test))] | ||
pub const TELIOD_CFG: &str = concatcp!(APP_DIR, "/teliod.cfg"); | ||
#[cfg(test)] | ||
pub const TELIOD_CFG: &str = "/tmp/teliod_config.json"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::{env::var, ops::Deref}; | ||
|
||
use rust_cgi::{http::StatusCode, text_response, Request, Response}; | ||
|
||
mod api; | ||
pub(crate) mod constants; | ||
|
||
pub struct CgiRequest { | ||
pub inner: Request, | ||
route: String, | ||
} | ||
|
||
impl CgiRequest { | ||
fn new(inner: Request) -> Self { | ||
Self { | ||
inner, | ||
route: var("PATH_INFO").unwrap_or_default(), | ||
} | ||
} | ||
|
||
pub fn route(&self) -> &str { | ||
&self.route | ||
} | ||
} | ||
|
||
impl Deref for CgiRequest { | ||
type Target = Request; | ||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} | ||
|
||
pub fn handle_request(request: Request) -> Response { | ||
let request = CgiRequest::new(request); | ||
|
||
if let Some(response) = api::handle_api(&request) { | ||
#[cfg(debug_assertions)] | ||
let response = trace_request(&request, &response).unwrap_or(text_response( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
"Tracing request failed.", | ||
)); | ||
return response; | ||
} | ||
|
||
text_response(StatusCode::BAD_REQUEST, "Invalid request.") | ||
} | ||
|
||
#[cfg(debug_assertions)] | ||
fn trace_request(request: &CgiRequest, response: &Response) -> Option<Response> { | ||
use std::{env::vars, fmt::Write}; | ||
let mut msg = String::new(); | ||
let _ = writeln!( | ||
&mut msg, | ||
"ENVIRONMENT:\n{:#?}\n", | ||
vars().collect::<Vec<_>>(), | ||
); | ||
let _ = writeln!( | ||
&mut msg, | ||
"REQUEST:\nmethod: {:?}\nuri: {:?}\npath: {:?}\nroute: {:?}\nquery: {:?}\n", | ||
request.method(), | ||
request.uri(), | ||
request.uri().path(), | ||
request.route(), | ||
request.uri().query(), | ||
); | ||
let _ = writeln!( | ||
&mut msg, | ||
"RESPONSE:\nstatus_code: {:?}\nbody: {:?}\n", | ||
response.status(), | ||
std::str::from_utf8(response.body()).unwrap_or("[Error] Response with invalid UTF-8."), | ||
); | ||
Some(text_response(StatusCode::OK, msg)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eu -o pipefail | ||
|
||
fail() { | ||
cat $DEV_DIR/cargo.log | ||
exit 0 | ||
} | ||
|
||
DEV_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
BIN_DIR="$(realpath $DEV_DIR/../../../target/debug/)" | ||
|
||
cargo build -F cgi -p teliod >"$DEV_DIR/cargo.log" 2>&1 || fail | ||
|
||
export PATH=$BIN_DIR:$PATH | ||
|
||
exec teliod cgi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
,/NordSecMeshnet.sh, | ||
,/NordSecurityMeshnet.sh, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.