This commit is contained in:
tursom 2024-10-17 11:27:40 +08:00
parent afa8551a03
commit 3004cb47fd
4 changed files with 51 additions and 26 deletions

View File

@ -1,8 +1,11 @@
const client = require("./client") const client = require("./client")
const axios = require('axios'); const axios = require('axios');
const http = require('http'); const http = require('http');
const fs = require('fs');
const { once } = require("node:events"); const { once } = require("node:events");
const logger = client.logger
fs.mkdir("./logs", { recursive: true }, (err) => { fs.mkdir("./logs", { recursive: true }, (err) => {
if (err) { if (err) {
logger.error("an error occurred while creating the logs directory: " + err); logger.error("an error occurred while creating the logs directory: " + err);

View File

@ -2,10 +2,10 @@ const SteamID = require('steamid');
const SteamUser = require('steam-user'); const SteamUser = require('steam-user');
const SteamTotp = require('steam-totp'); const SteamTotp = require('steam-totp');
const SteamCommunity = require('steamcommunity'); const SteamCommunity = require('steamcommunity');
fs = require('fs'); const fs = require('fs');
winston = require("winston"); const winston = require("winston");
logger = winston.createLogger({ const logger = winston.createLogger({
level: 'info', level: 'info',
format: winston.format.json(), format: winston.format.json(),
defaultMeta: { service: 'steam-logger' }, defaultMeta: { service: 'steam-logger' },
@ -25,7 +25,7 @@ steamUser.on("refreshToken", (refreshToken) => {
fs.writeFileSync('refresh.token', refreshToken); fs.writeFileSync('refresh.token', refreshToken);
}); });
config = require("./config.js"); const config = require("./config.js");
try { try {
refreshToken = fs.readFileSync('refresh.token', 'utf8'); refreshToken = fs.readFileSync('refresh.token', 'utf8');
@ -56,18 +56,24 @@ try {
}); });
} }
let webLogOn = false; steamLoginPromise = new Promise((resolve, reject) => {
steamUser.on('loggedOn', async () => { steamUser.on('loggedOn', async () => {
logger.info(`login to Steam as ${steamUser.steamID}`); logger.info(`login to Steam as ${steamUser.steamID}`);
if (!webLogOn) { resolve();
steamUser.webLogOn();
}
}); });
});
steamWebLoginPromise = new Promise((resolve, reject) => {
steamUser.on('webSession', async (sessionID, cookies) => { steamUser.on('webSession', async (sessionID, cookies) => {
logger.info(`web session received: ${sessionID}`);
webLogOn = true; webLogOn = true;
steamCommunity.setCookies(cookies); steamCommunity.setCookies(cookies);
steamCommunity.startConfirmationChecker(10000, config.identitySecret); steamCommunity.startConfirmationChecker(10000, config.identitySecret);
resolve()
});
}); });
async function getUserInfo(steamID, onUserInfoReceived) { async function getUserInfo(steamID, onUserInfoReceived) {
@ -97,8 +103,11 @@ async function getUserInfo(steamID, onUserInfoReceived) {
} }
module.exports = { module.exports = {
logger: logger,
steamUser: steamUser, steamUser: steamUser,
steamCommunity: steamCommunity, steamCommunity: steamCommunity,
getUserInfo: getUserInfo, getUserInfo: getUserInfo,
steamLoginPromise: steamLoginPromise,
steamWebLoginPromise: steamWebLoginPromise,
} }

View File

@ -1,5 +1,13 @@
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
module.exports = { module.exports = {
accountName: 'accountName', accountName: 'accountName',
password: 'password', password: 'password',
logonID: 5252596, logonID: getRandomInt(1000000, 999999999),
steamID: "xxxxxxxxxx",
chat: false,
}; };

View File

@ -1,9 +1,12 @@
const fs = require('fs');
const config = require('./config.js'); const config = require('./config.js');
dateformat = require('@matteo.collina/dateformat'); const logger = client.logger
client = require("./client.js") const dateformat = require('@matteo.collina/dateformat');
steamUser = client.steamUser
const client = require("./client.js")
const steamUser = client.steamUser
fs.mkdir("./logs", { recursive: true }, (err) => { fs.mkdir("./logs", { recursive: true }, (err) => {
if (err) { if (err) {
@ -45,6 +48,7 @@ async function logMessage(date, steamID, message, echo, ordinal) {
}); });
} }
client.steamLoginPromise.then(() => {
steamUser.chat.on("friendMessage", (message) => { steamUser.chat.on("friendMessage", (message) => {
// noinspection JSIgnoredPromiseFromCall // noinspection JSIgnoredPromiseFromCall
logMessage(dateToString(message.server_timestamp), message.steamid_friend, message.message, false, message.ordinal); logMessage(dateToString(message.server_timestamp), message.steamid_friend, message.message, false, message.ordinal);
@ -54,6 +58,7 @@ steamUser.chat.on("friendMessageEcho", (message) => {
// noinspection JSIgnoredPromiseFromCall // noinspection JSIgnoredPromiseFromCall
logMessage(dateToString(message.server_timestamp), message.steamid_friend, message.message, true, message.ordinal); logMessage(dateToString(message.server_timestamp), message.steamid_friend, message.message, true, message.ordinal);
}); });
});
/** /**
* @param {Date} date * @param {Date} date