cloudpub-common 3.0.2

Common code for the client, server, and GUI
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
// Create a module to contain the Protocol Buffers generated code
use anyhow::{anyhow, bail, Context, Result};
use bytes::{Bytes, BytesMut};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tracing::{debug, trace};
use urlencoding::encode;

use crate::fair_channel::FairGroup;
use crate::utils::get_version_number;
pub use prost::Message as ProstMessage;

pub trait Endpoint {
    fn credentials(&self) -> String;
    fn as_url(&self) -> String;
}

pub trait DefaultPort {
    fn default_port(&self) -> Option<u16>;
}

pub fn parse_enum<E: FromStr + Into<i32>>(name: &str) -> Result<i32> {
    let proto = E::from_str(name).map_err(|_| anyhow!("Invalid enum: {}", name))?;
    Ok(proto.into())
}

pub fn str_enum<E: TryFrom<i32> + ToString>(e: i32) -> String {
    e.try_into()
        .map(|e: E| e.to_string())
        .unwrap_or("unknown".to_string())
}

include!(concat!(env!("OUT_DIR"), "/protocol.rs"));

pub struct Data {
    pub data: Bytes,
    pub socket_addr: Option<std::net::SocketAddr>,
}

impl Display for Protocol {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Protocol::Http => write!(f, "http"),
            Protocol::Https => write!(f, "https"),
            Protocol::Tcp => write!(f, "tcp"),
            Protocol::Udp => write!(f, "udp"),
            Protocol::OneC => write!(f, "1c"),
            Protocol::Minecraft => write!(f, "minecraft"),
            Protocol::Webdav => write!(f, "webdav"),
            Protocol::Rtsp => write!(f, "rtsp"),
            Protocol::Rdp => write!(f, "rdp"),
            Protocol::Vnc => write!(f, "vnc"),
            Protocol::Ssh => write!(f, "ssh"),
        }
    }
}

impl FromStr for Protocol {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "http" => Ok(Protocol::Http),
            "https" => Ok(Protocol::Https),
            "tcp" => Ok(Protocol::Tcp),
            "udp" => Ok(Protocol::Udp),
            "1c" => Ok(Protocol::OneC),
            "minecraft" => Ok(Protocol::Minecraft),
            "webdav" => Ok(Protocol::Webdav),
            "rtsp" => Ok(Protocol::Rtsp),
            "rdp" => Ok(Protocol::Rdp),
            "vnc" => Ok(Protocol::Vnc),
            "ssh" => Ok(Protocol::Ssh),
            _ => bail!("Invalid protocol: {}", s),
        }
    }
}

impl DefaultPort for Protocol {
    fn default_port(&self) -> Option<u16> {
        match self {
            Protocol::Http => Some(80),
            Protocol::Https => Some(443),
            Protocol::Tcp => None,
            Protocol::Udp => None,
            Protocol::OneC => None,
            Protocol::Minecraft => Some(25565),
            Protocol::Webdav => None,
            Protocol::Rtsp => Some(554),
            Protocol::Rdp => Some(3389),
            Protocol::Vnc => Some(5900),
            Protocol::Ssh => Some(22),
        }
    }
}

impl FromStr for Role {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "none" => Ok(Role::Nobody),
            "admin" => Ok(Role::Admin),
            "reader" => Ok(Role::Reader),
            "writer" => Ok(Role::Writer),
            _ => bail!("Invalid access: {}", s),
        }
    }
}

impl Display for Role {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Role::Nobody => write!(f, "none"),
            Role::Admin => write!(f, "admin"),
            Role::Reader => write!(f, "reader"),
            Role::Writer => write!(f, "writer"),
        }
    }
}

impl FromStr for Auth {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "none" => Ok(Auth::None),
            "basic" => Ok(Auth::Basic),
            "form" => Ok(Auth::Form),
            _ => bail!("Invalid auth: {}", s),
        }
    }
}

impl Display for Auth {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Auth::None => write!(f, "none"),
            Auth::Basic => write!(f, "basic"),
            Auth::Form => write!(f, "form"),
        }
    }
}

