This commit is contained in:
Daniella / Tove 2023-02-05 07:49:18 +01:00
parent 58bb1d58c6
commit 3181706ef3
3 changed files with 8 additions and 9 deletions

View file

@ -1,10 +1,11 @@
use std::{ use std::{
collections::HashMap,
io::Read, io::Read,
io::Write, io::Write,
net::{Shutdown, TcpStream}, net::{Shutdown, TcpStream},
thread, thread,
time::{Duration, SystemTime}, time::{Duration, SystemTime},
vec, collections::HashMap, vec,
}; };
use crate::{io_sync, PacketType, SocketAdapter}; use crate::{io_sync, PacketType, SocketAdapter};
@ -17,15 +18,14 @@ pub fn client(ip: &str, port: u16, dest_ip: &str, dest_port: u16, key: &str, sle
let mut buf = [0; 1024]; let mut buf = [0; 1024];
let mut tcp = TcpStream::connect((ip, port)).unwrap(); let mut tcp = TcpStream::connect((ip, port)).unwrap();
println!("Syncing..."); println!("Syncing...");
tcp.write_all(&['R' as u8, 'P' as u8, 'F' as u8, 30]) tcp.write_all(&[b'R', b'P', b'F', 30]).unwrap();
.unwrap();
println!("Authenticating..."); println!("Authenticating...");
tcp.write_all(&(key.len() as u32).to_be_bytes()).unwrap(); tcp.write_all(&(key.len() as u32).to_be_bytes()).unwrap();
tcp.write_all(key.as_bytes()).unwrap(); tcp.write_all(key.as_bytes()).unwrap();
println!("Syncing..."); println!("Syncing...");
tcp.read_exact(&mut buf4).unwrap(); tcp.read_exact(&mut buf4).unwrap();
if buf4 != ['R' as u8, 'P' as u8, 'F' as u8, 30] { if buf4 != [b'R', b'P', b'F', 30] {
panic!("RPF30 header expected, but not found. Make sure the server is actually running revpfw3!"); panic!("RPF30 header expected, but not found. Make sure the server is actually running revpfw3!");
} }
tcp.write_all(&[PacketType::KeepAlive.ordinal() as u8]) tcp.write_all(&[PacketType::KeepAlive.ordinal() as u8])

View file

@ -23,7 +23,7 @@ pub fn server(port: u16, key: &str, sleep_delay_ms: u64) {
tcp.0.shutdown(Shutdown::Both).unwrap(); tcp.0.shutdown(Shutdown::Both).unwrap();
continue; continue;
}; };
if buf4 == ['R' as u8, 'P' as u8, 'F' as u8, 30] { if buf4 == [b'R', b'P', b'F', 30] {
println!("Compatible client connected."); println!("Compatible client connected.");
if tcp.0.read_exact(&mut buf4).is_ok() && u32::from_be_bytes(buf4) == key.len() as u32 { if tcp.0.read_exact(&mut buf4).is_ok() && u32::from_be_bytes(buf4) == key.len() as u32 {
println!("Key length matches."); println!("Key length matches.");
@ -38,8 +38,7 @@ pub fn server(port: u16, key: &str, sleep_delay_ms: u64) {
} }
}; };
tcp.write_all(&mut ['R' as u8, 'P' as u8, 'F' as u8, 30]) tcp.write_all(&[b'R', b'P', b'F', 30]).unwrap();
.unwrap();
tcpl.set_nonblocking(true).unwrap(); tcpl.set_nonblocking(true).unwrap();

View file

@ -2,7 +2,7 @@ use std::{
io::{Error, Read}, io::{Error, Read},
io::{ErrorKind, Write}, io::{ErrorKind, Write},
net::TcpStream, net::TcpStream,
time::{SystemTime, Duration}, time::SystemTime,
}; };
use crate::io_sync; use crate::io_sync;
@ -133,7 +133,7 @@ impl SocketAdapter {
} }
pub fn punish(&mut self, time: u128) { pub fn punish(&mut self, time: u128) {
if self.ignore_until == None { if self.ignore_until.is_none() {
self.ignore_until = Some(SystemTime::UNIX_EPOCH.elapsed().unwrap().as_micros()); self.ignore_until = Some(SystemTime::UNIX_EPOCH.elapsed().unwrap().as_micros());
} }
self.ignore_until = self.ignore_until.map(|x| x + time); self.ignore_until = self.ignore_until.map(|x| x + time);