Add proxy support and fix some bugs.

This commit is contained in:
eTronic
2018-10-25 17:45:34 -04:00
parent cc4dec9297
commit 4fdde0cba6
3 changed files with 66 additions and 21 deletions

45
package-lock.json generated
View File

@@ -4,6 +4,14 @@
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"agent-base": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-4.2.1.tgz",
"integrity": "sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg==",
"requires": {
"es6-promisify": "5.0.0"
}
},
"ajv": {
"version": "5.5.2",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-5.5.2.tgz",
@@ -1218,6 +1226,19 @@
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.80.tgz",
"integrity": "sha512-WClidEWEUNx7OfwXehB0qaxCuetjbKjev2SmXWgybWPLKAThBiMTF/2Pd8GSUDtoGOavxVzdkKwfFAPRSWlkLw=="
},
"es6-promise": {
"version": "4.2.5",
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.5.tgz",
"integrity": "sha512-n6wvpdE43VFtJq+lUDYDBFUwV8TZbuGXLV4D6wKafg13ldznKsyEvatubnmUe31zcvelSzOHF+XbaT+Bl9ObDg=="
},
"es6-promisify": {
"version": "5.0.0",
"resolved": "http://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz",
"integrity": "sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=",
"requires": {
"es6-promise": "4.2.5"
}
},
"escape-string-regexp": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
@@ -1981,6 +2002,30 @@
"sshpk": "1.15.1"
}
},
"https-proxy-agent": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-2.2.1.tgz",
"integrity": "sha512-HPCTS1LW51bcyMYbxUIOO4HEOlQ1/1qRaFWcyxvwaqUS9TY88aoEuHUY33kuAh1YhVVaDQhLZsnPd+XNARWZlQ==",
"requires": {
"agent-base": "4.2.1",
"debug": "3.2.6"
},
"dependencies": {
"debug": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
"integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
"requires": {
"ms": "2.1.1"
}
},
"ms": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
"integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg=="
}
}
},
"inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",

View File

@@ -11,6 +11,7 @@
"discord.js": "^11.4.2",
"faker": "^4.1.0",
"fs": "0.0.1-security",
"https-proxy-agent": "^2.2.1",
"request": "^2.87.0",
"request-promise": "^4.2.2",
"select-shell": "^1.1.2",

41
quiz.js
View File

@@ -1,6 +1,7 @@
import ws from 'ws';
import fs from 'fs';
import Discord from 'discord.js';
import HttpsProxyAgent from 'https-proxy-agent';
import Logger from 'simple-node-logger';
const config = JSON.parse(fs.readFileSync('config.json'));
@@ -10,7 +11,7 @@ const client = new Discord.Client();
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
class Player {
constructor({ objectId, username }) {
constructor({ objectId, username, proxy }) {
this.objectId = objectId;
this.username = username;
@@ -24,6 +25,7 @@ class Player {
this.controller = null;
this.actionIndex = 0;
this.lastAnswerIndex = -1;
this.agent = proxy ? new HttpsProxyAgent(proxy, { secureProxy: true }) : null;
}
cleanPrint(message) {
@@ -33,7 +35,10 @@ class Player {
}
connect() {
this.socket = new ws(`${config.PROD_SOCKET_URL}/socket.io/?EIO=3&transport=websocket`);
const options = {
agent: this.agent,
};
this.socket = new ws(`${config.PROD_SOCKET_URL}/socket.io/?EIO=3&transport=websocket`, options);
this.socket.on('open', this.handleOpen.bind(this));
this.socket.on('close', this.handleClose.bind(this));
this.socket.on('message', this.handleMessage.bind(this));
@@ -75,7 +80,13 @@ class Player {
const parseMessage = () => {
const extractedData = /\[(.*?)\]$/.exec(message);
if (!extractedData) return null;
return JSON.parse(extractedData[1]);
let messageJSON = null;
try {
messageJSON = JSON.parse(extractedData[0]);
} catch (error) {
messageJSON = JSON.parse(extractedData[1]);
}
return messageJSON;
}
// maybe a bit hackish? but works for every example I see so far and cleaner.
@@ -91,7 +102,7 @@ class Player {
this.sendAction('GAME_JOIN', { userId: this.objectId });
break;
case 430:
if (data[0]) {
if (data) {
log.info(`Player ${this.username} has connected!`);
this.sendAction('GAME_GET_STATUS', { userId: this.objectId });
this.sendAction('TIME_SYNC', null);
@@ -100,25 +111,13 @@ class Player {
}
break;
case 431:
const game = data[1];
let showQuestions = false;
if (data && data[0] && data[0].game) {
this.game = data[0].game;
if (game && game.questions) {
if (this.game && this.game.questions) {
if (game.questions.length !== this.game.questions.length) {
showQuestions = true;
}
} else {
showQuestions = true;
if (this.game.currentState === 'GAME_LOBBY' && this.isMaster) {
this.showQuestions();
}
}
this.game = game;
if (this.isMaster && showQuestions) {
this.showQuestions();
}
break;
case 42:
const type = data[0];
@@ -339,5 +338,5 @@ client.on('message', (msg) => {
}
});
client.login(config.discord_token).then(() => {
// game.run();
game.run();
});