Compare commits
47 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7d628be826 | |||
| 7a42ab3108 | |||
| ec573e1074 | |||
| 9c5033daea | |||
| fb4d857224 | |||
| e0d6260d1a | |||
| 53ea726c89 | |||
| c44ed79e6d | |||
| 518f5dbee7 | |||
| be57dbe51c | |||
| 8309c3b832 | |||
| 95d229c561 | |||
| 0b1456ebde | |||
| bd5b6e8233 | |||
| 05eecf8bdd | |||
| 2aedb6e938 | |||
| ab73d05471 | |||
| f72b0e5522 | |||
| 900312752b | |||
| 1b2868d68f | |||
| 33157d48d3 | |||
| e0b3193607 | |||
| 655c81174e | |||
| 8c053e75aa | |||
| 69aff6ec32 | |||
| c42d84864e | |||
| 6b01bfaad0 | |||
| 5ae0c1c47c | |||
| 812766d96a | |||
| 31d19df4a5 | |||
| 53776438a9 | |||
| 488b932282 | |||
| 370d7905eb | |||
| 0e88c9e64c | |||
| 254e5f2d18 | |||
| e077d4b2d5 | |||
| 14c4f52a93 | |||
| 8556465796 | |||
| 37e6c35b9c | |||
| b503df3399 | |||
| 928c76fc84 | |||
| ee5394f895 | |||
| 7b85bb1aeb | |||
| 028766c82b | |||
| 266cf4070f | |||
| ab0bad4f73 | |||
| f83ef938ab |
@@ -0,0 +1,10 @@
|
||||
.git*
|
||||
|
||||
tools*
|
||||
mysql*
|
||||
letsencrypt*
|
||||
test*
|
||||
|
||||
.env*
|
||||
.github*
|
||||
LICENSE*
|
||||
@@ -1,5 +1,7 @@
|
||||
WEB_PORT=3300
|
||||
|
||||
WEB_ORIGIN=http://localhost:3001
|
||||
|
||||
DB_HOSTNAME=database
|
||||
DB_DATABASE=chat
|
||||
DB_USERNAME=chat
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
patreon: krgamestudios
|
||||
ko_fi: krgamestudios
|
||||
custom: ["https://www.paypal.com/donate/?hosted_button_id=73Q82T2ZHV8AA"]
|
||||
@@ -26,7 +26,7 @@ jobs:
|
||||
docker_image: krgamestudios/chat-server
|
||||
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@v1
|
||||
uses: docker/login-action@v1
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
+1
-2
@@ -1,5 +1,4 @@
|
||||
|
||||
FROM node:15
|
||||
FROM node:18
|
||||
WORKDIR "/app"
|
||||
COPY package*.json ./
|
||||
RUN npm install --production
|
||||
|
||||
@@ -2,13 +2,34 @@
|
||||
|
||||
An API centric chat server. Uses Sequelize and mariaDB by default.
|
||||
|
||||
This server is available via docker hub at krgamestudios/chat-server.
|
||||
|
||||
# Setup
|
||||
|
||||
There are multiple ways to run this app - it can run on it's own via `npm start` (for production) or `npm run dev` (for development). it can also run inside docker using `docker-compose up --build` - run `node configure-script.js` to generate docker-compose.yml.
|
||||
There are multiple ways to run this app - it can run on it's own via `npm start` (for production) or `npm run dev` (for development). it can also run inside docker using `docker-compose up --build` - run `node configure-script.js` to generate docker-compose.yml and startup.sql.
|
||||
|
||||
To generate an authorization token, use [auth-server](https://github.com/krgamestudios/auth-server). A public-facing development auth-server is available here (tokens are valid for 10 minutes):
|
||||
|
||||
```
|
||||
POST https://dev-auth.krgamestudios.com/auth/login HTTP/1.1
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"email": "example@example.com",
|
||||
"password": "helloworld"
|
||||
}
|
||||
```
|
||||
|
||||
# API
|
||||
|
||||
This server uses socket.io for communication. Be aware that every chat message requires a valid JWT. See the [auth-server](https://github.com/krgamestudios/auth-server) for details.
|
||||
This server uses socket.io for communication. Be aware that every 'open chat', 'message' and 'report' signal requires a valid JWT, as part of the message:
|
||||
|
||||
```js
|
||||
socket.emit('message', {
|
||||
accessToken,
|
||||
text: inputRef.current.value
|
||||
});
|
||||
```
|
||||
|
||||
The event types are as follows:
|
||||
|
||||
|
||||
+5
-3
@@ -30,10 +30,11 @@ const question = (prompt, def = null) => {
|
||||
//project configuration
|
||||
const appName = await question('App Name', 'chat');
|
||||
const appWebAddress = await question('Web Addr', `${appName}.example.com`);
|
||||
const appWebOrigin = await question('Web Origin', `https://example.com`); //TODO: clean these up properly
|
||||
const appPort = await question('App Port', '3300');
|
||||
|
||||
const appDBUser = await question('DB User', appName);
|
||||
const appDBPass = await question('DB Pass', uuid());
|
||||
const appDBPass = await question('DB Pass', 'blastoise');
|
||||
const dbRootPass = await question('DB Root Pass');
|
||||
|
||||
const appSecretAccess = await question('Access Token Secret', uuid(32));
|
||||
@@ -59,6 +60,7 @@ services:
|
||||
- "traefik.http.services.${appName}service.loadbalancer.server.port=${appPort}"
|
||||
environment:
|
||||
- WEB_PORT=${appPort}
|
||||
- WEB_ORIGIN=${appWebOrigin}
|
||||
- DB_HOSTNAME=database
|
||||
- DB_DATABASE=${appName}
|
||||
- DB_USERNAME=${appDBUser}
|
||||
@@ -76,7 +78,7 @@ services:
|
||||
MYSQL_USER: ${appDBUser}
|
||||
MYSQL_PASSWORD: ${appDBPass}
|
||||
MYSQL_ROOT_PASSWORD: ${dbRootPass}
|
||||
networks:
|
||||
networks:
|
||||
- app-network
|
||||
volumes:
|
||||
- ./mysql:/var/lib/mysql
|
||||
@@ -108,7 +110,7 @@ networks:
|
||||
`;
|
||||
|
||||
const dockerfile = `
|
||||
FROM node:15
|
||||
FROM node:18
|
||||
WORKDIR "/app"
|
||||
COPY package*.json ./
|
||||
RUN npm install --production
|
||||
|
||||
Generated
+513
-3080
File diff suppressed because it is too large
Load Diff
+8
-9
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "chat-server",
|
||||
"version": "1.0.0",
|
||||
"version": "1.4.0",
|
||||
"description": "An API centric chat server. Uses Sequelize and mariaDB by default.",
|
||||
"main": "server/server.js",
|
||||
"scripts": {
|
||||
@@ -19,16 +19,15 @@
|
||||
},
|
||||
"homepage": "https://github.com/krgamestudios/chat-server#readme",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.19.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^8.2.0",
|
||||
"express": "^4.17.1",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"mariadb": "^2.5.2",
|
||||
"sequelize": "^6.5.0",
|
||||
"socket.io": "^4.0.0"
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.2",
|
||||
"jsonwebtoken": "^9.0.0",
|
||||
"mariadb": "^3.0.2",
|
||||
"sequelize": "^6.25.8",
|
||||
"socket.io": "^4.5.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.7"
|
||||
"nodemon": "^2.0.20"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
//middleware
|
||||
const tokenAuth = require('../utilities/token-auth');
|
||||
|
||||
router.use(tokenAuth);
|
||||
router.use((req, res, next) => {
|
||||
//check the user's admin status
|
||||
if (!req.user.mod) {
|
||||
return res.status(401).send('Mods only');
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
//basic route management
|
||||
router.get('/reports', require('./reports'));
|
||||
router.delete('/reports', require('./reports-delete'));
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,15 @@
|
||||
const { chatlog, reports } = require('../database/models');
|
||||
|
||||
//admin/reports
|
||||
const route = async (req, res) => {
|
||||
const reps = await reports.destroy({
|
||||
where: {
|
||||
chatlogIndex: req.body.chatlogIndex
|
||||
}
|
||||
});
|
||||
|
||||
//respond
|
||||
res.status(200).end();
|
||||
};
|
||||
|
||||
module.exports = route;
|
||||
@@ -0,0 +1,31 @@
|
||||
const { chatlog, reports } = require('../database/models');
|
||||
|
||||
//admin/reports
|
||||
const route = async (req, res) => {
|
||||
const reps = await reports.findAll({
|
||||
include: [{
|
||||
model: chatlog,
|
||||
required: true
|
||||
}],
|
||||
order: ['chatlogIndex']
|
||||
});
|
||||
|
||||
//collate
|
||||
const response = [];
|
||||
for(let i = 0; i < reps.length; i++) {
|
||||
//new chatlog
|
||||
if (response.length == 0 || response[response.length - 1].chatlogIndex != reps[i].chatlogIndex) {
|
||||
response.push(reps[i]);
|
||||
response[response.length - 1].reporter = [response[response.length - 1].reporter]; //reporters in an array
|
||||
continue;
|
||||
}
|
||||
|
||||
//multiple people reported this, add to the existing array
|
||||
response[response.length - 1].reporter.push(reps[i].reporter);
|
||||
}
|
||||
|
||||
//respond
|
||||
res.status(200).json(response);
|
||||
};
|
||||
|
||||
module.exports = route;
|
||||
+15
-15
@@ -35,7 +35,7 @@ const chat = io => {
|
||||
});
|
||||
|
||||
//from here, handles all normal messages
|
||||
socket.on('open chat', message => {
|
||||
socket.on('open chat', async message => {
|
||||
//handle rooms - only in a room if you've opened chat
|
||||
const newlyOpened = !socket.user.room;
|
||||
socket.user.room = socket.user.room || 'general'; //default to general
|
||||
@@ -46,11 +46,8 @@ const chat = io => {
|
||||
|
||||
socket.join(socket.user.room);
|
||||
|
||||
//broadcast to this room
|
||||
socket.broadcast.to(socket.user.room).emit('message', { emphasis: true, text: `${socket.user.username} entered chat` });
|
||||
|
||||
//log
|
||||
chatlog.create({
|
||||
const log = await chatlog.create({
|
||||
notification: true,
|
||||
username: socket.user.username,
|
||||
text: `${socket.user.username} entered chat`,
|
||||
@@ -58,6 +55,9 @@ const chat = io => {
|
||||
emphasis: true
|
||||
});
|
||||
|
||||
//broadcast to this room
|
||||
socket.broadcast.to(socket.user.room).emit('message', {timestamp: log.createdAt, emphasis: true, text: `${socket.user.username} entered chat` });
|
||||
|
||||
//send backlog to the user
|
||||
chatlog.findAll({
|
||||
where: {
|
||||
@@ -72,7 +72,7 @@ const chat = io => {
|
||||
}
|
||||
},
|
||||
order: [
|
||||
['id', 'DESC']
|
||||
['index', 'DESC']
|
||||
],
|
||||
limit: 50
|
||||
})
|
||||
@@ -109,18 +109,18 @@ const chat = io => {
|
||||
return;
|
||||
}
|
||||
|
||||
//broadcast to this room
|
||||
socket.broadcast.to(socket.user.room).emit('message', { username: socket.user.username, text: message.text });
|
||||
|
||||
//log
|
||||
chatlog.create({
|
||||
const log = await chatlog.create({
|
||||
username: socket.user.username,
|
||||
text: message.text,
|
||||
room: socket.user.room
|
||||
});
|
||||
|
||||
//broadcast to this room (with the id)
|
||||
socket.broadcast.to(socket.user.room).emit('message', log);
|
||||
});
|
||||
|
||||
socket.on('disconnect', reason => {
|
||||
socket.on('disconnect', async reason => {
|
||||
//broadcast to this room
|
||||
if (!socket.user) {
|
||||
return;
|
||||
@@ -140,14 +140,14 @@ const chat = io => {
|
||||
|
||||
socket.on('report', info => {
|
||||
//handle reports of malicious content
|
||||
if (!info.id) {
|
||||
if (!info.index) {
|
||||
return;
|
||||
}
|
||||
|
||||
//report
|
||||
reports.create({
|
||||
reporter: socket.user.username,
|
||||
chatlogId: info.id
|
||||
chatlogIndex: info.index
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -199,7 +199,7 @@ const executeCommand = (io, socket, command) => {
|
||||
}
|
||||
|
||||
case '/mute': {//NOTE: mutes globally, broadcasts only to admin's room
|
||||
if (socket.user.privilege != 'administrator' && socket.user.privilege != 'moderator') {
|
||||
if (!socket.user.admin && !socket.user.mod) {
|
||||
socket.emit('message', { emphasis: true, text: '/mute is only available to admins and mods' });
|
||||
break;
|
||||
}
|
||||
@@ -242,7 +242,7 @@ const executeCommand = (io, socket, command) => {
|
||||
}
|
||||
|
||||
case '/unmute': {
|
||||
if (socket.user.privilege != 'administrator' && socket.user.privilege != 'moderator') {
|
||||
if (!socket.user.admin && !socket.user.mod) {
|
||||
socket.emit('message', { emphasis: true, text: '/unmute is only available to admins and mods' });
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ const Sequelize = require('sequelize');
|
||||
const sequelize = require('..');
|
||||
|
||||
module.exports = sequelize.define('chatlog', {
|
||||
id: {
|
||||
index: {
|
||||
type: Sequelize.INTEGER(11),
|
||||
allowNull: false,
|
||||
autoIncrement: true,
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
const chatlog = require('./chatlog');
|
||||
const mute = require('./mute');
|
||||
const reports = require('./reports');
|
||||
|
||||
//relationships
|
||||
reports.belongsTo(chatlog);
|
||||
|
||||
module.exports = {
|
||||
chatlog: require('./chatlog'),
|
||||
mute: require('./mute'),
|
||||
reports: require('./reports')
|
||||
};
|
||||
chatlog,
|
||||
mute,
|
||||
reports,
|
||||
};
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
const Sequelize = require('sequelize');
|
||||
const sequelize = require('..');
|
||||
|
||||
const chatlog = require('./chatlog');
|
||||
|
||||
const reports = sequelize.define('reports', {
|
||||
id: {
|
||||
module.exports = sequelize.define('reports', {
|
||||
index: {
|
||||
type: Sequelize.INTEGER(11),
|
||||
allowNull: false,
|
||||
autoIncrement: true,
|
||||
@@ -17,7 +15,3 @@ const reports = sequelize.define('reports', {
|
||||
allowNull: false
|
||||
},
|
||||
});
|
||||
|
||||
chatlog.hasMany(reports, { foreignKey: 'chatlogId', foreignKeyConstraint: true });
|
||||
|
||||
module.exports = reports;
|
||||
+11
-4
@@ -7,19 +7,26 @@ const app = express();
|
||||
const server = require('http').Server(app);
|
||||
const io = require('socket.io')(server, {
|
||||
cors: {
|
||||
origin: '*'
|
||||
origin: process.env.WEB_ORIGIN
|
||||
}
|
||||
});
|
||||
const bodyParser = require('body-parser');
|
||||
const cors = require('cors');
|
||||
|
||||
//config
|
||||
app.use(bodyParser.json());
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(cors({
|
||||
credentials: true,
|
||||
origin: [`${process.env.WEB_ORIGIN}`], //because auth-server
|
||||
allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'Set-Cookie'],
|
||||
exposedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'Set-Cookie'],
|
||||
}));
|
||||
|
||||
//database connection
|
||||
const database = require('./database');
|
||||
|
||||
//admin stuff
|
||||
app.use('/admin', require('./admin'));
|
||||
|
||||
//access the chat
|
||||
require('./chat')(io.of('/chat'));
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
//middleware to authenticate the JWT token
|
||||
module.exports = (req, res, next) => {
|
||||
const authHeader = req.headers['authorization'];
|
||||
const token = authHeader?.split (' ')[1]; //'Bearer token'
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).send('No token found');
|
||||
}
|
||||
|
||||
return jwt.verify(token, process.env.SECRET_ACCESS, (err, user) => {
|
||||
if (err) {
|
||||
return res.status(403).send(err);
|
||||
}
|
||||
|
||||
req.user = user;
|
||||
|
||||
return next();
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
#use this while debugging
|
||||
CREATE DATABASE IF NOT EXISTS chat;
|
||||
CREATE USER IF NOT EXISTS 'chat'@'%' IDENTIFIED BY 'blastoise';
|
||||
GRANT ALL PRIVILEGES ON chat.* TO 'chat'@'%';
|
||||
Reference in New Issue
Block a user