cloudpub-client 3.6.1173

CloudPub CLI client for secure tunnel and service publishing
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
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
use anyhow::{anyhow, bail, Context, Result};
use backoff::backoff::Backoff;
use cloudpub_common::data::DataChannel;
use cloudpub_common::fair_channel::{fair_channel, FairSender};
use cloudpub_common::protocol::message::Message;
use cloudpub_common::protocol::{
    AgentInfo, ConnectState, Data, DataChannelAck, DataChannelData, DataChannelDataUdp,
    DataChannelEof, ErrorInfo, ErrorKind, HeartBeat, Protocol,
};
use cloudpub_common::transport::{AddrMaybeCached, SocketOpts, Transport, WebsocketTransport};
use cloudpub_common::utils::{
    get_platform, proto_to_socket_addr, socket_addr_to_proto, udp_connect,
};
use cloudpub_common::VERSION;
use dashmap::DashMap;
use parking_lot::RwLock;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpStream, UdpSocket};
use tokio::sync::mpsc;
use tokio::time::{self, Duration, Instant};
use tracing::{debug, error, info, trace, warn};

use cloudpub_common::constants::{
    run_control_chan_backoff, CONTROL_CHANNEL_SIZE, DATA_BUFFER_SIZE, DATA_CHANNEL_SIZE,
    DEFAULT_CLIENT_RETRY_INTERVAL_SECS, UDP_BUFFER_SIZE, UDP_TIMEOUT,
};
use futures::future::FutureExt;

use crate::config::{ClientConfig, ClientOpts};
use crate::upgrade::handle_upgrade_available;
use bytes::Bytes;
use cloudpub_common::transport::ProtobufStream;

#[cfg(feature = "plugins")]
use crate::plugins::plugin_trait::PluginHandle;
#[cfg(feature = "plugins")]
use crate::plugins::registry::PluginRegistry;

type Service = Arc<DataChannel>;

type Services = Arc<DashMap<String, Service>>;

/// A server-sent Error that the handshake ack path has already forwarded to
/// result_tx. Typed so the outer retry loop can tell a real server refusal
/// from a plain network failure: the server's own message must stay the last
/// one the GUI sees (no synthetic error-network on top of it), and a fatal
/// kind means retrying can never succeed (CLOUDPUB-1464).
#[derive(Debug)]
struct ServerRefusal {
    kind: ErrorKind,
}

impl std::fmt::Display for ServerRefusal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Server refused the handshake: {:?}", self.kind)
    }
}

impl std::error::Error for ServerRefusal {}

/// What the outer retry loop does about a run_control_channel failure.
struct RetryPlan {
    /// Send the synthetic t!("error-network") ErrorInfo. Only when no real
    /// server Error was forwarded for this failure - otherwise the synthetic
    /// toast overwrites the server's meaningful message in the GUI
    /// (CLOUDPUB-1464).
    send_synthetic_network_error: bool,
    /// Reconnect after backoff, or stop the client for good.
    retry: bool,
}

/// The retry decision for one run_control_channel failure. Pure so the
/// contract is unit-testable without sockets (CLOUDPUB-1464).
fn plan_retry(err: &anyhow::Error) -> RetryPlan {
    match err.downcast_ref::<ServerRefusal>() {
        Some(refusal) => RetryPlan {
            send_synthetic_network_error: false,
            retry: !refusal.kind.is_fatal(),
        },
        None => RetryPlan {
            send_synthetic_network_error: true,
            retry: true,
        },
    }
}

// Holds the state of a client
struct Client<T: Transport> {
    config: Arc<RwLock<ClientConfig>>,
    opts: ClientOpts,
    services: Services,
    transport: Arc<T>,
    connected: bool,
    #[cfg(feature = "plugins")]
    plugin_processes: Arc<DashMap<String, PluginHandle>>,
    data_channels: Arc<DashMap<u32, Arc<DataChannel>>>,
}

