Skip to content

Commit ed9fe61

Browse files
Auth cookie refresh on PC_EXIT
1 parent 55cf3f7 commit ed9fe61

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/PlayerManager.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ static void heartbeatPlayer(CNSocket* sock, CNPacketData* data) {
396396

397397
static void exitGame(CNSocket* sock, CNPacketData* data) {
398398
auto exitData = (sP_CL2FE_REQ_PC_EXIT*)data->buf;
399+
400+
// Refresh any auth cookie, in case "change character" was used
401+
Player* plr = getPlayer(sock);
402+
if (plr != nullptr) {
403+
// 5 seconds should be enough to log in again
404+
Database::refreshCookie(plr->accountId, 5);
405+
}
406+
399407
INITSTRUCT(sP_FE2CL_REP_PC_EXIT_SUCC, response);
400408

401409
response.iID = exitData->iID;

src/db/Database.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ namespace Database {
5656
// return true if cookie is valid for the account.
5757
// invalidates the stored cookie afterwards
5858
bool checkCookie(int accountId, const char *cookie);
59+
void refreshCookie(int accountId, int durationSec);
5960

6061
// interface for the /ban command
6162
bool banPlayer(int playerId, std::string& reason);

src/db/login.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,27 @@ bool Database::checkCookie(int accountId, const char *tryCookie) {
151151
return match;
152152
}
153153

154+
void Database::refreshCookie(int accountId, int durationSec) {
155+
std::lock_guard<std::mutex> lock(dbCrit);
156+
157+
const char* sql = R"(
158+
UPDATE Auth
159+
SET Expires = ?
160+
WHERE AccountID = ?;
161+
)";
162+
163+
int expires = getTimestamp() + durationSec;
164+
165+
sqlite3_stmt* stmt;
166+
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
167+
sqlite3_bind_int(stmt, 1, expires);
168+
sqlite3_bind_int(stmt, 2, accountId);
169+
int rc = sqlite3_step(stmt);
170+
sqlite3_finalize(stmt);
171+
if (rc != SQLITE_DONE)
172+
std::cout << "[WARN] Database fail on refreshCookie(): " << sqlite3_errmsg(db) << std::endl;
173+
}
174+
154175
void Database::updateSelected(int accountId, int slot) {
155176
std::lock_guard<std::mutex> lock(dbCrit);
156177

0 commit comments

Comments
 (0)