summaryrefslogtreecommitdiffhomepage
path: root/test/test-runner/src/main.rs
blob: f7d222296967555b6bbae6086da707eff620a4df (plain)
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
use futures::{FutureExt, SinkExt, StreamExt, pin_mut, select, select_biased};
use logging::LOGGER;
use std::{
    collections::{BTreeMap, HashMap},
    net::{IpAddr, SocketAddr},
    path::PathBuf,
    process::Stdio,
    sync::Arc,
    time::{Duration, SystemTime},
};
use util::OnDrop;

use tarpc::{context, server::Channel};
use test_rpc::{
    AppTrace, Service, SpawnOpts, UNPRIVILEGED_USER, meta::OsVersion, mullvad_daemon::SOCKET_PATH,
    net::SockHandleId, package::Package, transport::GrpcForwarder,
};
use tokio::{
    io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
    process::{ChildStdin, ChildStdout, Command},
    sync::{Mutex, broadcast::error::TryRecvError, oneshot},
    task,
    time::sleep,
};
use tokio_util::codec::{Decoder, LengthDelimitedCodec};

mod app;
mod forward;
mod logging;
mod net;
mod package;
mod sys;
mod util;

#[derive(Clone, Default)]
pub struct TestServer(Arc<Mutex<State>>);

#[derive(Default)]
struct State {
    spawned_procs: HashMap<u32, SpawnedProcess>,
}

struct SpawnedProcess {
    stdout: Option<ChildStdout>,
    stdin: Option<ChildStdin>,

    #[allow(dead_code)]
    abort_handle: OnDrop,
}

#[tarpc::server]
impl Service for TestServer {
    async fn install_app(
        self,
        _: context::Context,
        package: Package,
    ) -> Result<(), test_rpc::Error> {
        log::debug!("Installing app");

        package::install_package(package).await?;

        log::debug!("Install complete");

        Ok(())
    }

    async fn uninstall_app(
        self,
        _: context::Context,
        env: HashMap<String, String>,
    ) -> Result<(), test_rpc::Error> {
        log::debug!("Uninstalling app");

        package::uninstall_app(env).await?;

        log::debug!("Uninstalled app");

        Ok(())
    }

    async fn exec(
        self,
        _: context::Context,
        path: String,
        args: Vec<String>,
        env: BTreeMap<String, String>,
    ) -> Result<test_rpc::ExecResult, test_rpc::Error> {
        log::debug!("Exec {} (args: {args:?})", path);

        let mut cmd = Command::new(&path);
        cmd.stdout(Stdio::piped());
        cmd.stderr(Stdio::piped());
        cmd.stdin(Stdio::piped());
        cmd.args(args);

        #[cfg(target_os = "windows")]
        {
            // Make sure that PATH is updated
            cmd.env("PATH", sys::get_system_path_var()?);
            if let Some(home_dir) = dirs::home_dir() {
                cmd.env("USERPROFILE", home_dir);
            }
        }

        #[cfg(unix)]
        if let Some(home_dir) = dirs::home_dir() {
            cmd.env("HOME", home_dir);
        }

        cmd.envs(env);

        let output = cmd.output().await.map_err(|error| {
            log::error!("Failed to exec {}: {error}", path);
            test_rpc::Error::Syscall
        })?;

        let result = test_rpc::ExecResult {
            code: output.status.code(),
            stdout: output.stdout,
            stderr: output.stderr,
        };

        log::debug!("Finished exec: {:?}", result.code);

        Ok(result)
    }

    async fn mullvad_daemon_get_status(
        self,
        _: context::Context,
    ) -> test_rpc::mullvad_daemon::ServiceStatus {
        sys::get_daemon_status()
    }

    /// Get the installed app version
    async fn mullvad_version(self, _: context::Context) -> Result<String, test_rpc::Error> {
        app::version().await
    }

    async fn find_mullvad_app_traces(
        self,
        _: context::Context,
    ) -> Result<Vec<AppTrace>, test_rpc::Error> {
        app::find_traces()
    }

    async fn get_mullvad_app_cache_dir(
        self,
        _: context::Context,
    ) -> Result<PathBuf, test_rpc::Error> {
        app::find_cache_traces()
    }

    async fn send_tcp(
        self,
        _: context::Context,
        interface: Option<String>,
        bind_addr: SocketAddr,
        destination: SocketAddr,
    ) -> Result<(), test_rpc::Error> {
        net::send_tcp(interface, bind_addr, destination).await
    }

