Jump to content
McKay Development

Error: ETIMEDOUT and reload cookies


Recommended Posts

Posted

I has a bot, running this code:

'use strict';

const fs = require('fs');
const sslOptions = {
    key:    fs.readFileSync('/opt/psa/var/modules/letsencrypt/etc/live/site.com/privkey.pem'),
    cert:   fs.readFileSync('/opt/psa/var/modules/letsencrypt/etc/live/site.com/fullchain.pem'),
    requestCert:        true,
    rejectUnauthorized: false
};

const app = require('express')(),
	https = require('https'),
	server = https.createServer(sslOptions, app),
    io = require('socket.io')(server),
    redis = require('redis'),
    mysql = require('mysql'),
    config = require('./config.js'),
    SteamUser = require('steam-user'),
    SteamTotp = require('steam-totp'),
    SteamCommunity = require('steamcommunity'),
    TradeOfferManager = require('steam-tradeoffer-manager'),
    log4js = require('log4js'),
    requestify = require('requestify'),
    client = redis.createClient(),
    redisClient = redis.createClient();

const connection = mysql.createConnection({
    host: 'localhost',
    user: config.db.user,
    password: config.db.password,
    database: config.db.database
});

let bots = [];

connection.connect();
server.listen(8080);

connection.query("SET SESSION wait_timeout = 604800");

log4js.configure({
    appenders: {
        multi: {type: 'multiFile', base: 'logs/', property: 'categoryName', extension: '.log'},
        console: {type: 'console'}
    },
    categories: {
        default: {appenders: ['multi', 'console'], level: 'debug'}
    }
});


/**
 * Redis function
 */
redisClient.subscribe('deposit');
redisClient.subscribe('withdraw');
redisClient.subscribe('sendItem');
redisClient.on('message', (channel, message) => {
    if (channel == `deposit`) {
        const data = JSON.parse(message);
        bots[data.bot].deposit(data);
    }
    if (channel == `withdraw`) {
        const data = JSON.parse(message);
        bots[data.bot].withdraw(data);
    }
    if (channel == `sendItem`) {
        const data = JSON.parse(message);
        io.emit('notify', data);
    }
});

/**
 * Bot Function
 */

connection.query("SELECT * FROM `bots`", (err, row) => {
    if ((err) || (!row.length)) {
        console.log('Bots not found. Stop App.');
        console.log(err);
        return process.exit(0);
    }

    for (let i = 0; i < row.length; i++) {
        const bot = new Bot(row[i]);
        bots.push(bot);

        bot.log(`Launch BOT [${row[i].username}]`);
    }
});


class Bot {
    constructor(row) {
        this.bot = row;

        this.client = new SteamUser();
        this.community = new SteamCommunity();
        this.manager = new TradeOfferManager({
            steam: this.client,
            community: this.community,
            language: 'en',
            cancelTime: 300000,
            pollInterval: 5000
        });

        this.logger = log4js.getLogger(`bot_${this.bot['id']}`);

        this.logOnOptions = {
            'accountName': this.bot['username'],
            'password': this.bot['password'],
            'twoFactorCode': SteamTotp.generateAuthCode(this.bot['shared'])
        };

        this.client.logOn(this.logOnOptions);

        this.client.on('loggedOn', () => {
            this.log('Logged in Steam!');
        });

        this.client.on('webSession', (sessionid, cookies) => {
			this.manager.setCookies(cookies, function(err) {
				if (err) {
					console.log(err);
					process.exit(1); // Fatal error since we couldn't get our API key
					return;
				}
			});

            this.community.setCookies(cookies);
            this.community.startConfirmationChecker(5000, this.bot['identity']);

            this.community.on('sessionExpired', () => {
                this.client.webLogOn();
            });
        });
		
		this.manager.on('newOffer', (offer) => {
                this.log(`NEW OFFER FROM ${offer.partner.getSteamID64()} #${offer.id}`);
                if (offer.itemsToGive.length !== 0 && offer.itemsToReceive.length === 0 &&this.client.myFriends[offer.partner.getSteamID64()]) {
                    offer.accept((err, status) => {
                        if (err) {
                            this.log(err);
                        } else {
                            this.log(`OFFER #${offer.id} ACCEPTED`);
                        }
                    });
                } else if (offer.itemsToReceive.length !== 0 && offer.itemsToGive.length === 0 && this.client.myFriends[offer.partner.getSteamID64()]) {
                    offer.accept((err, status) => {
                        if (err) {
                            this.log(err);
                        } else {
                            this.newShopItems(offer);
                            this.log(`OFFER #${offer.id} ACCEPTED. NEW ITEMS TO SHOP ADDED`);
                        }
                    });
                } else {
                    offer.decline((err) => {
                    });
                }
            });

        this.manager.on('sentOfferChanged', (offer, oldState) => {
                if (offer.state == 7 && offer.confirmationMethod == 0) {
                    this.log(`Offer #${offer.id} DEPOSIT declined!`);

                    io.emit('notify', {
                        steamid: offer.partner.getSteamID64(),
                        msg: `You decline offer`,
                        status: 'warning'
                    });
                }
                if (offer.state == 7 && offer.confirmationMethod == 2) {
                    this.log(`Offer #${offer.id} WITHDRAW declined!`);

                    this.declinedItems(offer);

                    io.emit('notify', {
                        steamid: offer.partner.getSteamID64(),
                        msg: `You decline offer`,
                        status: 'warning'
                    });
                }
                if (offer.state == 3 && offer.confirmationMethod == 0) {
                    this.log(`Offer #${offer.id} DEPOSIT accepted!`);

                    io.emit('notify', {
                        steamid: offer.partner.getSteamID64(),
                        msg: `Trade accept`,
                        status: 'success'
                    });

                    this.getItems(offer);
                }
                if (offer.state == 3 && offer.confirmationMethod == 2) {
                    this.log(`Offer #${offer.id} WITHDRAW accepted!`);

                    this.updateItems(offer);
                }
        });
    }

