Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check server session when login/refresh and disconnect - fix #83 #84

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions app/server/src/modules/api/v2/account/ping.request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export const pingRequest: RequestType = {
);

// const account = await getAccountFromRequest(request);
const serverSessionByAccount = await System.db.get([
const { value: serverSession } = await System.db.get([
"serverSessionByAccount",
accountId,
]);

if (!serverSessionByAccount)
if (!serverSession)
return Response.json(
{ status: 403 },
{
Expand All @@ -34,12 +34,10 @@ export const pingRequest: RequestType = {

const ip = getIpFromRequest(request);

const serverSession = serverSessionByAccount.value;

if (
serverSession.ip !== ip ||
serverSession.server !== server ||
serverSession.ticketId !== ticketId
serverSession?.ip !== ip ||
serverSession?.server !== server ||
serverSession?.ticketId !== ticketId
)
return Response.json(
{ status: 403 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const claimSessionRequest: RequestType = {
expireIn: SERVER_SESSION_EXPIRE_TIME,
},
);
await System.sessions.checkAccountSession(account.accountId);

return Response.json(
{
Expand Down
1 change: 1 addition & 0 deletions app/server/src/system/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ export const System = (() => {
email: $email,
otp: $otp,
tasks: $tasks,
sessions: $sessions,
};
})();
105 changes: 66 additions & 39 deletions app/server/src/system/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,46 @@ import { getServerSessionList } from "shared/utils/main.ts";
export const sessions = () => {
const sessionMap: Record<string, any> = {};

const $checkSessions = async () => {
const sessionList = await getServerSessionList();
console.log(`Checking sessions... (${sessionList.length})`);
const $disconnectFromLastServer = (accountId: string, server: string) => {
const headers = new Headers();
headers.append("auth-server", performance.now() + "");

//check disconnected accounts
for (const accountId of Object.keys(sessionMap)) {
const foundSession = sessionList.find(({ key }) => key[1] === accountId);
console.error(`${server}/auth/user-disconnected?accountId=${accountId}`);
fetch(`${server}/auth/user-disconnected?accountId=${accountId}`, {
headers,
})
.then(async (response) => console.error(await response.json()))
.catch((e) => {
console.error(e);
//we don't really care if server receives our petition, the session is being invalidated anyway
});
};

const session = sessionMap[accountId];
const $disconnectFromLastServer = () => {
const headers = new Headers();
headers.append("auth-server", performance.now() + "");
const $checkSessions = async () => {
const currentSessions = Object.keys(sessionMap);
const targetSessions = (await getServerSessionList()).map<string>(
({ key: [, accountId] }) => accountId,
);
console.log(
`Checking sessions... (${currentSessions.length}..${targetSessions.length})`,
);

fetch(
`${session.server}/auth/user-disconnected?accountId=${accountId}`,
{
headers,
},
).catch(() => {
//we don't really care if server receives our petition, the session is being invalidated anyway
});
delete sessionMap[accountId];
};
const toDeleteSessions = currentSessions.filter(
(accountId) => !targetSessions.includes(accountId),
);

if (foundSession) {
const { sessionId, ticketId, server } = foundSession.value;
//check if session/server/ticket is still the same
if (
session.sessionId !== sessionId ||
session.ticketId !== ticketId ||
session.server !== server
)
$disconnectFromLastServer();
continue;
}
//account is disconnected
$disconnectFromLastServer();
//remove not active sessions
for (const accountId of toDeleteSessions) {
$disconnectFromLastServer(accountId, sessionMap[accountId].server);
delete sessionMap[accountId];
}

//update sessions
for (const {
key: [, accountId],
value: session,
} of sessionList)
sessionMap[accountId] = session;
const accountCheckList = [
...new Set([...currentSessions, ...targetSessions]),
].filter((accountId) => toDeleteSessions.includes(accountId));

//check accounts
for (const accountId of accountCheckList) checkAccountSession(accountId);
};

const load = () => {
Expand All @@ -61,7 +56,39 @@ export const sessions = () => {
$checkSessions();
};

const checkAccountSession = async (accountId: string) => {
const foundSession = await System.db.get([
"serverSessionByAccount",
accountId,
]);
const session = sessionMap[accountId];

console.warn(
accountId,
"->",
session?.server,
"->",
foundSession?.value?.server,
"<<<<",
);

const currentSession = foundSession.value;

//check if session server exists and changed if so disconnect from last server
if (
session &&
(session.sessionId !== currentSession.sessionId ||
session.ticketId !== currentSession.ticketId ||
session.server !== currentSession.server)
) {
$disconnectFromLastServer(accountId, session.server);
}
//reassign server session
sessionMap[accountId] = currentSession;
};

return {
load,
checkAccountSession,
};
};
Loading