fix: implement granular session health detection with SteamAuthError and smart conditional replacement logic

This commit is contained in:
2026-02-21 04:54:36 +01:00
parent 559c7bfdef
commit 2ef8dd06e7
2 changed files with 29 additions and 19 deletions

View File

@@ -7,7 +7,7 @@ import axios from 'axios';
import fs from 'fs'; import fs from 'fs';
import { pathToFileURL } from 'url'; import { pathToFileURL } from 'url';
import { fetchProfileData, scrapeBanStatus } from './services/steam-web'; import { fetchProfileData, scrapeBanStatus } from './services/steam-web';
import { scrapeCooldown } from './services/scraper'; import { scrapeCooldown, SteamAuthError } from './services/scraper';
import { steamClient, LocalSteamAccount } from './services/steam-client'; import { steamClient, LocalSteamAccount } from './services/steam-client';
import { BackendService } from './services/backend'; import { BackendService } from './services/backend';
@@ -247,7 +247,12 @@ const scrapeAccountData = async (account: Account) => {
if (backend) await backend.pushCooldown(account.steamId, undefined, now.toISOString()); if (backend) await backend.pushCooldown(account.steamId, undefined, now.toISOString());
} }
} catch (e: any) { } catch (e: any) {
if (e.message.includes('cookie') || e.message.includes('Sign In')) account.authError = true; if (e instanceof SteamAuthError) {
account.authError = true;
} else {
console.error(`[Scraper] Temporary error for ${account.personaName}: ${e.message}`);
}
}
} }
} }
if (backend && !account._id.startsWith('shared_')) { if (backend && !account._id.startsWith('shared_')) {

View File

@@ -6,6 +6,14 @@ export interface CooldownData {
expiresAt?: Date; expiresAt?: Date;
} }
// Custom error to identify session death
export class SteamAuthError extends Error {
constructor(message: string) {
super(message);
this.name = "SteamAuthError";
}
}
export const scrapeCooldown = async (steamId: string, steamLoginSecure: string): Promise<CooldownData> => { export const scrapeCooldown = async (steamId: string, steamLoginSecure: string): Promise<CooldownData> => {
const url = `https://steamcommunity.com/profiles/${steamId}/gcpd/730?tab=matchmaking`; const url = `https://steamcommunity.com/profiles/${steamId}/gcpd/730?tab=matchmaking`;
@@ -15,16 +23,21 @@ export const scrapeCooldown = async (steamId: string, steamLoginSecure: string):
'Cookie': steamLoginSecure, 'Cookie': steamLoginSecure,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}, },
timeout: 10000 timeout: 10000,
validateStatus: (status) => status < 500 // Allow redirects to handle them manually
}); });
// If Steam redirects us to the login page, the cookie is dead
if (response.data.includes('Sign In') || response.request.path.includes('/login')) {
throw new SteamAuthError('Invalid or expired steamLoginSecure cookie');
}
const $ = cheerio.load(response.data); const $ = cheerio.load(response.data);
if (response.data.includes('Sign In') || !response.data.includes('Personal Game Data')) { if (!response.data.includes('Personal Game Data')) {
throw new Error('Invalid or expired steamLoginSecure cookie'); throw new SteamAuthError('Session invalid: Personal Game Data not accessible');
} }
// 1. Locate the specific table containing cooldown info
let expirationDate: Date | undefined = undefined; let expirationDate: Date | undefined = undefined;
$('table').each((_, table) => { $('table').each((_, table) => {
@@ -36,15 +49,10 @@ export const scrapeCooldown = async (steamId: string, steamLoginSecure: string):
rows.each((_, row) => { rows.each((_, row) => {
const dateText = $(row).find('td').eq(expirationIndex).text().trim(); const dateText = $(row).find('td').eq(expirationIndex).text().trim();
if (dateText && dateText !== '') { if (dateText && dateText !== '') {
// Steam uses 'GMT' which some JS engines don't parse well, replace with 'UTC'
const cleanDateText = dateText.replace(' GMT', ' UTC'); const cleanDateText = dateText.replace(' GMT', ' UTC');
const parsed = new Date(cleanDateText); const parsed = new Date(cleanDateText);
if (!isNaN(parsed.getTime())) { if (!isNaN(parsed.getTime())) {
// We want the newest expiration date found if (!expirationDate || parsed > (expirationDate as Date)) expirationDate = parsed;
if (!expirationDate || parsed > (expirationDate as Date)) {
expirationDate = parsed;
}
} }
} }
}); });
@@ -52,11 +60,7 @@ export const scrapeCooldown = async (steamId: string, steamLoginSecure: string):
}); });
if (expirationDate && (expirationDate as Date).getTime() > Date.now()) { if (expirationDate && (expirationDate as Date).getTime() > Date.now()) {
console.log(`[Scraper] Found active cooldown until: ${(expirationDate as Date).toISOString()}`); return { isActive: true, expiresAt: expirationDate };
return {
isActive: true,
expiresAt: expirationDate
};
} }
const content = $('#personal_game_data_content').text(); const content = $('#personal_game_data_content').text();
@@ -66,7 +70,8 @@ export const scrapeCooldown = async (steamId: string, steamLoginSecure: string):
return { isActive: false }; return { isActive: false };
} catch (error: any) { } catch (error: any) {
console.error(`[Scraper] Error for ${steamId}:`, error.message); if (error instanceof SteamAuthError) throw error;
throw error; console.error(`[Scraper] Network/Internal Error for ${steamId}:`, error.message);
throw error; // Generic errors don't trigger re-auth
} }
}; };