ppaass_v3_protocol/
message.rs

1use crate::UnifiedAddress;
2use bytes::Bytes;
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5/// The encryption in Handshake message used to
6/// switch the encryption key
7#[derive(Debug, Serialize, Deserialize, Clone)]
8pub enum Encryption {
9    /// The data will send in plain
10    Plain,
11    /// The data will send with aes encryption
12    Aes(#[serde(with = "crate::hex")] Bytes),
13    /// The data will send with blowfish encryption
14    Blowfish(#[serde(with = "crate::hex")] Bytes),
15}
16
17/// The handshake message between agent and proxy.
18/// When the tcp connection created between agent and proxy,
19/// the handshake will happen as the first message used to
20/// communicate the authentication information and exchange
21/// the encryption key.
22///
23/// The **encryption key** is encrypted with **RSA private key**
24/// which assigned to each user. The other side should decrypt
25/// the encryption key with **RSA public key** to raw key.
26#[derive(Debug, Serialize, Deserialize, Clone)]
27pub struct HandshakeRequest {
28    /// The authentication information, usually it should be a JWT or
29    /// a username, or even username&password with some kind of format
30    pub authentication: String,
31    /// The encryption used to carry the **encryption key**
32    pub encryption: Encryption,
33}
34
35/// The handshake response, exchange the proxy side encryption
36/// to agent
37#[derive(Debug, Serialize, Deserialize, Clone)]
38pub struct HandshakeResponse {
39    /// The encryption used to carry the **encryption key**
40    pub encryption: Encryption,
41}
42
43#[derive(Debug, Serialize, Deserialize, Clone)]
44pub enum TunnelControlRequest {
45    Heartbeat(HeartbeatRequest),
46    TunnelInit(TunnelInitRequest),
47}
48
49#[derive(Debug, Serialize, Deserialize, Clone)]
50pub enum TunnelControlResponse {
51    Heartbeat(HeartbeatResponse),
52    TunnelInit(TunnelInitResponse),
53}
54
55/// The tcp destination initialize message used to initialize the destination
56#[derive(Debug, Serialize, Deserialize, Clone)]
57pub struct TunnelInitRequest {
58    /// The destination that the destination is going to connect
59    pub destination_address: UnifiedAddress,
60    /// If the destination should keep alive
61    pub keep_alive: bool,
62}
63
64/// The failure reason for destination init
65#[derive(Debug, Serialize, Deserialize, Clone)]
66pub enum TunnelInitFailureReason {
67    /// Authenticate the user fail
68    AuthenticateFail,
69    /// Initialize destination with destination fail
70    InitWithDestinationFail,
71}
72
73/// The tcp destination initialize message used to initialize the destination
74#[derive(Debug, Serialize, Deserialize, Clone)]
75pub enum TunnelInitResponse {
76    Success,
77    Failure(TunnelInitFailureReason),
78}
79
80#[derive(Debug, Serialize, Deserialize, Clone)]
81pub struct HeartbeatRequest {
82    request_date_time: DateTime<Utc>,
83}
84
85impl HeartbeatRequest {
86    pub fn new() -> Self {
87        Self {
88            request_date_time: Utc::now(),
89        }
90    }
91
92    pub fn request_date_time(&self) -> &DateTime<Utc> {
93        &self.request_date_time
94    }
95}
96
97#[derive(Debug, Serialize, Deserialize, Clone)]
98pub struct HeartbeatResponse {
99    response_date_time: DateTime<Utc>,
100}
101
102impl HeartbeatResponse {
103    pub fn new() -> Self {
104        Self {
105            response_date_time: Utc::now(),
106        }
107    }
108
109    pub fn response_date_time(&self) -> &DateTime<Utc> {
110        &self.response_date_time
111    }
112}