impl FromStr for ProxyProtocol {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "none" => Ok(ProxyProtocol::None),
            "v2" => Ok(ProxyProtocol::V2),
            _ => bail!("Invalid proxy protocol: {}", s),
        }
    }
}

impl Display for ProxyProtocol {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            ProxyProtocol::None => write!(f, "none"),
            ProxyProtocol::V2 => write!(f, "v2"),
        }
    }
}

impl FromStr for FilterAction {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "allow" => Ok(FilterAction::FilterAllow),
            "deny" => Ok(FilterAction::FilterDeny),
            "redirect" => Ok(FilterAction::FilterRedirect),
            "log" => Ok(FilterAction::FilterLog),
            _ => bail!("Invalid filter action: {}", s),
        }
    }
}

impl PartialEq for ClientEndpoint {
    fn eq(&self, other: &Self) -> bool {
        self.local_proto == other.local_proto
            && self.local_addr == other.local_addr
            && self.local_port == other.local_port
    }
}

impl Display for ClientEndpoint {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        if let Some(name) = self.description.as_ref() {
            if !name.is_empty() {
                write!(f, "[{}] ", name)?;
            }
        }
        write!(f, "{}", self.as_url())
    }
}

impl Endpoint for ClientEndpoint {
    fn credentials(&self) -> String {
        let mut s = String::new();
        if !self.username.is_empty() {
            s.push_str(&encode(&self.username));
        }
        if !self.password.is_empty() {
            s.push(':');
            s.push_str(&encode(&self.password));
        }
        if !s.is_empty() {
            s.push('@');
        }
        s
    }

    fn as_url(&self) -> String {
        match self.local_proto.try_into().unwrap() {
            Protocol::OneC | Protocol::Minecraft | Protocol::Webdav => {
                let credentials = self.credentials();
                format!(
                    "{}://{}{}",
                    str_enum::<Protocol>(self.local_proto),
                    credentials,
                    &self.local_addr
                )
            }
            Protocol::Http
            | Protocol::Https
            | Protocol::Tcp
            | Protocol::Udp
            | Protocol::Rtsp
            | Protocol::Rdp
            | Protocol::Vnc
            | Protocol::Ssh => {
                let credentials = self.credentials();
                format!(
                    "{}://{}{}:{}{}",
                    str_enum::<Protocol>(self.local_proto),
                    credentials,
                    self.local_addr,
                    self.local_port,
                    self.local_path
                )
            }
        }
    }
}

impl Endpoint for ServerEndpoint {
    fn credentials(&self) -> String {
        String::new()
    }

    fn as_url(&self) -> String {
        format!(
            "{}://{}:{}",
            str_enum::<Protocol>(self.remote_proto),
            self.remote_addr,
            self.remote_port,
        )
    }
}

impl Display for ServerEndpoint {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        let client = self.client.as_ref().unwrap();
        if self.error.is_empty() {
            write!(
                f,
                "{} -> {}://{}{}:{}{}",
                client,
                Protocol::try_from(self.remote_proto).unwrap(),
                client.credentials(),
                self.remote_addr,
                self.remote_port,
                client.local_path
            )
        } else {
            write!(f, "{} -> {}", client, self.error)
        }
    }
}

impl AgentInfo {
    pub fn is_support_server_control(&self) -> bool {
        get_version_number(&self.version) >= get_version_number("2.1.1")
    }

    pub fn is_support_backpressure(&self) -> bool {
        get_version_number(&self.version) >= get_version_number("2.2.0")
    }

    pub fn get_unique_id(&self) -> String {
        if self.hwid.is_empty() {
            self.agent_id.clone()
        } else {
            self.hwid.clone()
        }
    }
}

impl PartialEq for ServerEndpoint {
    fn eq(&self, other: &Self) -> bool {
        self.client == other.client
    }
}

