274 lines
11 KiB
JavaScript
274 lines
11 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.steamClient = void 0;
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const os_1 = __importDefault(require("os"));
|
|
const simple_vdf_1 = require("simple-vdf");
|
|
const chokidar_1 = __importDefault(require("chokidar"));
|
|
class SteamClientService {
|
|
steamPath = null;
|
|
onAccountsChanged = null;
|
|
constructor() {
|
|
this.detectSteamPath();
|
|
}
|
|
detectSteamPath() {
|
|
const platform = os_1.default.platform();
|
|
const home = os_1.default.homedir();
|
|
if (platform === 'win32') {
|
|
const possiblePaths = [
|
|
'C:\\Program Files (x86)\\Steam',
|
|
'C:\\Program Files\\Steam',
|
|
path_1.default.join(process.env.APPDATA || '', 'Steam'),
|
|
];
|
|
this.steamPath = possiblePaths.find(p => fs_1.default.existsSync(p)) || null;
|
|
}
|
|
else if (platform === 'linux') {
|
|
const possiblePaths = [
|
|
path_1.default.join(home, '.steam/steam'),
|
|
path_1.default.join(home, '.local/share/Steam'),
|
|
path_1.default.join(home, '.var/app/com.valvesoftware.Steam/.steam/steam'), // Flatpak
|
|
path_1.default.join(home, 'snap/steam/common/.steam/steam'), // Snap
|
|
];
|
|
this.steamPath = possiblePaths.find(p => fs_1.default.existsSync(p)) || null;
|
|
}
|
|
if (this.steamPath) {
|
|
console.log(`[SteamClient] Detected Steam path: ${this.steamPath}`);
|
|
}
|
|
}
|
|
getLoginUsersPath() {
|
|
if (!this.steamPath)
|
|
return null;
|
|
return path_1.default.join(this.steamPath, 'config', 'loginusers.vdf');
|
|
}
|
|
getConfigVdfPath() {
|
|
if (!this.steamPath)
|
|
return null;
|
|
return path_1.default.join(this.steamPath, 'config', 'config.vdf');
|
|
}
|
|
/**
|
|
* Safe Atomic Write: Writes to a temp file and renames it.
|
|
* This prevents file corruption if the app crashes during write.
|
|
*/
|
|
safeWriteVdf(filePath, data) {
|
|
const tempPath = `${filePath}.tmp_${Date.now()}`;
|
|
const dir = path_1.default.dirname(filePath);
|
|
try {
|
|
if (!fs_1.default.existsSync(dir))
|
|
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
const vdfContent = (0, simple_vdf_1.stringify)(data);
|
|
fs_1.default.writeFileSync(tempPath, vdfContent, 'utf-8');
|
|
// Atomic rename
|
|
fs_1.default.renameSync(tempPath, filePath);
|
|
}
|
|
catch (e) {
|
|
console.error(`[SteamClient] Atomic write failed for ${filePath}: ${e.message}`);
|
|
if (fs_1.default.existsSync(tempPath))
|
|
fs_1.default.unlinkSync(tempPath);
|
|
throw e;
|
|
}
|
|
}
|
|
startWatching(callback) {
|
|
this.onAccountsChanged = callback;
|
|
const loginUsersPath = this.getLoginUsersPath();
|
|
if (loginUsersPath && fs_1.default.existsSync(loginUsersPath)) {
|
|
this.readLocalAccounts();
|
|
chokidar_1.default.watch(loginUsersPath, { persistent: true, ignoreInitial: true }).on('change', () => {
|
|
console.log(`[SteamClient] loginusers.vdf changed, re-scanning...`);
|
|
this.readLocalAccounts();
|
|
});
|
|
}
|
|
}
|
|
readLocalAccounts() {
|
|
const filePath = this.getLoginUsersPath();
|
|
if (!filePath || !fs_1.default.existsSync(filePath))
|
|
return;
|
|
try {
|
|
const content = fs_1.default.readFileSync(filePath, 'utf-8');
|
|
if (!content.trim())
|
|
return; // Empty file
|
|
const data = (0, simple_vdf_1.parse)(content);
|
|
if (!data || !data.users)
|
|
return;
|
|
const accounts = [];
|
|
for (const [steamId64, userData] of Object.entries(data.users)) {
|
|
const user = userData;
|
|
if (!user || !user.AccountName)
|
|
continue;
|
|
accounts.push({
|
|
steamId: steamId64,
|
|
accountName: user.AccountName,
|
|
personaName: user.PersonaName || user.AccountName,
|
|
timestamp: parseInt(user.Timestamp) || 0
|
|
});
|
|
}
|
|
if (this.onAccountsChanged)
|
|
this.onAccountsChanged(accounts);
|
|
}
|
|
catch (error) {
|
|
console.error('[SteamClient] Error parsing loginusers.vdf:', error);
|
|
}
|
|
}
|
|
extractAccountConfig(accountName) {
|
|
const configPath = this.getConfigVdfPath();
|
|
if (!configPath || !fs_1.default.existsSync(configPath))
|
|
return null;
|
|
try {
|
|
const content = fs_1.default.readFileSync(configPath, 'utf-8');
|
|
const data = (0, simple_vdf_1.parse)(content);
|
|
const accounts = data?.InstallConfigStore?.Software?.Valve?.Steam?.Accounts;
|
|
return (accounts && accounts[accountName]) ? accounts[accountName] : null;
|
|
}
|
|
catch (e) {
|
|
console.error('[SteamClient] Failed to extract config.vdf data');
|
|
return null;
|
|
}
|
|
}
|
|
injectAccountConfig(accountName, accountData) {
|
|
const configPath = this.getConfigVdfPath();
|
|
if (!configPath)
|
|
return;
|
|
let data = {
|
|
InstallConfigStore: {
|
|
Software: {
|
|
Valve: {
|
|
Steam: {
|
|
Accounts: {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
if (fs_1.default.existsSync(configPath)) {
|
|
try {
|
|
const content = fs_1.default.readFileSync(configPath, 'utf-8');
|
|
const parsed = (0, simple_vdf_1.parse)(content);
|
|
if (parsed && typeof parsed === 'object')
|
|
data = parsed;
|
|
}
|
|
catch (e) { }
|
|
}
|
|
// Ensure safe nesting
|
|
if (!data.InstallConfigStore)
|
|
data.InstallConfigStore = {};
|
|
if (!data.InstallConfigStore.Software)
|
|
data.InstallConfigStore.Software = {};
|
|
if (!data.InstallConfigStore.Software.Valve)
|
|
data.InstallConfigStore.Software.Valve = {};
|
|
if (!data.InstallConfigStore.Software.Valve.Steam)
|
|
data.InstallConfigStore.Software.Valve.Steam = {};
|
|
if (!data.InstallConfigStore.Software.Valve.Steam.Accounts)
|
|
data.InstallConfigStore.Software.Valve.Steam.Accounts = {};
|
|
data.InstallConfigStore.Software.Valve.Steam.Accounts[accountName] = accountData;
|
|
try {
|
|
this.safeWriteVdf(configPath, data);
|
|
console.log(`[SteamClient] Safely injected session for ${accountName}`);
|
|
}
|
|
catch (e) { }
|
|
}
|
|
async setAutoLoginUser(accountName, accountConfig, steamId) {
|
|
const platform = os_1.default.platform();
|
|
const loginUsersPath = this.getLoginUsersPath();
|
|
if (loginUsersPath) {
|
|
let data = { users: {} };
|
|
if (fs_1.default.existsSync(loginUsersPath)) {
|
|
try {
|
|
const content = fs_1.default.readFileSync(loginUsersPath, 'utf-8');
|
|
const parsed = (0, simple_vdf_1.parse)(content);
|
|
if (parsed && parsed.users)
|
|
data = parsed;
|
|
}
|
|
catch (e) { }
|
|
}
|
|
if (!data.users)
|
|
data.users = {};
|
|
let found = false;
|
|
for (const [id, user] of Object.entries(data.users)) {
|
|
const u = user;
|
|
if (u.AccountName?.toLowerCase() === accountName.toLowerCase()) {
|
|
u.mostrecent = "1";
|
|
u.RememberPassword = "1";
|
|
u.AllowAutoLogin = "1";
|
|
u.WantsOfflineMode = "0";
|
|
u.SkipOfflineModeWarning = "1";
|
|
u.WasNonInteractive = "0";
|
|
found = true;
|
|
}
|
|
else {
|
|
u.mostrecent = "0";
|
|
}
|
|
}
|
|
if (!found && steamId && accountName) {
|
|
console.log(`[SteamClient] Provisioning new user profile for ${accountName}`);
|
|
data.users[steamId] = {
|
|
AccountName: accountName,
|
|
PersonaName: accountName,
|
|
RememberPassword: "1",
|
|
mostrecent: "1",
|
|
AllowAutoLogin: "1",
|
|
WantsOfflineMode: "0",
|
|
SkipOfflineModeWarning: "1",
|
|
WasNonInteractive: "0",
|
|
Timestamp: Math.floor(Date.now() / 1000).toString()
|
|
};
|
|
}
|
|
try {
|
|
this.safeWriteVdf(loginUsersPath, data);
|
|
}
|
|
catch (e) { }
|
|
}
|
|
if (accountConfig && accountName) {
|
|
this.injectAccountConfig(accountName, accountConfig);
|
|
}
|
|
// --- Linux Registry / Registry.vdf Hardening ---
|
|
if (platform === 'linux') {
|
|
const regLocations = [
|
|
path_1.default.join(os_1.default.homedir(), '.steam', 'registry.vdf'),
|
|
path_1.default.join(os_1.default.homedir(), '.steam', 'steam', 'registry.vdf')
|
|
];
|
|
for (const regPath of regLocations) {
|
|
if (!fs_1.default.existsSync(path_1.default.dirname(regPath)))
|
|
continue;
|
|
let regData = { Registry: { HKCU: { Software: { Valve: { Steam: {
|
|
AutoLoginUser: "",
|
|
RememberPassword: "1",
|
|
AlreadyLoggedIn: "1"
|
|
} } } } } };
|
|
if (fs_1.default.existsSync(regPath)) {
|
|
try {
|
|
const content = fs_1.default.readFileSync(regPath, 'utf-8');
|
|
const parsed = (0, simple_vdf_1.parse)(content);
|
|
if (parsed && typeof parsed === 'object')
|
|
regData = parsed;
|
|
}
|
|
catch (e) { }
|
|
}
|
|
// Deep merge helper
|
|
const ensurePath = (obj, keys) => {
|
|
let curr = obj;
|
|
for (const key of keys) {
|
|
if (!curr[key] || typeof curr[key] !== 'object')
|
|
curr[key] = {};
|
|
curr = curr[key];
|
|
}
|
|
return curr;
|
|
};
|
|
const steamKey = ensurePath(regData, ['Registry', 'HKCU', 'Software', 'Valve', 'Steam']);
|
|
steamKey.AutoLoginUser = accountName;
|
|
steamKey.RememberPassword = "1";
|
|
steamKey.AlreadyLoggedIn = "1";
|
|
steamKey.WantsOfflineMode = "0";
|
|
try {
|
|
this.safeWriteVdf(regPath, regData);
|
|
}
|
|
catch (e) { }
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
exports.steamClient = new SteamClientService();
|