-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (209 loc) · 8.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict'
// Modules
const express = require('express')
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(server)
const passport = require('passport')
const session = require('express-session')
const sharedsession = require('express-socket.io-session')
const SteamStrategy = require('passport-steam').Strategy
// Site stuff
const TradeBot = require('./lib/index')
const Trade = new TradeBot({ io })
const config = require('./config/config')
// Web server
server.listen(config.websitePort)
console.log('[!] Website server is online.')
console.log('[!] Socket server is online.')
// Passport
passport.serializeUser((user, done) => {
done(null, user)
})
passport.deserializeUser((obj, done) => {
done(null, obj)
})
passport.use(new SteamStrategy({
returnURL: `${config.website}/auth/steam/return`,
realm: `${config.website}/`,
apiKey: config.steamApiKey,
},
(identifier, profile, done) => {
process.nextTick(() => {
const user = profile
user.identifier = identifier
return done(null, user)
})
}))
const sessionMiddleware = session({
secret: 'csg0tradebot',
name: 'csg0trade',
resave: true,
saveUninitialized: true,
})
app.use(sessionMiddleware)
app.use(passport.initialize())
app.use(passport.session())
app.use('/static', express.static('./static'))
// Routes
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/index.html`)
})
// Auth Routes
app.get('/auth/steam', passport.authenticate('steam'))
app.get('/auth/steam/return', passport.authenticate('steam', { failureRedirect: '/auth/steam' }), (req, res) => {
// Successful authentication, redirect home.
res.redirect('/')
})
app.get('/logout', (req, res) => {
req.logout()
res.redirect('/')
})
// Sockets
io.use(sharedsession(sessionMiddleware))
io.on('connection', (socket) => {
let userObject = false
if (
typeof socket.handshake.session.passport !== 'undefined' &&
typeof socket.handshake.session.passport.user !== 'undefined' &&
typeof socket.handshake.session.passport.user.id !== 'undefined'
) {
userObject = socket.handshake.session.passport.user
}
socket.emit('site', config.site)
socket.emit('user', userObject)
socket.on('get user inv', (steamID64) => {
Trade.getInventory(steamID64, config.appID, config.contextID, (err, data) => {
socket.emit('user inv', { error: err, items: data })
})
})
socket.on('get bot inv', (id) => {
Trade.getInventory(config.bots[id].steamID64, config.appID, config.contextID, (err, data) => {
socket.emit('bot inv', { error: err, items: data })
})
})
socket.on('get bots inv', () => {
const params = []
Object.keys(config.bots).forEach((index) => {
const bot = config.bots[index]
params.push({
id: index,
steamID64: bot.steamID64,
appID: config.appID,
contextID: config.contextID,
})
})
Trade.getInventories(params, (data) => {
socket.emit('bots inv', data)
socket.emit('bots floats', Trade.getFloatValues())
})
})
socket.on('get pricelist', () => {
socket.emit('pricelist', Trade.getPriceList())
})
socket.on('get rates', () => {
socket.emit('rates', {
ignore: Trade.getIgnorePrice(),
trash: Trade.getTrashPrice(),
user: Trade.getUserRates(),
bot: Trade.getBotRates(),
})
})
socket.on('get offer', (data) => {
socket.emit('offer status', {
error: null,
status: 4,
})
const link = data.tradelink
const offerData = data
if (
link.indexOf('steamcommunity.com/tradeoffer/new/') === -1 ||
link.indexOf('?partner=') === -1 ||
link.indexOf('&token=') === -1
) {
socket.emit('offer status', {
error: 'Invalid trade link!',
status: false,
})
} else {
Trade.validateOffer(offerData, (err, success) => {
socket.emit('offer status', {
error: err,
status: (success) ? 1 : false,
})
if (!err && success) {
if (typeof config.bots[offerData.bot_id] === 'undefined') {
offerData.bot_id = Object.keys(config.bots)[0]
}
const Bot = Trade.getBot(offerData.bot_id)
const offer = Bot.manager.createOffer(offerData.tradelink)
offer.addTheirItems(offerData.user.map(assetid => ({
assetid,
appid: config.appID,
contextid: config.contextID,
amount: 1,
})))
if (offerData.bot.length) {
offer.addMyItems(offerData.bot.map(assetid => ({
assetid,
appid: config.appID,
contextid: config.contextID,
amount: 1,
})))
}
offer.setMessage(config.tradeMessage)
offer.getUserDetails((detailsError, me, them) => {
if (detailsError) {
socket.emit('offer status', {
error: detailsError,
status: false,
})
} else if (me.escrowDays + them.escrowDays > 0) {
socket.emit('offer status', {
error: 'You must have 2FA enabled, we do not accept trades that go into Escrow.',
status: false,
})
} else {
offer.send((errSend, status) => {
if (errSend) {
socket.emit('offer status', {
error: errSend,
status: false,
})
} else {
console.log('[!!!!!] Sent a trade: ', data)
if (status === 'pending') {
socket.emit('offer status', {
error: null,
status: 2,
})
Trade.botConfirmation(data.bot_id, offer.id, (errConfirm) => {
if (!errConfirm) {
socket.emit('offer status', {
error: null,
status: 3,
offer: offer.id,
})
} else {
socket.emit('offer status', {
error: errConfirm,
status: false,
})
}
})
} else {
socket.emit('offer status', {
error: null,
status: 3,
offer: offer.id,
})
}
}
})
}
})
}
})
}
})
})