impl<T: 'static + Transport> Client<T> {
    // Create a Client from `[client]` config block
    async fn from(config: Arc<RwLock<ClientConfig>>, opts: ClientOpts) -> Result<Client<T>> {
        let transport = Arc::new(
            T::new(&config.clone().read().transport)
                .with_context(|| "Failed to create the transport")?,
        );
        Ok(Client {
            config,
            opts,
            services: Default::default(),
            transport,
            connected: false,
            #[cfg(feature = "plugins")]
            plugin_processes: Arc::new(DashMap::new()),
            data_channels: Arc::new(DashMap::new()),
        })
    }

    // The entrypoint of Client
    async fn run(
        &mut self,
        mut command_rx: mpsc::Receiver<Message>,
        result_tx: mpsc::Sender<Message>,
    ) -> Result<()> {
        let transport = self.transport.clone();

        let mut retry_backoff = run_control_chan_backoff(DEFAULT_CLIENT_RETRY_INTERVAL_SECS);

        let mut start = Instant::now();
        result_tx
            .send(Message::ConnectState(ConnectState::Connecting.into()))
            .await
            .context("Can't send Connecting event")?;
        while let Err(err) = self
            .run_control_channel(transport.clone(), &mut command_rx, &result_tx)
            .boxed()
            .await
        {
            if result_tx.is_closed() {
                // The client is shutting down
                break;
            }

            let plan = plan_retry(&err);

            if self.connected {
                if plan.send_synthetic_network_error {
                    result_tx
                        .send(Message::Error(ErrorInfo {
                            kind: ErrorKind::HandshakeFailed.into(),
                            message: crate::t!("error-network"),
                            guid: String::new(),
                        }))
                        .await
                        .context("Can't send Error event")?;
                }
                result_tx
                    .send(Message::ConnectState(ConnectState::Disconnected.into()))
                    .await
                    .context("Can't send Disconnected event")?;
                if plan.retry {
                    result_tx
                        .send(Message::ConnectState(ConnectState::Connecting.into()))
                        .await
                        .context("Can't send Connecting event")?;
                }
                self.connected = false;
            }

            self.services.clear();
            #[cfg(feature = "plugins")]
            self.plugin_processes.clear();
            self.data_channels.clear();

            if !plan.retry {
                error!("Unrecoverable server refusal, stop retrying: {:#}", err);
                break;
            }

            if start.elapsed() > Duration::from_secs(3) {
                // The client runs for at least 3 secs and then disconnects
                retry_backoff.reset();
            }

            if let Some(duration) = retry_backoff.next_backoff() {
                warn!("{:#}. Retry in {:?}...", err, duration);
                time::sleep(duration).await;
            }

            start = Instant::now();
        }

        self.services.clear();
        #[cfg(feature = "plugins")]
        self.plugin_processes.clear();
        self.data_channels.clear();

        Ok(())
    }

    async fn run_control_channel(
        &mut self,
        transport: Arc<T>,
        command_rx: &mut mpsc::Receiver<Message>,
        result_tx: &mpsc::Sender<Message>,
    ) -> Result<()> {
        let url = self.config.read().server.clone();
        let port = url.port().unwrap_or(443);
        let host = url.host_str().context("Failed to get host")?;
        let mut host_and_port = format!("{}:{}", host, port);

        let (mut conn, _remote_addr) = loop {
            let mut remote_addr = AddrMaybeCached::new(&host_and_port);
            remote_addr
                .resolve()
                .await
                .context("Failed to resolve server address")?;

            let mut conn = transport.connect(&remote_addr).await.context(format!(
                "Failed to connect control channel to {}",
                host_and_port
            ))?;

            self.connected = true;

            T::hint(&conn, SocketOpts::for_control_channel());

            let (email, password) = if let Some(ref cred) = self.opts.credentials {
                (cred.0.clone(), cred.1.clone())
            } else {
                (String::new(), String::new())
            };

            let token = self
                .config
                .read()
                .token
                .clone()
                .unwrap_or_default()
                .to_string();

            let hwid = self.config.read().get_hwid();

            let agent_info = AgentInfo {
                agent_id: self.config.read().agent_id.clone(),
                token,
                email,
                password,
                hostname: hostname::get()?.to_string_lossy().into_owned(),
                version: VERSION.to_string(),
                gui: self.opts.gui,
                platform: get_platform(),
                hwid,
                server_host_and_port: host_and_port.clone(),
                // The release line this agent self-upgrades in: the server
                // offers upgrades out of download/<branch>/ (CLOUDPUB-1527)
                branch: cloudpub_common::BRANCH.to_string(),
                transient: self.opts.transient,
                secondary: self.opts.secondary,
                is_service: self.opts.is_service,
            };

            debug!("Sending hello: {:?}", agent_info);

            let hello_send = Message::AgentHello(agent_info);

            conn.send_message(&hello_send)
                .await
                .context("Failed to send hello message")?;

            debug!("Reading ack");
            match conn
                .recv_message()
                .await
                .context("Failed to read ack message")?
            {
                Some(msg) => match msg {
                    Message::AgentAck(args) => {
                        if !args.token.is_empty() {
                            let mut c = self.config.write();
                            c.token = Some(args.token.as_str().into());
                            c.save().context("Write config")?;
                        }
                        break (conn, remote_addr);
                    }
                    Message::Redirect(r) => {
                        host_and_port = r.host_and_port.clone();
                        debug!("Redirecting to {}", host_and_port);
                        continue;
                    }
                    Message::Error(err) => {
                        result_tx
                            .send(Message::Error(err.clone()))
                            .await
                            .context("Can't send server error event")?;
                        // The server's message is now the last one the GUI
                        // saw; the typed refusal keeps the outer loop from
                        // masking it and carries the kind for the retry
                        // decision (CLOUDPUB-1464).
                        return Err(ServerRefusal {
                            kind: err.kind.try_into().unwrap_or(ErrorKind::Fatal),
                        }
                        .into());
                    }
                    v => bail!("Unexpected ack message: {:?}", v),
                },
                None => bail!("Connection closed while reading ack message"),
            };
        };

        debug!("Control channel established");

        result_tx
            .send(Message::ConnectState(ConnectState::Connected.into()))
            .await
            .context("Can't send Connected event")?;

        let (to_server_tx, mut to_server_rx) = fair_channel::<Message>(CONTROL_CHANNEL_SIZE);
        // Used to break long running setup

        let heartbeat_timeout = self.config.read().heartbeat_timeout;

        loop {
            tokio::select! {
                cmd = to_server_rx.recv() => {
                    if let Some(cmd) = cmd {
                        conn.send_message(&cmd).await.context("Failed to send command")?;
                    }
                },
                cmd = command_rx.recv() => {
                    if let Some(cmd) = cmd {
                        debug!("Received message: {:?}", cmd);
                        match cmd {
                            Message::PerformUpgrade(info) => {
                                let config_clone = self.config.clone();
                                if let Err(e) = handle_upgrade_available(
                                    &info.version,
                                    config_clone,
                                    self.opts.gui,
                                    command_rx,
                                    result_tx,
                                )
                                .await
                                {
                                    result_tx.send(Message::Error(ErrorInfo {
                                        kind: ErrorKind::Fatal.into(),
                                        message: e.to_string(),
                                        guid: String::new(),
                                    }))
                                    .await
                                    .context("Can't send Error event")?;
                                }
                            }
                            Message::Stop(_x) => {
                                info!("Stopping the client");
                                break;
                            }
                            Message::Break(break_msg) => {
                                info!("Breaking operation for guid: {}", break_msg.guid);
                                #[cfg(feature = "plugins")]
                                if let Some((_, handle)) = self.plugin_processes.remove(&break_msg.guid) {
                                    info!("Dropped plugin handle for guid: {}", break_msg.guid);
                                    drop(handle);
                                }
                            }
                            cmd => {
                                conn.send_message(&cmd).await.context("Failed to send message")?;
                            }
                        };
                    } else {
                        debug!("No more commands, shutting down...");
                        break;
                    }
                },
                val = conn.recv_message() => {
                    match val? {
                        Some(val) => {
                            match val {
                                Message::EndpointAck(mut endpoint) => {
                                    // The guid flows into plugin file paths
                                    // ({guid}.conf/.pid/.lock, the onec publish
                                    // dir), so a server-sent guid with path
                                    // metacharacters must never get that far:
                                    // reject the whole ack at receipt
                                    // (CLOUDPUB-1313).
                                    if !is_valid_guid(&endpoint.guid) {
                                        warn!(
                                            "Rejecting EndpointAck with unsafe guid: {:?}",
                                            endpoint.guid
                                        );
                                        continue;
                                    }
                                    #[cfg(feature = "plugins")]
                                    {
                                        let to_server_tx = to_server_tx.clone();
                                        let config = self.config.clone();
                                        let opts = self.opts.clone();
                                        if endpoint.error.is_empty() {
                                            let protocol: Protocol = endpoint
                                                .client
                                                .as_ref()
                                                .unwrap()
                                                .local_proto
                                                .try_into()
                                                .unwrap_or(Protocol::Tcp);
                                            if let Some(plugin) = PluginRegistry::new().get(protocol) {
                                                // Duplicate EndpointStart for a publication this
                                                // client already serves (the server re-sends them
                                                // after EndpointStartAll and on repeated
                                                // registrations): replacing the handle would kill
                                                // the running subprocess mid-flight.
                                                let already_running = self
                                                    .plugin_processes
                                                    .get(&endpoint.guid)
                                                    .map(|h| h.same_config(&endpoint) && h.is_healthy())
                                                    .unwrap_or(false);
                                                if already_running {
                                                    debug!(
                                                        "Endpoint {} already running with the same config, \
                                                         ignoring duplicate start",
                                                        endpoint.guid
                                                    );
                                                } else {
                                                    let handle = PluginHandle::spawn(
                                                        plugin,
                                                        endpoint.clone(),
                                                        config,
                                                        opts,
                                                        to_server_tx,
                                                    );
                                                    self.plugin_processes.insert(endpoint.guid.clone(), handle);
                                                }
                                            } else {
                                                endpoint.status = Some("online".into());
                                                let _ = to_server_tx.send(Message::EndpointStatus(endpoint.clone())).await;
                                            }
                                        }
                                    }
                                    #[cfg(not(feature = "plugins"))]
                                    if endpoint.error.is_empty() {
                                        // A denied publication (the ack carries the error) must
                                        // not be claimed online - the server has no listener for
                                        // it, and the claim only forged a green status in the
                                        // dashboard (CLOUDPUB-1466).
                                        endpoint.status = Some("online".into());
                                        let _ = to_server_tx.send(Message::EndpointStatus(endpoint.clone())).await;
                                    }
                                    result_tx
                                        .send(Message::EndpointAck(endpoint))
                                        .await
                                        .context("Can't send EndpointAck event")?;
                                }

                                Message::CreateDataChannelWithId(create_msg) => {
                                    let channel_id = create_msg.channel_id;
                                    let endpoint = create_msg.endpoint.unwrap();

                                    trace!("Creating data channel {} for endpoint {:?}", channel_id, endpoint.guid);

                                    // Create channels for data flow
                                    let (to_service_tx, to_service_rx) = mpsc::channel::<Data>(DATA_CHANNEL_SIZE);

                                    // Register the data channel
                                    let data_channel = Arc::new(DataChannel::new_client(channel_id, to_service_tx.clone()));
                                    self.data_channels.insert(channel_id, data_channel.clone());

                                    // Check if endpoint handled by plugin server
                                    let client = endpoint.client.unwrap();
                                    #[allow(unused_mut)]
                                    let mut local_addr = format!("{}:{}", client.local_addr, client.local_port);
                                    #[cfg(feature = "plugins")]
                                    if let Some(handle) = self.plugin_processes.get(&endpoint.guid) {
                                        if let Some(port) = handle.value().port() {
                                            local_addr = format!("127.0.0.1:{}", port);
                                        }
                                    }

                                    // Immediately start handling the data channel
                                    let data_channels = self.data_channels.clone();
                                    let protocol: Protocol = client.local_proto.try_into().unwrap();

                                    let to_server_tx_cloned = to_server_tx.clone();
                                    tokio::spawn(async move {
                                        if let Err(err) = if protocol == Protocol::Udp {
                                            handle_udp_data_channel(
                                                data_channel,
                                                local_addr,
                                                to_server_tx_cloned.clone(),
                                                to_service_rx
                                            ).await
                                        } else {
                                            handle_tcp_data_channel(
                                                data_channel,
                                                local_addr,
                                                to_server_tx_cloned.clone(),
                                                to_service_rx
                                            ).await
                                        } {
                                            error!("DataChannel {{ channel_id: {} }}: {:?}", channel_id, err);
                                            to_server_tx_cloned
                                                .send(Message::DataChannelEof(
                                                        DataChannelEof {
                                                            channel_id,
                                                            error: err.to_string()
                                                    })
                                                ).await.ok();
                                        }
                                        if let Some((_, dc)) = data_channels.remove(&channel_id) { dc.close() }
                                    });
                                },

                                Message::DataChannelData(data) => {
                                    // Forward data to the appropriate data channel
                                    let to_service_tx = self.data_channels.get(&data.channel_id).map(|ch| ch.data_tx.clone());
                                    if let Some(tx) = to_service_tx {
                                        if let Err(err) = tx.send(Data {
                                            data: data.data.into(),
                                            socket_addr: None
                                        }).await {
                                            self.data_channels.remove(&data.channel_id);
                                            error!("Error send to data channel {}: {:?}", data.channel_id, err);
                                        }
                                    } else {
                                        trace!("Data channel {} not found, dropping data", data.channel_id);
                                    }
                                },

                                Message::DataChannelDataUdp(data) => {
                                    // Forward UDP data to the appropriate data channel
                                    let to_service_tx = self.data_channels.get(&data.channel_id).map(|ch| ch.data_tx.clone());
                                    if let Some(tx) = to_service_tx {
                                        let socket_addr = data.socket_addr.as_ref()
                                            .map(proto_to_socket_addr)
                                            .transpose()
                                            .unwrap_or_else(|err| {
                                                error!("Invalid socket address for UDP data channel {}: {:?}", data.channel_id, err);
                                                None
                                            });

                                        if let Err(err) = tx.send(Data {
                                            data: data.data.into(),
                                            socket_addr,
                                        }).await {
                                            self.data_channels.remove(&data.channel_id);
                                            error!("Error send to UDP data channel {}: {:?}", data.channel_id, err);
                                        }
                                    } else {
                                        trace!("UDP Data channel {} not found, dropping data", data.channel_id);
                                    }
                                },

                                Message::DataChannelEof(eof) => {
                                    // Signal EOF by dropping the data channel
                                    if let Some((_, dc)) = self.data_channels.remove(&eof.channel_id) { dc.close() }
                                    if eof.error.is_empty() {
                                        // Normal EOF without error
                                        trace!("Data channel {} closed by server", eof.channel_id);
                                    } else {
                                        // EOF with error
                                        trace!("Data channel {} closed by server with error: {}", eof.channel_id, eof.error);
                                    }
                                },

                                Message::DataChannelAck(DataChannelAck { channel_id, consumed }) => {
                                    if let Some(ch) = self.data_channels.get(&channel_id) {
                                        ch.add_capacity(consumed);
                                    }
                                }

                                Message::EndpointStopAck(ref ep) => {
                                    self.services.remove(&ep.guid);
                                    #[cfg(feature = "plugins")]
                                    self.plugin_processes.remove(&ep.guid);
                                    result_tx.send(val).await.context("Can't send result message")?;
                                }

                                Message::EndpointRemoveAck(ref ep) => {
                                    self.services.remove(&ep.guid);
                                    #[cfg(feature = "plugins")]
                                    self.plugin_processes.remove(&ep.guid);
                                    result_tx.send(val).await.context("Can't send result message")?;
                                }

                                Message::HeartBeat(_) => {
                                    conn.send_message(&Message::HeartBeat(HeartBeat{})).await.context("Failed to send heartbeat")?;
                                },

                                Message::Error(ref err) => {
                                    let kind: ErrorKind = err.kind.try_into().unwrap_or(ErrorKind::Fatal);
                                    result_tx.send(val.clone()).await.context("Can't send result message")?;
                                    if kind.is_fatal() {
                                        error!("Fatal error received, stop client: {:?}", err);
                                        break;
                                    }
                                }

                                Message::Break(break_msg) => {
                                    info!("Breaking operation for guid: {}", break_msg.guid);
                                    #[cfg(feature = "plugins")]
                                    self.plugin_processes.remove(&break_msg.guid);
                                }

                                Message::PerformUpgrade(info) => {
                                    let config_clone = self.config.clone();
                                    #[cfg(feature = "plugins")]
                                    self.plugin_processes.clear();
                                    self.services.clear();
                                    self.data_channels.clear();

                                    if let Err(e) = handle_upgrade_available(
                                        &info.version,
                                        config_clone,
                                        self.opts.gui,
                                        command_rx,
                                        result_tx,
                                    )
                                    .await
                                    {
                                        conn.send_message(&Message::Error(ErrorInfo {
                                            kind: ErrorKind::UpgradeFailed.into(),
                                            message: e.to_string(),
                                            guid: String::new(),
                                        }))
                                        .await
                                        .context("Can't send Error event")?;
                                    }
                                }

                                v => {
                                    result_tx.send(v).await.context("Can't send result message")?;
                                }
                            }
                        },
                        None => {
                            debug!("Connection closed by server");
                            break;
                        }
                    }
                },
                _ = time::sleep(Duration::from_secs(heartbeat_timeout)), if heartbeat_timeout != 0 => {
                    return Err(anyhow!("Heartbeat timed out"))
                }
            }
        }

        info!("Control channel shutdown");
        result_tx
            .send(Message::ConnectState(ConnectState::Disconnected.into()))
            .await
            .context("Can't send Disconnected event")?;
        conn.close().await.ok();
        time::sleep(Duration::from_millis(100)).await; // Give some time for the connection to close gracefully
        Ok(())
    }
}

