Skip to content

Commit

Permalink
Update RNG
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Sep 22, 2024
1 parent 8df918b commit f80a081
Show file tree
Hide file tree
Showing 38 changed files with 465 additions and 394 deletions.
53 changes: 28 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions wtx-instances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pb-rs = { default-features = false, version = "0.10" }
[dependencies]
argon2 = { default-features = false, version = "0.5" }
quick-protobuf = { default-features = false, version = "0.8" }
rand_chacha = { default-features = false, version = "0.3" }
rand_core = { default-features = false, feature = ["getrandom"], version = "0.6" }
serde = { default-features = false, version = "1.0" }
serde_json = { default-features = false, features = ["alloc"], version = "1.0" }
tokio = { default-features = false, features = ["macros", "rt-multi-thread"], version = "1.0" }
Expand Down
6 changes: 3 additions & 3 deletions wtx-instances/generic-examples/client-api-framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use wtx::{
},
data_transformation::dnsn::SerdeJson,
http::client_framework::ClientFrameworkTokio,
misc::{NoStdRng, Uri},
misc::{simple_seed, Uri, Xorshift64},
web_socket::{FrameBufferVec, HeadersBuffer, WebSocketBuffer, WebSocketClient},
};

Expand Down Expand Up @@ -97,7 +97,7 @@ async fn http_pair(
async fn web_socket_pair() -> wtx::Result<
Pair<
PkgsAux<GenericThrottlingApi, SerdeJson, WsParams>,
(FrameBufferVec, WebSocketClient<(), NoStdRng, TcpStream, WebSocketBuffer>),
(FrameBufferVec, WebSocketClient<(), Xorshift64, TcpStream, WebSocketBuffer>),
>,
> {
let mut fb = FrameBufferVec::default();
Expand All @@ -107,7 +107,7 @@ async fn web_socket_pair() -> wtx::Result<
&mut fb,
[],
&mut HeadersBuffer::default(),
NoStdRng::default(),
Xorshift64::from(simple_seed()),
TcpStream::connect(uri.hostname_with_implied_port()).await?,
&uri,
WebSocketBuffer::default(),
Expand Down
2 changes: 2 additions & 0 deletions wtx-instances/generic-examples/grpc-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use wtx::{
server_framework::{post, Router, ServerFrameworkBuilder, State},
ReqResBuffer, StatusCode,
},
misc::{simple_seed, Xorshift64},
};
use wtx_instances::grpc_bindings::wtx::{GenericRequest, GenericResponse};

Expand All @@ -28,6 +29,7 @@ async fn main() -> wtx::Result<()> {
.listen_tls(
(wtx_instances::CERT, wtx_instances::KEY),
&wtx_instances::host_from_args(),
Xorshift64::from(simple_seed()),
|error| eprintln!("{error}"),
)
.await
Expand Down
4 changes: 2 additions & 2 deletions wtx-instances/generic-examples/http2-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use tokio::net::TcpStream;
use wtx::{
http::{Method, ReqResBuffer, Request},
http2::{Http2Buffer, Http2ErrorCode, Http2Params, Http2Tokio},
misc::{from_utf8_basic, NoStdRng, Uri},
misc::{from_utf8_basic, simple_seed, Uri, Xorshift64},
};

#[tokio::main]
async fn main() -> wtx::Result<()> {
let uri = Uri::new("http://www.example.com");
let (frame_reader, mut http2) = Http2Tokio::connect(
Http2Buffer::new(NoStdRng::default()),
Http2Buffer::new(Xorshift64::from(simple_seed())),
Http2Params::default(),
TcpStream::connect(uri.hostname_with_implied_port()).await?.into_split(),
)
Expand Down
4 changes: 2 additions & 2 deletions wtx-instances/generic-examples/http2-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ extern crate wtx_instances;
use wtx::{
http::{LowLevelServer, ReqResBuffer, Request, Response, StatusCode},
http2::{Http2Buffer, Http2Params},
misc::{StdRng, TokioRustlsAcceptor},
misc::{simple_seed, TokioRustlsAcceptor, Xorshift64},
};

#[tokio::main]
async fn main() -> wtx::Result<()> {
LowLevelServer::tokio_http2(
&wtx_instances::host_from_args(),
|| Ok(((), Http2Buffer::new(StdRng::default()), Http2Params::default())),
|| Ok(((), Http2Buffer::new(Xorshift64::from(simple_seed())), Http2Params::default())),
|error| eprintln!("{error}"),
handle,
|| Ok(((), ReqResBuffer::empty())),
Expand Down
4 changes: 2 additions & 2 deletions wtx-instances/generic-examples/web-socket-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tokio::{
net::TcpStream,
};
use wtx::{
misc::{StdRng, Uri},
misc::{simple_seed, Uri, Xorshift64},
web_socket::{
FrameBufferVec, FrameMutVec, HeadersBuffer, OpCode, WebSocketBuffer, WebSocketClient,
},
Expand All @@ -27,7 +27,7 @@ async fn main() -> wtx::Result<()> {
fb,
[],
&mut HeadersBuffer::default(),
StdRng::default(),
Xorshift64::from(simple_seed()),
TcpStream::connect(uri.hostname_with_implied_port()).await?,
&uri.to_ref(),
WebSocketBuffer::default(),
Expand Down
4 changes: 2 additions & 2 deletions wtx-instances/generic-examples/web-socket-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tokio::net::TcpStream;
use tokio_rustls::server::TlsStream;
use wtx::{
http::LowLevelServer,
misc::{StdRng, TokioRustlsAcceptor},
misc::{TokioRustlsAcceptor, Xorshift64},
web_socket::{FrameBufferVec, OpCode, WebSocketBuffer, WebSocketServer},
};

Expand All @@ -35,7 +35,7 @@ async fn main() -> wtx::Result<()> {

async fn handle(
fb: &mut FrameBufferVec,
mut ws: WebSocketServer<(), StdRng, TlsStream<TcpStream>, &mut WebSocketBuffer>,
mut ws: WebSocketServer<(), Xorshift64, TlsStream<TcpStream>, &mut WebSocketBuffer>,
) -> wtx::Result<()> {
loop {
let mut frame = ws.read_frame(fb).await?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
//! The `CorsMiddleware` middleware inserts permissive CORS headers in every response.
use wtx::http::server_framework::{get, CorsMiddleware, Router, ServerFrameworkBuilder};
use wtx::{
http::server_framework::{get, CorsMiddleware, Router, ServerFrameworkBuilder},
misc::{simple_seed, Xorshift64},
};

#[tokio::main]
async fn main() -> wtx::Result<()> {
let router = Router::new(wtx::paths!(("/hello", get(hello))), (), CorsMiddleware::permissive())?;
ServerFrameworkBuilder::new(router)
.without_aux()
.listen("0.0.0.0:9000", |error: wtx::Error| eprintln!("{error:?}"))
.listen("0.0.0.0:9000", Xorshift64::from(simple_seed()), |error: wtx::Error| {
eprintln!("{error:?}")
})
.await?;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Different types of redirects.
use wtx::http::{
server_framework::{get, Redirect, Router, ServerFrameworkBuilder, StateClean},
ReqResBuffer, StatusCode,
use wtx::{
http::{
server_framework::{get, Redirect, Router, ServerFrameworkBuilder, StateClean},
ReqResBuffer, StatusCode,
},
misc::{simple_seed, Xorshift64},
};

#[tokio::main]
Expand All @@ -11,7 +14,9 @@ async fn main() -> wtx::Result<()> {
Router::paths(wtx::paths!(("/permanent", get(permanent)), ("/temporary", get(temporary))))?;
ServerFrameworkBuilder::new(router)
.without_aux()
.listen(&wtx_instances::host_from_args(), |error| eprintln!("{error}"))
.listen(&wtx_instances::host_from_args(), Xorshift64::from(simple_seed()), |error| {
eprintln!("{error}")
})
.await
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
//!
//! ALTER TABLE session ADD CONSTRAINT session__user__fk FOREIGN KEY (user_id) REFERENCES "user" (id);
//! ```
//!
use argon2::{Algorithm, Argon2, Block, Params, Version};
use rand_chacha::{rand_core::SeedableRng, ChaCha20Rng};
use tokio::net::TcpStream;
use wtx::{
database::{Executor, Record},
http::{
server_framework::{get, post, Router, ServerFrameworkBuilder, State, StateClean},
ReqResBuffer, ReqResData, SessionDecoder, SessionEnforcer, SessionTokio, StatusCode,
},
misc::{Rng, StdRng},
misc::Rng,
pool::{PostgresRM, SimplePoolTokio},
};

Expand All @@ -47,7 +49,7 @@ const ARGON2_PARAMS: Params = {
const LOGIN: &str = "/login";
const LOGOUT: &str = "/logout";

type ConnAux = (Session, StdRng);
type ConnAux = (Session, ChaCha20Rng);
type Pool = SimplePoolTokio<PostgresRM<wtx::Error, TcpStream>>;
type Session = SessionTokio<u32, wtx::Error, Pool>;

Expand All @@ -59,7 +61,7 @@ async fn main() -> wtx::Result<()> {
(),
)?;
let pool = Pool::new(4, PostgresRM::tokio("postgres://USER:PASSWORD@localhost/DB_NAME".into()));
let mut rng = StdRng::default();
let mut rng = ChaCha20Rng::from_entropy();
let mut key = [0; 16];
rng.fill_slice(&mut key);
let (expired_sessions, session) = Session::builder(key, pool).build();
Expand All @@ -68,9 +70,10 @@ async fn main() -> wtx::Result<()> {
eprintln!("{err}");
}
});
let rng_clone = rng.clone();
ServerFrameworkBuilder::new(router)
.with_conn_aux(move || (session.clone(), rng))
.listen("0.0.0.0:9000", |error: wtx::Error| eprintln!("{error:?}"))
.with_conn_aux(move || (session.clone(), rng_clone.clone()))
.listen("0.0.0.0:9000", rng, |error: wtx::Error| eprintln!("{error:?}"))
.await?;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use wtx::{
},
ReqResBuffer, Request, Response, StatusCode,
},
misc::FnFutWrapper,
misc::{simple_seed, FnFutWrapper, Xorshift64},
pool::{PostgresRM, SimplePoolTokio},
};

Expand All @@ -45,7 +45,9 @@ async fn main() -> wtx::Result<()> {
let pool = Pool::new(4, rm);
ServerFrameworkBuilder::new(router)
.with_req_aux(move || pool.clone())
.listen(&wtx_instances::host_from_args(), |error| eprintln!("{error:?}"))
.listen(&wtx_instances::host_from_args(), Xorshift64::from(simple_seed()), |error| {
eprintln!("{error:?}")
})
.await
}

Expand Down
Loading

0 comments on commit f80a081

Please sign in to comment.