summaryrefslogtreecommitdiff
path: root/src/console.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/console.rs')
-rw-r--r--src/console.rs123
1 files changed, 87 insertions, 36 deletions
diff --git a/src/console.rs b/src/console.rs
index 1596cd9..14324fa 100644
--- a/src/console.rs
+++ b/src/console.rs
@@ -1,8 +1,11 @@
-use std::{net::IpAddr, collections::VecDeque, io, };
-use axum::{http::{Method, Uri}, response::Response};
+use axum::{
+ http::{Method, Uri},
+ response::Response,
+};
use lazy_static::lazy_static;
use serde::Serialize;
use serde_json::{ser::Formatter, Value};
+use std::{collections::VecDeque, io, net::IpAddr};
use tokio::sync::Mutex;
use crate::types::http::ResponseCode;
@@ -12,7 +15,7 @@ struct LogMessage {
method: Method,
uri: Uri,
path: String,
- body: String
+ body: String,
}
impl ToString for LogMessage {
@@ -31,7 +34,7 @@ impl ToString for LogMessage {
Method::CONNECT => "#3fe0ad",
Method::TRACE => "#e03fc5",
Method::OPTIONS => "#423fe0",
- _ => "white"
+ _ => "white",
};
format!("<div><span class='ip'>{}</span> <span class='method' style='color: {};'>{}</span> <span class='path'>{}{}</span> <span class='body'>{}</span></div>", ip, color, self.method, self.path, self.uri, self.body)
}
@@ -42,36 +45,43 @@ lazy_static! {
}
pub async fn log(ip: IpAddr, method: Method, uri: Uri, path: Option<String>, body: Option<String>) {
-
- if uri.to_string().starts_with("/console") { return; }
+ if uri.to_string().starts_with("/console") {
+ return;
+ }
let path = path.unwrap_or_default();
let body = body.unwrap_or_default();
tracing::info!("{} {} {}{} {}", &ip, &method, &path, &uri, &body);
-
+
let message = LogMessage {
- ip,
- method,
- uri,
- path,
- body: beautify(body)
+ ip,
+ method,
+ uri,
+ path,
+ body: beautify(body),
};
-
+
let mut lock = LOG.lock().await;
if lock.len() > 200 {
lock.pop_back();
}
- lock.push_front(message);
+ lock.push_front(message);
}
struct HtmlFormatter;
impl Formatter for HtmlFormatter {
- fn write_null<W>(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_null<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
writer.write_all(b"<span class='null'>null</span>")
}
- fn write_bool<W>(&mut self, writer: &mut W, value: bool) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_bool<W>(&mut self, writer: &mut W, value: bool) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let s = if value {
b"<span class='bool'>true</span>" as &[u8]
} else {
@@ -80,65 +90,104 @@ impl Formatter for HtmlFormatter {
writer.write_all(s)
}
- fn write_i8<W>(&mut self, writer: &mut W, value: i8) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_i8<W>(&mut self, writer: &mut W, value: i8) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn write_i16<W>(&mut self, writer: &mut W, value: i16) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_i16<W>(&mut self, writer: &mut W, value: i16) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn write_i32<W>(&mut self, writer: &mut W, value: i32) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_i32<W>(&mut self, writer: &mut W, value: i32) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn write_i64<W>(&mut self, writer: &mut W, value: i64) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_i64<W>(&mut self, writer: &mut W, value: i64) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn write_u8<W>(&mut self, writer: &mut W, value: u8) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_u8<W>(&mut self, writer: &mut W, value: u8) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn write_u16<W>(&mut self, writer: &mut W, value: u16) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_u16<W>(&mut self, writer: &mut W, value: u16) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn write_u32<W>(&mut self, writer: &mut W, value: u32) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_u32<W>(&mut self, writer: &mut W, value: u32) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn write_u64<W>(&mut self, writer: &mut W, value: u64) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_u64<W>(&mut self, writer: &mut W, value: u64) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn write_f32<W>(&mut self, writer: &mut W, value: f32) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_f32<W>(&mut self, writer: &mut W, value: f32) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn write_f64<W>(&mut self, writer: &mut W, value: f64) -> io::Result<()> where W: ?Sized + io::Write {
+ fn write_f64<W>(&mut self, writer: &mut W, value: f64) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
let buff = format!("<span class='number'>{value}</span>");
writer.write_all(buff.as_bytes())
}
- fn begin_string<W>(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + io::Write {
+ fn begin_string<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
writer.write_all(b"<span class='string'>\"")
}
- fn end_string<W>(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + io::Write {
+ fn end_string<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
writer.write_all(b"\"</span>")
}
- fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()> where W: ?Sized + io::Write {
+ fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
if first {
writer.write_all(b"<span class='key'>")
} else {
@@ -146,15 +195,17 @@ impl Formatter for HtmlFormatter {
}
}
- fn end_object_key<W>(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + io::Write {
+ fn end_object_key<W>(&mut self, writer: &mut W) -> io::Result<()>
+ where
+ W: ?Sized + io::Write,
+ {
writer.write_all(b"</span>")
}
-
}
fn beautify(body: String) -> String {
if body.is_empty() {
- return String::new()
+ return String::new();
}
let Ok(mut json) = serde_json::from_str::<Value>(&body) else {
return body
@@ -165,13 +216,12 @@ fn beautify(body: String) -> String {
let mut writer: Vec<u8> = Vec::with_capacity(128);
let mut serializer = serde_json::Serializer::with_formatter(&mut writer, HtmlFormatter);
if json.serialize(&mut serializer).is_err() {
- return body
+ return body;
}
String::from_utf8_lossy(&writer).to_string()
}
pub async fn generate() -> Response {
-
let lock = LOG.lock().await;
let mut html = r#"<!DOCTYPE html>
@@ -183,7 +233,8 @@ pub async fn generate() -> Response {
<title>XSSBook - Console</title>
</head>
<body>
- "#.to_string();
+ "#
+ .to_string();
for message in lock.iter() {
html.push_str(&message.to_string());
@@ -192,4 +243,4 @@ pub async fn generate() -> Response {
html.push_str("</body></html>");
ResponseCode::Success.html(&html)
-} \ No newline at end of file
+}