pub async fn run_client(
    config: Arc<RwLock<ClientConfig>>,
    opts: ClientOpts,
    command_rx: mpsc::Receiver<Message>,
    result_tx: mpsc::Sender<Message>,
) -> Result<()> {
    let mut client = Client::<WebsocketTransport>::from(config, opts)
        .await
        .context("Failed to create Websocket client")?;
    client.run(command_rx, result_tx).await
}
async fn handle_tcp_data_channel(
    data_channel: Arc<DataChannel>,
    local_addr: String,
    to_server_tx: FairSender<Message>,
    mut data_rx: mpsc::Receiver<Data>,
) -> Result<()> {
    trace!("Handling client {:?} to {}", data_channel, local_addr);

    // Connect to local service immediately
    let mut local_stream = TcpStream::connect(&local_addr)
        .await
        .with_context(|| format!("Failed to connect to local service at {}", local_addr))?;

    // Set TCP_NODELAY for low latency
    local_stream
        .set_nodelay(true)
        .context("Failed to set TCP_NODELAY")?;

    let mut buf = [0u8; DATA_BUFFER_SIZE]; // Smaller buffer for low latency

    loop {
        tokio::select! {
            res = local_stream.read(&mut buf) => {
                match res {
                    Ok(0) => {
                        trace!("EOF received from local service for {:?}", data_channel);
                        if let Err(err) = to_server_tx.send(Message::DataChannelEof(DataChannelEof {
                            channel_id: data_channel.id,
                            error: String::new()
                        }))
                        .await {
                            trace!("Failed to send EOF to server for {:?}: {:#}", data_channel, err);
                        }
                        break;
                    },
                    Ok(n) => {
                        //debug!("Read {} bytes from local service for {:?}", n, data_channel);
                        if data_channel.wait_for_capacity(n as u32).await.is_err() {
                            trace!("Data channel {} closed when waiting for capacity", data_channel.id);
                            break;
                        }
                        if let Err(err) = to_server_tx.send(Message::DataChannelData(DataChannelData {
                            channel_id: data_channel.id,
                            data: buf[0..n].to_vec()
                        }))
                        .await {
                            trace!("Failed to send data to server for {:?}: {:#}", data_channel, err);
                            break;
                        }
                    },
                    Err(e) => {
                        return Err(e).context("Failed to read from local service");
                    }
                }
            }

            // Receive data from server via control channel and write to local service
            data_result = data_rx.recv() => {
                match data_result {
                    Some(data) => {
                        trace!("Received {} bytes from server for {:?}", data.data.len(), data_channel);
                        local_stream.write_all(&data.data).await.context("Failed to write data to local service")?;
                        to_server_tx.send(Message::DataChannelAck(
                            DataChannelAck {
                                channel_id: data_channel.id,
                                consumed: data.data.len() as u32
                            }
                        )).await.with_context(|| "Failed to send TCP traffic ack to the server")?;
                    },
                    None => {
                        trace!("EOF received from server for {:?}", data_channel);
                        break;
                    }
                }
            }

            _ = data_channel.closed() => {
                trace!("Data channel {} closed", data_channel.id);
                break;
            }
        }
    }
    Ok(())
}

