mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-07-12 18:24:55 +03:00
Compare commits
4 Commits
eb8c2d091b
...
config-wat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83ec01a640 | ||
|
|
aef05f386f | ||
|
|
38920917cd | ||
|
|
1075a90c5b |
1791
Cargo.lock
generated
1791
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -67,6 +67,7 @@ rustls = { version = "0.23.12", default-features = false, features = [
|
|||||||
] }
|
] }
|
||||||
rcgen = "0.13.1"
|
rcgen = "0.13.1"
|
||||||
sha2 = "0.10.8"
|
sha2 = "0.10.8"
|
||||||
|
notify = "8.2.0"
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
libc = "0.2.148"
|
libc = "0.2.148"
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ futures-core = "0.3.30"
|
|||||||
log = "0.4.22"
|
log = "0.4.22"
|
||||||
input-event = { path = "../input-event", version = "0.3.0" }
|
input-event = { path = "../input-event", version = "0.3.0" }
|
||||||
memmap = "0.7"
|
memmap = "0.7"
|
||||||
tempfile = "3.8"
|
tempfile = "3.25.0"
|
||||||
thiserror = "2.0.0"
|
thiserror = "2.0.0"
|
||||||
tokio = { version = "1.32.0", features = [
|
tokio = { version = "1.32.0", features = [
|
||||||
"io-util",
|
"io-util",
|
||||||
@@ -41,7 +41,8 @@ wayland-protocols-wlr = { version = "0.3.1", features = [
|
|||||||
"client",
|
"client",
|
||||||
], optional = true }
|
], optional = true }
|
||||||
x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true }
|
x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true }
|
||||||
ashpd = { version = "0.11.0", default-features = false, features = [
|
ashpd = { version = "0.13.9", default-features = false, features = [
|
||||||
|
"input_capture",
|
||||||
"tokio",
|
"tokio",
|
||||||
], optional = true }
|
], optional = true }
|
||||||
reis = { version = "0.5.0", features = ["tokio"], optional = true }
|
reis = { version = "0.5.0", features = ["tokio"], optional = true }
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ use ashpd::{
|
|||||||
desktop::{
|
desktop::{
|
||||||
Session,
|
Session,
|
||||||
input_capture::{
|
input_capture::{
|
||||||
Activated, ActivatedBarrier, Barrier, BarrierID, Capabilities, InputCapture, Region,
|
Activated, ActivatedBarrier, Barrier, BarrierID, Capabilities, CreateSessionOptions,
|
||||||
Zones,
|
InputCapture, Region, ReleaseOptions, Zones,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
enumflags2::BitFlags,
|
enumflags2::BitFlags,
|
||||||
@@ -58,8 +58,8 @@ enum LibeiNotifyEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct LibeiInputCapture<'a> {
|
pub struct LibeiInputCapture {
|
||||||
input_capture: Pin<Box<InputCapture<'a>>>,
|
input_capture: Pin<Box<InputCapture>>,
|
||||||
capture_task: JoinHandle<Result<(), CaptureError>>,
|
capture_task: JoinHandle<Result<(), CaptureError>>,
|
||||||
event_rx: Receiver<(Position, CaptureEvent)>,
|
event_rx: Receiver<(Position, CaptureEvent)>,
|
||||||
notify_capture: Sender<LibeiNotifyEvent>,
|
notify_capture: Sender<LibeiNotifyEvent>,
|
||||||
@@ -130,12 +130,15 @@ fn select_barriers(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn update_barriers(
|
async fn update_barriers(
|
||||||
input_capture: &InputCapture<'_>,
|
input_capture: &InputCapture,
|
||||||
session: &Session<'_, InputCapture<'_>>,
|
session: &Session<InputCapture>,
|
||||||
active_clients: &[Position],
|
active_clients: &[Position],
|
||||||
next_barrier_id: &mut NonZeroU32,
|
next_barrier_id: &mut NonZeroU32,
|
||||||
) -> Result<(Vec<ICBarrier>, HashMap<BarrierID, Position>), ashpd::Error> {
|
) -> Result<(Vec<ICBarrier>, HashMap<BarrierID, Position>), ashpd::Error> {
|
||||||
let zones = input_capture.zones(session).await?.response()?;
|
let zones = input_capture
|
||||||
|
.zones(session, Default::default())
|
||||||
|
.await?
|
||||||
|
.response()?;
|
||||||
log::debug!("zones: {zones:?}");
|
log::debug!("zones: {zones:?}");
|
||||||
|
|
||||||
let (barriers, id_map) = select_barriers(&zones, active_clients, next_barrier_id);
|
let (barriers, id_map) = select_barriers(&zones, active_clients, next_barrier_id);
|
||||||
@@ -144,31 +147,38 @@ async fn update_barriers(
|
|||||||
|
|
||||||
let ashpd_barriers: Vec<Barrier> = barriers.iter().copied().map(|b| b.into()).collect();
|
let ashpd_barriers: Vec<Barrier> = barriers.iter().copied().map(|b| b.into()).collect();
|
||||||
let response = input_capture
|
let response = input_capture
|
||||||
.set_pointer_barriers(session, &ashpd_barriers, zones.zone_set())
|
.set_pointer_barriers(
|
||||||
|
session,
|
||||||
|
&ashpd_barriers,
|
||||||
|
zones.zone_set(),
|
||||||
|
Default::default(),
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
let response = response.response()?;
|
let response = response.response()?;
|
||||||
log::debug!("{response:?}");
|
log::debug!("{response:?}");
|
||||||
Ok((barriers, id_map))
|
Ok((barriers, id_map))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_session<'a>(
|
async fn create_session(
|
||||||
input_capture: &'a InputCapture<'a>,
|
input_capture: &InputCapture,
|
||||||
) -> std::result::Result<(Session<'a, InputCapture<'a>>, BitFlags<Capabilities>), ashpd::Error> {
|
) -> std::result::Result<(Session<InputCapture>, BitFlags<Capabilities>), ashpd::Error> {
|
||||||
log::debug!("creating input capture session");
|
log::debug!("creating input capture session");
|
||||||
|
let create_session_options = CreateSessionOptions::default().set_capabilities(
|
||||||
|
Capabilities::Keyboard | Capabilities::Pointer | Capabilities::Touchscreen,
|
||||||
|
);
|
||||||
input_capture
|
input_capture
|
||||||
.create_session(
|
.create_session(None, create_session_options)
|
||||||
None,
|
|
||||||
Capabilities::Keyboard | Capabilities::Pointer | Capabilities::Touchscreen,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn connect_to_eis(
|
async fn connect_to_eis(
|
||||||
input_capture: &InputCapture<'_>,
|
input_capture: &InputCapture,
|
||||||
session: &Session<'_, InputCapture<'_>>,
|
session: &Session<InputCapture>,
|
||||||
) -> Result<(ei::Context, Connection, EiConvertEventStream), CaptureError> {
|
) -> Result<(ei::Context, Connection, EiConvertEventStream), CaptureError> {
|
||||||
log::debug!("connect_to_eis");
|
log::debug!("connect_to_eis");
|
||||||
let fd = input_capture.connect_to_eis(session).await?;
|
let fd = input_capture
|
||||||
|
.connect_to_eis(session, Default::default())
|
||||||
|
.await?;
|
||||||
|
|
||||||
// create unix stream from fd
|
// create unix stream from fd
|
||||||
let stream = UnixStream::from(fd);
|
let stream = UnixStream::from(fd);
|
||||||
@@ -201,10 +211,10 @@ async fn libei_event_handler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LibeiInputCapture<'_> {
|
impl LibeiInputCapture {
|
||||||
pub async fn new() -> std::result::Result<Self, LibeiCaptureCreationError> {
|
pub async fn new() -> std::result::Result<Self, LibeiCaptureCreationError> {
|
||||||
let input_capture = Box::pin(InputCapture::new().await?);
|
let input_capture = Box::pin(InputCapture::new().await?);
|
||||||
let input_capture_ptr = input_capture.as_ref().get_ref() as *const InputCapture<'static>;
|
let input_capture_ptr = input_capture.as_ref().get_ref() as *const InputCapture;
|
||||||
let first_session = Some(create_session(unsafe { &*input_capture_ptr }).await?);
|
let first_session = Some(create_session(unsafe { &*input_capture_ptr }).await?);
|
||||||
|
|
||||||
let (event_tx, event_rx) = mpsc::channel(1);
|
let (event_tx, event_rx) = mpsc::channel(1);
|
||||||
@@ -238,10 +248,10 @@ impl LibeiInputCapture<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn do_capture(
|
async fn do_capture(
|
||||||
input_capture: *const InputCapture<'static>,
|
input_capture: *const InputCapture,
|
||||||
mut capture_event: Receiver<LibeiNotifyEvent>,
|
mut capture_event: Receiver<LibeiNotifyEvent>,
|
||||||
notify_release: Arc<Notify>,
|
notify_release: Arc<Notify>,
|
||||||
session: Option<(Session<'_, InputCapture<'_>>, BitFlags<Capabilities>)>,
|
session: Option<(Session<InputCapture>, BitFlags<Capabilities>)>,
|
||||||
event_tx: Sender<(Position, CaptureEvent)>,
|
event_tx: Sender<(Position, CaptureEvent)>,
|
||||||
cancellation_token: CancellationToken,
|
cancellation_token: CancellationToken,
|
||||||
) -> Result<(), CaptureError> {
|
) -> Result<(), CaptureError> {
|
||||||
@@ -307,7 +317,7 @@ async fn do_capture(
|
|||||||
|
|
||||||
// disable capture
|
// disable capture
|
||||||
log::debug!("disabling input capture");
|
log::debug!("disabling input capture");
|
||||||
if let Err(e) = input_capture.disable(&session).await {
|
if let Err(e) = input_capture.disable(&session, Default::default()).await {
|
||||||
log::warn!("input_capture.disable(&session) {e}");
|
log::warn!("input_capture.disable(&session) {e}");
|
||||||
}
|
}
|
||||||
if let Err(e) = session.close().await {
|
if let Err(e) = session.close().await {
|
||||||
@@ -336,8 +346,8 @@ async fn do_capture(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn do_capture_session(
|
async fn do_capture_session(
|
||||||
input_capture: &InputCapture<'_>,
|
input_capture: &InputCapture,
|
||||||
session: &mut Session<'_, InputCapture<'_>>,
|
session: &mut Session<InputCapture>,
|
||||||
event_tx: &Sender<(Position, CaptureEvent)>,
|
event_tx: &Sender<(Position, CaptureEvent)>,
|
||||||
active_clients: &[Position],
|
active_clients: &[Position],
|
||||||
next_barrier_id: &mut NonZeroU32,
|
next_barrier_id: &mut NonZeroU32,
|
||||||
@@ -356,7 +366,7 @@ async fn do_capture_session(
|
|||||||
update_barriers(input_capture, session, active_clients, next_barrier_id).await?;
|
update_barriers(input_capture, session, active_clients, next_barrier_id).await?;
|
||||||
|
|
||||||
log::debug!("enabling session");
|
log::debug!("enabling session");
|
||||||
input_capture.enable(session).await?;
|
input_capture.enable(session, Default::default()).await?;
|
||||||
|
|
||||||
// cancellation token to release session
|
// cancellation token to release session
|
||||||
let release_session = Arc::new(Notify::new());
|
let release_session = Arc::new(Notify::new());
|
||||||
@@ -462,9 +472,9 @@ async fn do_capture_session(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn release_capture<'a>(
|
async fn release_capture(
|
||||||
input_capture: &InputCapture<'a>,
|
input_capture: &InputCapture,
|
||||||
session: &Session<'a, InputCapture<'a>>,
|
session: &Session<InputCapture>,
|
||||||
activated: Activated,
|
activated: Activated,
|
||||||
current_pos: Position,
|
current_pos: Position,
|
||||||
) -> Result<(), CaptureError> {
|
) -> Result<(), CaptureError> {
|
||||||
@@ -484,9 +494,10 @@ async fn release_capture<'a>(
|
|||||||
};
|
};
|
||||||
// release 1px to the right of the entered zone
|
// release 1px to the right of the entered zone
|
||||||
let cursor_position = (x as f64 + dx, y as f64 + dy);
|
let cursor_position = (x as f64 + dx, y as f64 + dy);
|
||||||
input_capture
|
let release_options = ReleaseOptions::default()
|
||||||
.release(session, activated.activation_id(), Some(cursor_position))
|
.set_activation_id(activated.activation_id())
|
||||||
.await?;
|
.set_cursor_position(Some(cursor_position));
|
||||||
|
input_capture.release(session, release_options).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -561,7 +572,7 @@ async fn handle_ei_event(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl LanMouseInputCapture for LibeiInputCapture<'_> {
|
impl LanMouseInputCapture for LibeiInputCapture {
|
||||||
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> {
|
async fn create(&mut self, pos: Position) -> Result<(), CaptureError> {
|
||||||
let _ = self
|
let _ = self
|
||||||
.notify_capture
|
.notify_capture
|
||||||
@@ -598,7 +609,7 @@ impl LanMouseInputCapture for LibeiInputCapture<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for LibeiInputCapture<'_> {
|
impl Drop for LibeiInputCapture {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if !self.terminated {
|
if !self.terminated {
|
||||||
/* this workaround is needed until async drop is stabilized */
|
/* this workaround is needed until async drop is stabilized */
|
||||||
@@ -607,10 +618,10 @@ impl Drop for LibeiInputCapture<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stream for LibeiInputCapture<'_> {
|
impl Stream for LibeiInputCapture {
|
||||||
type Item = Result<(Position, CaptureEvent), CaptureError>;
|
type Item = Result<(Position, CaptureEvent), CaptureError>;
|
||||||
|
|
||||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
||||||
match self.capture_task.poll_unpin(cx) {
|
match self.capture_task.poll_unpin(cx) {
|
||||||
Poll::Ready(r) => match r.expect("failed to join") {
|
Poll::Ready(r) => match r.expect("failed to join") {
|
||||||
Ok(()) => Poll::Ready(None),
|
Ok(()) => Poll::Ready(None),
|
||||||
|
|||||||
@@ -40,7 +40,9 @@ wayland-protocols-misc = { version = "0.3.1", features = [
|
|||||||
"client",
|
"client",
|
||||||
], optional = true }
|
], optional = true }
|
||||||
x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true }
|
x11 = { version = "2.21.0", features = ["xlib", "xtest"], optional = true }
|
||||||
ashpd = { version = "0.11.0", default-features = false, features = [
|
ashpd = { version = "0.13.9", default-features = false, features = [
|
||||||
|
"remote_desktop",
|
||||||
|
"screencast",
|
||||||
"tokio",
|
"tokio",
|
||||||
], optional = true }
|
], optional = true }
|
||||||
reis = { version = "0.5.0", features = ["tokio"], optional = true }
|
reis = { version = "0.5.0", features = ["tokio"], optional = true }
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ use tokio::task::JoinHandle;
|
|||||||
|
|
||||||
use ashpd::desktop::{
|
use ashpd::desktop::{
|
||||||
PersistMode, Session,
|
PersistMode, Session,
|
||||||
remote_desktop::{DeviceType, RemoteDesktop},
|
remote_desktop::{DeviceType, RemoteDesktop, SelectDevicesOptions},
|
||||||
};
|
};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
@@ -40,15 +40,15 @@ struct Devices {
|
|||||||
keyboard: Arc<RwLock<Option<(ei::Device, ei::Keyboard)>>>,
|
keyboard: Arc<RwLock<Option<(ei::Device, ei::Keyboard)>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct LibeiEmulation<'a> {
|
pub(crate) struct LibeiEmulation {
|
||||||
context: ei::Context,
|
context: ei::Context,
|
||||||
conn: event::Connection,
|
conn: event::Connection,
|
||||||
devices: Devices,
|
devices: Devices,
|
||||||
ei_task: JoinHandle<()>,
|
ei_task: JoinHandle<()>,
|
||||||
error: Arc<Mutex<Option<EmulationError>>>,
|
error: Arc<Mutex<Option<EmulationError>>>,
|
||||||
libei_error: Arc<AtomicBool>,
|
libei_error: Arc<AtomicBool>,
|
||||||
_remote_desktop: RemoteDesktop<'a>,
|
_remote_desktop: RemoteDesktop,
|
||||||
session: Session<'a, RemoteDesktop<'a>>,
|
session: Session<RemoteDesktop>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the path to the RemoteDesktop token file
|
/// Get the path to the RemoteDesktop token file
|
||||||
@@ -84,27 +84,26 @@ fn write_token(token: &str) -> io::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_ei_fd<'a>()
|
async fn get_ei_fd() -> Result<(RemoteDesktop, Session<RemoteDesktop>, OwnedFd), ashpd::Error> {
|
||||||
-> Result<(RemoteDesktop<'a>, Session<'a, RemoteDesktop<'a>>, OwnedFd), ashpd::Error> {
|
|
||||||
let remote_desktop = RemoteDesktop::new().await?;
|
let remote_desktop = RemoteDesktop::new().await?;
|
||||||
|
|
||||||
let restore_token = read_token();
|
let restore_token = read_token();
|
||||||
|
|
||||||
log::debug!("creating session ...");
|
log::debug!("creating session ...");
|
||||||
let session = remote_desktop.create_session().await?;
|
let session = remote_desktop.create_session(Default::default()).await?;
|
||||||
|
|
||||||
log::debug!("selecting devices ...");
|
log::debug!("selecting devices ...");
|
||||||
remote_desktop
|
let options = SelectDevicesOptions::default()
|
||||||
.select_devices(
|
.set_devices(DeviceType::Keyboard | DeviceType::Pointer)
|
||||||
&session,
|
.set_persist_mode(PersistMode::ExplicitlyRevoked)
|
||||||
DeviceType::Keyboard | DeviceType::Pointer,
|
.set_restore_token(restore_token.as_deref());
|
||||||
restore_token.as_deref(),
|
remote_desktop.select_devices(&session, options).await?;
|
||||||
PersistMode::ExplicitlyRevoked,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
log::info!("requesting permission for input emulation");
|
log::info!("requesting permission for input emulation");
|
||||||
let start_response = remote_desktop.start(&session, None).await?.response()?;
|
let start_response = remote_desktop
|
||||||
|
.start(&session, None, Default::default())
|
||||||
|
.await?
|
||||||
|
.response()?;
|
||||||
|
|
||||||
// The restore token is only valid once, we need to re-save it each time
|
// The restore token is only valid once, we need to re-save it each time
|
||||||
if let Some(token_str) = start_response.restore_token() {
|
if let Some(token_str) = start_response.restore_token() {
|
||||||
@@ -113,11 +112,13 @@ async fn get_ei_fd<'a>()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let fd = remote_desktop.connect_to_eis(&session).await?;
|
let fd = remote_desktop
|
||||||
|
.connect_to_eis(&session, Default::default())
|
||||||
|
.await?;
|
||||||
Ok((remote_desktop, session, fd))
|
Ok((remote_desktop, session, fd))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LibeiEmulation<'_> {
|
impl LibeiEmulation {
|
||||||
pub(crate) async fn new() -> Result<Self, LibeiEmulationCreationError> {
|
pub(crate) async fn new() -> Result<Self, LibeiEmulationCreationError> {
|
||||||
let (_remote_desktop, session, eifd) = get_ei_fd().await?;
|
let (_remote_desktop, session, eifd) = get_ei_fd().await?;
|
||||||
let stream = UnixStream::from(eifd);
|
let stream = UnixStream::from(eifd);
|
||||||
@@ -152,14 +153,14 @@ impl LibeiEmulation<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for LibeiEmulation<'_> {
|
impl Drop for LibeiEmulation {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.ei_task.abort();
|
self.ei_task.abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Emulation for LibeiEmulation<'_> {
|
impl Emulation for LibeiEmulation {
|
||||||
async fn consume(
|
async fn consume(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: Event,
|
event: Event,
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
use ashpd::{
|
use ashpd::{
|
||||||
desktop::{
|
desktop::{
|
||||||
PersistMode, Session,
|
PersistMode, Session,
|
||||||
remote_desktop::{Axis, DeviceType, KeyState, RemoteDesktop},
|
remote_desktop::{
|
||||||
|
Axis, DeviceType, KeyState, NotifyPointerAxisOptions, RemoteDesktop,
|
||||||
|
SelectDevicesOptions,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
zbus::AsyncDrop,
|
zbus::AsyncDrop,
|
||||||
};
|
};
|
||||||
@@ -17,32 +20,31 @@ use crate::error::EmulationError;
|
|||||||
|
|
||||||
use super::{Emulation, EmulationHandle, error::XdpEmulationCreationError};
|
use super::{Emulation, EmulationHandle, error::XdpEmulationCreationError};
|
||||||
|
|
||||||
pub(crate) struct DesktopPortalEmulation<'a> {
|
pub(crate) struct DesktopPortalEmulation {
|
||||||
proxy: RemoteDesktop<'a>,
|
proxy: RemoteDesktop,
|
||||||
session: Session<'a, RemoteDesktop<'a>>,
|
session: Session<RemoteDesktop>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> DesktopPortalEmulation<'a> {
|
impl DesktopPortalEmulation {
|
||||||
pub(crate) async fn new() -> Result<DesktopPortalEmulation<'a>, XdpEmulationCreationError> {
|
pub(crate) async fn new() -> Result<DesktopPortalEmulation, XdpEmulationCreationError> {
|
||||||
log::debug!("connecting to org.freedesktop.portal.RemoteDesktop portal ...");
|
log::debug!("connecting to org.freedesktop.portal.RemoteDesktop portal ...");
|
||||||
let proxy = RemoteDesktop::new().await?;
|
let proxy = RemoteDesktop::new().await?;
|
||||||
|
|
||||||
// retry when user presses the cancel button
|
// retry when user presses the cancel button
|
||||||
log::debug!("creating session ...");
|
log::debug!("creating session ...");
|
||||||
let session = proxy.create_session().await?;
|
let session = proxy.create_session(Default::default()).await?;
|
||||||
|
|
||||||
log::debug!("selecting devices ...");
|
log::debug!("selecting devices ...");
|
||||||
proxy
|
let options = SelectDevicesOptions::default()
|
||||||
.select_devices(
|
.set_devices(DeviceType::Keyboard | DeviceType::Pointer)
|
||||||
&session,
|
.set_persist_mode(PersistMode::ExplicitlyRevoked);
|
||||||
DeviceType::Keyboard | DeviceType::Pointer,
|
proxy.select_devices(&session, options).await?;
|
||||||
None,
|
|
||||||
PersistMode::ExplicitlyRevoked,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
log::info!("requesting permission for input emulation");
|
log::info!("requesting permission for input emulation");
|
||||||
let _devices = proxy.start(&session, None).await?.response()?;
|
let _devices = proxy
|
||||||
|
.start(&session, None, Default::default())
|
||||||
|
.await?
|
||||||
|
.response()?;
|
||||||
|
|
||||||
log::debug!("started session");
|
log::debug!("started session");
|
||||||
let session = session;
|
let session = session;
|
||||||
@@ -52,7 +54,7 @@ impl<'a> DesktopPortalEmulation<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Emulation for DesktopPortalEmulation<'_> {
|
impl Emulation for DesktopPortalEmulation {
|
||||||
async fn consume(
|
async fn consume(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: input_event::Event,
|
event: input_event::Event,
|
||||||
@@ -62,7 +64,7 @@ impl Emulation for DesktopPortalEmulation<'_> {
|
|||||||
Pointer(p) => match p {
|
Pointer(p) => match p {
|
||||||
PointerEvent::Motion { time: _, dx, dy } => {
|
PointerEvent::Motion { time: _, dx, dy } => {
|
||||||
self.proxy
|
self.proxy
|
||||||
.notify_pointer_motion(&self.session, dx, dy)
|
.notify_pointer_motion(&self.session, dx, dy, Default::default())
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
PointerEvent::Button {
|
PointerEvent::Button {
|
||||||
@@ -75,7 +77,12 @@ impl Emulation for DesktopPortalEmulation<'_> {
|
|||||||
_ => KeyState::Pressed,
|
_ => KeyState::Pressed,
|
||||||
};
|
};
|
||||||
self.proxy
|
self.proxy
|
||||||
.notify_pointer_button(&self.session, button as i32, state)
|
.notify_pointer_button(
|
||||||
|
&self.session,
|
||||||
|
button as i32,
|
||||||
|
state,
|
||||||
|
Default::default(),
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
PointerEvent::AxisDiscrete120 { axis, value } => {
|
PointerEvent::AxisDiscrete120 { axis, value } => {
|
||||||
@@ -84,7 +91,12 @@ impl Emulation for DesktopPortalEmulation<'_> {
|
|||||||
_ => Axis::Horizontal,
|
_ => Axis::Horizontal,
|
||||||
};
|
};
|
||||||
self.proxy
|
self.proxy
|
||||||
.notify_pointer_axis_discrete(&self.session, axis, value / 120)
|
.notify_pointer_axis_discrete(
|
||||||
|
&self.session,
|
||||||
|
axis,
|
||||||
|
value / 120,
|
||||||
|
Default::default(),
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
PointerEvent::Axis {
|
PointerEvent::Axis {
|
||||||
@@ -101,7 +113,12 @@ impl Emulation for DesktopPortalEmulation<'_> {
|
|||||||
Axis::Horizontal => (value, 0.),
|
Axis::Horizontal => (value, 0.),
|
||||||
};
|
};
|
||||||
self.proxy
|
self.proxy
|
||||||
.notify_pointer_axis(&self.session, dx, dy, true)
|
.notify_pointer_axis(
|
||||||
|
&self.session,
|
||||||
|
dx,
|
||||||
|
dy,
|
||||||
|
NotifyPointerAxisOptions::default().set_finish(true),
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -117,7 +134,12 @@ impl Emulation for DesktopPortalEmulation<'_> {
|
|||||||
_ => KeyState::Pressed,
|
_ => KeyState::Pressed,
|
||||||
};
|
};
|
||||||
self.proxy
|
self.proxy
|
||||||
.notify_keyboard_keycode(&self.session, key as i32, state)
|
.notify_keyboard_keycode(
|
||||||
|
&self.session,
|
||||||
|
key as i32,
|
||||||
|
state,
|
||||||
|
Default::default(),
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
KeyboardEvent::Modifiers { .. } => {
|
KeyboardEvent::Modifiers { .. } => {
|
||||||
@@ -141,7 +163,7 @@ impl Emulation for DesktopPortalEmulation<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsyncDrop for DesktopPortalEmulation<'_> {
|
impl AsyncDrop for DesktopPortalEmulation {
|
||||||
#[doc = r" Perform the async cleanup."]
|
#[doc = r" Perform the async cleanup."]
|
||||||
#[allow(clippy::type_complexity, clippy::type_repetition_in_bounds)]
|
#[allow(clippy::type_complexity, clippy::type_repetition_in_bounds)]
|
||||||
fn async_drop<'async_trait>(
|
fn async_drop<'async_trait>(
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ pub(crate) enum CaptureType {
|
|||||||
EnterOnly,
|
EnterOnly,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
enum CaptureRequest {
|
enum CaptureRequest {
|
||||||
/// capture must release the mouse
|
/// capture must release the mouse
|
||||||
Release,
|
Release,
|
||||||
@@ -59,6 +59,8 @@ enum CaptureRequest {
|
|||||||
Destroy(CaptureHandle),
|
Destroy(CaptureHandle),
|
||||||
/// reenable input capture
|
/// reenable input capture
|
||||||
Reenable,
|
Reenable,
|
||||||
|
/// set release bind
|
||||||
|
SetReleaseBind(Vec<scancode::Linux>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Capture {
|
impl Capture {
|
||||||
@@ -131,6 +133,10 @@ impl Capture {
|
|||||||
pub(crate) async fn event(&mut self) -> ICaptureEvent {
|
pub(crate) async fn event(&mut self) -> ICaptureEvent {
|
||||||
self.event_rx.recv().await.expect("channel closed")
|
self.event_rx.recv().await.expect("channel closed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn set_release_bind(&mut self, bind: Vec<scancode::Linux>) {
|
||||||
|
let _ = self.request_tx.send(CaptureRequest::SetReleaseBind(bind));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// debounce a statement `$st`, i.e. the statement is executed only if the
|
/// debounce a statement `$st`, i.e. the statement is executed only if the
|
||||||
@@ -205,6 +211,9 @@ impl CaptureTask {
|
|||||||
CaptureRequest::Create(h, p, t) => self.add_capture(h, p, t),
|
CaptureRequest::Create(h, p, t) => self.add_capture(h, p, t),
|
||||||
CaptureRequest::Destroy(h) => self.remove_capture(h),
|
CaptureRequest::Destroy(h) => self.remove_capture(h),
|
||||||
CaptureRequest::Release => { /* nothing to do */ }
|
CaptureRequest::Release => { /* nothing to do */ }
|
||||||
|
CaptureRequest::SetReleaseBind(bind) => {
|
||||||
|
self.release_bind.borrow_mut().clone_from(&bind);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_ = self.cancellation_token.cancelled() => return,
|
_ = self.cancellation_token.cancelled() => return,
|
||||||
}
|
}
|
||||||
@@ -295,6 +304,9 @@ impl CaptureTask {
|
|||||||
self.remove_capture(h);
|
self.remove_capture(h);
|
||||||
capture.destroy(h).await?;
|
capture.destroy(h).await?;
|
||||||
}
|
}
|
||||||
|
CaptureRequest::SetReleaseBind(bind) => {
|
||||||
|
self.release_bind.borrow_mut().clone_from(&bind);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_ = self.cancellation_token.cancelled() => break,
|
_ = self.cancellation_token.cancelled() => break,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ use slab::Slab;
|
|||||||
|
|
||||||
use lan_mouse_ipc::{ClientConfig, ClientHandle, ClientState, Position};
|
use lan_mouse_ipc::{ClientConfig, ClientHandle, ClientState, Position};
|
||||||
|
|
||||||
|
use crate::config::ConfigClient;
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
pub struct ClientManager {
|
pub struct ClientManager {
|
||||||
clients: Rc<RefCell<Slab<(ClientConfig, ClientState)>>>,
|
clients: Rc<RefCell<Slab<(ClientConfig, ClientState)>>>,
|
||||||
@@ -24,6 +26,25 @@ impl ClientManager {
|
|||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_with_config(&self, config_client: ConfigClient) -> ClientHandle {
|
||||||
|
let config = ClientConfig {
|
||||||
|
hostname: config_client.hostname,
|
||||||
|
fix_ips: config_client.ips.into_iter().collect(),
|
||||||
|
port: config_client.port,
|
||||||
|
pos: config_client.pos,
|
||||||
|
cmd: config_client.enter_hook,
|
||||||
|
};
|
||||||
|
let state = ClientState {
|
||||||
|
active: config_client.active,
|
||||||
|
ips: HashSet::from_iter(config.fix_ips.iter().cloned()),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let handle = self.add_client();
|
||||||
|
self.set_config(handle, config);
|
||||||
|
self.set_state(handle, state);
|
||||||
|
handle
|
||||||
|
}
|
||||||
|
|
||||||
/// add a new client to this manager
|
/// add a new client to this manager
|
||||||
pub fn add_client(&self) -> ClientHandle {
|
pub fn add_client(&self) -> ClientHandle {
|
||||||
self.clients.borrow_mut().insert(Default::default()) as ClientHandle
|
self.clients.borrow_mut().insert(Default::default()) as ClientHandle
|
||||||
@@ -230,6 +251,15 @@ impl ClientManager {
|
|||||||
.and_then(|(c, _)| c.cmd.clone())
|
.and_then(|(c, _)| c.cmd.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns all clients that are currently registered
|
||||||
|
pub(crate) fn registered_clients(&self) -> Vec<ClientHandle> {
|
||||||
|
self.clients
|
||||||
|
.borrow()
|
||||||
|
.iter()
|
||||||
|
.map(|(h, _)| h as ClientHandle)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
/// returns all clients that are currently active
|
/// returns all clients that are currently active
|
||||||
pub(crate) fn active_clients(&self) -> Vec<ClientHandle> {
|
pub(crate) fn active_clients(&self) -> Vec<ClientHandle> {
|
||||||
self.clients
|
self.clients
|
||||||
|
|||||||
109
src/config.rs
109
src/config.rs
@@ -1,6 +1,7 @@
|
|||||||
use crate::capture_test::TestCaptureArgs;
|
use crate::capture_test::TestCaptureArgs;
|
||||||
use crate::emulation_test::TestEmulationArgs;
|
use crate::emulation_test::TestEmulationArgs;
|
||||||
use clap::{Parser, Subcommand, ValueEnum};
|
use clap::{Parser, Subcommand, ValueEnum};
|
||||||
|
use notify::{EventKind, RecommendedWatcher, Watcher};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::env::{self, VarError};
|
use std::env::{self, VarError};
|
||||||
@@ -46,7 +47,7 @@ fn default_path() -> Result<PathBuf, VarError> {
|
|||||||
Ok(PathBuf::from(default_path))
|
Ok(PathBuf::from(default_path))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
|
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
|
||||||
struct ConfigToml {
|
struct ConfigToml {
|
||||||
capture_backend: Option<CaptureBackend>,
|
capture_backend: Option<CaptureBackend>,
|
||||||
emulation_backend: Option<EmulationBackend>,
|
emulation_backend: Option<EmulationBackend>,
|
||||||
@@ -244,8 +245,14 @@ pub struct Config {
|
|||||||
cert_path: PathBuf,
|
cert_path: PathBuf,
|
||||||
/// path to the config file used
|
/// path to the config file used
|
||||||
config_path: PathBuf,
|
config_path: PathBuf,
|
||||||
|
/// path to config directory (parent of above)
|
||||||
|
config_dir: PathBuf,
|
||||||
/// the (optional) toml config and it's path
|
/// the (optional) toml config and it's path
|
||||||
config_toml: Option<ConfigToml>,
|
config_toml: Option<ConfigToml>,
|
||||||
|
// filesystem watcher
|
||||||
|
watcher: notify::RecommendedWatcher,
|
||||||
|
// channel for filesystem events
|
||||||
|
watch_rx: tokio::sync::mpsc::Receiver<Result<notify::Event, notify::Error>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ConfigClient {
|
pub struct ConfigClient {
|
||||||
@@ -311,6 +318,8 @@ pub enum ConfigError {
|
|||||||
Io(#[from] io::Error),
|
Io(#[from] io::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Var(#[from] VarError),
|
Var(#[from] VarError),
|
||||||
|
#[error(transparent)]
|
||||||
|
Watcher(#[from] notify::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_RELEASE_KEYS: [scancode::Linux; 4] =
|
const DEFAULT_RELEASE_KEYS: [scancode::Linux; 4] =
|
||||||
@@ -342,12 +351,55 @@ impl Config {
|
|||||||
.or(config_toml.as_ref().and_then(|c| c.cert_path.clone()))
|
.or(config_toml.as_ref().and_then(|c| c.cert_path.clone()))
|
||||||
.unwrap_or(default_path()?.join(CERT_FILE_NAME));
|
.unwrap_or(default_path()?.join(CERT_FILE_NAME));
|
||||||
|
|
||||||
Ok(Config {
|
let (tx, watch_rx) = tokio::sync::mpsc::channel(16);
|
||||||
|
let watcher = RecommendedWatcher::new(
|
||||||
|
move |res| {
|
||||||
|
let _ = tx.blocking_send(res);
|
||||||
|
},
|
||||||
|
notify::Config::default(),
|
||||||
|
)?;
|
||||||
|
let config_dir = config_path
|
||||||
|
.parent()
|
||||||
|
.expect("config directory")
|
||||||
|
.to_path_buf();
|
||||||
|
let mut config = Config {
|
||||||
args,
|
args,
|
||||||
cert_path,
|
cert_path,
|
||||||
config_path,
|
config_path,
|
||||||
|
config_dir,
|
||||||
config_toml,
|
config_toml,
|
||||||
})
|
watcher,
|
||||||
|
watch_rx,
|
||||||
|
};
|
||||||
|
config.watch()?;
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn watch(&mut self) -> Result<(), notify::Error> {
|
||||||
|
self.watcher
|
||||||
|
.watch(&self.config_dir, notify::RecursiveMode::NonRecursive)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unwatch(&mut self) -> Result<(), notify::Error> {
|
||||||
|
self.watcher.unwatch(&self.config_dir)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn changed(&mut self) -> Result<(), notify::Error> {
|
||||||
|
loop {
|
||||||
|
let event = self.watch_rx.recv().await.expect("channel closed");
|
||||||
|
let event = event.expect("filesystem event");
|
||||||
|
if event.paths.contains(&self.config_path)
|
||||||
|
&& matches!(
|
||||||
|
event.kind,
|
||||||
|
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_)
|
||||||
|
)
|
||||||
|
&& self.read_from_disk()?
|
||||||
|
{
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// the command to run
|
/// the command to run
|
||||||
@@ -428,11 +480,8 @@ impl Config {
|
|||||||
|
|
||||||
/// set authorized keys
|
/// set authorized keys
|
||||||
pub fn set_authorized_keys(&mut self, fingerprints: HashMap<String, String>) {
|
pub fn set_authorized_keys(&mut self, fingerprints: HashMap<String, String>) {
|
||||||
if fingerprints.is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if self.config_toml.is_none() {
|
if self.config_toml.is_none() {
|
||||||
self.config_toml = Default::default();
|
self.config_toml = Some(Default::default());
|
||||||
}
|
}
|
||||||
self.config_toml
|
self.config_toml
|
||||||
.as_mut()
|
.as_mut()
|
||||||
@@ -440,38 +489,58 @@ impl Config {
|
|||||||
.authorized_fingerprints = Some(fingerprints);
|
.authorized_fingerprints = Some(fingerprints);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_back(&self) -> Result<(), io::Error> {
|
pub fn read_from_disk(&mut self) -> Result<bool, io::Error> {
|
||||||
log::info!("writing config to {:?}", &self.config_path);
|
log::info!("reading config from {:?}", &self.config_path);
|
||||||
/* load the current configuration file */
|
|
||||||
let current_config = match fs::read_to_string(&self.config_path) {
|
let current_config = fs::read_to_string(&self.config_path)?;
|
||||||
Ok(c) => c.parse::<DocumentMut>().unwrap_or_default(),
|
let current_config = match current_config.parse::<DocumentMut>() {
|
||||||
|
Ok(c) => c,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::info!("{:?} {e} => creating new config", self.config_path());
|
log::warn!("{:?} {e}", self.config_path());
|
||||||
Default::default()
|
return Ok(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let _current_config =
|
let mut changed = false;
|
||||||
toml_edit::de::from_document::<ConfigToml>(current_config).unwrap_or_default();
|
match toml_edit::de::from_document::<ConfigToml>(current_config) {
|
||||||
|
Ok(current_config) => {
|
||||||
|
changed = self
|
||||||
|
.config_toml
|
||||||
|
.as_ref()
|
||||||
|
.is_none_or(|c| c != ¤t_config);
|
||||||
|
self.config_toml.replace(current_config);
|
||||||
|
}
|
||||||
|
Err(e) => log::warn!("{:?} {e}", self.config_path()),
|
||||||
|
};
|
||||||
|
Ok(changed)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_back(&mut self) -> Result<(), io::Error> {
|
||||||
|
log::info!("writing config to {:?}", &self.config_path);
|
||||||
/* the new config */
|
/* the new config */
|
||||||
let new_config = self.config_toml.clone().unwrap_or_default();
|
let new_config = self.config_toml.clone().unwrap_or_default();
|
||||||
// let new_config = toml_edit::ser::to_document::<ConfigToml>(&new_config).expect("fixme");
|
|
||||||
let new_config = toml_edit::ser::to_string_pretty(&new_config).expect("config");
|
let new_config = toml_edit::ser::to_string_pretty(&new_config).expect("config");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO merge documents => eventually we might want to split this up into clients configured
|
* TODO merge with current config file to preserve comments
|
||||||
|
* => eventually we might want to split this up into clients configured
|
||||||
* via the config file and clients managed through the GUI / frontend.
|
* via the config file and clients managed through the GUI / frontend.
|
||||||
* The latter should be saved to $XDG_DATA_HOME instead of $XDG_CONFIG_HOME,
|
* The latter should be saved to $XDG_DATA_HOME instead of $XDG_CONFIG_HOME,
|
||||||
* and clients configured through .config could be made permanent.
|
* and clients configured through .config could be made permanent.
|
||||||
* For now we just override the config file.
|
* For now we just override the config file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
let _ = self.unwatch();
|
||||||
/* write new config to file */
|
/* write new config to file */
|
||||||
if let Some(p) = self.config_path().parent() {
|
if let Some(p) = self.config_path().parent() {
|
||||||
fs::create_dir_all(p)?;
|
fs::create_dir_all(p)?;
|
||||||
}
|
}
|
||||||
let mut f = File::create(self.config_path())?;
|
{
|
||||||
f.write_all(new_config.as_bytes())?;
|
let mut f = File::create(self.config_path())?;
|
||||||
|
f.write_all(new_config.as_bytes())?;
|
||||||
|
f.sync_all()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = self.watch();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ use crate::{
|
|||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use hickory_resolver::ResolveError;
|
use hickory_resolver::ResolveError;
|
||||||
use lan_mouse_ipc::{
|
use lan_mouse_ipc::{
|
||||||
AsyncFrontendListener, ClientConfig, ClientHandle, ClientState, FrontendEvent, FrontendRequest,
|
AsyncFrontendListener, ClientHandle, FrontendEvent, FrontendRequest, IpcError,
|
||||||
IpcError, IpcListenerCreationError, Position, Status,
|
IpcListenerCreationError, Position, Status,
|
||||||
};
|
};
|
||||||
use log;
|
use log;
|
||||||
use std::{
|
use std::{
|
||||||
@@ -83,21 +83,7 @@ impl Service {
|
|||||||
pub async fn new(config: Config) -> Result<Self, ServiceError> {
|
pub async fn new(config: Config) -> Result<Self, ServiceError> {
|
||||||
let client_manager = ClientManager::default();
|
let client_manager = ClientManager::default();
|
||||||
for client in config.clients() {
|
for client in config.clients() {
|
||||||
let config = ClientConfig {
|
client_manager.add_with_config(client);
|
||||||
hostname: client.hostname,
|
|
||||||
fix_ips: client.ips.into_iter().collect(),
|
|
||||||
port: client.port,
|
|
||||||
pos: client.pos,
|
|
||||||
cmd: client.enter_hook,
|
|
||||||
};
|
|
||||||
let state = ClientState {
|
|
||||||
active: client.active,
|
|
||||||
ips: HashSet::from_iter(config.fix_ips.iter().cloned()),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let handle = client_manager.add_client();
|
|
||||||
client_manager.set_config(handle, config);
|
|
||||||
client_manager.set_state(handle, state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// load certificate
|
// load certificate
|
||||||
@@ -164,6 +150,7 @@ impl Service {
|
|||||||
event = self.emulation.event() => self.handle_emulation_event(event),
|
event = self.emulation.event() => self.handle_emulation_event(event),
|
||||||
event = self.capture.event() => self.handle_capture_event(event),
|
event = self.capture.event() => self.handle_capture_event(event),
|
||||||
event = self.resolver.event() => self.handle_resolver_event(event),
|
event = self.resolver.event() => self.handle_resolver_event(event),
|
||||||
|
_ = self.config.changed() => self.handle_config_change(),
|
||||||
r = signal::ctrl_c() => break r.expect("failed to wait for CTRL+C"),
|
r = signal::ctrl_c() => break r.expect("failed to wait for CTRL+C"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -255,6 +242,30 @@ impl Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_config_change(&mut self) {
|
||||||
|
for h in self.client_manager.registered_clients() {
|
||||||
|
self.remove_client(h);
|
||||||
|
}
|
||||||
|
for c in self.config.clients() {
|
||||||
|
let handle = self.client_manager.add_with_config(c);
|
||||||
|
log::info!("added client {handle}");
|
||||||
|
let (c, s) = self.client_manager.get_state(handle).unwrap();
|
||||||
|
if s.active {
|
||||||
|
self.client_manager.deactivate_client(handle);
|
||||||
|
self.activate_client(handle);
|
||||||
|
}
|
||||||
|
self.notify_frontend(FrontendEvent::Created(handle, c, s));
|
||||||
|
}
|
||||||
|
let release_bind = self.config.release_bind();
|
||||||
|
self.capture.set_release_bind(release_bind);
|
||||||
|
let authorized_keys = self.config.authorized_fingerprints();
|
||||||
|
self.authorized_keys
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.clone_from(&authorized_keys);
|
||||||
|
self.sync_frontend();
|
||||||
|
}
|
||||||
|
|
||||||
async fn handle_frontend_pending(&mut self) {
|
async fn handle_frontend_pending(&mut self) {
|
||||||
while let Some(event) = self.pending_frontend_events.pop_front() {
|
while let Some(event) = self.pending_frontend_events.pop_front() {
|
||||||
self.frontend_listener.broadcast(event).await;
|
self.frontend_listener.broadcast(event).await;
|
||||||
@@ -477,7 +488,7 @@ impl Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn activate_client(&mut self, handle: ClientHandle) {
|
fn activate_client(&mut self, handle: ClientHandle) {
|
||||||
log::debug!("activating client");
|
log::debug!("activating client {handle}");
|
||||||
|
|
||||||
/* resolve dns on activate */
|
/* resolve dns on activate */
|
||||||
self.resolve(handle);
|
self.resolve(handle);
|
||||||
|
|||||||
Reference in New Issue
Block a user