wlproxy 0.2.2

Filter/modify Wayland messages from clients
Documentation
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use {
    clap::Parser,
    rustix::{
        fd::{AsFd, FromRawFd, OwnedFd, RawFd},
        fs::{flock, OpenOptionsExt},
    },
    sd_notify::NotifyState,
    std::{
        collections::{HashMap, HashSet},
        fmt::Display,
        fs::{remove_file, File},
        io::Cursor,
        os::unix::net::{UnixListener, UnixStream},
        path::PathBuf,
        sync::{Arc, Mutex, OnceLock},
        thread::spawn,
    },
};

use uds::UnixStreamExt;
use wlproxy::proto::{self, read_arg_string};
use wlproxy::ObjType;

fn default_upstream() -> PathBuf {
    let runtime_dir =
        std::env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR must be set for Wayland");
    let socket_name = std::env::var("WAYLAND_DISPLAY").unwrap_or_else(|_| "wayland-0".to_string());
    PathBuf::from(runtime_dir).join(socket_name)
}

#[derive(Parser, Clone)]
#[command(name = "wlproxy")]
struct Args {
    /// Full path to compositor Wayland socket.
    #[arg(short = 'u', long = "upstream")]
    upstream: Option<PathBuf>,
    /// Force all xdg toplevels to have the same app id
    #[arg(short = 'a', long = "app-id")]
    app_id: Option<String>,
    /// Prefix the app id instead of replacing
    #[arg(short = 'A', long = "prefix-app-id")]
    prefix_app_id: bool,
    /// Force all xdg toplevels to have the same title
    #[arg(short = 't', long = "title")]
    title: Option<String>,
    /// Prefix the title instead of replacing
    #[arg(short = 'T', long = "prefix-title")]
    prefix_title: bool,
    /// Wayland interfaces to block (can be specified multiple times)
    #[arg(short = 'b', long = "block", value_delimiter = ',')]
    block: Vec<String>,
    /// Suppress warnings about unknown interface names
    #[arg(short = 'q', long = "quiet")]
    quiet: bool,
    /// Print debug messages
    #[arg(long = "debug")]
    debug: bool,
    /// Full path for the new Wayland socket
    downstream: PathBuf,
}

fn known_protocols() -> &'static [&'static str] {
    static LIST: OnceLock<Vec<&'static str>> = OnceLock::new();
    LIST.get_or_init(|| {
        let mut list: Vec<&'static str> = include_str!("../known_protocols.txt")
            .lines()
            .filter(|l| {
                let t = l.trim();
                !t.is_empty() && !t.starts_with('#')
            })
            .collect();
        list.sort();
        list.dedup();
        list
    })
}

trait Errorize<T> {
    fn context(self, text: &str) -> Result<T, String>;
}

impl<T, E: Display> Errorize<T> for Result<T, E> {
    fn context(self, text: &str) -> Result<T, String> {
        match self {
            Ok(x) => Ok(x),
            Err(e) => Err(format!("{}: {}", text, e)),
        }
    }
}

fn is_blocked_interface(name: &str, block: &[String]) -> bool {
    block.iter().any(|b| b == name)
}

fn validate_interfaces(block: &[String], quiet: bool) {
    if quiet || block.is_empty() {
        return;
    }
    for name in block {
        if !known_protocols().contains(&name.as_str()) {
            eprintln!(
                "Warning: unknown Wayland interface \"{}\" in --block list",
                name
            );
        }
    }
}

fn drop_fds(fds: &mut Vec<RawFd>) {
    for fd in fds.drain(..) {
        drop(unsafe { OwnedFd::from_raw_fd(fd) });
    }
}

struct AncillaryReader<'a> {
    reader: &'a UnixStream,
    fds: &'a mut Vec<RawFd>,
}

impl<'a> std::io::Read for AncillaryReader<'a> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let mut fd_buf = [0i32; 8];
        let (n, nfds) = self.reader.recv_fds(buf, &mut fd_buf)?;
        self.fds.extend(&fd_buf[..nfds]);
        Ok(n)
    }
}

struct AncillaryWriter<'a> {
    writer: &'a UnixStream,
    fds: Vec<RawFd>,
}

