Skip to content

Commit

Permalink
add: basic client authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed Apr 28, 2024
1 parent 1dd992e commit 69549da
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/bin/transistor-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::io::{Error, ErrorKind::*};

use transistor::*;

const PORT: u16 = 2426;

fn main() -> Result<(), Error> {
/* parse server adddress from command line arguments */
let args: Vec<String> = env::args().collect();
Expand All @@ -24,7 +22,7 @@ fn main() -> Result<(), Error> {

let mut client = Client::new(server)?;

client.start();
client.start()?;

loop { }

Expand Down
6 changes: 1 addition & 5 deletions src/bin/transistor-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::io::Error;

use transistor::*;

const PORT: u16 = 2426;

fn main() -> Result<(), Error> {
println!("[INF] transistor server startup!");

Expand All @@ -16,9 +14,7 @@ fn main() -> Result<(), Error> {

println!("{:?}", server);

loop {

}
loop { }

Ok(())
}
21 changes: 18 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{mem, u32};
use std::{fs, net::TcpStream};
use std::io::{Error, Read, Write};
use std::io::{Error, ErrorKind::*, Read, Write};

use serde::{Deserialize, Serialize};

use crate::{config_dir, tcp_stream_write};
use crate::{config_dir, tcp_stream_read, tcp_stream_write};

pub type Cid = u32;

Expand All @@ -27,8 +28,22 @@ impl Client {
})
}

pub fn start(&mut self) {
pub fn start(&mut self) -> Result<(), Error>{
/* send cid to server */
tcp_stream_write!(self.tcp, self.cid);

/* get display counts; 0 is unauthorized */
let mut buffer = [0u8; mem::size_of::<u32>()];
tcp_stream_read!(self.tcp, buffer);
let disp_cnt: u32 = bincode::deserialize(&buffer).unwrap();

println!("cnt: {}", disp_cnt);

if disp_cnt < 1 {
return Err(Error::new(NotConnected, "[ERR] authorization failed"));
}

Ok(())
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pub use display::*;
pub use client::*;
pub use server::*;

pub const PORT: u16 = 2426;
pub const MAX_DISPLAYS: u32 = 128;
7 changes: 5 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::thread;
use display_info::DisplayInfo;
use mouce::Mouse;

use crate::tcp_stream_write;
use crate::{client::*, tcp_stream_read};
use crate::display::*;

Expand Down Expand Up @@ -66,11 +67,13 @@ fn handle_client(port: u16, clients: Arc<Mutex<HashMap<Cid, Client>>>, config: P
/* read cid from remote client */
let mut buffer = [0u8; mem::size_of::<Cid>()];
tcp_stream_read!(stream, buffer);
let cid = Cid::from_be_bytes(buffer);
let cid = bincode::deserialize(&buffer).unwrap();

println!("incoming cid: {}", cid);

/* reject not known client */
if !authorized.contains(&cid) {

tcp_stream_write!(stream, 0);
}

let client = Client {
Expand Down

0 comments on commit 69549da

Please sign in to comment.