From 00137fffe7a0f1b5f3a86222e780d46435105acb Mon Sep 17 00:00:00 2001 From: Alex Corn Date: Mon, 2 Sep 2024 21:59:10 -0400 Subject: [PATCH] Minor example script updates --- examples/basicbot.js | 10 +++-- examples/legacycdkeysdumper.js | 62 ++++++++++++++--------------- examples/lib/collect_credentials.js | 41 +++++++++++++++++++ 3 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 examples/lib/collect_credentials.js diff --git a/examples/basicbot.js b/examples/basicbot.js index 951d20ba..25ebd16e 100644 --- a/examples/basicbot.js +++ b/examples/basicbot.js @@ -7,9 +7,13 @@ const SteamUser = require('../index.js'); // Replace this with `require('steam-user');` if used outside of the module directory let client = new SteamUser(); -client.logOn({ - accountName: 'username', - password: 'password' +const collectCredentials = require('./lib/collect_credentials'); + +collectCredentials().then((credentials) => { + client.logOn({ + accountName: credentials.accountName, + password: credentials.password + }); }); client.on('loggedOn', function(details) { diff --git a/examples/legacycdkeysdumper.js b/examples/legacycdkeysdumper.js index de6d9615..7d181d62 100644 --- a/examples/legacycdkeysdumper.js +++ b/examples/legacycdkeysdumper.js @@ -1,10 +1,7 @@ -const SteamUser = require('steam-user'); -const fs = require('fs'); +const SteamUser = require('../index.js'); // Replace this with `require('steam-user');` if used outside of the module directory +const {writeFile} = require('fs/promises'); -const credentials = { - accountName: 'username', // your steam username - password: 'password', // your steam password -}; +const collectCredentials = require('./lib/collect_credentials'); const client = new SteamUser({ enablePicsCache: true, @@ -15,49 +12,52 @@ client.on('loggedOn', () => { }); client.on('ownershipCached', async () => { + // These keys in appdata indicate the presence of a legacy cd-key + const LEGACY_CD_KEY_INDICATORS = [ + 'hadthirdpartycdkey', + 'legacykeydisklocation', + 'legacykeyfromapp', + 'legacykeylinkedexternally', + 'legacykeyproofofpurchaseticket', + 'legacykeyregistrationmethod', + 'legacykeyregistrylocation', + 'showcdkeyinmenu', + 'showcdkeyonlaunch', + 'supportscdkeycopytoclipboard', + 'thirdpartycdkey', + ]; + let apps = client.getOwnedApps({ excludeExpiring: false, excludeFree: false, excludeShared: false }).filter(appid => { // these keys should indicate presence of a legacy cd key - return [ - 'hadthirdpartycdkey', - 'legacykeydisklocation', - 'legacykeyfromapp', - 'legacykeylinkedexternally', - 'legacykeyproofofpurchaseticket', - 'legacykeyregistrationmethod', - 'legacykeyregistrylocation', - 'showcdkeyinmenu', - 'showcdkeyonlaunch', - 'supportscdkeycopytoclipboard', - 'thirdpartycdkey', - ].some((key) => Object.keys((client.picsCache - && client.picsCache.apps[appid] - && client.picsCache.apps[appid].appinfo - && client.picsCache.apps[appid].appinfo.extended) || {}).map((k) => k.toLowerCase()) - .includes(key)); + return LEGACY_CD_KEY_INDICATORS.some((key) => Object.keys(client.picsCache?.apps[appid]?.appinfo?.extended || {}) + .map((k) => k.toLowerCase()) + .includes(key) + ); }); console.log(`Requesting legacy cd keys for ${apps.length} owned apps...`); let keys = {}; for (let appid of apps) { + let appName = client.picsCache?.apps[appid]?.appinfo?.common?.name || appid; try { let {key} = await client.getLegacyGameKey(appid); keys[appid] = key; + console.log(`Successfully retrieved key for "${appName}" (${appid})`); } catch (err) { - if (err.eresult !== SteamUser.EResult.Fail) { // usually just means no cd key present - console.error(`App: ${appid}`, err); - } + console.log(`Failed to retrieve key for "${appName}" (${appid}): ${SteamUser.EResult[err.eresult] || err.eresult || err.message}`); } } - fs.writeFileSync('legacy_keys.json', JSON.stringify(keys, null, 4)); + + await writeFile('legacy_keys.json', JSON.stringify(keys, null, '\t')); console.log('Done! Logging off...'); client.logOff(); }); -client.on('disconnected', () => process.exit(0)); - -credentials.logonID = Math.round(Date.now() / 1000); // To avoid getting kicked by LogonSessionReplaced -client.logOn(credentials); +collectCredentials().then((credentials) => { + credentials.logonID = Math.round(Date.now() / 1000); // To avoid getting kicked by LogonSessionReplaced + client.logOn(credentials); +}); diff --git a/examples/lib/collect_credentials.js b/examples/lib/collect_credentials.js new file mode 100644 index 00000000..5f0c7e9d --- /dev/null +++ b/examples/lib/collect_credentials.js @@ -0,0 +1,41 @@ +const {createInterface} = require('readline'); + +/** + * @returns {Promise<{accountName: string, password: string}>} + */ +module.exports = async function collectCredentials() { + let accountName = await promptAsync('Account Name: '); + let password = await promptAsync('Password: ', true); + return {accountName, password}; +} + +/** + * + * @param {string} question + * @param {boolean} [sensitiveInput=false] + * @return Promise + */ +function promptAsync(question, sensitiveInput) { + return new Promise((resolve) => { + let rl = createInterface({ + input: process.stdin, + output: sensitiveInput ? null : process.stdout, + terminal: true + }); + + if (sensitiveInput) { + // We have to write the question manually if we didn't give readline an output stream + process.stdout.write(`${question.trim()} [masked] `); + } + + rl.question(question, (result) => { + if (sensitiveInput) { + // We have to manually print a newline + process.stdout.write('\n'); + } + + rl.close(); + resolve(result); + }); + }); +}