    log(msg) {
        this.logger.debug(`[${this.bot['username']}]: ${msg}`);
    }

    newShopItems(offer) {
        offer.getExchangeDetails((err, status, tradeInitTime, receivedItems, sentItems) => {
            if (!err) {
                requestify.post(`https://${config.namesite}/api/newItemsShop`, {
                    items: receivedItems,
                    bot: this.bot['id']
                })
                    .then((response) => {
                        const data = JSON.parse(response.body);
                    }, (error) => {
                        console.log(error);
                    });
            }
        });
    }

    getItems(offer) {
        offer.getExchangeDetails((err, status, tradeInitTime, receivedItems, sentItems) => {
            if (!err) {
                const newReceivedItems = receivedItems.map(item => item.name);
				const items = receivedItems;
				const newItems = [];

                this.log(`OFFER #${offer.id} FROM ${offer.partner.getSteamID64()} ITEMS: ${newReceivedItems.join(',')}`);

				items.forEach( (item) => {
					newItems.push({
						market_hash_name: item.market_hash_name,
						type: item.type,
						new_assetid: item.new_assetid,
						classid: item.icon_url
					});
				});
				
				client.set(`items_bot_${this.bot['id']}`, `${JSON.stringify(newItems)}`, redis.print);

                requestify.post(`https://${config.namesite}/api/newItems`, {
					steamid: offer.partner.getSteamID64(),
					bot: this.bot['id']
                })
                    .then((response) => {
                        const data = JSON.parse(response.body);
                        if (data.success) {
                            this.log(`OFFER #${offer.id} FROM ${offer.partner.getSteamID64()} ACCEPTED`);
                            io.emit('notify', {
                                msg: `Inv update`,
                                status: 'success',
								steamid: offer.partner.getSteamID64()
                            });
                            io.emit('updateInventory', {
                                steamid: offer.partner.getSteamID64(),
                            });
                        }
                        if (!data.success) this.log(`OFFER #${offer.id} FROM ${offer.partner.getSteamID64()} NOT ACCEPTED`);
                    }, (error) => {
                        console.log(error);
                    });
            } else {
				console.log(err);
			}
        });
    }

    declinedItems(offer) {
        const items = offer.itemsToGive;

        items.forEach( (item) => {
            connection.query(`UPDATE items SET status = 0 WHERE assetid = ${item['id']}`, (err, row) => {
                if (err) {
                    this.log(`BD Error: ${err.toString()}`);
                }
            });
        });

        io.emit('updateInventory', {
            steamid: offer.partner.getSteamID64(),
        });
    }

    updateItems(offer) {
        const items = offer.itemsToGive;

        items.forEach( (item) => {
            connection.query(`UPDATE bots SET items = items - 1 WHERE id = ${parseInt(this.bot['id'])}`, (err, row) => {
                if (err) {
                    this.log(`BD Error: ${err.toString()}`);
                } else {
                    connection.query(`DELETE FROM items WHERE assetid = ${item['id']}`, (err1, row) => {
                        if (err1) {
                            this.log(`BD Error: ${err1.toString()}`);
                        }
                    });
                }
            });
        });

        io.emit('updateInventory', {
            steamid: offer.partner.getSteamID64(),
        });
    }