    async fn send_udp(
        self,
        _: context::Context,
        interface: Option<String>,
        bind_addr: SocketAddr,
        destination: SocketAddr,
    ) -> Result<(), test_rpc::Error> {
        net::send_udp(interface, bind_addr, destination).await
    }

    async fn send_ping(
        self,
        _: context::Context,
        destination: IpAddr,
        interface: Option<String>,
        size: usize,
    ) -> Result<(), test_rpc::Error> {
        net::send_ping(destination, interface.as_deref(), size)
            .await
            .map_err(|e| test_rpc::Error::Ping(e.to_string()))
    }

    async fn geoip_lookup(
        self,
        ctx: context::Context,
        mullvad_host: String,
    ) -> Result<test_rpc::AmIMullvad, test_rpc::Error> {
        let timeout = ctx
            .deadline
            .duration_since(SystemTime::now())
            .ok()
            // account for some time to send the RPC response
            .and_then(|d| d.checked_sub(Duration::from_millis(500)))
            .unwrap_or_default();

        test_rpc::net::geoip_lookup(mullvad_host, timeout).await
    }

    async fn resolve_hostname(
        self,
        _: context::Context,
        hostname: String,
    ) -> Result<Vec<SocketAddr>, test_rpc::Error> {
        Ok(tokio::net::lookup_host(&format!("{hostname}:0"))
            .await
            .map_err(|error| {
                log::debug!("resolve_hostname failed: {error}");
                test_rpc::Error::DnsResolution
            })?
            .collect())
    }

    async fn start_tcp_forward(
        self,
        _: context::Context,
        bind_addr: SocketAddr,
        via_addr: SocketAddr,
    ) -> Result<(SockHandleId, SocketAddr), test_rpc::Error> {
        forward::start_server(bind_addr, via_addr).await
    }

    async fn stop_tcp_forward(
        self,
        _: context::Context,
        id: SockHandleId,
    ) -> Result<(), test_rpc::Error> {
        forward::stop_server(id)
    }

    async fn get_interface_ip(
        self,
        _: context::Context,
        interface: String,
    ) -> Result<IpAddr, test_rpc::Error> {
        net::get_interface_ip(&interface)
    }

    async fn get_interface_mtu(
        self,
        _: context::Context,
        interface: String,
    ) -> Result<u16, test_rpc::Error> {
        net::get_interface_mtu(&interface)
    }

    async fn get_interface_mac(
        self,
        _: context::Context,
        interface: String,
    ) -> Result<Option<[u8; 6]>, test_rpc::Error> {
        net::get_interface_mac(&interface)
    }

    async fn get_default_interface(self, _: context::Context) -> Result<String, test_rpc::Error> {
        Ok(net::get_default_interface().to_owned())
    }

    async fn poll_output(
        self,
        _: context::Context,
    ) -> Result<Vec<test_rpc::logging::Output>, test_rpc::Error> {
        let mut listener = LOGGER.0.lock().await;
        if let Ok(output) = listener.recv().await {
            let mut buffer = vec![output];
            while let Ok(output) = listener.try_recv() {
                buffer.push(output);
            }
            Ok(buffer)
        } else {
            Err(test_rpc::Error::Logger(
                test_rpc::logging::Error::StandardOutput,
            ))
        }
    }

    async fn try_poll_output(
        self,
        _: context::Context,
    ) -> Result<Vec<test_rpc::logging::Output>, test_rpc::Error> {
        let mut listener = LOGGER.0.lock().await;
        match listener.try_recv() {
            Ok(output) => {
                let mut buffer = vec![output];
                while let Ok(output) = listener.try_recv() {
                    buffer.push(output);
                }
                Ok(buffer)
            }
            Err(TryRecvError::Empty) => Ok(Vec::new()),
            Err(_) => Err(test_rpc::Error::Logger(
                test_rpc::logging::Error::StandardOutput,
            )),
        }
    }

    async fn get_mullvad_app_logs(self, _: context::Context) -> test_rpc::logging::LogOutput {
        logging::get_mullvad_app_logs().await
    }

    async fn restart_mullvad_daemon(self, _: context::Context) -> Result<(), test_rpc::Error> {
        sys::restart_app().await
    }

    /// Stop the Mullvad VPN application.
    async fn stop_mullvad_daemon(self, _: context::Context) -> Result<(), test_rpc::Error> {
        sys::stop_app().await
    }

    /// Start the Mullvad VPN application.
    async fn start_mullvad_daemon(self, _: context::Context) -> Result<(), test_rpc::Error> {
        sys::start_app().await
    }