// UDP port map for managing forwarders per remote address
type UdpPortMap = Arc<DashMap<SocketAddr, mpsc::Sender<Bytes>>>;

async fn handle_udp_data_channel(
    data_channel: Arc<DataChannel>,
    local_addr: String,
    to_server_tx: FairSender<Message>,
    mut data_rx: mpsc::Receiver<Data>,
) -> Result<()> {
    trace!(
        "Handling client UDP channel {:?} to {}",
        data_channel,
        local_addr
    );

    let port_map: UdpPortMap = Arc::new(DashMap::new());

    loop {
        let data_channel = data_channel.clone();
        // Receive data from server via control channel
        tokio::select! {
            data = data_rx.recv() => {
                match data {
                    Some(data) => {
                        let external_addr = data.socket_addr.unwrap();

                        if !port_map.contains_key(&external_addr) {
                            // This packet is from an address we haven't seen for a while,
                            // which is not in the UdpPortMap.
                            // So set up a mapping (and a forwarder) for it

                            match udp_connect(&local_addr).await {
                                Ok(s) => {
                                    let (to_service_tx, to_service_rx) = mpsc::channel(DATA_CHANNEL_SIZE);
                                    port_map.insert(external_addr, to_service_tx);
                                    tokio::spawn(run_udp_forwarder(
                                        s,
                                        to_service_rx,
                                        to_server_tx.clone(),
                                        external_addr,
                                        data_channel,
                                        port_map.clone(),
                                    ));
                                }
                                Err(e) => {
                                    error!(
                                        "Failed to create UDP forwarder for {}: {:#}",
                                        external_addr, e
                                    );
                                }
                            }
                        }

                        // Now there should be a udp forwarder that can receive the packet
                        if let Some(tx) = port_map.get(&external_addr) {
                            let _ = tx.send(data.data).await;
                        }
                    }
                    None => {
                        trace!("EOF received from server for UDP {:?}", data_channel);
                        break;
                    }
                }
            }
            _ = data_channel.closed() => {
                trace!("Data channel {} closed", data_channel.id);
                break;
            }
        }
    }
    Ok(())
}

