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
//! Log requests to the console or a file.

// If file logging is enabled
use std::fs::{File, OpenOptions};
use std::io::{self, prelude::*};
use std::path::Path;
use std::sync::Mutex;

use crate::{extension::RealIp, HeaderType, Middleware, Request, Response};

/// Define Log Levels
#[derive(Debug)]
pub enum Level {
    /// Give lots of information on what's going on.
    ///
    /// Adds Request Headers and Body
    Debug,

    /// Give a reasonable amount of information on what's going on.
    ///
    /// So IP, Method and Path
    Info,
}

/// Log requests to the console or a file.
#[derive(Debug)]
pub struct Logger {
    /// What level of logs to show
    level: Level,

    /// What header to use to get the clients actual IP
    real_ip: Option<HeaderType>,

    /// Optional file to write logs to
    file: Option<Mutex<File>>,

    /// If logs should also be printed to stdout
    console: bool,
}

impl Logger {
    /// Make a new logger
    ///
    /// The default settings are as follows
    ///
    /// - Log Level: `Level::Info`
    ///
    /// - File: `None`
    ///
    /// - Console: `true`
    /// ## Example
    /// ```rust
    /// // Import Lib
    /// use afire::extension::logger::{Logger, Level};
    ///
    /// // Create a new logger
    /// let logger = Logger::new();
    /// ```
    pub fn new() -> Logger {
        Logger {
            level: Level::Info,
            real_ip: None,
            file: None,
            console: true,
        }
    }

    /// Set the log Level of a logger
    /// ## Example
    /// ```rust
    /// // Import Lib
    /// use afire::extension::logger::{Logger, Level};
    ///
    /// // Create a new logger
    /// let logger = Logger::new()
    ///     .level(Level::Debug);
    /// ```
    pub fn level(self, level: Level) -> Self {
        Self { level, ..self }
    }

    /// Uses the [`crate::extension::RealIp`] extension for log IPs.
    /// You will need to supply the header that will contain the IP address, for example the [X-Forwarded-For header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For) ([`HeaderType::XForwardedFor`])
    ///
    /// **Warning**: Make sure your reverse proxy is overwriting the specified header on the incoming requests so clients cant spoof their original Ips.
    pub fn real_ip(self, real_ip: HeaderType) -> Self {
        Self {
            real_ip: Some(real_ip),
            ..self
        }
    }

    /// Set the log file of a logger
    /// ## Example
    /// ```rust
    /// // Import Lib
    /// use afire::extension::logger::{Logger, Level};
    ///
    /// // Create a new logger and enable logging to file
    /// # fn run() {
    /// let logger = Logger::new()
    ///     .file("nose.txt");
    /// # }
    /// ```
    pub fn file(self, file: impl AsRef<Path>) -> io::Result<Self> {
        Ok(Self {
            file: Some(Mutex::new(
                OpenOptions::new()
                    .create(true)
                    .write(true)
                    .append(true)
                    .open(file)?,
            )),
            ..self
        })
    }

    /// Enable writing events to stdout
    /// ## Example
    /// ```rust
    /// // Import Lib
    /// use afire::extension::logger::{Logger, Level};
    ///
    /// // Create a new logger and enable console
    /// let logger = Logger::new()
    ///     .console(true );
    /// ```
    pub fn console(self, console: bool) -> Self {
        Self { console, ..self }
    }

    /// Take a request and log it
    fn log(&self, req: &Request) {
        let ip = match &self.real_ip {
            Some(i) => req.real_ip_header(i),
            None => req.address.ip(),
        };

        match self.level {
            // Add Headers and Body to this one
            Level::Debug => {
                // Format headers as strings
                let mut headers = "".to_string();
                for i in &*req.headers {
                    headers += &format!("{}: {}, ", i.name, i.value);
                }
                if headers.len() >= 2 {
                    headers = headers[0..headers.len() - 2].to_string()
                }

                // Format Query as string
                let mut query = "".to_string();
                for i in req.query.iter() {
                    query += &format!("{}: {}, ", i[0], i[1]);
                }
                if query.len() >= 2 {
                    query = query[0..query.len() - 2].to_string()
                }

                let mut new_path = req.path.to_owned();
                if new_path.is_empty() {
                    new_path = "/".to_string();
                }

                self.send_log(format!(
                    "[{ip}] {} {} [{}] ({}) {{{}}}",
                    req.method,
                    new_path,
                    query,
                    headers,
                    String::from_utf8_lossy(&req.body).replace('\n', "\\n")
                ))
            }

            Level::Info => {
                let mut new_path = req.path.clone();
                if new_path.is_empty() {
                    new_path = "/".to_string();
                }

                self.send_log(format!("[{ip}] {} {}{}", req.method, new_path, req.query))
            }
        }
    }

    /// Send log data to file / stdout
    fn send_log(&self, data: String) {
        if self.console {
            println!("{data}");
        }

        if let Some(i) = &self.file {
            if let Err(e) = writeln!(i.lock().unwrap(), "{data}") {
                eprintln!("[-] Erm... Error writhing to log file: {e}")
            }
        }
    }
}

impl Middleware for Logger {
    fn end(&self, req: &Request, _res: &Response) {
        self.log(req);
    }
}

// Impl Default for Response
impl Default for Logger {
    fn default() -> Logger {
        Logger::new()
    }
}