    /// Disable the Mullvad VPN system service.
    async fn disable_mullvad_daemon(self, _: context::Context) -> Result<(), test_rpc::Error> {
        #[cfg(not(target_os = "windows"))]
        {
            log::warn!("disable_mullvad_daemon is only implemented on Windows");
            return Err(test_rpc::Error::Syscall);
        }
        #[cfg(target_os = "windows")]
        {
            sys::disable_system_service_startup().await
        }
    }

    async fn enable_mullvad_daemon(self, _: context::Context) -> Result<(), test_rpc::Error> {
        #[cfg(not(target_os = "windows"))]
        {
            log::warn!("enable_mullvad_daemon is only implemented on Windows");
            return Err(test_rpc::Error::Syscall);
        }
        #[cfg(target_os = "windows")]
        {
            sys::enable_system_service_startup().await
        }
    }

    async fn set_daemon_log_level(
        self,
        _: context::Context,
        verbosity_level: test_rpc::mullvad_daemon::Verbosity,
    ) -> Result<(), test_rpc::Error> {
        sys::set_daemon_log_level(verbosity_level).await
    }

    async fn set_daemon_environment(
        self,
        _: context::Context,
        env: HashMap<String, String>,
    ) -> Result<(), test_rpc::Error> {
        sys::set_daemon_environment(env).await
    }

    async fn get_daemon_environment(
        self,
        _: context::Context,
    ) -> Result<HashMap<String, String>, test_rpc::Error> {
        sys::get_daemon_environment().await
    }

    async fn copy_file(
        self,
        _: context::Context,
        src: String,
        dest: String,
    ) -> Result<(), test_rpc::Error> {
        tokio::fs::copy(&src, &dest).await.map_err(|error| {
            log::error!("Failed to copy \"{src}\" to \"{dest}\": {error}");
            test_rpc::Error::Syscall
        })?;
        Ok(())
    }

    /// Write a slice as the entire contents of a file.
    ///
    /// See the documention of [`tokio::fs::write`] for details of the behavior.
    async fn write_file(
        self,
        _: context::Context,
        dest: PathBuf,
        bytes: Vec<u8>,
    ) -> Result<(), test_rpc::Error> {
        tokio::fs::write(&dest, bytes).await.map_err(|error| {
            log::error!(
                "Failed to write to \"{dest}\": {error}",
                dest = dest.display()
            );
            test_rpc::Error::Syscall
        })?;
        Ok(())
    }

    async fn reboot(self, _: context::Context) -> Result<(), test_rpc::Error> {
        sys::reboot()
    }

    async fn make_device_json_old(self, _: context::Context) -> Result<(), test_rpc::Error> {
        app::make_device_json_old().await
    }

    async fn spawn(self, _: context::Context, opts: SpawnOpts) -> Result<u32, test_rpc::Error> {
        let mut cmd = Command::new(&opts.path);
        cmd.args(&opts.args);

        // Make sure that PATH is updated
        // TODO: We currently do not need this on non-Windows
        #[cfg(target_os = "windows")]
        cmd.env("PATH", sys::get_system_path_var()?);

        cmd.envs(opts.env);

        if opts.attach_stdin {
            cmd.stdin(Stdio::piped());
        } else {
            cmd.stdin(Stdio::null());
        }

        if opts.attach_stdout {
            cmd.stdout(Stdio::piped());
        }

        cmd.stderr(Stdio::piped());
        cmd.kill_on_drop(true);

        let mut child = util::as_unprivileged_user(UNPRIVILEGED_USER, || cmd.spawn())
            .map_err(|error| {
                log::error!("Failed to drop privileges: {error}");
                test_rpc::Error::Syscall
            })?
            .map_err(|error| {
                log::error!("Failed to spawn {}: {error}", opts.path);
                test_rpc::Error::Syscall
            })?;

        let pid = child
            .id()
            .expect("Child hasn't been polled to completion yet");

        log::info!("spawned {} (args={:?}) (pid={pid})", opts.path, opts.args);

        let (abort_tx, abort_rx) = oneshot::channel();
        let abort_handle = || {
            let _ = abort_tx.send(());
        };

        let spawned_process = SpawnedProcess {
            stdout: child.stdout.take(),
            stdin: child.stdin.take(),
            abort_handle: OnDrop::new(Box::new(abort_handle)),
        };

        let mut state = self.0.lock().await;
        state.spawned_procs.insert(pid, spawned_process);
        drop(state);

        // spawn a task to log child stdout
        if let Some(stderr) = child.stderr.take() {
            task::spawn(async move {
                let mut stderr = BufReader::new(stderr);
                let mut line = String::new();
                loop {
                    match stderr.read_line(&mut line).await {
                        Ok(0) => break,
                        Ok(_) => {
                            let trimmed = line.trim_end_matches(['\r', '\n']);
                            log::info!("child stderr (pid={pid}): {trimmed}");
                            line.clear();
                        }
                        Err(e) => {
                            log::error!("failed to read child stderr (pid={pid}): {e}");
                            break;
                        }
                    }
                }
            });
        }

        // spawn a task to monitor if the child exits
        task::spawn(async move {
            select! {
                result = child.wait().fuse() => match result {
                    Err(e) => {
                        log::error!("failed to await child process (pid={pid}): {e}");
                    }
                    Ok(status) => {
                        log::info!("child process (pid={pid}) exited with status: {status}");
                    }
                },

                _ = abort_rx.fuse() => {
                    if let Err(e) = child.kill().await {
                        log::error!("failed to kill child process (pid={pid}): {e}");
                    }
                }
            }

            let mut state = self.0.lock().await;
            state.spawned_procs.remove(&pid);
        });

        Ok(pid)
    }