impl<'a> AncillaryWriter<'a> {
    fn new(writer: &'a UnixStream, fds: &[RawFd]) -> Self {
        Self {
            writer,
            fds: fds.to_vec(),
        }
    }
}

impl<'a> std::io::Write for AncillaryWriter<'a> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let fds = std::mem::take(&mut self.fds);
        if fds.is_empty() {
            self.writer.write(buf)
        } else {
            self.writer.send_fds(buf, &fds)
        }
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.writer.flush()
    }
}

fn handle_client_to_server(
    downstream: &UnixStream,
    upstream: &UnixStream,
    objects: &Arc<Mutex<HashMap<u32, ObjType>>>,
    xdgwmbase_type_id: &Arc<Mutex<Option<(u32, u32)>>>,
    blocked_objects: &Arc<Mutex<HashSet<u32>>>,
    args: &Args,
) -> Result<(), String> {
    let mut ancillary_accum = vec![];
    while let Some(mut packet) = proto::read_packet(&mut AncillaryReader {
        reader: downstream,
        fds: &mut ancillary_accum,
    })
    .context("Error reading message")?
    {
        let should_block = {
            let mut objects = objects.lock().unwrap();
            let mut blocked = blocked_objects.lock().unwrap();

            if blocked.contains(&packet.id) {
                true
            } else {
                let o = objects.get(&packet.id).cloned();
                if args.debug {
                    eprintln!(
                        "Received packet from downstream for tracked object {:?} with {} ancillary FDs: {:?}",
                        o,
                        ancillary_accum.len(),
                        packet
                    );
                }
                if let Some(o) = o {
                    match o {
                        ObjType::Display => {
                            if packet.opcode == 1 {
                                let obj_id = proto::read_arg_uint(&mut Cursor::new(&packet.body))
                                    .context("Error reading registry id")?;
                                objects.insert(obj_id, ObjType::Registry);
                            }
                            false
                        }
                        ObjType::Registry => {
                            if packet.opcode == 0 {
                                let mut cursor = Cursor::new(&packet.body);
                                let obj_type_id = proto::read_arg_uint(&mut cursor)
                                    .context("Error/eof reading bind object type id")?;
                                let interface_name = proto::read_arg_string(&mut cursor)
                                    .context("Error reading bind message type string")?;
                                let version = proto::read_arg_uint(&mut cursor)
                                    .context("Error reading bind message version")?;
                                let obj_id = proto::read_arg_uint(&mut cursor)
                                    .context("Error/eof reading bind object id")?;

                                if let Some(ref name) = interface_name {
                                    if is_blocked_interface(name, &args.block) {
                                        if args.debug {
                                            eprintln!("Blocked bind for interface: {}", name);
                                        }
                                        blocked.insert(obj_id);
                                        true
                                    } else if let Some((want_type_id, _version)) =
                                        *xdgwmbase_type_id.lock().unwrap()
                                    {
                                        if obj_type_id == want_type_id {
                                            objects.insert(
                                                obj_id,
                                                ObjType::XdgWmBase { ver: version },
                                            );
                                        }
                                        false
                                    } else {
                                        false
                                    }
                                } else {
                                    false
                                }
                            } else {
                                false
                            }
                        }
                        ObjType::XdgWmBase { ver } => match ver {
                            0..=6 => {
                                if packet.opcode == 2 {
                                    let obj_id =
                                        proto::read_arg_uint(&mut Cursor::new(&packet.body))
                                            .context(
                                                "Error reading xdg wm base create surface id",
                                            )?;
                                    objects.insert(obj_id, ObjType::XdgSurface { ver });
                                }
                                false
                            }
                            _ => {
                                return Err(
                                    format!("Unsupported xdg_wm_base object version {ver}",),
                                )
                            }
                        },
                        ObjType::XdgSurface { ver } => match ver {
                            0..=6 => {
                                if packet.opcode == 1 {
                                    let obj_id =
                                        proto::read_arg_uint(&mut Cursor::new(&packet.body))
                                            .context(
                                                "Error reading xdg surface create toplevel id",
                                            )?;
                                    objects.insert(obj_id, ObjType::XdgToplevel { ver });
                                }
                                false
                            }
                            _ => {
                                return Err(
                                    format!("Unsupported xdg_surface object version {ver}",),
                                )
                            }
                        },
                        ObjType::XdgToplevel { ver } => match ver {
                            0..=6 => {
                                match packet.opcode {
                                    2 => {
                                        if let Some(title) = &args.title {
                                            let read_title =
                                                read_arg_string(&mut packet.body.as_slice())
                                                    .context("Error reading app id message body")?;
                                            packet.body.clear();
                                            let new_title = if args.prefix_title {
                                                format!(
                                                    "{}{}",
                                                    title,
                                                    read_title.unwrap_or_default()
                                                )
                                            } else {
                                                title.clone()
                                            };
                                            proto::write_arg_string(&mut packet.body, &new_title)
                                                .unwrap();
                                            if args.debug {
                                                eprintln!(
                                                    "Modified title; new message: {:?}",
                                                    packet
                                                );
                                            }
                                        }
                                    }
                                    3 => {
                                        if let Some(app_id) = &args.app_id {
                                            let read_app_id =
                                                read_arg_string(&mut packet.body.as_slice())
                                                    .context("Error reading app id message body")?;
                                            packet.body.clear();
                                            let new_app_id = if args.prefix_app_id {
                                                format!(
                                                    "{}{}",
                                                    app_id,
                                                    read_app_id.unwrap_or_default()
                                                )
                                            } else {
                                                app_id.clone()
                                            };
                                            proto::write_arg_string(&mut packet.body, &new_app_id)
                                                .unwrap();
                                            if args.debug {
                                                eprintln!(
                                                    "Modified app id; new message: {:?}",
                                                    packet
                                                );
                                            }
                                        }
                                    }
                                    _ => (),
                                };
                                false
                            }
                            _ => {
                                return Err(format!(
                                    "Unsupported xdg_toplevel object version {ver}",
                                ))
                            }
                        },
                    }
                } else {
                    false
                }
            }
        };

        if should_block {
            drop_fds(&mut ancillary_accum);
            continue;
        }

        proto::write_packet(
            &mut AncillaryWriter::new(upstream, &ancillary_accum),
            &packet,
        )
        .context("Error writing message")?;
        drop_fds(&mut ancillary_accum);
    }
    Ok(())
}

