This repository has been archived by the owner on Oct 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
ws.js
147 lines (133 loc) · 3.84 KB
/
ws.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
const WebSocket = require('ws');
const domain = require('domain');
const SteamUser = require('./steam');
const poster = require('./post');
const config = require('./config');
const resultEnum = require('./Eresult');
const purchaseResultEnum = require('./EPurchaseResult');
module.exports = server => {
const wss = new WebSocket.Server({server});
wss.on('connection', ws => {
wsSend(ws, {
action: 'connect',
result: 'success',
server: config ? config.name : 'Unknown',
});
let steamClient = new SteamUser();
steamClient.setWebSocket(ws);
ws.on('message', message => dispatchMessage(ws, steamClient, message));
ws.on('close', () => steamClient.logOff());
});
};
function dispatchMessage(ws, steam, message) {
let data = parseJSON(message);
if (!data.action) return;
switch (data.action) {
case 'ping':
pong(ws, data);
break;
case 'logOn':
doLogOn(ws, steam, data);
break;
case 'authCode':
doAuth(ws, steam, data);
break;
case 'redeem':
doRedeem(ws, steam, data);
break;
default:
return;
}
}
function pong(ws, data) {
wsSend(ws, {
action: 'pong',
count: data.count || 0,
});
}
function doLogOn(ws, steam, data) {
runSafely(ws, 'logOn', () => {
steam.logOn({
accountName: data.username.trim(),
password: data.password.trim(),
twoFactorCode: data.authcode.trim(),
rememberPassword: false,
dontRememberMachine: true,
});
});
steam.once('accountInfo', (name, country) => {
wsSend(ws, {
action: 'logOn',
result: 'success',
detail: {
name: name,
country: country,
},
});
})
}
function doAuth(ws, steam, data) {
if (!data.authCode || data.authCode.trim() === '') {
wsSendError(ws, 'logOn', 'AuthCodeError');
return;
}
runSafely(ws, 'logOn', () => steam.emit('inputAuthCode', data.authCode));
}
function doRedeem(ws, steam, data) {
runSafely(ws, 'redeem', () => {
data.keys.forEach(async key => redeemKey(steam, key).then(res => {
wsSend(ws, res);
if (config && config.enableLog) {
for (let subId in res.detail.packages) {
if (res.detail.packages.hasOwnProperty(subId)) {
poster(config.postUrl, subId, res.detail.packages[subId], config.id);
break;
}
}
}
}));
});
}
function redeemKey(steam, key) {
return new Promise(resolve => {
steam.redeemKey(key, (result, detail, packages) => {
resolve({
action: 'redeem',
detail: {
key: key,
result: resultEnum[result],
detail: purchaseResultEnum[detail],
packages: packages,
},
});
});
})
}
function wsSendError(ws, action, message) {
wsSend(ws, {
action: action,
result: 'failed',
message: message
});
}
function wsSend(ws, stuff) {
try {
let data = typeof stuff === 'string' ? stuff : JSON.stringify(stuff);
ws.send(data);
} catch (error) {
// do nothing...
}
}
function runSafely(ws, action, runnable, ...parameters) {
let dm = domain.create();
dm.on('error', err => wsSendError(ws, action, err.message || 'something went wrong...'));
parameters && parameters.forEach(p => dm.add(p));
dm.run(runnable);
}
function parseJSON(json, defaultValue = {}) {
try {
return JSON.parse(json);
} catch (ex) {
return defaultValue;
}
}