// New Protocol Buffers message reading and writing functions
pub async fn read_message<T: AsyncRead + Unpin>(conn: &mut T) -> Result<message::Message> {
    let mut buf = [0u8; std::mem::size_of::<u32>()];
    conn.read_exact(&mut buf).await?;
    let len = u32::from_le_bytes(buf) as usize;
    if !(1..=1024 * 1024 * 10).contains(&len) {
        bail!("Invalid message length: {}", len);
    }
    let mut buf = vec![0u8; len];
    conn.read_exact(&mut buf).await?;

    let proto_msg =
        Message::decode(buf.as_slice()).context("Failed to decode Protocol Buffers message")?;

    Ok(proto_msg.message.unwrap())
}

pub async fn write_message<T: AsyncWrite + Unpin>(
    conn: &mut T,
    msg: &message::Message,
) -> Result<()> {
    let proto_msg = Message {
        message: Some(msg.clone()),
    };

    debug!("Sending proto message: {:?}", msg);

    let mut buf = BytesMut::new();
    proto_msg
        .encode(&mut buf)
        .context("Failed to encode Protocol Buffers message")?;

    let len = buf.len() as u32;
    let len_bytes = len.to_le_bytes();

    conn.write_all(&len_bytes).await?;
    conn.write_all(&buf).await?;
    conn.flush().await?;
    Ok(())
}

impl FairGroup for message::Message {
    fn group_id(&self) -> Option<u32> {
        match self {
            message::Message::DataChannelData(lhs) => Some(lhs.channel_id),
            message::Message::DataChannelDataUdp(lhs) => Some(lhs.channel_id),
            message::Message::DataChannelEof(lhs) => Some(lhs.channel_id),
            message::Message::DataChannelAck(lhs) => Some(lhs.channel_id),
            _ => None,
        }
    }

    fn get_size(&self) -> Option<usize> {
        match self {
            message::Message::DataChannelData(data) => Some(data.data.len()),
            message::Message::DataChannelDataUdp(data) => Some(data.data.len()),
            _ => None, // Control messages have no size
        }
    }
}

pub type UdpPacketLen = u16; // `u16` should be enough for any practical UDP traffic on the Internet
                             //
#[derive(Deserialize, Serialize, Debug)]
pub struct UdpHeader {
    from: std::net::SocketAddr,
    len: UdpPacketLen,
}

#[derive(Debug)]
pub struct UdpTraffic {
    pub from: std::net::SocketAddr,
    pub data: Bytes,
}

impl UdpTraffic {
    pub async fn write<T: AsyncWrite + Unpin>(&self, writer: &mut T) -> Result<()> {
        let hdr = UdpHeader {
            from: self.from,
            len: self.data.len() as UdpPacketLen,
        };

        let v = bincode::serde::encode_to_vec(&hdr, bincode::config::legacy()).unwrap();

        trace!("Write {:?} of length {}", hdr, v.len());
        writer.write_u8(v.len() as u8).await?;
        writer.write_all(&v).await?;

        writer.write_all(&self.data).await?;

        Ok(())
    }

    #[allow(dead_code)]
    pub async fn write_slice<T: AsyncWrite + Unpin>(
        writer: &mut T,
        from: std::net::SocketAddr,
        data: &[u8],
    ) -> Result<()> {
        let hdr = UdpHeader {
            from,
            len: data.len() as UdpPacketLen,
        };

        let v = bincode::serde::encode_to_vec(&hdr, bincode::config::legacy()).unwrap();

        trace!("Write {:?} of length {}", hdr, v.len());
        writer.write_u8(v.len() as u8).await?;
        writer.write_all(&v).await?;

        writer.write_all(data).await?;

        Ok(())
    }

    pub async fn read<T: AsyncRead + Unpin>(reader: &mut T, hdr_len: u8) -> Result<UdpTraffic> {
        let mut buf = vec![0; hdr_len as usize];
        reader
            .read_exact(&mut buf)
            .await
            .with_context(|| "Failed to read udp header")?;

        let hdr: UdpHeader = bincode::serde::decode_from_slice(&buf, bincode::config::legacy())
            .with_context(|| "Failed to deserialize UdpHeader")?
            .0;

        trace!("hdr {:?}", hdr);

        let mut data = BytesMut::new();
        data.resize(hdr.len as usize, 0);
        reader.read_exact(&mut data).await?;

        Ok(UdpTraffic {
            from: hdr.from,
            data: data.freeze(),
        })
    }
}