Serve quic with config bootstrap
Browse files at this commit56893d1953858837f75e7383a0b22efec4dfe423Parent: 4c71a7e- feat
- serve quic with config bootstrap
5 changed files +167 -0
- .gitignore +1 -0
- configs/server_config.example.toml +8 -0
- crates/server/src/lib.rs +58 -0
- crates/server/src/main.rs +4 -0
- crates/server/src/network.rs +96 -0
.gitignoremodified+1 -0
| @@ -1,2 +1,3 @@ | |||
| 1 | /target | 1 | /target |
| 2 | Cargo.lock | 2 | Cargo.lock |
| 3 | +configs/server_config.toml | ||
configs/server_config.example.tomladded+8 -0
| @@ -0,0 +1,8 @@ | |||
| 1 | +[server_config] | ||
| 2 | +database_prefix = "wss" | ||
| 3 | +database_address = "127.0.0.1:8000" | ||
| 4 | +namespace = "yumush" | ||
| 5 | +database = "yumush" | ||
| 6 | +username = "yumush" | ||
| 7 | +password = "yumush" | ||
| 8 | +server_address = "127.0.0.1:2026" | ||
crates/server/src/lib.rsmodified+58 -0
| @@ -1 +1,59 @@ | |||
| 1 | +use common::utils::TOML; | ||
| 2 | + | ||
| 3 | +use crate::network::serve; | ||
| 4 | + | ||
| 5 | +pub mod authentication; | ||
| 6 | +pub mod community; | ||
| 7 | +pub(crate) mod database_; | ||
| 8 | +pub(crate) mod error; | ||
| 9 | +pub mod message; | ||
| 1 | pub mod network; | 10 | pub mod network; |
| 11 | +pub mod request; | ||
| 12 | +pub mod user; | ||
| 13 | +pub mod user_community; | ||
| 14 | + | ||
| 15 | +#[cfg(test)] | ||
| 16 | +mod test; | ||
| 17 | + | ||
| 18 | +const SERVER_CONFIG_FILE_PATH: &str = concat!( | ||
| 19 | + env!("CARGO_MANIFEST_DIR"), | ||
| 20 | + "/../../configs/server_config.toml" | ||
| 21 | +); | ||
| 22 | + | ||
| 23 | +pub struct ServerConfig { | ||
| 24 | + database_prefix: String, | ||
| 25 | + database_address: String, | ||
| 26 | + namespace: String, | ||
| 27 | + database: String, | ||
| 28 | + username: String, | ||
| 29 | + password: String, | ||
| 30 | + server_address: String, | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +impl Default for ServerConfig { | ||
| 34 | + fn default() -> Self { | ||
| 35 | + let mut toml = TOML::naive_toml_parser(SERVER_CONFIG_FILE_PATH).unwrap(); | ||
| 36 | + | ||
| 37 | + Self { | ||
| 38 | + database_prefix: toml.fields.remove("database_prefix").unwrap(), | ||
| 39 | + database_address: toml.fields.remove("database_address").unwrap(), | ||
| 40 | + namespace: toml.fields.remove("namespace").unwrap(), | ||
| 41 | + database: toml.fields.remove("database").unwrap(), | ||
| 42 | + username: toml.fields.remove("username").unwrap(), | ||
| 43 | + password: toml.fields.remove("password").unwrap(), | ||
| 44 | + server_address: toml.fields.remove("server_address").unwrap(), | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +pub async fn showtime() { | ||
| 50 | + rustls::crypto::aws_lc_rs::default_provider() | ||
| 51 | + .install_default() | ||
| 52 | + .unwrap(); | ||
| 53 | + let server_config = ServerConfig::default(); | ||
| 54 | + | ||
| 55 | + let database_connection = database_::connect(&server_config).await.unwrap(); | ||
| 56 | + database_::migrate(&database_connection).await.unwrap(); | ||
| 57 | + database_::health(&database_connection).await.unwrap(); | ||
| 58 | + serve(&server_config.server_address, &database_connection).await; | ||
| 59 | +} | ||
crates/server/src/main.rsmodified+4 -0
| @@ -1,4 +1,8 @@ | |||
| 1 | +use server::showtime; | ||
| 2 | + | ||
| 1 | #[tokio::main] | 3 | #[tokio::main] |
| 2 | async fn main() { | 4 | async fn main() { |
| 3 | println!("Hello, world!"); | 5 | println!("Hello, world!"); |
| 6 | + | ||
| 7 | + showtime().await; | ||
| 4 | } | 8 | } |
crates/server/src/network.rsmodified+96 -0
| @@ -0,0 +1,96 @@ | |||
| 1 | +use std::{collections::HashMap, sync::LazyLock}; | ||
| 2 | + | ||
| 3 | +use common::{ | ||
| 4 | + network::{AUTHENTICATION_MAX_READ_LENGHT, Network}, | ||
| 5 | + request::Request, | ||
| 6 | + response::Response, | ||
| 7 | +}; | ||
| 8 | +use quinn::{Connection, Endpoint, Incoming, RecvStream, SendStream}; | ||
| 9 | +use tokio::sync::RwLock; | ||
| 10 | + | ||
| 11 | +use crate::{ | ||
| 12 | + authentication::authenticate, database_::DB, error::Error, request::handle_request, user::User, | ||
| 13 | +}; | ||
| 14 | + | ||
| 15 | +static USER_SEND_STREAM_MAP: LazyLock<RwLock<HashMap<User, SendStream>>> = | ||
| 16 | + LazyLock::new(|| RwLock::new(HashMap::new())); | ||
| 17 | + | ||
| 18 | +#[cfg(feature = "rcgen")] | ||
| 19 | +fn config() -> quinn::ServerConfig { | ||
| 20 | + use rustls::pki_types::{CertificateDer, PrivatePkcs8KeyDer}; | ||
| 21 | + | ||
| 22 | + let certificate = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap(); | ||
| 23 | + let certificate_der = CertificateDer::from(certificate.cert); | ||
| 24 | + let certificate_key = PrivatePkcs8KeyDer::from(certificate.signing_key.serialize_der()); | ||
| 25 | + quinn::ServerConfig::with_single_cert(vec![certificate_der], certificate_key.into()).unwrap() | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +pub async fn serve(server_address: &str, database_connection: &DB) { | ||
| 29 | + let endpoint = Endpoint::server(config(), server_address.parse().unwrap()).unwrap(); | ||
| 30 | + | ||
| 31 | + while let Some(incoming) = endpoint.accept().await { | ||
| 32 | + let database_connection = database_connection.clone(); | ||
| 33 | + tokio::spawn(handle_connection(incoming, database_connection)); | ||
| 34 | + } | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +async fn handle_connection(incoming: Incoming, database_connection: DB) { | ||
| 38 | + match incoming.await { | ||
| 39 | + Ok(connection) => { | ||
| 40 | + if let Err(error_value) = establish_connection(&connection, &database_connection).await | ||
| 41 | + { | ||
| 42 | + eprintln!("Error = Endpoint Accept | {}", error_value.to_string()); | ||
| 43 | + connection.close(0u8.into(), b"kendine iyi bak"); | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + Err(error_value) => eprintln!("Error = Connection | {}", error_value.to_string()), | ||
| 47 | + } | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +async fn establish_connection( | ||
| 51 | + connection: &Connection, | ||
| 52 | + database_connection: &DB, | ||
| 53 | +) -> Result<(), Error> { | ||
| 54 | + let (send_stream, receive_stream) = connection.accept_bi().await?; | ||
| 55 | + | ||
| 56 | + let authentication_request = | ||
| 57 | + Network::receive_request(receive_stream, Some(AUTHENTICATION_MAX_READ_LENGHT)).await?; | ||
| 58 | + if let Request::Authentication(authentication) = authentication_request { | ||
| 59 | + let authentication_token = authentication.get_authentication_token(); | ||
| 60 | + let user = authenticate(authentication_token, database_connection).await?; | ||
| 61 | + let response = Response::Authentication(common::user::User::new( | ||
| 62 | + user.get_id().as_str(), | ||
| 63 | + user.get_username().as_str(), | ||
| 64 | + )); | ||
| 65 | + tokio::spawn(listen(connection.clone(), database_connection.clone())); | ||
| 66 | + | ||
| 67 | + Network::send_response(&response, send_stream).await?; | ||
| 68 | + | ||
| 69 | + let send_stream = connection.open_uni().await?; | ||
| 70 | + USER_SEND_STREAM_MAP.write().await.insert(user, send_stream); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + Err(Error::Common(common::error::Error::Authenticate)) | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +async fn listen(connection: Connection, database_connection: DB) { | ||
| 77 | + let read_and_answer = async |send_stream: SendStream, | ||
| 78 | + receive_stream: RecvStream, | ||
| 79 | + database_connection: DB| | ||
| 80 | + -> Result<(), Error> { | ||
| 81 | + let request = Network::receive_request(receive_stream, None).await?; | ||
| 82 | + let response = handle_request(request, &database_connection).await; | ||
| 83 | + Network::send_response(&response, send_stream).await?; | ||
| 84 | + | ||
| 85 | + Ok(()) | ||
| 86 | + }; | ||
| 87 | + | ||
| 88 | + while let Ok((send_stream, receive_stream)) = connection.accept_bi().await { | ||
| 89 | + let database_connection = database_connection.clone(); | ||
| 90 | + tokio::spawn(read_and_answer( | ||
| 91 | + send_stream, | ||
| 92 | + receive_stream, | ||
| 93 | + database_connection, | ||
| 94 | + )); | ||
| 95 | + } | ||
| 96 | +} | ||