mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-06 10:11:28 +03:00
Encryption and One-Way-Control (#200)
This is a major rewrite of the core networking logic enabling one-way control and encryption through the webrtc-dtls crate. closes #164 closes #104
This commit is contained in:
committed by
GitHub
parent
0d074e19f1
commit
7677fae14b
@@ -2,7 +2,7 @@ use input_event::{Event as InputEvent, KeyboardEvent, PointerEvent};
|
||||
use num_enum::{IntoPrimitive, TryFromPrimitive, TryFromPrimitiveError};
|
||||
use paste::paste;
|
||||
use std::{
|
||||
fmt::{Debug, Display},
|
||||
fmt::{Debug, Display, Formatter},
|
||||
mem::size_of,
|
||||
};
|
||||
use thiserror::Error;
|
||||
@@ -18,14 +18,39 @@ pub enum ProtocolError {
|
||||
/// event type does not exist
|
||||
#[error("invalid event id: `{0}`")]
|
||||
InvalidEventId(#[from] TryFromPrimitiveError<EventType>),
|
||||
/// position type does not exist
|
||||
#[error("invalid event id: `{0}`")]
|
||||
InvalidPosition(#[from] TryFromPrimitiveError<Position>),
|
||||
}
|
||||
|
||||
/// Position of a client
|
||||
#[derive(Clone, Copy, Debug, TryFromPrimitive, IntoPrimitive)]
|
||||
#[repr(u8)]
|
||||
pub enum Position {
|
||||
Left,
|
||||
Right,
|
||||
Top,
|
||||
Bottom,
|
||||
}
|
||||
|
||||
impl Display for Position {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
let pos = match self {
|
||||
Position::Left => "left",
|
||||
Position::Right => "right",
|
||||
Position::Top => "top",
|
||||
Position::Bottom => "bottom",
|
||||
};
|
||||
write!(f, "{pos}")
|
||||
}
|
||||
}
|
||||
|
||||
/// main lan-mouse protocol event type
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum ProtoEvent {
|
||||
/// notify a client that the cursor entered its region
|
||||
/// notify a client that the cursor entered its region at the given position
|
||||
/// [`ProtoEvent::Ack`] with the same serial is used for synchronization between devices
|
||||
Enter(u32),
|
||||
Enter(Position),
|
||||
/// notify a client that the cursor left its region
|
||||
/// [`ProtoEvent::Ack`] with the same serial is used for synchronization between devices
|
||||
Leave(u32),
|
||||
@@ -36,19 +61,25 @@ pub enum ProtoEvent {
|
||||
/// Ping event for tracking unresponsive clients.
|
||||
/// A client has to respond with [`ProtoEvent::Pong`].
|
||||
Ping,
|
||||
/// Response to [`ProtoEvent::Ping`]
|
||||
Pong,
|
||||
/// Response to [`ProtoEvent::Ping`], true if emulation is enabled / available
|
||||
Pong(bool),
|
||||
}
|
||||
|
||||
impl Display for ProtoEvent {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ProtoEvent::Enter(s) => write!(f, "Enter({s})"),
|
||||
ProtoEvent::Leave(s) => write!(f, "Leave({s})"),
|
||||
ProtoEvent::Ack(s) => write!(f, "Ack({s})"),
|
||||
ProtoEvent::Input(e) => write!(f, "{e}"),
|
||||
ProtoEvent::Ping => write!(f, "ping"),
|
||||
ProtoEvent::Pong => write!(f, "pong"),
|
||||
ProtoEvent::Pong(alive) => {
|
||||
write!(
|
||||
f,
|
||||
"pong: {}",
|
||||
if *alive { "alive" } else { "not available" }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,7 +116,7 @@ impl ProtoEvent {
|
||||
},
|
||||
},
|
||||
ProtoEvent::Ping => EventType::Ping,
|
||||
ProtoEvent::Pong => EventType::Pong,
|
||||
ProtoEvent::Pong(_) => EventType::Pong,
|
||||
ProtoEvent::Enter(_) => EventType::Enter,
|
||||
ProtoEvent::Leave(_) => EventType::Leave,
|
||||
ProtoEvent::Ack(_) => EventType::Ack,
|
||||
@@ -139,8 +170,8 @@ impl TryFrom<[u8; MAX_EVENT_SIZE]> for ProtoEvent {
|
||||
},
|
||||
))),
|
||||
EventType::Ping => Ok(Self::Ping),
|
||||
EventType::Pong => Ok(Self::Pong),
|
||||
EventType::Enter => Ok(Self::Enter(decode_u32(&mut buf)?)),
|
||||
EventType::Pong => Ok(Self::Pong(decode_u8(&mut buf)? != 0)),
|
||||
EventType::Enter => Ok(Self::Enter(decode_u8(&mut buf)?.try_into()?)),
|
||||
EventType::Leave => Ok(Self::Leave(decode_u32(&mut buf)?)),
|
||||
EventType::Ack => Ok(Self::Ack(decode_u32(&mut buf)?)),
|
||||
}
|
||||
@@ -203,8 +234,8 @@ impl From<ProtoEvent> for ([u8; MAX_EVENT_SIZE], usize) {
|
||||
},
|
||||
},
|
||||
ProtoEvent::Ping => {}
|
||||
ProtoEvent::Pong => {}
|
||||
ProtoEvent::Enter(serial) => encode_u32(buf, len, serial),
|
||||
ProtoEvent::Pong(alive) => encode_u8(buf, len, alive as u8),
|
||||
ProtoEvent::Enter(pos) => encode_u8(buf, len, pos as u8),
|
||||
ProtoEvent::Leave(serial) => encode_u32(buf, len, serial),
|
||||
ProtoEvent::Ack(serial) => encode_u32(buf, len, serial),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user