started the websocket communication
This commit is contained in:
parent
a4b1902fa5
commit
145cbb9243
3 changed files with 107 additions and 4 deletions
79
Cargo.lock
generated
79
Cargo.lock
generated
|
|
@ -8,12 +8,27 @@ version = "1.5.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.22.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2261d10cca569e4643e526d8dc2e62e433cc8aba21ab764233731f8d369bf394"
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.10.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.10.1"
|
||||
|
|
@ -26,6 +41,45 @@ version = "1.0.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
|
||||
|
||||
[[package]]
|
||||
name = "cpufeatures"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
||||
dependencies = [
|
||||
"block-buffer",
|
||||
"crypto-common",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4bb6743198531e02858aeaea5398fcc883e71851fcbcb5a2f773e2fb6cb1edf2"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.177"
|
||||
|
|
@ -57,6 +111,8 @@ dependencies = [
|
|||
name = "multiplayer-game"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"sha1",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
|
|
@ -122,6 +178,17 @@ version = "1.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "sha1"
|
||||
version = "0.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures",
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.6"
|
||||
|
|
@ -186,12 +253,24 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.1+wasi-snapshot-preview1"
|
||||
|
|
|
|||
|
|
@ -4,4 +4,6 @@ version = "0.1.0"
|
|||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.22.1"
|
||||
sha1 = "0.10.6"
|
||||
tokio = { version = "1.48.0", features = ["net", "fs", "signal", "process", "io-std", "full"] }
|
||||
|
|
|
|||
|
|
@ -3,17 +3,23 @@ use crate::{
|
|||
response::Response,
|
||||
};
|
||||
|
||||
use tokio::io;
|
||||
use tokio::io::{self, AsyncWriteExt};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use base64::prelude::*;
|
||||
use sha1::{Digest, Sha1};
|
||||
|
||||
pub struct WebsocketConnection {
|
||||
stream: TcpStream,
|
||||
}
|
||||
|
||||
impl WebsocketConnection {
|
||||
pub fn initialize_connection(req: Request, stream: TcpStream) -> tokio::io::Result<Self> {
|
||||
pub async fn initialize_connection(
|
||||
req: Request,
|
||||
mut stream: TcpStream,
|
||||
) -> tokio::io::Result<Self> {
|
||||
let (mut upgrade, mut connection, mut key_exists) = (false, false, false);
|
||||
let mut key_val;
|
||||
let mut key_val: Box<str> = "".into();
|
||||
|
||||
for i in req.headers {
|
||||
match i {
|
||||
|
|
@ -32,15 +38,31 @@ impl WebsocketConnection {
|
|||
RequestHeader::Other { name, value } => {
|
||||
if name == "Sec-WebSocket-Key".into() {
|
||||
key_val = value.clone();
|
||||
key_exists = true;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
if !upgrade || !connection || !key_exists {
|
||||
if upgrade && connection && key_exists {
|
||||
let magic_val = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||
let mut hasher = Sha1::new();
|
||||
hasher.update(key_val.as_bytes());
|
||||
hasher.update(magic_val);
|
||||
|
||||
let result = hasher.finalize();
|
||||
let result = BASE64_STANDARD.encode(result);
|
||||
|
||||
Response::new().with_code(200).with_header(crate::response::ResponseHeader::Location)
|
||||
|
||||
Ok(Self { stream })
|
||||
} else {
|
||||
Response::new()
|
||||
.with_code(crate::response::ResponseCode::BadRequest)
|
||||
.respond(&mut stream)
|
||||
.await?;
|
||||
stream.flush().await?;
|
||||
Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong request"))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue