Basic ugly server working

This commit is contained in:
2021-05-14 21:17:38 +02:00
parent 0b76ba2069
commit bb12b57bf1
5 changed files with 106 additions and 1 deletions

View File

@ -1,3 +1,64 @@
mod http;
mod udp;
use std::thread;
use threadpool::ThreadPool;
use std::net::{TcpListener, UdpSocket};
use openssl::ssl::{SslMethod, SslAcceptor, SslStream, SslFiletype};
use std::sync::Arc;
use std::sync::Mutex;
fn main() {
println!("Hello, world!");
let mut threads = Vec::new();
let pool = ThreadPool::new(256);
let pool_mutex = Arc::new(Mutex::new(pool));
let pool_mutex_ref = pool_mutex.clone();
threads.push(thread::spawn(move || {
let mut tcp_socket = TcpListener::bind("[::]:8080").unwrap();
for stream in tcp_socket.incoming() {
pool_mutex_ref.lock().unwrap().execute(|| {
let stream = stream.unwrap();
http::connection_handler(http::Stream::Tcp(stream));
});
}
}));
let pool_mutex_ref = pool_mutex.clone();
threads.push(thread::spawn(move || {
let mut ssl_socket = TcpListener::bind("[::]:8443").unwrap();
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
acceptor.set_certificate_chain_file("/home/lorenz/Certificates/chakotay.pem").unwrap();
acceptor.set_private_key_file("/home/lorenz/Certificates/priv/chakotay.key", SslFiletype::PEM).unwrap();
acceptor.check_private_key().unwrap();
let acceptor = Arc::new(acceptor.build());
for stream in ssl_socket.incoming() {
let acceptor = acceptor.clone();
pool_mutex_ref.lock().unwrap().execute(move || {
let stream = stream.unwrap();
let stream = acceptor.accept(stream).unwrap();
http::connection_handler(http::Stream::Ssl(stream));
});
}
}));
let pool_mutex_ref = pool_mutex.clone();
threads.push(thread::spawn(move || {
let mut udp_socket = UdpSocket::bind("[::]:12345").unwrap();
let mut buf = [0; 65_536];
loop {
let (size, addr) = udp_socket.recv_from(&mut buf).unwrap();
let req = udp::Request::new(&udp_socket, addr, size, &buf);
pool_mutex_ref.lock().unwrap().execute(|| udp::handler(req));
}
}));
for thread in threads {
thread.join();
}
}