Generate room codes, python test script

This commit is contained in:
Dane Johnson 2022-09-29 15:28:38 -05:00
parent 23342c89ec
commit 09c5fe3d8b
4 changed files with 66 additions and 5 deletions

12
Cargo.lock generated
View File

@ -162,6 +162,7 @@ dependencies = [
name = "hexland-server"
version = "0.1.0"
dependencies = [
"sha2",
"tungstenite",
]
@ -425,6 +426,17 @@ dependencies = [
"digest",
]
[[package]]
name = "sha2"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "syn"
version = "1.0.100"

View File

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tungstenite = { version = "0.17.3", features = ["native-tls"] }
tungstenite = { version = "0.17.3", features = ["native-tls"] }
sha2 = "0.10.6"

View File

@ -1,23 +1,39 @@
use std::net::TcpListener;
use std::thread;
use std::sync::{Arc, Mutex};
use tungstenite::protocol::Message;
use sha2::{Sha256, Digest};
fn main() {
let code_generator = Arc::new(Mutex::new(CodeGenerator {
counter: 0,
salt: [0; 32], // TODO have this be randomized on startup
}));
let server = TcpListener::bind("127.0.0.1:8080").unwrap();
for stream in server.incoming() {
thread::spawn ( move || {
thread::spawn ( { let code_generator = Arc::clone(&code_generator); move || {
let mut ws = tungstenite::accept(stream.unwrap()).unwrap();
println!("New client!");
ws.write_message(Message::Text("HOSTJOIN:".to_string())).unwrap();
loop {
let message = ws.read_message();
println!("{:?}", message);
if message.is_err() {
break
match message {
Err(_) => break,
Ok(Message::Text(msg)) =>
match msg.as_str().trim() {
"HOST:" => {
ws.write_message(Message::Text(code_generator.lock().unwrap().generate())).unwrap();
},
_ => unimplemented!(),
},
_ => unimplemented!(),
}
}
});
}});
}
}
@ -28,3 +44,21 @@ struct Room {
struct Player {
name: String,
}
struct CodeGenerator {
counter: u64,
salt: [u8; 32],
}
impl CodeGenerator {
fn generate(&mut self) -> String {
let count = self.counter;
self.counter += 1;
let mut hasher = Sha256::new();
hasher.update(self.salt);
hasher.update(count.to_be_bytes());
format!("{:x}", hasher.finalize())[..6].to_string()
}
}

14
test/test.py Normal file
View File

@ -0,0 +1,14 @@
import asyncio
import websockets
import re
async def test():
async with websockets.connect("ws://localhost:8080") as ws:
response = await ws.recv()
assert(response == "HOSTJOIN:")
await ws.send("HOST:")
response = await ws.recv()
assert(re.match(r"(\d|[a-f]){6}", response))
asyncio.get_event_loop().run_until_complete(test())
print("All tests passed")