Generate room codes, python test script
This commit is contained in:
parent
23342c89ec
commit
09c5fe3d8b
12
Cargo.lock
generated
12
Cargo.lock
generated
@ -162,6 +162,7 @@ dependencies = [
|
|||||||
name = "hexland-server"
|
name = "hexland-server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"sha2",
|
||||||
"tungstenite",
|
"tungstenite",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -425,6 +426,17 @@ dependencies = [
|
|||||||
"digest",
|
"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]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.100"
|
version = "1.0.100"
|
||||||
|
@ -6,4 +6,5 @@ edition = "2021"
|
|||||||
# 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]
|
||||||
tungstenite = { version = "0.17.3", features = ["native-tls"] }
|
tungstenite = { version = "0.17.3", features = ["native-tls"] }
|
||||||
|
sha2 = "0.10.6"
|
42
src/main.rs
42
src/main.rs
@ -1,23 +1,39 @@
|
|||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use tungstenite::protocol::Message;
|
use tungstenite::protocol::Message;
|
||||||
|
|
||||||
|
use sha2::{Sha256, Digest};
|
||||||
|
|
||||||
fn main() {
|
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();
|
let server = TcpListener::bind("127.0.0.1:8080").unwrap();
|
||||||
for stream in server.incoming() {
|
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();
|
let mut ws = tungstenite::accept(stream.unwrap()).unwrap();
|
||||||
println!("New client!");
|
println!("New client!");
|
||||||
ws.write_message(Message::Text("HOSTJOIN:".to_string())).unwrap();
|
ws.write_message(Message::Text("HOSTJOIN:".to_string())).unwrap();
|
||||||
loop {
|
loop {
|
||||||
let message = ws.read_message();
|
let message = ws.read_message();
|
||||||
println!("{:?}", message);
|
println!("{:?}", message);
|
||||||
if message.is_err() {
|
match message {
|
||||||
break
|
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 {
|
struct Player {
|
||||||
name: String,
|
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
14
test/test.py
Normal 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")
|
Loading…
Reference in New Issue
Block a user