Skip to content

Commit

Permalink
[mail] integrate mail bug reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNewCivilian committed Dec 27, 2020
1 parent a25e6e1 commit 087fc8e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ server/node_modules/
backend/node_modules/

backend/log/

backend/src/secrets.js

backend/src/secrets.json
5 changes: 5 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "ISC",
"dependencies": {
"@babel/preset-env": "^7.9.6",
"nodemailer": "^6.4.17",
"two.js": "^0.7.0-stable.1",
"uuid": "^8.3.2",
"ws": "^7.2.5"
Expand Down
27 changes: 27 additions & 0 deletions backend/src/mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const nodemailer = require('nodemailer');
const secrets = require('./secrets.json');
const { getUnixTime } = require('./websocket/helpers');

const sendMail = (subject, body) => {
const mailTransporter = nodemailer.createTransport({
host: secrets.emailSmtp,
port: secrets.emailPort,
secure: false, // true for 465, false for other ports
auth: {
user: secrets.emailUsername,
pass: secrets.emailPassword,
},
pool: true
});
mailTransporter.sendMail({
from: '"' + secrets.emailName + '" <' + secrets.emailUsername + '>',
to: '"David" <[email protected]>',
subject: `Kingscastle.io - ${subject}`,
text: body,
});
console.log(`${getUnixTime()} [MAIL] Mail send`);
};

module.exports = {
sendMail,
};
33 changes: 20 additions & 13 deletions backend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@ const fs = require('fs');
const process = require('process');
const SocketActions = require('./websocket');
const { ROOT_FOLDER } = require('./config');
const { sendMail } = require('./mail');
const { getUnixTime } = require('./websocket/helpers');

const socketPort = 3031;
const websocket = new WebSocket.Server({ port: socketPort });

const access = fs.createWriteStream(`${ROOT_FOLDER}/log/node.access.log`, { flags: 'a+' });
const error = fs.createWriteStream(`${ROOT_FOLDER}/log/node.error.log`, { flags: 'a+' });
const accessStream = fs.createWriteStream(`${ROOT_FOLDER}/log/node.access.log`, { flags: 'a+' });
const errorStream = fs.createWriteStream(`${ROOT_FOLDER}/log/node.error.log`, { flags: 'a+' });

// redirect stdout / stderr
process.__defineGetter__('stdout', () => { return access});
process.__defineGetter__('stderr', () => { return error});
process.__defineGetter__('stdout', () => { return accessStream});
process.__defineGetter__('stderr', () => { return errorStream});

console.log(`Websocket Server started on ${socketPort}`);
console.log(`${getUnixTime()} [START] Websocket Server started on ${socketPort}`);

websocket.on('connection', (connection) => {
SocketActions.onConnect(websocket, connection);
SocketActions.onConnect(websocket, connection);

connection.on('message', (message) => {
SocketActions.onMessage(websocket, connection, message);
});

connection.on('close', () => {
SocketActions.onClose(websocket, connection);
});
connection.on('message', (message) => {
try {
SocketActions.onMessage(websocket, connection, message);
} catch (error) {
console.log(`${getUnixTime()} [ERROR] ${error}`);
sendMail('[ERROR]', error);
}
});

connection.on('close', () => {
SocketActions.onClose(websocket, connection);
});
});
6 changes: 5 additions & 1 deletion backend/src/websocket/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const User = require('./user');
const Session = require('./session');
const fs = require('fs');
const { ROOT_FOLDER } = require('../config');
const { sendMail } = require('../mail');
const { timeStamp } = require('console');

const sessions = {};

Expand Down Expand Up @@ -39,8 +41,10 @@ const checkSessionsAlive = () => {
const session = sessions[sessionKey];
if ((session.userA && session.userA.lastAction < currentTime - 2400)
|| (session.userB && session.userB.lastAction < currentTime - 2400)
)
) {
sendMail('[Timeout]', JSON.stringify(session));
endGame(session, 'none after timeout');
}
});
}

Expand Down

0 comments on commit 087fc8e

Please sign in to comment.