Skip to content

Commit

Permalink
Added Ability to put constant SessionKey
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Mar 31, 2024
1 parent 042eaa1 commit b87f3f5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ pub struct AppContext {
impl AppContext {
pub async fn new(settings_reader: SettingsReader) -> Self {
let connection_settings = settings_reader.get_connections_settings().await;

let token_secret_key = if let Some(session_key) = settings_reader.get_session_key().await {
AesKey::new(get_token_secret_key_from_settings(session_key.as_bytes()).as_slice())
} else {
AesKey::new(generate_random_token_secret_key().as_slice())
};

Self {
settings_reader,
http_connections: AtomicIsize::new(0),
id: AtomicI64::new(0),
connection_settings,
saved_client_certs: SavedClientCert::new(),
token_secret_key: AesKey::new(generate_token_secret_key().as_slice()),
token_secret_key,
client_certificates: ClientCertificatesCache::new(),
}
}
Expand All @@ -37,7 +44,7 @@ impl AppContext {
}
}

fn generate_token_secret_key() -> Vec<u8> {
fn generate_random_token_secret_key() -> Vec<u8> {
let mut result = Vec::with_capacity(48);

let mut key = vec![];
Expand All @@ -52,3 +59,19 @@ fn generate_token_secret_key() -> Vec<u8> {

result
}

fn get_token_secret_key_from_settings(session_key: &[u8]) -> Vec<u8> {
let mut result = Vec::with_capacity(48);

let mut key = vec![];

while result.len() < 48 {
if key.len() == 0 {
key = session_key.to_vec();
}

result.push(key.pop().unwrap());
}

result
}
1 change: 1 addition & 0 deletions src/settings/connections_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DEFAULT_CONNECT_TO_REMOTE_TIMEOUT: Duration = Duration::from_secs(5);
pub struct ConnectionsSettings {
pub buffer_size: Option<String>,
pub connect_to_remote_timeout: Option<String>,
pub session_key: Option<String>,
}

impl ConnectionsSettings {
Expand Down
12 changes: 12 additions & 0 deletions src/settings/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ impl SettingsReader {
result
}

pub async fn get_session_key(&self) -> Option<String> {
let read_access = self.settings.read().await;

if let Some(global_settings) = read_access.global_settings.as_ref() {
if let Some(connection_settings) = global_settings.connection_settings.as_ref() {
return connection_settings.session_key.clone();
}
}

None
}

pub async fn get_http_endpoint_modify_headers_settings(
&self,
endpoint_info: &HttpServerConnectionInfo,
Expand Down

0 comments on commit b87f3f5

Please sign in to comment.