// Run a UdpSocket for the visitor `from`
async fn run_udp_forwarder(
    s: UdpSocket,
    mut to_service_rx: mpsc::Receiver<Bytes>,
    to_server_tx: FairSender<Message>,
    from: SocketAddr,
    data_channel: Arc<DataChannel>,
    port_map: UdpPortMap,
) -> Result<()> {
    trace!("UDP forwarder created for {} on {:?}", from, data_channel);
    let mut buf = vec![0u8; UDP_BUFFER_SIZE];

    loop {
        tokio::select! {
            // Receive from the server
            data = to_service_rx.recv() => {
                if let Some(data) = data {
                    s.send(&data).await.with_context(|| "Failed to send UDP traffic to the service")?;
                    to_server_tx.send(Message::DataChannelAck(
                        DataChannelAck {
                            channel_id: data_channel.id,
                            consumed: data.len() as u32
                        }
                    )).await.with_context(|| "Failed to send UDP traffic ack to the server")?;
                } else {
                    break;
                }
            },

            // Receive from the service
            val = s.recv(&mut buf) => {
                let len = match val {
                    Ok(v) => v,
                    Err(_) => break
                };

                if data_channel.wait_for_capacity(len as u32).await.is_err() {
                    break;
                }

                to_server_tx.send(Message::DataChannelDataUdp(
                    DataChannelDataUdp {
                    channel_id: data_channel.id,
                    data: buf[..len].to_vec(),
                    socket_addr: Some(socket_addr_to_proto(&from)),
                })).await.with_context(|| "Failed to send UDP traffic to the server")?;
            },

            // No traffic for the duration of UDP_TIMEOUT, clean up the state
            _ = time::sleep(Duration::from_secs(UDP_TIMEOUT)) => {
                break;
            }

            _ = data_channel.closed() => {
                trace!("Data channel {} closed", data_channel.id);
                break;
            }
        }
    }

    port_map.remove(&from);

    debug!("UDP forwarder dropped for {} on {:?}", from, data_channel);
    Ok(())
}

