netter 0.3.2

Netter is a CLI tool for fast and easy server startup!
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
use hyper::{body::{self, Bytes}, header::{HeaderName, HeaderValue}, server::conn::http1, service::service_fn, Request, Response, StatusCode};
use log::{debug, trace, error, info};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::str::FromStr;
use derive_more::Debug;
use std::fs::File;
use std::io::BufReader;
use rustls::ServerConfig;
use rustls_pemfile::{certs, pkcs8_private_keys};
use tokio::net::TcpListener;
use tokio_rustls::TlsAcceptor;
use hyper_util::rt::TokioIo;
use crate::{core::{config_parser::load_config, language::{interpreter::Interpreter, lexer::AstNode}}, state};

#[derive(Debug)]
pub struct Routes {
    method: String,
    path: String,
    response: Resp,
}

#[derive(Debug)]
pub struct Resp {
    body: String,
    headers: Vec<(String, String)>,
    status: u16,
}

#[derive(Debug)]
pub struct Server {
    addr: Vec<u16>,
    port: u16,
    routes: Vec<Routes>,
    #[debug(skip)]
    interpreter: Option<Arc<Mutex<Interpreter>>>,
    tls_config: Option<TlsConfig>,
    rustls_config: Option<Arc<ServerConfig>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TlsConfig {
    pub(crate) enabled: bool,
    pub(crate) cert_path: String,
    pub(crate) key_path: String,
}

#[allow(dead_code)]
pub trait HTTP {
    fn new(addr: Vec<u16>, port: u16, routes: Routes) -> Self;
    async fn start(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
}

// #[derive(Serialize, Deserialize)]
// pub struct ConfigHttp {
//     host: String,
//     port: u16,
//     routes: Vec<RouteConfig>,
//     tls: Option<TlsConfig>,
// }

#[derive(Serialize, Deserialize)]
pub struct RouteConfig {
    method: String,
    path: String,
    response: RespConfig,
}

#[derive(Serialize, Deserialize)]
pub struct RespConfig {
    body: String,
    headers: Vec<(String, String)>,
    status: u16,
}

impl Server {
    pub fn configure(file_path: &str) -> Result<Self, Box<dyn std::error::Error>> {
        debug!("Read config data");

        let config = load_config(file_path)?;

        let routes = config.routes.into_iter().map(|route| Routes {
            method: route.method,
            path: route.path,
            response: Resp {
                body: route.response.body,
                headers: route.response.headers,
                status: route.response.status,
            },
        }).collect();

        let tls_config = config.tls;
        let rustls_config = if let Some(tls) = &tls_config {
            if tls.enabled {
                match load_rustls_config(&tls.cert_path, &tls.key_path) {
                    Ok(config) => Some(Arc::new(config)),
                    Err(e) => {
                        error!("Failed to load TLS configuration: {}", e);
                        None
                    }
                }
            } else {
                None
            }
        } else {
            None
        };

        Ok(Server {
            addr: config.host.split('.')
                .map(|s| s.parse::<u16>().unwrap_or(127))
                .collect(),
            port: config.port,
            routes,
            interpreter: None,
            tls_config,
            rustls_config,
        })
    }

    pub fn from_interpreter(
        addr: Vec<u16>, 
        port: u16, 
        interpreter: Interpreter,
        tls_config: Option<TlsConfig>,
    ) -> Self {
        let rustls_config = if let Some(tls) = &tls_config {
            if tls.enabled {
                match load_rustls_config(&tls.cert_path, &tls.key_path) {
                    Ok(config) => Some(Arc::new(config)),
                    Err(e) => {
                        error!("Failed to load TLS configuration: {}", e);
                        None
                    }
                }
            } else {
                None
            }
        } else {
            None
        };

        Server {
            addr,
            port,
            routes: Vec::new(),
            interpreter: Some(Arc::new(Mutex::new(interpreter))),
            tls_config,
            rustls_config,
        }
    }

    pub fn from_ast(
        addr: Vec<u16>, 
        port: u16, 
        ast: &AstNode,
        tls_config: Option<TlsConfig>,
    ) -> Result<Self, String> {
        let mut interpreter = Interpreter::new();
        interpreter.interpret(ast)?;
        
        Ok(Server::from_interpreter(addr, port, interpreter, tls_config))
    }

    pub fn enable_tls(&mut self, cert_path: String, key_path: String) -> Result<(), Box<dyn std::error::Error>> {
        let tls_config = TlsConfig {
            enabled: true,
            cert_path: cert_path.clone(),
            key_path: key_path.clone(),
        };

        let rustls_config = load_rustls_config(&cert_path, &key_path)?;
        
        self.tls_config = Some(tls_config);
        self.rustls_config = Some(Arc::new(rustls_config));
        
        Ok(())
    }

    pub fn disable_tls(&mut self) {
        if let Some(tls_config) = &mut self.tls_config {
            tls_config.enabled = false;
        }
        self.rustls_config = None;
    }