    async fn read_child_stdout(
        self,
        _: context::Context,
        pid: u32,
    ) -> Result<Option<String>, test_rpc::Error> {
        let mut state = self.0.lock().await;
        let child = state
            .spawned_procs
            .get_mut(&pid)
            .ok_or(test_rpc::Error::UnknownPid(pid))?;

        let Some(stdout) = child.stdout.as_mut() else {
            return Ok(None);
        };

        let mut buf = vec![0u8; 512];

        let n = select_biased! {
            result = stdout.read(&mut buf).fuse() => result
                .map_err(|e| format!("Failed to read from child stdout: {e}"))
                .map_err(test_rpc::Error::Other)?,

            _ = sleep(Duration::from_millis(500)).fuse() => return Ok(Some(String::new())),
        };

        // check for EOF
        if n == 0 {
            child.stdout = None;
            return Ok(None);
        }

        buf.truncate(n);
        let output = String::from_utf8(buf)
            .map_err(|_| test_rpc::Error::Other("Child wrote non UTF-8 to stdout".into()))?;

        Ok(Some(output))
    }

    async fn write_child_stdin(
        self,
        _: context::Context,
        pid: u32,
        data: String,
    ) -> Result<(), test_rpc::Error> {
        let mut state = self.0.lock().await;
        let child = state
            .spawned_procs
            .get_mut(&pid)
            .ok_or(test_rpc::Error::UnknownPid(pid))?;

        let Some(stdin) = child.stdin.as_mut() else {
            return Err(test_rpc::Error::Other("Child stdin is closed.".into()));
        };

        stdin
            .write_all(data.as_bytes())
            .await
            .map_err(|e| format!("Error writing to child stdin: {e}"))
            .map_err(test_rpc::Error::Other)?;

        log::debug!("wrote {} bytes to pid {pid}", data.len());

        Ok(())
    }

    async fn close_child_stdin(self, _: context::Context, pid: u32) -> Result<(), test_rpc::Error> {
        let mut state = self.0.lock().await;
        let child = state
            .spawned_procs
            .get_mut(&pid)
            .ok_or(test_rpc::Error::UnknownPid(pid))?;

        child.stdin = None;

        Ok(())
    }

    async fn kill_child(self, _: context::Context, pid: u32) -> Result<(), test_rpc::Error> {
        let mut state = self.0.lock().await;
        let child = state
            .spawned_procs
            .remove(&pid)
            .ok_or(test_rpc::Error::UnknownPid(pid))?;

        drop(child); // I swear officer, it's not what you think!

        Ok(())
    }

    async fn get_os_version(self, _: context::Context) -> Result<OsVersion, test_rpc::Error> {
        sys::get_os_version()
    }

    #[cfg_attr(not(target_os = "macos"), allow(unused_variables))]
    async fn ifconfig_alias_add(
        self,
        _: context::Context,
        interface: String,
        alias: IpAddr,
    ) -> Result<(), test_rpc::Error> {
        #[cfg(not(target_os = "macos"))]
        return Err(test_rpc::Error::TargetNotImplemented);

        #[cfg(target_os = "macos")]
        talpid_macos::net::add_alias(&interface, alias)
            .await
            .map_err(|e| format!("{e:#}"))
            .map_err(test_rpc::Error::Other)
    }