/// A guid arriving from the server ends up in filesystem paths (the httpd
/// plugin's {guid}.conf/.pid/.lock, the onec publish dir), so it is validated
/// once, at receipt: strictly ASCII alphanumeric plus `-`/`_`, which covers
/// every UUID the server legitimately sends and rejects `.`, `/` and `\`
/// outright (CLOUDPUB-1313).
fn is_valid_guid(guid: &str) -> bool {
    !guid.is_empty()
        && guid
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
}

#[cfg(test)]
mod tests {
    use super::*;

    // HYPOTHESIS: endpoint.guid arrives from the server in EndpointAck and is
    // pushed straight into filesystem paths ({guid}.conf/.pid/.lock in the
    // httpd plugin, publish_dir.join(guid) in onec), so a malicious server
    // can traverse out of the working dir with a guid like "../evil"
    // (CLOUDPUB-1313). The receipt handler must reject such a guid before any
    // plugin spawns and writes files.
    // RESULT: red first - is_valid_guid did not exist; green once the
    // EndpointAck arm gates on it and skips processing for unsafe guids.
    // STATUS: FIXED - see CLOUDPUB-1313
    #[test]
    fn test_endpoint_ack_rejects_path_traversal_guid() {
        // Everything the server legitimately sends: UUIDs and simple ids.
        assert!(is_valid_guid("550e8400-e29b-41d4-a716-446655440000"));
        assert!(is_valid_guid("abc_DEF-123"));

        // Path metacharacters in every convention must be rejected outright.
        assert!(!is_valid_guid("../evil"));
        assert!(!is_valid_guid("a/b"));
        assert!(!is_valid_guid("a\\b"));
        assert!(!is_valid_guid(".."));
        assert!(!is_valid_guid("."));
        assert!(!is_valid_guid(""));
        assert!(!is_valid_guid("x/../../etc/cron.d/job"));
        assert!(!is_valid_guid("null\0byte"));
    }

