summaryrefslogtreecommitdiffstats
path: root/src/frontend.rs
blob: 05f83f2e2d483f1524ba049438a0490da2ce3c92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use anyhow::{anyhow, Result};
use std::{cmp::min, io::ErrorKind, net::IpAddr, str, time::Duration};

#[cfg(unix)]
use std::{
    env,
    path::{Path, PathBuf},
};

use tokio::io::ReadHalf;
use tokio::io::{AsyncReadExt, AsyncWriteExt, WriteHalf};

#[cfg(unix)]
use tokio::net::UnixListener;
#[cfg(unix)]
use tokio::net::UnixStream;

#[cfg(windows)]
use tokio::net::TcpListener;
#[cfg(windows)]
use tokio::net::TcpStream;

use serde::{Deserialize, Serialize};

use crate::{
    client::{ClientConfig, ClientHandle, ClientState, Position},
    config::{Config, Frontend},
};

/// cli frontend
pub mod cli;

/// gtk frontend
#[cfg(feature = "gtk")]
pub mod gtk;

pub fn run_frontend(config: &Config) -> Result<()> {
    match config.frontend {
        #[cfg(feature = "gtk")]
        Frontend::Gtk => {
            gtk::run();
        }
        #[cfg(not(feature = "gtk"))]
        Frontend::Gtk => panic!("gtk frontend requested but feature not enabled!"),
        Frontend::Cli => {
            cli::run()?;
        }
    };
    Ok(())
}

fn exponential_back_off(duration: &mut Duration) -> &Duration {
    let new = duration.saturating_mul(2);
    *duration = min(new, Duration::from_secs(1));
    duration
}

/// wait for the lan-mouse socket to come online
#[cfg(unix)]
pub fn wait_for_service() -> Result<std::os::unix::net::UnixStream> {
    let socket_path = FrontendListener::socket_path()?;
    let mut duration = Duration::from_millis(1);
    loop {
        use std::os::unix::net::UnixStream;
        if let Ok(stream) = UnixStream::connect(&socket_path) {
            break Ok(stream);
        }
        // a signaling mechanism or inotify could be used to
        // improve this
        std::thread::sleep(*exponential_back_off(&mut duration));
    }
}