fn handle_server_to_client(
    upstream: &UnixStream,
    downstream: &UnixStream,
    objects: &Arc<Mutex<HashMap<u32, ObjType>>>,
    xdgwmbase_type_id: &Arc<Mutex<Option<(u32, u32)>>>,
    args: &Args,
) -> Result<(), String> {
    let mut ancillary_accum = vec![];
    let mut cache_reg_id = None;
    while let Some(packet) = proto::read_packet(&mut AncillaryReader {
        reader: upstream,
        fds: &mut ancillary_accum,
    })
    .context("Error reading message")?
    {
        if args.debug {
            eprintln!(
                "Received packet from upstream with {} ancillary FDs: {:?}",
                ancillary_accum.len(),
                packet
            );
        }

        if (packet.id, packet.opcode) == (1, 1) {
            let mut cursor = Cursor::new(&packet.body);
            let obj_id =
                proto::read_arg_uint(&mut cursor).context("Error reading display delete obj id")?;
            objects.lock().unwrap().remove(&obj_id);
            if cache_reg_id == Some(obj_id) {
                cache_reg_id = None;
            }
        }
        if let Some(reg_id) = match &cache_reg_id {
            Some(r) => Some(*r),
            None => {
                if let Some(ObjType::Registry) = objects.lock().unwrap().get(&packet.id) {
                    cache_reg_id = Some(packet.id);
                    Some(packet.id)
                } else {
                    None
                }
            }
        } {
            if reg_id == packet.id && packet.opcode == 0 {
                let mut cursor = Cursor::new(&packet.body);
                let type_id = proto::read_arg_uint(&mut cursor)
                    .context("Error reading global message type id")?;
                let type_str = proto::read_arg_string(&mut cursor)
                    .context("Error reading global message type string")?;
                let version = proto::read_arg_uint(&mut cursor)
                    .context("Error reading global message version")?;
                if type_str.as_deref() == Some("xdg_wm_base") {
                    *xdgwmbase_type_id.lock().unwrap() = Some((type_id, version));
                }

                if let Some(ref name) = type_str {
                    if !args.block.is_empty() && args.block.iter().any(|b| b == name) {
                        if args.debug {
                            eprintln!("Blocked global: {}", name);
                        }
                        drop_fds(&mut ancillary_accum);
                        continue;
                    }
                }
            }
        }

        proto::write_packet(
            &mut AncillaryWriter::new(downstream, &ancillary_accum),
            &packet,
        )
        .context("Error writing message")?;
        drop_fds(&mut ancillary_accum);
    }
    Ok(())
}