    // HYPOTHESIS: two defects around the outer retry loop in run()
    // (CLOUDPUB-1464). (1) After run_control_channel fails, the loop always
    // sends a synthetic HandshakeFailed/t!("error-network") ErrorInfo -
    // self.connected is already true right after TCP connect, before the
    // handshake - so the server's meaningful refusal, which the ack path had
    // just forwarded, is overwritten in the GUI by "Network error". (2) The
    // loop never inspects the error kind, so a fatal refusal (guid owned by
    // another account, sent as ErrorKind::Fatal by the fixed server) is
    // retried forever and the GUI hangs in "Connecting".
    // SOLUTION: the ack path returns the typed ServerRefusal after forwarding
    // the server Error, and the loop acts on plan_retry(): a forwarded
    // refusal suppresses the synthetic error, a fatal one ends the loop with
    // ConnectState::Disconnected as the final state.
    // RESULT: red first - plan_retry() unconditionally returned the status
    // quo (synthetic error + retry); green once it downcasts ServerRefusal
    // and honors ErrorKind::is_fatal().
    // STATUS: FIXED - see CLOUDPUB-1464
    #[test]
    fn fatal_server_refusal_stops_retrying_without_masking() {
        let err: anyhow::Error = ServerRefusal {
            kind: ErrorKind::Fatal,
        }
        .into();
        let plan = plan_retry(&err);
        assert!(
            !plan.retry,
            "a fatal server refusal can never succeed by retrying"
        );
        assert!(
            !plan.send_synthetic_network_error,
            "the server's own message must stay the last one the GUI sees"
        );
    }