#[cfg(windows)]
pub fn wait_for_service() -> Result<std::net::TcpStream> {
    let mut duration = Duration::from_millis(1);
    loop {
        use std::net::TcpStream;
        if let Ok(stream) = TcpStream::connect("127.0.0.1:5252") {
            break Ok(stream);
        }
        std::thread::sleep(*exponential_back_off(&mut duration));
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub enum FrontendRequest {
    /// activate/deactivate client
    Activate(ClientHandle, bool),
    /// add a new client
    Create,
    /// change the listen port (recreate udp listener)
    ChangePort(u16),
    /// remove a client
    Delete(ClientHandle),
    /// request an enumeration of all clients
    Enumerate(),
    /// resolve dns
    ResolveDns(ClientHandle),
    /// update hostname
    UpdateHostname(ClientHandle, Option<String>),
    /// update port
    UpdatePort(ClientHandle, u16),
    /// update position
    UpdatePosition(ClientHandle, Position),
    /// update fix-ips
    UpdateFixIps(ClientHandle, Vec<IpAddr>),
    /// request the state of the given client
    GetState(ClientHandle),
    /// request reenabling input capture
    EnableCapture,
    /// request reenabling input emulation
    EnableEmulation,
}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
pub enum Status {
    #[default]
    Disabled,
    Enabled,
}

impl From<Status> for bool {
    fn from(status: Status) -> Self {
        match status {
            Status::Enabled => true,
            Status::Disabled => false,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FrontendEvent {
    /// a client was created
    Created(ClientHandle, ClientConfig, ClientState),
    /// no such client
    NoSuchClient(ClientHandle),
    /// state changed
    State(ClientHandle, ClientConfig, ClientState),
    /// the client was deleted
    Deleted(ClientHandle),
    /// new port, reason of failure (if failed)
    PortChanged(u16, Option<String>),
    /// list of all clients, used for initial state synchronization
    Enumerate(Vec<(ClientHandle, ClientConfig, ClientState)>),
    /// an error occured
    Error(String),
    /// capture status
    CaptureStatus(Status),
    /// emulation status
    EmulationStatus(Status),
}

pub struct FrontendListener {
    #[cfg(windows)]
    listener: TcpListener,
    #[cfg(unix)]
    listener: UnixListener,
    #[cfg(unix)]
    socket_path: PathBuf,
    #[cfg(unix)]
    tx_streams: Vec<WriteHalf<UnixStream>>,
    #[cfg(windows)]
    tx_streams: Vec<WriteHalf<TcpStream>>,
}

impl FrontendListener {
    #[cfg(all(unix, not(target_os = "macos")))]
    pub fn socket_path() -> Result<PathBuf> {
        let xdg_runtime_dir = match env::var("XDG_RUNTIME_DIR") {
            Ok(d) => d,
            Err(e) => return Err(anyhow!("could not find XDG_RUNTIME_DIR: {e}")),
        };
        let xdg_runtime_dir = Path::new(xdg_runtime_dir.as_str());
        Ok(xdg_runtime_dir.join("lan-mouse-socket.sock"))
    }

    #[cfg(all(unix, target_os = "macos"))]
    pub fn socket_path() -> Result<PathBuf> {
        let home = match env::var("HOME") {
            Ok(d) => d,
            Err(e) => return Err(anyhow!("could not find HOME: {e}")),
        };
        let home = Path::new(home.as_str());
        let path = home
            .join("Library")
            .join("Caches")
            .join("lan-mouse-socket.sock");
        Ok(path)
    }

    pub async fn new() -> Option<Result<Self>> {
        #[cfg(unix)]
        let (socket_path, listener) = {
            let socket_path = match Self::socket_path() {
                Ok(path) => path,
                Err(e) => return Some(Err(e)),
            };

            log::debug!("remove socket: {:?}", socket_path);
            if socket_path.exists() {
                // try to connect to see if some other instance
                // of lan-mouse is already running
                match UnixStream::connect(&socket_path).await {
                    // connected -> lan-mouse is already running
                    Ok(_) => return None,
                    // lan-mouse is not running but a socket was left behind
                    Err(e) => {
                        log::debug!("{socket_path:?}: {e} - removing left behind socket");
                        let _ = std::fs::remove_file(&socket_path);
                    }
                }
            }
            let listener = match UnixListener::bind(&socket_path) {
                Ok(ls) => ls,
                // some other lan-mouse instance has bound the socket in the meantime
                Err(e) if e.kind() == ErrorKind::AddrInUse => return None,
                Err(e) => return Some(Err(anyhow!("failed to bind lan-mouse-socket: {e}"))),
            };
            (socket_path, listener)
        };

        #[cfg(windows)]
        let listener = match TcpListener::bind("127.0.0.1:5252").await {
            Ok(ls) => ls,
            // some other lan-mouse instance has bound the socket in the meantime
            Err(e) if e.kind() == ErrorKind::AddrInUse => return None,
            Err(e) => return Some(Err(anyhow!("failed to bind lan-mouse-socket: {e}"))),
        };

        let adapter = Self {
            listener,
            #[cfg(unix)]
            socket_path,
            tx_streams: vec![],
        };

        Some(Ok(adapter))
    }

    #[cfg(unix)]
    pub async fn accept(&mut self) -> Result<ReadHalf<UnixStream>> {
        let stream = self.listener.accept().await?.0;
        let (rx, tx) = tokio::io::split(stream);
        self.tx_streams.push(tx);
        Ok(rx)
    }

    #[cfg(windows)]
    pub async fn accept(&mut self) -> Result<ReadHalf<TcpStream>> {
        let stream = self.listener.accept().await?.0;
        let (rx, tx) = tokio::io::split(stream);
        self.tx_streams.push(tx);
        Ok(rx)
    }

    pub(crate) async fn broadcast(&mut self, notify: FrontendEvent) {
        // encode event
        let json = serde_json::to_string(&notify).unwrap();
        let payload = json.as_bytes();
        let len = payload.len().to_be_bytes();
        log::debug!("broadcasting event to streams: {json}");
        let mut keep = vec![];
        // TODO do simultaneously
        for tx in self.tx_streams.iter_mut() {
            // write len + payload
            if tx.write(&len).await.is_err() {
                keep.push(false);
                continue;
            }
            if tx.write(payload).await.is_err() {
                keep.push(false);
                continue;
            }
            keep.push(true);
        }

        // could not find a better solution because async
        let mut keep = keep.into_iter();
        self.tx_streams.retain(|_| keep.next().unwrap());
    }
}

#[cfg(unix)]
impl Drop for FrontendListener {
    fn drop(&mut self) {
        log::debug!("remove socket: {:?}", self.socket_path);
        let _ = std::fs::remove_file(&self.socket_path);
    }
}

#[cfg(unix)]
pub async fn wait_for_request(stream: &mut ReadHalf<UnixStream>) -> Result<FrontendRequest> {
    let len = stream.read_u64().await?;
    assert!(len <= 256);
    let mut buf = [0u8; 256];
    stream.read_exact(&mut buf[..len as usize]).await?;
    Ok(serde_json::from_slice(&buf[..len as usize])?)
}

#[cfg(windows)]
pub async fn wait_for_request(stream: &mut ReadHalf<TcpStream>) -> Result<FrontendRequest> {
    let len = stream.read_u64().await?;
    let mut buf = [0u8; 256];
    stream.read_exact(&mut buf[..len as usize]).await?;
    Ok(serde_json::from_slice(&buf[..len as usize])?)
}