Removed express, added authentication

This commit is contained in:
2018-10-29 03:09:32 +11:00
parent 729209288d
commit 09901ca10f
4 changed files with 46 additions and 16 deletions

View File

@@ -59,6 +59,8 @@ client.on('ready', async () => {
console.log("Logged in as: " + client.user.username + " - " + client.user.id); console.log("Logged in as: " + client.user.username + " - " + client.user.id);
shared.ConnectToServer(client.user.username, process.env.SERVER_ADDRESS, process.env.SERVER_PORT, process.env.SERVER_PASS_KEY);
//ADAM updates stamina (1) and health by 1% every 2 min. //ADAM updates stamina (1) and health by 1% every 2 min.
cron.schedule('*/2 * * * *', () => { //TODO: move this to Mori cron.schedule('*/2 * * * *', () => { //TODO: move this to Mori
console.log('Updating STAMINA every 2 min.'); console.log('Updating STAMINA every 2 min.');

View File

@@ -1,26 +1,32 @@
// .env Variables // .env Variables
require("dotenv").config({path: "../.env"}); require("dotenv").config({path: "../.env"});
//server tools //socket.io setup
let express = require("express"); let server = require("http").createServer();
let socket = require("socket.io"); let io = require("socket.io")(server);
let ioAuth = require("socketio-auth");
//express setup ioAuth(io, {
let app = express(); authenticate: function(socket, data, callback) {
let server = app.listen(process.env.SERVER_PORT, () => { return callback(null, data.SERVER_PASS_KEY === process.env.SERVER_PASS_KEY);
console.log("Listening to requests on port " + process.env.SERVER_PORT); },
postAuthenticate: function(socket, data) {
console.log("Authenticated: " + data.username);
socket.client.username = data.username;
}
}); });
//shared code //shared code
let calcRandom = require('../Shared/calc_random'); let calcRandom = require('../Shared/calc_random');
//socket.io setup
let io = socket(server);
//TODO: isolate these responses to specific bots //TODO: isolate these responses to specific bots
io.on("connection", async (socket) => { io.on("connection", async (socket) => {
console.log("made socket connection"); console.log("made socket connection");
socket.on("disconnect", async () => {
console.log(socket.client.username + " disconnected");
});
//update the playerbase's stamina on command //update the playerbase's stamina on command
socket.on("updateStamina", async ({ userID, data }) => { socket.on("updateStamina", async ({ userID, data }) => {
console.log("updating stamina for all users..."); console.log("updating stamina for all users...");
@@ -112,3 +118,7 @@ io.on("connection", async (socket) => {
} }
}); });
}); });
//listen
server.listen(process.env.SERVER_PORT);
console.log("listening on port " + process.env.SERVER_PORT);

View File

@@ -13,8 +13,8 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"dotenv": "^6.1.0", "dotenv": "^6.1.0",
"express": "^4.16.4",
"forever": "^0.15.3", "forever": "^0.15.3",
"socket.io": "^2.1.1" "socket.io": "^2.1.1",
"socketio-auth": "^0.1.1"
} }
} }

View File

@@ -1,10 +1,28 @@
//initialize the exports //initialize the exports
exports = module.exports = {}; exports = module.exports = {};
require("dotenv").config({path: "../.env"}); //global, because why not?
let socketClient = require("socket.io-client");
let io;
let initialized = false;
//socket tools //ConnectToServer
let io = require("socket.io-client")(`${process.env.SERVER_ADDRESS}:${process.env.SERVER_PORT}`); //username - the bot's username
//address - the server's web address
//port - the server's port
//pass - the server's passcode
exports.ConnectToServer = function(username, address, port, pass) { //NOTE: this doesn't need to be in a function, I just want to display the username server-side
//prevent double-initialization
if (initialized) return;
//socket tools
io = socketClient(`${address}:${port}`);
io.on("connect", () => io.emit("authentication", {SERVER_PASS_KEY: pass, username: username}) );
io.on("authenticated", () => console.log("Authenticated with server: " + `${address}:${port}`));
io.on("disconnect", () => console.log("disconnected from server: " + `${address}:${port}`));
initialized = true;
}
//SendServerData //SendServerData
//dataType - the type of data being sent //dataType - the type of data being sent