    #[test]
    fn non_fatal_server_refusal_still_retries_without_masking() {
        // HandshakeFailed stays non-fatal by design: the server labels
        // transient causes (DB errors) with it, so the client keeps retrying.
        let err: anyhow::Error = ServerRefusal {
            kind: ErrorKind::HandshakeFailed,
        }
        .into();
        let plan = plan_retry(&err);
        assert!(plan.retry, "transient refusals must keep retrying");
        assert!(
            !plan.send_synthetic_network_error,
            "the forwarded server message must not be overwritten by the \
             synthetic network error"
        );
    }

    #[test]
    fn lease_busy_refusal_still_retries() {
        // LeaseBusy is temporary by construction (CLOUDPUB-1339) - the fix
        // for CLOUDPUB-1464 must not turn it into a stop.
        let err: anyhow::Error = ServerRefusal {
            kind: ErrorKind::LeaseBusy,
        }
        .into();
        assert!(plan_retry(&err).retry);
    }

    #[test]
    fn plain_network_failure_keeps_synthetic_error_and_retry() {
        // No server Error was forwarded: today's behavior stays - report the
        // localized network error and reconnect.
        let err = anyhow::anyhow!("Failed to connect control channel to example.com:443");
        let plan = plan_retry(&err);
        assert!(plan.retry);
        assert!(plan.send_synthetic_network_error);
    }

    #[test]
    fn server_refusal_is_detected_through_context_wrapping() {
        // anyhow context layers must not hide the refusal from the loop.
        let err = anyhow::Error::new(ServerRefusal {
            kind: ErrorKind::Fatal,
        })
        .context("run_control_channel failed");
        assert!(!plan_retry(&err).retry);
    }
}