    pub fn is_tls_enabled(&self) -> bool {
        self.tls_config.as_ref().map_or(false, |c| c.enabled) && self.rustls_config.is_some()
    }
}

fn load_rustls_config(cert_path: &str, key_path: &str) -> Result<ServerConfig, Box<dyn std::error::Error>> {
    let cert_file = File::open(cert_path)?;
    let mut cert_reader = BufReader::new(cert_file);

    let mut cert_chain = Vec::new();
    for cert_result in certs(&mut cert_reader) {
        let cert = cert_result?;
        cert_chain.push(cert.to_owned());
    }

    if cert_chain.is_empty() {
        return Err("No certificates found".into());
    }

    let key_file = File::open(key_path)?;
    let mut key_reader = BufReader::new(key_file);

    let mut keys = Vec::new();
    for key_result in pkcs8_private_keys(&mut key_reader) {
        let key = key_result?;
        keys.push(key.into());
    }
    
    if keys.is_empty() {
        return Err("No private keys found".into());
    }
    
    let private_key = keys.remove(0);

    let config = ServerConfig::builder()
        .with_no_client_auth()
        .with_single_cert(cert_chain, private_key)?;
    
    Ok(config)
}

async fn handler(
    req: Request<body::Incoming>,
    server_state: Arc<Mutex<Server>>
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
    let server = server_state.lock().unwrap();
    
    if let Some(interpreter) = &server.interpreter {
        let method = req.method().as_str().to_string();
        let path = req.uri().path().to_string();
        
        let params = HashMap::new();
        
        let response_obj = crate::core::language::interpreter::handle_request(
            &interpreter.lock().unwrap(),
            &method,
            &path,
            params
        );
        
        let mut response = Response::new(full(response_obj.body.unwrap_or_default()));
        *response.status_mut() = StatusCode::from_u16(response_obj.status).unwrap_or(StatusCode::OK);
        
        for (key, value) in &response_obj.headers {
            if let Ok(header_name) = HeaderName::from_str(key) {
                if let Ok(header_value) = HeaderValue::from_str(value) {
                    response.headers_mut().insert(header_name, header_value);
                }
            }
        }
        
        return Ok(response);
    }
    
    for route in &server.routes {
        if req.method().as_str() == route.method && req.uri().path() == route.path {
            let mut response = Response::new(full(route.response.body.clone()));
            *response.status_mut() = StatusCode::from_u16(route.response.status).unwrap_or(StatusCode::OK);
            for (key, value) in &route.response.headers {
                if let Ok(header_name) = HeaderName::from_str(key) {
                    if let Ok(header_value) = HeaderValue::from_str(value) {
                        response.headers_mut().insert(header_name, header_value);
                    }
                }
            }
            return Ok(response);
        }
    }

    let mut not_found = Response::new(empty());
    *not_found.status_mut() = StatusCode::NOT_FOUND;
    Ok(not_found)
}

impl HTTP for Server {
    fn new(addr: Vec<u16>, port: u16, routes: Routes) -> Self {
        Server {
            addr,
            port,
            routes: vec![routes],
            interpreter: None,
            tls_config: None,
            rustls_config: None,
        }
    }

    async fn start(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let addr_str = self.addr.iter().map(|n| n.to_string()).collect::<Vec<_>>().join(".");
        let addr = format!("{}:{}", addr_str, self.port);
        
        let protocol = if self.is_tls_enabled() { "HTTPS" } else { "HTTP" };
        state::save_state(String::from(protocol), addr_str.clone(), self.port)
            .map_err(|_| "Failed to save state")?;

        let server_state = Arc::new(Mutex::new(self.clone()));
        state::load_state();

        let listener = TcpListener::bind(&addr).await?;
        
        info!("Server starting at {}", addr);

        if self.is_tls_enabled() {
            info!("Starting HTTPS server on {}", addr);
            
            let tls_config = self.rustls_config.clone()
                .ok_or("TLS is enabled but configuration is missing")?;
                
            let tls_acceptor = TlsAcceptor::from(tls_config);
            trace!("HTTPS server started on {}", addr);

            loop {
                let (tcp_stream, _) = listener.accept().await?;
                let acceptor = tls_acceptor.clone();
                let server_clone = server_state.clone();
                
                tokio::task::spawn(async move {
                    match acceptor.accept(tcp_stream).await {
                        Ok(tls_stream) => {
                            let io = TokioIo::new(tls_stream);
                            
                            if let Err(err) = http1::Builder::new()
                                .serve_connection(io, service_fn(move |req| {
                                    let server_clone = server_clone.clone();
                                    async move { handler(req, server_clone).await }
                                }))
                                .await
                            {
                                error!("Error serving TLS connection: {}", err);
                            }
                        },
                        Err(err) => {
                            error!("TLS handshake failed: {}", err);
                        }
                    }
                });
            }
        } else {
            info!("Starting HTTP server on {}", addr);

            loop {
                let (tcp_stream, _) = listener.accept().await?;
                let io = TokioIo::new(tcp_stream);
                
                let server_clone = server_state.clone();
                
                tokio::task::spawn(async move {
                    if let Err(err) = http1::Builder::new()
                        .serve_connection(io, service_fn(move |req| {
                            let server_clone = server_clone.clone();
                            async move { handler(req, server_clone).await }
                        }))
                        .await
                    {
                        error!("Error serving connection: {}", err);
                    }
                });
            }
        }
    }
}

impl Clone for Server {
    fn clone(&self) -> Self {
        Server {
            addr: self.addr.clone(),
            port: self.port,
            routes: self.routes.clone(),
            interpreter: self.interpreter.clone(),
            tls_config: self.tls_config.clone(),
            rustls_config: self.rustls_config.clone(),
        }
    }
}

impl Clone for Routes {
    fn clone(&self) -> Self {
        Routes {
            method: self.method.clone(),
            path: self.path.clone(),
            response: self.response.clone(),
        }
    }
}

impl Clone for Resp {
    fn clone(&self) -> Self {
        Resp {
            body: self.body.clone(),
            headers: self.headers.clone(),
            status: self.status,
        }
    }
}

fn empty() -> BoxBody<Bytes, hyper::Error> {
    Empty::<Bytes>::new()
        .map_err(|never| match never {})
        .boxed()
}

fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> {
    Full::new(chunk.into())
        .map_err(|never| match never {})
        .boxed()
}