Require initial handshake message before accepting connections
This commit is contained in:
parent
6d63ee291c
commit
59e0c934e2
1 changed files with 37 additions and 2 deletions
39
src/main.rs
39
src/main.rs
|
@ -64,7 +64,38 @@ fn create_socket() -> PathBuf {
|
||||||
fn handle_client(stream: UnixStream) {
|
fn handle_client(stream: UnixStream) {
|
||||||
let reader = BufReader::new(stream);
|
let reader = BufReader::new(stream);
|
||||||
|
|
||||||
for line in reader.lines() {
|
let start_time = SystemTime::now();
|
||||||
|
|
||||||
|
const TIMEOUT: Duration = Duration::from_secs(3);
|
||||||
|
let handshake_msg = String::from("handshake");
|
||||||
|
|
||||||
|
let mut lines = reader.lines();
|
||||||
|
|
||||||
|
match lines.next() {
|
||||||
|
Some(Ok(msg)) if msg == handshake_msg => {
|
||||||
|
if SystemTime::now().duration_since(start_time).unwrap() >= TIMEOUT {
|
||||||
|
println!("Client took too long to send first message");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(Ok(msg)) => {
|
||||||
|
println!("First line isn't handshake: {}", msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Some(Err(e)) => {
|
||||||
|
println!("Failed to get first line: {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
println!("Client terminated before first message");
|
||||||
|
// If the stream ends here, abort
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Got correct handshake");
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
dbg!(line.unwrap());
|
dbg!(line.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +105,11 @@ fn handle_client(stream: UnixStream) {
|
||||||
fn client(path: String) {
|
fn client(path: String) {
|
||||||
let mut stream = UnixStream::connect(path).unwrap();
|
let mut stream = UnixStream::connect(path).unwrap();
|
||||||
|
|
||||||
stream.write_all(b"Hello World").unwrap();
|
writeln!(stream, "handshake").unwrap();
|
||||||
|
|
||||||
|
thread::sleep(Duration::from_secs(1));
|
||||||
|
|
||||||
|
writeln!(stream, "Hello world").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn monitor_child(socket_path: PathBuf) {
|
fn monitor_child(socket_path: PathBuf) {
|
||||||
|
|
Loading…
Reference in a new issue