Skip to content

Commit

Permalink
Merge pull request #3 from freedelity/version
Browse files Browse the repository at this point in the history
check that protocol version match between client and server
  • Loading branch information
ndusart authored Feb 6, 2024
2 parents c474802 + d541818 commit eb4b1c2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- New flag `--hash` which allow to specify the hash algorithm used for computing blocks checksums.

- Server and client check that they are using the same version of the procotol before going on.

# 1.0.0 (2024-02-01)

Initial release
8 changes: 7 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::hash_file::hash_file;
use crate::sync::Loan;
use crate::HashAlgorithm;
use crate::{check_status, write_string, ResumableReadString, ResumableWriteFileBlock};
use crate::{
check_status, write_string, ResumableReadString, ResumableWriteFileBlock, PROTOCOL_VERSION,
};
use anyhow::anyhow;
use futures::future::OptionFuture;
use std::collections::VecDeque;
Expand Down Expand Up @@ -31,6 +33,10 @@ pub async fn new_process(options: ClientProcessOptions) -> Result<(), anyhow::Er
stream.write_all(options.secret.as_bytes()).await?;
check_status(&mut stream).await?;

// send protocol version
stream.write_u8(PROTOCOL_VERSION).await?;
check_status(&mut stream).await?;

// send dest path
write_string(
&mut stream,
Expand Down
4 changes: 4 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use anyhow::{anyhow, bail};
use int_enum::IntEnum;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

pub const PROTOCOL_VERSION: u8 = 1;

#[repr(u8)]
#[derive(Debug, IntEnum)]
pub enum StatusCode {
Expand All @@ -12,6 +14,7 @@ pub enum StatusCode {
PermissionDenied = 3,
ClientAlreadyConnected = 4,
UnknownHashAlgorithm = 5,
UnsupportedProtocolVersion = 6,
}

pub async fn write_string<T: AsyncWriteExt + std::marker::Unpin, S: Into<String>>(
Expand Down Expand Up @@ -56,6 +59,7 @@ pub async fn check_status<T: AsyncReadExt + std::marker::Unpin>(
StatusCode::UnknownHashAlgorithm => {
Err(anyhow!("Hash algorithm is not supported on remote end"))
}
StatusCode::UnsupportedProtocolVersion => Err(anyhow!("Protocol version mismatch")),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod utils;
use common::{
check_status, read_string, write_status, write_string, ResumableAsyncWriteAll,
ResumableReadFileBlock, ResumableReadString, ResumableWriteFileBlock, ResumableWriteString,
StatusCode,
StatusCode, PROTOCOL_VERSION,
};

use remote_start::{remote_start_server, RemoteStartOptions};
Expand Down
8 changes: 7 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::hash_file::hash_file;
use crate::HashAlgorithm;
use crate::{
read_string, write_status, ResumableAsyncWriteAll, ResumableReadFileBlock,
ResumableWriteString, StatusCode,
ResumableWriteString, StatusCode, PROTOCOL_VERSION,
};
use futures::future::OptionFuture;
use std::fs::File;
Expand All @@ -25,7 +25,13 @@ pub async fn process_new_client(
if client_secret != secret.as_bytes() {
return Ok(StatusCode::InvalidSecret);
}
write_status(client, StatusCode::Ack).await?;

// check client protocol version
let client_protocol_version = client.read_u8().await?;
if client_protocol_version != PROTOCOL_VERSION {
return Ok(StatusCode::UnsupportedProtocolVersion);
}
write_status(client, StatusCode::Ack).await?;

// get dest path
Expand Down

0 comments on commit eb4b1c2

Please sign in to comment.