mirror of
https://github.com/Ratstail91/SANCTUM.git
synced 2025-11-29 02:24:27 +11:00
Removed express, added authentication
This commit is contained in:
@@ -59,6 +59,8 @@ client.on('ready', async () => {
|
||||
|
||||
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.
|
||||
cron.schedule('*/2 * * * *', () => { //TODO: move this to Mori
|
||||
console.log('Updating STAMINA every 2 min.');
|
||||
|
||||
@@ -1,26 +1,32 @@
|
||||
// .env Variables
|
||||
require("dotenv").config({path: "../.env"});
|
||||
|
||||
//server tools
|
||||
let express = require("express");
|
||||
let socket = require("socket.io");
|
||||
//socket.io setup
|
||||
let server = require("http").createServer();
|
||||
let io = require("socket.io")(server);
|
||||
let ioAuth = require("socketio-auth");
|
||||
|
||||
//express setup
|
||||
let app = express();
|
||||
let server = app.listen(process.env.SERVER_PORT, () => {
|
||||
console.log("Listening to requests on port " + process.env.SERVER_PORT);
|
||||
ioAuth(io, {
|
||||
authenticate: function(socket, data, callback) {
|
||||
return callback(null, data.SERVER_PASS_KEY === process.env.SERVER_PASS_KEY);
|
||||
},
|
||||
postAuthenticate: function(socket, data) {
|
||||
console.log("Authenticated: " + data.username);
|
||||
socket.client.username = data.username;
|
||||
}
|
||||
});
|
||||
|
||||
//shared code
|
||||
let calcRandom = require('../Shared/calc_random');
|
||||
|
||||
//socket.io setup
|
||||
let io = socket(server);
|
||||
|
||||
//TODO: isolate these responses to specific bots
|
||||
io.on("connection", async (socket) => {
|
||||
console.log("made socket connection");
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
console.log(socket.client.username + " disconnected");
|
||||
});
|
||||
|
||||
//update the playerbase's stamina on command
|
||||
socket.on("updateStamina", async ({ userID, data }) => {
|
||||
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);
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"dotenv": "^6.1.0",
|
||||
"express": "^4.16.4",
|
||||
"forever": "^0.15.3",
|
||||
"socket.io": "^2.1.1"
|
||||
"socket.io": "^2.1.1",
|
||||
"socketio-auth": "^0.1.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,28 @@
|
||||
//initialize the 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
|
||||
let io = require("socket.io-client")(`${process.env.SERVER_ADDRESS}:${process.env.SERVER_PORT}`);
|
||||
//ConnectToServer
|
||||
//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
|
||||
//dataType - the type of data being sent
|
||||
|
||||
Reference in New Issue
Block a user