fix: no longer need to store whole cliargs

This commit is contained in:
əlemi 2023-05-09 02:14:24 +02:00
parent c200a0c3a8
commit 5268db4833
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 8 additions and 6 deletions

View file

@ -84,8 +84,10 @@ impl JoinAttempt {
pub struct AppState {
store: Arc<Mutex<HashMap<Uuid, JoinAttempt>>>,
db: DatabaseConnection,
cfg: CliArgs,
secret: String,
token_duration: u32,
time_window: u32,
fallback: bool,
}
#[tokio::main]
@ -126,7 +128,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// CUSTOM
.route("/register/unmigrated", post(register_unmigrated))
.fallback(fallback_route)
.with_state(AppState { store, db, cfg, secret });
.with_state(AppState { store, db, token_duration, time_window, fallback, secret });
info!(target: "MAIN", "serving Yggdrasil on {}", &addr);

View file

@ -14,7 +14,7 @@ pub async fn validate(State(state): State<AppState>, Json(payload): Json<proto::
).one(&state.db).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
if let Some(t) = token {
if Utc::now() - t.created_at > Duration::seconds(state.cfg.token_duration as i64) {
if Utc::now() - t.created_at > Duration::seconds(state.token_duration as i64) {
warn!(target: "AUTH", "[VALIDATE] expired token!");
return Err(StatusCode::UNAUTHORIZED);
}

View file

@ -24,7 +24,7 @@ pub async fn join(State(state): State<AppState>, Json(payload): Json<proto::Join
if tokens.iter().any(|x| {
x.access_token == payload.accessToken
&& Utc::now() - x.created_at < Duration::seconds(state.cfg.token_duration as i64)
&& Utc::now() - x.created_at < Duration::seconds(state.token_duration as i64)
}) {
state.store.lock().await.insert(payload.selectedProfile, JoinAttempt::new(payload.serverId.clone()));
info!(target: "SESSION", "[JOIN] user {} has joined server {}", payload.selectedProfile, payload.serverId);
@ -43,7 +43,7 @@ pub async fn has_joined_wrapper(State(state): State<AppState>, Query(query): Que
match has_joined_local(&state, username, server_id, user_ip).await {
Ok(r) => Ok(r),
Err(e) => {
if state.cfg.fallback {
if state.fallback {
Ok(has_joined_microsoft(&state, username, server_id, user_ip).await?)
} else {
Err(e)
@ -82,7 +82,7 @@ pub async fn has_joined_local(state: &AppState, username: &String, server_id: &S
match state.store.lock().await.get(&user.uuid) {
Some(join) => {
if Utc::now() - join.time < Duration::seconds(state.cfg.time_window as i64)
if Utc::now() - join.time < Duration::seconds(state.time_window as i64)
&& join.server.to_lowercase() == server_id.to_lowercase() {
let response = proto::JoinResponse {
id: user.uuid.simple().to_string(),