2023-01-28 23:04:00 +00:00
|
|
|
use axum::{
|
|
|
|
http::{Method, Uri},
|
|
|
|
response::Response,
|
|
|
|
};
|
2023-01-28 07:51:34 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use serde::Serialize;
|
|
|
|
use serde_json::{ser::Formatter, Value};
|
2023-01-28 23:04:00 +00:00
|
|
|
use std::{collections::VecDeque, io, net::IpAddr};
|
2023-01-28 07:51:34 +00:00
|
|
|
use tokio::sync::Mutex;
|
2023-01-27 21:04:04 +00:00
|
|
|
|
2023-01-28 22:57:52 +00:00
|
|
|
use crate::types::http::ResponseCode;
|
2023-01-28 07:51:34 +00:00
|
|
|
|
|
|
|
struct LogMessage {
|
|
|
|
ip: IpAddr,
|
|
|
|
method: Method,
|
|
|
|
uri: Uri,
|
|
|
|
path: String,
|
2023-01-28 23:04:00 +00:00
|
|
|
body: String,
|
2023-01-28 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for LogMessage {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
let mut ip = self.ip.to_string();
|
|
|
|
if ip.contains("::ffff:") {
|
2023-01-28 22:57:52 +00:00
|
|
|
ip = ip.as_str()[7..].to_string();
|
2023-01-28 07:51:34 +00:00
|
|
|
}
|
|
|
|
let color = match self.method {
|
|
|
|
Method::GET => "#3fe04f",
|
|
|
|
Method::POST => "#853fe0",
|
|
|
|
Method::PATCH => "#e0773f",
|
|
|
|
Method::PUT => "#e0cb3f",
|
|
|
|
Method::HEAD => "#3f75e0",
|
|
|
|
Method::DELETE => "#e04c3f",
|
|
|
|
Method::CONNECT => "#3fe0ad",
|
|
|
|
Method::TRACE => "#e03fc5",
|
|
|
|
Method::OPTIONS => "#423fe0",
|
2023-01-28 23:04:00 +00:00
|
|
|
_ => "white",
|
2023-01-28 07:51:34 +00:00
|
|
|
};
|
|
|
|
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)
|
2023-01-27 21:04:04 +00:00
|
|
|
}
|
2023-01-28 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref LOG: Mutex<VecDeque<LogMessage>> = Mutex::new(VecDeque::with_capacity(200));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn log(ip: IpAddr, method: Method, uri: Uri, path: Option<String>, body: Option<String>) {
|
2023-01-28 23:04:00 +00:00
|
|
|
if uri.to_string().starts_with("/console") {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-28 07:51:34 +00:00
|
|
|
|
|
|
|
let path = path.unwrap_or_default();
|
|
|
|
let body = body.unwrap_or_default();
|
|
|
|
|
2023-01-28 16:52:32 +00:00
|
|
|
tracing::info!("{} {} {}{} {}", &ip, &method, &path, &uri, &body);
|
2023-01-28 23:04:00 +00:00
|
|
|
|
2023-01-28 07:51:34 +00:00
|
|
|
let message = LogMessage {
|
2023-01-28 23:04:00 +00:00
|
|
|
ip,
|
|
|
|
method,
|
|
|
|
uri,
|
|
|
|
path,
|
|
|
|
body: beautify(body),
|
2023-01-28 07:51:34 +00:00
|
|
|
};
|
2023-01-28 23:04:00 +00:00
|
|
|
|
2023-01-28 07:51:34 +00:00
|
|
|
let mut lock = LOG.lock().await;
|
|
|
|
if lock.len() > 200 {
|
|
|
|
lock.pop_back();
|
|
|
|
}
|
2023-01-28 23:04:00 +00:00
|
|
|
lock.push_front(message);
|
2023-01-28 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct HtmlFormatter;
|
|
|
|
impl Formatter for HtmlFormatter {
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_null<W>(&mut self, writer: &mut W) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(b"<span class='null'>null</span>")
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_bool<W>(&mut self, writer: &mut W, value: bool) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 07:51:34 +00:00
|
|
|
let s = if value {
|
|
|
|
b"<span class='bool'>true</span>" as &[u8]
|
|
|
|
} else {
|
|
|
|
b"<span class='bool'>false</span>" as &[u8]
|
|
|
|
};
|
|
|
|
writer.write_all(s)
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_i8<W>(&mut self, writer: &mut W, value: i8) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_i16<W>(&mut self, writer: &mut W, value: i16) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_i32<W>(&mut self, writer: &mut W, value: i32) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_i64<W>(&mut self, writer: &mut W, value: i64) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_u8<W>(&mut self, writer: &mut W, value: u8) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_u16<W>(&mut self, writer: &mut W, value: u16) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_u32<W>(&mut self, writer: &mut W, value: u32) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_u64<W>(&mut self, writer: &mut W, value: u64) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_f32<W>(&mut self, writer: &mut W, value: f32) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn write_f64<W>(&mut self, writer: &mut W, value: f64) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 22:57:52 +00:00
|
|
|
let buff = format!("<span class='number'>{value}</span>");
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(buff.as_bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn begin_string<W>(&mut self, writer: &mut W) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(b"<span class='string'>\"")
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn end_string<W>(&mut self, writer: &mut W) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(b"\"</span>")
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 07:51:34 +00:00
|
|
|
if first {
|
|
|
|
writer.write_all(b"<span class='key'>")
|
|
|
|
} else {
|
|
|
|
writer.write_all(b"<span class='key'>,")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:04:00 +00:00
|
|
|
fn end_object_key<W>(&mut self, writer: &mut W) -> io::Result<()>
|
|
|
|
where
|
|
|
|
W: ?Sized + io::Write,
|
|
|
|
{
|
2023-01-28 07:51:34 +00:00
|
|
|
writer.write_all(b"</span>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn beautify(body: String) -> String {
|
2023-01-28 19:22:29 +00:00
|
|
|
if body.is_empty() {
|
2023-01-28 23:04:00 +00:00
|
|
|
return String::new();
|
2023-01-28 07:51:34 +00:00
|
|
|
}
|
|
|
|
let Ok(mut json) = serde_json::from_str::<Value>(&body) else {
|
|
|
|
return body
|
|
|
|
};
|
|
|
|
if json["password"].is_string() {
|
|
|
|
json["password"] = Value::String("********".to_owned());
|
|
|
|
}
|
|
|
|
let mut writer: Vec<u8> = Vec::with_capacity(128);
|
|
|
|
let mut serializer = serde_json::Serializer::with_formatter(&mut writer, HtmlFormatter);
|
2023-01-28 19:22:29 +00:00
|
|
|
if json.serialize(&mut serializer).is_err() {
|
2023-01-28 23:04:00 +00:00
|
|
|
return body;
|
2023-01-28 07:51:34 +00:00
|
|
|
}
|
|
|
|
String::from_utf8_lossy(&writer).to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn generate() -> Response {
|
|
|
|
let lock = LOG.lock().await;
|
|
|
|
|
|
|
|
let mut html = r#"<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta http-equiv="refresh" content="5">
|
|
|
|
<link rel="stylesheet" href="css/console.css">
|
|
|
|
<title>XSSBook - Console</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
2023-01-28 23:04:00 +00:00
|
|
|
"#
|
|
|
|
.to_string();
|
2023-01-28 07:51:34 +00:00
|
|
|
|
|
|
|
for message in lock.iter() {
|
|
|
|
html.push_str(&message.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
html.push_str("</body></html>");
|
2023-01-27 21:04:04 +00:00
|
|
|
|
2023-01-28 07:51:34 +00:00
|
|
|
ResponseCode::Success.html(&html)
|
2023-01-28 23:04:00 +00:00
|
|
|
}
|