fn main() -> Result<(), String> {
    let args = Args::parse();

    validate_interfaces(&args.block, args.quiet);

    let lock_path = args.downstream.with_extension("lock");
    let filelock = File::options()
        .mode(0o660)
        .write(true)
        .create(true)
        .custom_flags(libc::O_CLOEXEC)
        .open(&lock_path)
        .context("Error opening lock file")?;
    flock(
            filelock.as_fd(),
            rustix::fs::FlockOperation::NonBlockingLockExclusive,
        ).context("Error getting exclusive lock for downstream listener, is another compositor already listening?")?;
    let _defer = defer::defer(|| {
        _ = remove_file(&lock_path);
    });
    _ = remove_file(&args.downstream);
    let downstream =
        UnixListener::bind(&args.downstream).context("Error creating downstream listener")?;
    let _defer1 = defer::defer(|| {
        _ = remove_file(&args.downstream);
    });

    // If the system booted with systemd, inform systemd that wlproxy is ready using
    // notify. Other services that depend on wlproxy can start now.
    if let Ok(true) = sd_notify::booted() {
        if args.debug {
            eprintln!("Init detected as being systemd. Notifying of readiness.");
        }
        if let Err(e) = sd_notify::notify(&[NotifyState::Ready]) {
            eprintln!("Warning, failed to notify systemd with error: {}", e);
        }
    }

    // Listen for connections
    loop {
        let (downstream, _) = downstream
            .accept()
            .context("Error accepting downstream connection")?;
        let upstream_path = args.upstream.clone().unwrap_or_else(default_upstream);
        let upstream =
            UnixStream::connect(&upstream_path).context("Error creating upstream connection")?;

        let objects = Arc::new(Mutex::new(HashMap::new()));
        objects.lock().unwrap().insert(1, ObjType::Display);
        let xdgwmbase_type_id = Arc::new(Mutex::new(None));
        let blocked_objects: Arc<Mutex<HashSet<u32>>> = Arc::new(Mutex::new(HashSet::new()));
        spawn({
            let downstream = downstream.try_clone().unwrap();
            let upstream = upstream.try_clone().unwrap();
            let objects = objects.clone();
            let xdgwmbase_type_id = xdgwmbase_type_id.clone();
            let blocked_objects = blocked_objects.clone();
            let args = args.clone();
            move || {
                let _defer = defer::defer({
                    let downstream = downstream.try_clone().unwrap();
                    let upstream = upstream.try_clone().unwrap();
                    move || {
                        _ = downstream.shutdown(std::net::Shutdown::Both);
                        _ = upstream.shutdown(std::net::Shutdown::Both);
                    }
                });
                if let Err(e) = handle_client_to_server(
                    &downstream,
                    &upstream,
                    &objects,
                    &xdgwmbase_type_id,
                    &blocked_objects,
                    &args,
                ) {
                    eprintln!("Warning, client->server thread exiting with error: {}", e);
                }
            }
        });
        spawn({
            let downstream = downstream.try_clone().unwrap();
            let upstream = upstream.try_clone().unwrap();
            let objects = objects.clone();
            let xdgwmbase_type_id = xdgwmbase_type_id.clone();
            let args = args.clone();
            move || {
                let _defer = defer::defer({
                    let downstream = downstream.try_clone().unwrap();
                    let upstream = upstream.try_clone().unwrap();
                    move || {
                        _ = downstream.shutdown(std::net::Shutdown::Both);
                        _ = upstream.shutdown(std::net::Shutdown::Both);
                    }
                });
                if let Err(e) = handle_server_to_client(
                    &upstream,
                    &downstream,
                    &objects,
                    &xdgwmbase_type_id,
                    &args,
                ) {
                    eprintln!("Warning, server->client thread exiting with error: {}", e);
                }
            }
        });
    }
}