From 1c70efccd9c3ab0de1d7a0da1a29296f7ba7c890 Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Sat, 27 May 2023 22:36:42 +0200 Subject: Use mt_auth --- src/net.rs | 109 +++---------------------------------------------------------- 1 file changed, 5 insertions(+), 104 deletions(-) (limited to 'src') diff --git a/src/net.rs b/src/net.rs index 2598651..512c802 100644 --- a/src/net.rs +++ b/src/net.rs @@ -2,9 +2,6 @@ use crate::{GfxEvent, NetEvent}; use cgmath::{Deg, Point3, Vector3}; use futures::future::OptionFuture; use mt_net::{CltSender, ReceiverExt, SenderExt, ToCltPkt, ToSrvPkt}; -use rand::RngCore; -use sha2::Sha256; -use srp::{client::SrpClient, groups::G_2048}; use std::{future::Future, time::Duration}; use tokio::{ sync::mpsc, @@ -12,18 +9,10 @@ use tokio::{ }; use winit::event_loop::EventLoopProxy; -enum AuthState { - Init(Interval), - Verify(Vec, SrpClient<'static, Sha256>), - Done, -} - struct Conn { tx: CltSender, - auth: AuthState, + auth: mt_auth::Auth, send_pos_iv: Option, - username: String, - password: String, pos: Point3, pitch: Deg, yaw: Deg, @@ -41,24 +30,15 @@ pub(crate) async fn run( let (tx, mut rx, worker) = mt_net::connect("localhost:30000").await.unwrap(); let mut conn = Conn { + auth: mt_auth::Auth::new(tx.clone(), "shrek", "boobies", "en_US"), tx, - auth: AuthState::Init(interval(Duration::from_millis(100))), send_pos_iv: None, - username: "shrek".into(), // shrek is love, shrek is life <3 - password: "boobies".into(), pos: Point3::new(0.0, 0.0, 0.0), pitch: Deg(0.0), yaw: Deg(0.0), events: evt_out, }; - let init_pkt = ToSrvPkt::Init { - serialize_version: 29, - proto_version: 40..=40, - player_name: conn.username.clone(), - send_full_item_meta: false, - }; - let worker_thread = tokio::spawn(worker.run()); loop { @@ -68,12 +48,7 @@ pub(crate) async fn run( Some(Err(e)) => eprintln!("{e}"), Some(Ok(v)) => conn.handle_pkt(v).await, }, - Some(_) = maybe_tick(match &mut conn.auth { - AuthState::Init(iv) => Some(iv), - _ => None, - }) => { - conn.tx.send(&init_pkt).await.unwrap(); - } + _ = conn.auth.poll() => {} Some(_) = maybe_tick(conn.send_pos_iv.as_mut()) => { conn.tx .send(&ToSrvPkt::PlayerPos(mt_net::PlayerPos { @@ -124,76 +99,9 @@ impl Conn { async fn handle_pkt(&mut self, pkt: ToCltPkt) { use ToCltPkt::*; - match pkt { - Hello { - auth_methods, - username: name, - .. - } => { - use mt_net::AuthMethod; - - if !matches!(self.auth, AuthState::Init(_)) { - return; - } - - let srp = SrpClient::::new(&G_2048); - - let mut rand_bytes = vec![0; 32]; - rand::thread_rng().fill_bytes(&mut rand_bytes); - - if self.username != name { - panic!("username changed"); - } - - if auth_methods.contains(AuthMethod::FirstSrp) { - let verifier = srp.compute_verifier( - self.username.to_lowercase().as_bytes(), - self.password.as_bytes(), - &rand_bytes, - ); - - self.tx - .send(&ToSrvPkt::FirstSrp { - salt: rand_bytes, - verifier, - empty_passwd: self.password.is_empty(), - }) - .await - .unwrap(); + self.auth.handle_pkt(&pkt).await; - self.auth = AuthState::Done; - } else if auth_methods.contains(AuthMethod::Srp) { - let a = srp.compute_public_ephemeral(&rand_bytes); - - self.tx - .send(&ToSrvPkt::SrpBytesA { a, no_sha1: true }) - .await - .unwrap(); - - self.auth = AuthState::Verify(rand_bytes, srp); - } else { - panic!("unsupported auth methods: {auth_methods:?}"); - } - } - SrpBytesSaltB { salt, b } => { - if let AuthState::Verify(a, srp) = &self.auth { - let m = srp - .process_reply( - a, - self.username.to_lowercase().as_bytes(), - self.password.as_bytes(), - &salt, - &b, - ) - .unwrap() - .proof() - .into(); - - self.tx.send(&ToSrvPkt::SrpBytesM { m }).await.unwrap(); - - self.auth = AuthState::Done; - } - } + match pkt { NodeDefs(defs) => { self.events.send_event(GfxEvent::NodeDefs(defs.0)).ok(); } @@ -201,13 +109,6 @@ impl Conn { println!("kicked: {reason}"); } AcceptAuth { player_pos, .. } => { - self.tx - .send(&ToSrvPkt::Init2 { - lang: "en_US".into(), // localization is unironically overrated - }) - .await - .unwrap(); - self.pos = player_pos; self.send_pos_iv = Some(interval(Duration::from_millis(100))); } -- cgit v1.2.3