    deposit(data) {
        const items = data.items;
        const trade_link = data.trade_link;
        const steamid = data.steamid;
		
        this.log(`NEW REQUEST TO DEPOSIT FROM ${steamid}`);

        let sendItems = [];

        items.forEach((item) => {
            sendItems.push({
                appid: 570,
                contextid: 2,
                amount: 1,
                assetid: item
            });
        });

        const offer = this.manager.createOffer(trade_link);
        offer.addTheirItems(sendItems);
        offer.setMessage(``);

        offer.send((err, status) => {
            if (err) {
                this.log(`ERROR ON SENDING USER ${steamid} OFFER: ${err.toString()}`);
                io.emit('notify', {
                    steamid: steamid,
                    msg: `Error send: ${err.toString()}`,
                    status: 'warning'
                });
                return;
            }

            this.log(`OFFER FOR ${steamid} #${offer.id} SENT`);
            io.emit('notify', {
                steamid: steamid,
                msg: `Offer send`,
				trade: offer.id,
                status: 'success'
            });
        });
    }

    withdraw(data) {
        const items = data.items;
        const trade_link = data.trade_link;
        const steamid = data.steamid;

        this.log(`NEW REQUEST TO WITHDRAW FROM ${steamid}`);

        let sendItems = [];

        items.forEach((item) => {
            sendItems.push({
                appid: 570,
                contextid: 2,
                amount: 1,
                assetid: item
            });
        });

        const offer = this.manager.createOffer(trade_link);
        offer.addMyItems(sendItems);
        offer.setMessage(``);

        offer.send((err, status) => {
            if (err) {
                this.log(`ERROR ON SENDING USER ${steamid} OFFER: ${err.toString()}`);
				this.declinedItems(offer);
                io.emit('notify', {
                    steamid: steamid,
                    msg: `Error with send: ${err.toString()}`,
                    status: 'warning'
                });
                return;
            }

            this.log(`OFFER FOR ${steamid} #${offer.id} SENT`);

			this.community.checkConfirmations();
			
            this.community.acceptConfirmationForObject(this.bot['identity'], offer.id, function (err) {
                io.emit('notify', {
                    steamid: steamid,
                    msg: `Send`,
					trade: offer.id,
                    status: 'success'
                });
            });
        });
    }
}

After 24 hours of work i received Error: ETIMEDOUT, or error with old cookies. How i can refresh cookies, for example every 6 hours and how i can fix error ETIMEDOUT? Because if i restart bot, all work fine. Thank for reply!

Posted (edited)
6 hours ago, Dr. McKay said:

 

Why? What is the problem?

I can refuse to use confirmation checker if you recommend an alternative.

Edited by artyak
Posted
On 3/2/2020 at 12:52 PM, artyak said:

Why? What is the problem?

I can refuse to use confirmation checker if you recommend an alternative.

Dont use confirmation checker, thats how I got like 5 ips blacklisted before I realized what was causing it.
I use accept all confirmations on confirmation event. It has 1.7% fail ratio for the event to emit, as much as I know. The other way is to accept all confirmations 10 seconds after the trade is sent, it might fail again but its still a workaround.

Posted
10 minutes ago, sNIP said:

Dont use confirmation checker, thats how I got like 5 ips blacklisted before I realized what was causing it.
I use accept all confirmations on confirmation event. It has 1.7% fail ratio for the event to emit, as much as I know. The other way is to accept all confirmations 10 seconds after the trade is sent, it might fail again but its still a workaround.

Oh, i understand. I will read about it and make a changes. Maybe you can say something about reload cookies and about ETIMEDOUT error?

Posted (edited)

Using a confirmation checker puts unnecessary load on steam's servers. 

 

After receiving or sending a trade offer, obtain the offer's ID and then call this method:

community.acceptConfirmationForObject('YOUR_IDENTITY_SECRET', 'OFFER ID', (err) => {
 	if (err) { 
		console.log(err);
	}
					
	// code here
})

 

Edited by vrtgn
Posted
8 hours ago, sNIP said:

Dont use confirmation checker, thats how I got like 5 ips blacklisted before I realized what was causing it.
I use accept all confirmations on confirmation event. It has 1.7% fail ratio for the event to emit, as much as I know. The other way is to accept all confirmations 10 seconds after the trade is sent, it might fail again but its still a workaround.

 

8 minutes ago, vrtgn said:

Using a confirmation checker puts unnecessary load on steam's servers. 

 

Now my server IP was banned, I’m confused, I don’t know what to do

Posted (edited)
On 3/5/2020 at 7:34 PM, artyak said:

Now my server IP was banned, I’m confused, I don’t know what to do

You're not meant to use the method I gave in conjunction with the confirmation checker. 

The confirmation checker (in simple terms) works by telling Steam every X seconds: "Hey, I wanna confirm everything waiting to be confirmed". It will tell steam even if there is nothing waiting to be confirmed thus the load it puts on servers - imagine loads of bots doing this. 

Whereas the method I put tells steams servers: "Hey, I wanna confirm this specific trade". Therefore, it only tells steam when there is something to be confirmed. 

Edited by vrtgn

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...