    #[cfg_attr(not(target_os = "macos"), allow(unused_variables))]
    async fn ifconfig_alias_remove(
        self,
        _: context::Context,
        interface: String,
        alias: IpAddr,
    ) -> Result<(), test_rpc::Error> {
        #[cfg(not(target_os = "macos"))]
        return Err(test_rpc::Error::TargetNotImplemented);

        #[cfg(target_os = "macos")]
        talpid_macos::net::remove_alias(&interface, alias)
            .await
            .map_err(|e| format!("{e:#}"))
            .map_err(test_rpc::Error::Other)
    }
}

/// The baud rate of the serial connection between the test manager and the test runner.
/// There is a known issue with setting a baud rate at all or macOS, and the workaround
/// is to set it to zero: https://github.com/serialport/serialport-rs/pull/58
///
/// Keep this constant in sync with `test-manager/src/run_tests.rs`
const BAUD: u32 = if cfg!(target_os = "macos") { 0 } else { 115200 };

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Unknown RPC")]
    UnknownRpc,
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    logging::init_logger().unwrap();

    let mut args = std::env::args();
    let _ = args.next();
    let path = args.next().expect("serial/COM path must be provided");

    loop {
        log::info!("Connecting to {}", path);

        let serial_stream =
            tokio_serial::SerialStream::open(&tokio_serial::new(&path, BAUD)).unwrap();
        let (runner_transport, mullvad_daemon_transport, _completion_handle) =
            test_rpc::transport::create_server_transports(serial_stream);

        log::info!("Running server");

        tokio::spawn(forward_to_mullvad_daemon_interface(
            mullvad_daemon_transport,
        ));

        let server = tarpc::server::BaseChannel::with_defaults(runner_transport);
        server.execute(TestServer::default().serve()).await;

        log::error!("Restarting server since it stopped");
    }
}

/// Forward data between the test manager and Mullvad management interface socket
async fn forward_to_mullvad_daemon_interface(proxy_transport: GrpcForwarder) {
    const IPC_READ_BUF_SIZE: usize = 16 * 1024;

    let mut srv_read_buf = [0u8; IPC_READ_BUF_SIZE];
    let mut proxy_transport = LengthDelimitedCodec::new().framed(proxy_transport);

    loop {
        // Wait for input from the test manager before connecting to the UDS or named pipe.
        // Connect at the last moment since the daemon may not even be running when the
        // test runner first starts.
        let first_message = match proxy_transport.next().await {
            Some(Ok(bytes)) => {
                if bytes.is_empty() {
                    log::debug!("ignoring EOF from client");
                    continue;
                }
                bytes
            }
            Some(Err(error)) => {
                log::error!("daemon client channel error: {error}");
                break;
            }
            None => break,
        };

        log::info!("mullvad daemon: connecting");

        let mut daemon_socket_endpoint =
            match parity_tokio_ipc::Endpoint::connect(SOCKET_PATH).await {
                Ok(uds_endpoint) => uds_endpoint,
                Err(error) => {
                    log::error!("mullvad daemon: failed to connect: {error}");
                    // send EOF
                    let _ = proxy_transport.send(bytes::Bytes::new()).await;
                    continue;
                }
            };

        log::info!("mullvad daemon: connected");

        if let Err(error) = daemon_socket_endpoint.write_all(&first_message).await {
            log::error!("writing to uds failed: {error}");
            continue;
        }

        loop {
            let srv_read = daemon_socket_endpoint.read(&mut srv_read_buf);
            pin_mut!(srv_read);

            match futures::future::select(srv_read, proxy_transport.next()).await {
                futures::future::Either::Left((read, _)) => match read {
                    Ok(num_bytes) => {
                        if num_bytes == 0 {
                            log::debug!("uds EOF; restarting server");
                            break;
                        }
                        if let Err(error) = proxy_transport
                            .send(srv_read_buf[..num_bytes].to_vec().into())
                            .await
                        {
                            log::error!("writing to client channel failed: {error}");
                            break;
                        }
                    }
                    Err(error) => {
                        log::error!("reading from uds failed: {error}");
                        let _ = proxy_transport.send(bytes::Bytes::new()).await;
                        break;
                    }
                },
                futures::future::Either::Right((read, _)) => match read {
                    Some(Ok(bytes)) => {
                        if bytes.is_empty() {
                            log::debug!("management interface EOF; restarting server");
                            break;
                        }
                        if let Err(error) = daemon_socket_endpoint.write_all(&bytes).await {
                            log::error!("writing to uds failed: {error}");
                            break;
                        }
                    }
                    Some(Err(error)) => {
                        log::error!("daemon client channel error: {error}");
                        break;
                    }
                    None => break,
                },
            }
        }

        log::info!("mullvad daemon: disconnected");
    }
}