feat(core): run bounded real gateway sessions
This commit is contained in:
+60
-1
@@ -1,5 +1,6 @@
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::error::{CoreError, Result};
|
||||
|
||||
@@ -126,6 +127,47 @@ struct Timestamp {
|
||||
nanosecond: u32,
|
||||
}
|
||||
|
||||
pub(crate) fn now_utc() -> Result<String> {
|
||||
system_time_utc(SystemTime::now())
|
||||
}
|
||||
|
||||
fn system_time_utc(now: SystemTime) -> Result<String> {
|
||||
let seconds = now
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| CoreError::InvalidArgument)?
|
||||
.as_secs();
|
||||
let days = seconds / 86_400;
|
||||
let day_seconds = seconds % 86_400;
|
||||
let shifted = days
|
||||
.checked_add(719_468)
|
||||
.ok_or(CoreError::InvalidArgument)?;
|
||||
let era = shifted / 146_097;
|
||||
let day_of_era = shifted % 146_097;
|
||||
let year_of_era =
|
||||
(day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
|
||||
let mut year = year_of_era + era * 400;
|
||||
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
|
||||
let month_prime = (5 * day_of_year + 2) / 153;
|
||||
let day = day_of_year - (153 * month_prime + 2) / 5 + 1;
|
||||
let month = if month_prime < 10 {
|
||||
month_prime + 3
|
||||
} else {
|
||||
month_prime - 9
|
||||
};
|
||||
if month <= 2 {
|
||||
year += 1;
|
||||
}
|
||||
if year > 9_999 {
|
||||
return Err(CoreError::InvalidArgument);
|
||||
}
|
||||
let hour = day_seconds / 3_600;
|
||||
let minute = (day_seconds % 3_600) / 60;
|
||||
let second = day_seconds % 60;
|
||||
Ok(format!(
|
||||
"{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z"
|
||||
))
|
||||
}
|
||||
|
||||
fn timestamp(value: &str, exact_seconds: bool) -> Option<Timestamp> {
|
||||
let bytes = value.as_bytes();
|
||||
if bytes.len() < 20
|
||||
@@ -452,6 +494,10 @@ impl ConnectionManifest {
|
||||
&self.gateway.public_identity
|
||||
}
|
||||
|
||||
pub(crate) fn features(&self) -> &[String] {
|
||||
&self.tunnel.features
|
||||
}
|
||||
|
||||
pub(crate) fn admission(
|
||||
&self,
|
||||
client_nonce: String,
|
||||
@@ -712,8 +758,21 @@ pub(crate) fn decode_stable_error(bytes: &[u8]) -> Result<DecodedStableError> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod stable_error_tests {
|
||||
use super::decode_stable_error;
|
||||
use super::{decode_stable_error, system_time_utc};
|
||||
use crate::error::CoreError;
|
||||
use std::time::{Duration, UNIX_EPOCH};
|
||||
|
||||
#[test]
|
||||
fn system_clock_conversion_is_exact_at_epoch_and_leap_day() {
|
||||
assert_eq!(
|
||||
system_time_utc(UNIX_EPOCH).as_deref(),
|
||||
Ok("1970-01-01T00:00:00Z")
|
||||
);
|
||||
assert_eq!(
|
||||
system_time_utc(UNIX_EPOCH + Duration::from_secs(1_709_251_199)).as_deref(),
|
||||
Ok("2024-02-29T23:59:59Z")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stable_error_uses_exact_rc5_bounds_and_preserves_retryability() {
|
||||
|
||||
Reference in New Issue
Block a user