fix: no longer need to store whole cliargs
This commit is contained in:
parent
c200a0c3a8
commit
5268db4833
3 changed files with 8 additions and 6 deletions
|
@ -84,8 +84,10 @@ impl JoinAttempt {
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
store: Arc<Mutex<HashMap<Uuid, JoinAttempt>>>,
|
store: Arc<Mutex<HashMap<Uuid, JoinAttempt>>>,
|
||||||
db: DatabaseConnection,
|
db: DatabaseConnection,
|
||||||
cfg: CliArgs,
|
|
||||||
secret: String,
|
secret: String,
|
||||||
|
token_duration: u32,
|
||||||
|
time_window: u32,
|
||||||
|
fallback: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -126,7 +128,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
// CUSTOM
|
// CUSTOM
|
||||||
.route("/register/unmigrated", post(register_unmigrated))
|
.route("/register/unmigrated", post(register_unmigrated))
|
||||||
.fallback(fallback_route)
|
.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);
|
info!(target: "MAIN", "serving Yggdrasil on {}", &addr);
|
||||||
|
|
||||||
|
|
|
@ -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)?;
|
).one(&state.db).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||||
|
|
||||||
if let Some(t) = token {
|
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!");
|
warn!(target: "AUTH", "[VALIDATE] expired token!");
|
||||||
return Err(StatusCode::UNAUTHORIZED);
|
return Err(StatusCode::UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ pub async fn join(State(state): State<AppState>, Json(payload): Json<proto::Join
|
||||||
|
|
||||||
if tokens.iter().any(|x| {
|
if tokens.iter().any(|x| {
|
||||||
x.access_token == payload.accessToken
|
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()));
|
state.store.lock().await.insert(payload.selectedProfile, JoinAttempt::new(payload.serverId.clone()));
|
||||||
info!(target: "SESSION", "[JOIN] user {} has joined server {}", payload.selectedProfile, payload.serverId);
|
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 {
|
match has_joined_local(&state, username, server_id, user_ip).await {
|
||||||
Ok(r) => Ok(r),
|
Ok(r) => Ok(r),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if state.cfg.fallback {
|
if state.fallback {
|
||||||
Ok(has_joined_microsoft(&state, username, server_id, user_ip).await?)
|
Ok(has_joined_microsoft(&state, username, server_id, user_ip).await?)
|
||||||
} else {
|
} else {
|
||||||
Err(e)
|
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) {
|
match state.store.lock().await.get(&user.uuid) {
|
||||||
Some(join) => {
|
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() {
|
&& join.server.to_lowercase() == server_id.to_lowercase() {
|
||||||
let response = proto::JoinResponse {
|
let response = proto::JoinResponse {
|
||||||
id: user.uuid.simple().to_string(),
|
id: user.uuid.simple().to_string(),
|
||||||
|
|
Loading…
Reference in a new issue