1pub mod config;
2mod connection;
3pub mod crypto;
4pub mod error;
5pub mod event;
6pub mod server;
7pub mod user;
8use crate::crypto::{generate_aes_encryption_token, generate_blowfish_encryption_token, RsaCrypto};
9use crate::error::CommonError;
10use crate::event::{LogEvent, LogEventLevel};
11use chrono::{DateTime, Local, NaiveDateTime, NaiveTime};
12pub use connection::*;
13pub use ppaass_protocol::*;
14use rand::random;
15use std::borrow::Cow;
16use std::net::{SocketAddr, ToSocketAddrs};
17use std::path::Path;
18use std::str::FromStr;
19use tokio::sync::mpsc::Sender;
20use tracing::{error, Level};
21use tracing_appender::non_blocking::WorkerGuard;
22use tracing_subscriber::fmt::time::ChronoUtc;
23use uuid::Uuid;
24#[inline(always)]
26pub fn generate_uuid() -> String {
27 Uuid::new_v4().to_string().replace("-", "").to_uppercase()
28}
29
30#[inline(always)]
32pub fn random_generate_encryption() -> Encryption {
33 let random_number = random::<u64>();
34 if random_number % 2 == 0 {
35 Encryption::Aes(generate_aes_encryption_token())
36 } else {
37 Encryption::Blowfish(generate_blowfish_encryption_token())
38 }
39}
40
41#[inline(always)]
42pub fn rsa_encrypt_encryption<'a>(
43 raw_encryption: &'a Encryption,
44 rsa_crypto: &RsaCrypto,
45) -> Result<Cow<'a, Encryption>, CommonError> {
46 match raw_encryption {
47 Encryption::Plain => Ok(Cow::Borrowed(raw_encryption)),
48 Encryption::Aes(token) => {
49 let encrypted_token = rsa_crypto.encrypt(&token)?;
50 Ok(Cow::Owned(Encryption::Aes(encrypted_token)))
51 }
52 Encryption::Blowfish(token) => {
53 let encrypted_token = rsa_crypto.encrypt(&token)?;
54 Ok(Cow::Owned(Encryption::Blowfish(encrypted_token)))
55 }
56 }
57}
58
59#[inline(always)]
60pub fn rsa_decrypt_encryption<'a>(
61 encrypted_encryption: &'a Encryption,
62 rsa_crypto: &RsaCrypto,
63) -> Result<Cow<'a, Encryption>, CommonError> {
64 match encrypted_encryption {
65 Encryption::Plain => Ok(Cow::Borrowed(encrypted_encryption)),
66 Encryption::Aes(token) => {
67 let decrypted_token = rsa_crypto.decrypt(&token)?;
68 Ok(Cow::Owned(Encryption::Aes(decrypted_token)))
69 }
70 Encryption::Blowfish(token) => {
71 let decrypted_token = rsa_crypto.decrypt(&token)?;
72 Ok(Cow::Owned(Encryption::Blowfish(decrypted_token)))
73 }
74 }
75}
76
77pub fn init_logger(
79 log_folder: &Path,
81 log_name_prefix: &str,
83 max_log_level: &str,
85) -> Result<WorkerGuard, CommonError> {
86 let (trace_file_appender, _trace_appender_guard) = tracing_appender::non_blocking(
87 tracing_appender::rolling::daily(log_folder, log_name_prefix),
88 );
89 tracing_subscriber::fmt()
90 .with_max_level(Level::from_str(max_log_level)?)
91 .with_writer(trace_file_appender)
92 .with_line_number(true)
93 .with_level(true)
94 .with_thread_ids(true)
95 .with_thread_names(true)
96 .with_timer(ChronoUtc::rfc_3339())
97 .with_ansi(false)
98 .init();
99 Ok(_trace_appender_guard)
100}
101
102#[inline(always)]
103pub fn parse_to_socket_addresses<I, T>(addresses: I) -> Result<Vec<SocketAddr>, CommonError>
104where
105 I: Iterator<Item = T>,
106 T: AsRef<str>,
107{
108 let proxy_addresses = addresses
109 .into_iter()
110 .filter_map(|addr| addr.as_ref().to_socket_addrs().ok())
111 .flatten()
112 .collect::<Vec<SocketAddr>>();
113 Ok(proxy_addresses)
114}
115
116pub async fn publish_server_log_event(
117 log_event_sender: &Sender<LogEvent>,
118 log_event_level: LogEventLevel,
119 message: String,
120) {
121 if let Err(e) = log_event_sender
122 .send(LogEvent {
123 level: log_event_level,
124 timestamp: Local::now(),
125 message,
126 })
127 .await
128 {
129 error!("Fail to send log event: {e:?}");
130 }
131}