Basic ugly server working
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
/.idea
|
||||||
|
@ -7,3 +7,6 @@ edition = "2018"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
threadpool = "1.0"
|
||||||
|
json = "0.12.4"
|
||||||
|
openssl = {version = "0.10", features = ["vendored"]}
|
||||||
|
17
src/http.rs
Normal file
17
src/http.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
use std::net::TcpStream;
|
||||||
|
use openssl::ssl::SslStream;
|
||||||
|
|
||||||
|
pub enum Stream {
|
||||||
|
Tcp(TcpStream),
|
||||||
|
Ssl(SslStream<TcpStream>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Stream {}
|
||||||
|
|
||||||
|
pub fn connection_handler(client: Stream) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn request_handler(client: Stream) {
|
||||||
|
|
||||||
|
}
|
63
src/main.rs
63
src/main.rs
@ -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() {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
23
src/udp.rs
Normal file
23
src/udp.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
use std::net::{UdpSocket, SocketAddr};
|
||||||
|
|
||||||
|
pub struct Request {
|
||||||
|
socket: UdpSocket,
|
||||||
|
address: SocketAddr,
|
||||||
|
size: usize,
|
||||||
|
buf: [u8; 65_536],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Request {
|
||||||
|
pub fn new(socket: &UdpSocket, address: SocketAddr, size: usize, buf: &[u8; 65_536]) -> Request {
|
||||||
|
Request {
|
||||||
|
socket: socket.try_clone().unwrap(),
|
||||||
|
address,
|
||||||
|
size,
|
||||||
|
buf: buf.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handler(request: Request) {
|
||||||
|
// TODO handle UDP requests
|
||||||
|
}
|
Reference in New Issue
Block a user