Compare commits
74 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9935bc9c2f | |||
| e7b631c9c5 | |||
| b6bf405a71 | |||
| cf40c7a92c | |||
| bddff784b7 | |||
| bcd09a58bc | |||
| 47de3e4330 | |||
| db66d61abe | |||
| f5fceb5c4e | |||
| b4b490dc6f | |||
| 496048f62b | |||
| 2e8c3f98ca | |||
| 582c0b453c | |||
| 9db6dbf63b | |||
| 3700372e92 | |||
| 416ab2f3f9 | |||
| 1b5cbaea17 | |||
| 0f2b8d3f52 | |||
| 88c2239fdb | |||
| b5f9d0a7fc | |||
| 1ec29e4519 | |||
| a15c43b3d0 | |||
| 9bc96bdb5f | |||
| 4bdcee11ea | |||
| e1cd1ec001 | |||
| e8a9a79687 | |||
| 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 | |||
| 0b5cc49e6e |
@@ -0,0 +1,10 @@
|
|||||||
|
.git*
|
||||||
|
|
||||||
|
tools*
|
||||||
|
mysql*
|
||||||
|
letsencrypt*
|
||||||
|
test*
|
||||||
|
|
||||||
|
.env*
|
||||||
|
.github*
|
||||||
|
LICENSE*
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
WEB_PORT=3300
|
WEB_PORT=3300
|
||||||
|
|
||||||
DB_HOSTNAME=database
|
WEB_ORIGIN=http://localhost:3001
|
||||||
|
|
||||||
|
DB_HOSTNAME=localhost
|
||||||
|
DB_PORTNAME=3306
|
||||||
|
|
||||||
DB_DATABASE=chat
|
DB_DATABASE=chat
|
||||||
DB_USERNAME=chat
|
DB_USERNAME=chat
|
||||||
DB_PASSWORD=blastoise
|
DB_PASSWORD=blastoise
|
||||||
|
|||||||
@@ -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"]
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "npm"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "weekly"
|
||||||
|
|
||||||
|
|
||||||
@@ -5,19 +5,21 @@ on:
|
|||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- v1.*
|
- v1.*
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
push_to_registry:
|
push_to_registry:
|
||||||
name: Push Docker Image to Docker Hub
|
name: Push Docker Image to Docker Hub
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check Out The Repo
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Setup QEMU
|
||||||
uses: docker/setup-qemu-action@v1
|
uses: docker/setup-qemu-action@v3
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v1
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
- name: Get Smart Tag
|
- name: Get Smart Tag
|
||||||
id: prepare
|
id: prepare
|
||||||
@@ -26,13 +28,13 @@ jobs:
|
|||||||
docker_image: krgamestudios/chat-server
|
docker_image: krgamestudios/chat-server
|
||||||
|
|
||||||
- name: Login to DockerHub
|
- name: Login to DockerHub
|
||||||
uses: docker/login-action@v1
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
- name: Push to Docker Hub
|
- name: Push to Docker Hub
|
||||||
uses: docker/build-push-action@v2
|
uses: docker/build-push-action@v6
|
||||||
with:
|
with:
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ steps.prepare.outputs.tag }}
|
tags: ${{ steps.prepare.outputs.tag }}
|
||||||
|
|||||||
+3
-3
@@ -1,8 +1,8 @@
|
|||||||
|
|
||||||
FROM node:15
|
FROM node:22-bookworm-slim
|
||||||
WORKDIR "/app"
|
WORKDIR "/app"
|
||||||
COPY package*.json ./
|
COPY package*.json /app
|
||||||
RUN npm install --production
|
RUN npm install --omit=dev
|
||||||
COPY . /app
|
COPY . /app
|
||||||
EXPOSE 3300
|
EXPOSE 3300
|
||||||
USER node
|
USER node
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2021 Kayne Ruse, KR Game Studios
|
Copyright (c) 2021-2023 Kayne Ruse, KR Game Studios
|
||||||
|
|
||||||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,34 @@
|
|||||||
|
|
||||||
An API centric chat server. Uses Sequelize and mariaDB by default.
|
An API centric chat server. Uses Sequelize and mariaDB by default.
|
||||||
|
|
||||||
|
This server is available via docker hub at krgamestudios/chat-server.
|
||||||
|
|
||||||
# Setup
|
# 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
|
# 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:
|
The event types are as follows:
|
||||||
|
|
||||||
@@ -19,6 +40,7 @@ on 'error' -> Server emits and logs an error
|
|||||||
on 'open chat' -> Preps the server for your messages, places you in the room 'general'
|
on 'open chat' -> Preps the server for your messages, places you in the room 'general'
|
||||||
on 'message' -> Server broadcasts to all other users in your room
|
on 'message' -> Server broadcasts to all other users in your room
|
||||||
on 'disconnect' -> Server will no longer accept your messages
|
on 'disconnect' -> Server will no longer accept your messages
|
||||||
|
on 'report' -> Report the chatlog with the index 'id'
|
||||||
|
|
||||||
|
|
||||||
Chat Commands:
|
Chat Commands:
|
||||||
|
|||||||
+59
-28
@@ -30,10 +30,30 @@ const question = (prompt, def = null) => {
|
|||||||
//project configuration
|
//project configuration
|
||||||
const appName = await question('App Name', 'chat');
|
const appName = await question('App Name', 'chat');
|
||||||
const appWebAddress = await question('Web Addr', `${appName}.example.com`);
|
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 appPort = await question('App Port', '3300');
|
||||||
|
|
||||||
|
//configure the database address
|
||||||
|
let dbLocation = '';
|
||||||
|
while (typeof dbLocation != 'string' || /^[le]/i.test(dbLocation[0]) == false) {
|
||||||
|
dbLocation = await question('[l]ocal or [e]xternal database?');
|
||||||
|
}
|
||||||
|
|
||||||
|
let appDBHost = '';
|
||||||
|
let appDBPort = '';
|
||||||
|
|
||||||
|
if (/^[l]/i.test(dbLocation[0])) {
|
||||||
|
appDBHost = 'database';
|
||||||
|
appDBPort = '3306';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
appDBHost = await question('DB Host');
|
||||||
|
appDBPort = await question('DB Port', '3306');
|
||||||
|
}
|
||||||
|
|
||||||
|
//configure the database account
|
||||||
const appDBUser = await question('DB User', appName);
|
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 dbRootPass = await question('DB Root Pass');
|
||||||
|
|
||||||
const appSecretAccess = await question('Access Token Secret', uuid(32));
|
const appSecretAccess = await question('Access Token Secret', uuid(32));
|
||||||
@@ -42,37 +62,42 @@ const question = (prompt, def = null) => {
|
|||||||
|
|
||||||
//generate the files
|
//generate the files
|
||||||
const ymlfile = `
|
const ymlfile = `
|
||||||
version: '3'
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
${appName}:
|
${appName}:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
ports:
|
ports:
|
||||||
- "${appPort}"
|
- ${appPort}
|
||||||
labels:
|
labels:
|
||||||
- "traefik.enable=true"
|
- traefik.enable=true
|
||||||
- "traefik.http.routers.${appName}router.rule=Host(\`${appWebAddress}\`)"
|
- traefik.http.routers.${appName}router.rule=Host(\`${appWebAddress}\`)
|
||||||
- "traefik.http.routers.${appName}router.entrypoints=websecure"
|
- traefik.http.routers.${appName}router.entrypoints=websecure
|
||||||
- "traefik.http.routers.${appName}router.tls.certresolver=myresolver"
|
- traefik.http.routers.${appName}router.tls.certresolver=myresolver
|
||||||
- "traefik.http.routers.${appName}router.service=${appName}service@docker"
|
- traefik.http.routers.${appName}router.service=${appName}service@docker
|
||||||
- "traefik.http.services.${appName}service.loadbalancer.server.port=${appPort}"
|
- traefik.http.services.${appName}service.loadbalancer.server.port=${appPort}
|
||||||
environment:
|
environment:
|
||||||
- WEB_PORT=${appPort}
|
- WEB_PORT=${appPort}
|
||||||
- DB_HOSTNAME=database
|
- WEB_ORIGIN=${appWebOrigin}
|
||||||
|
- DB_HOSTNAME=${appDBHost}
|
||||||
|
- DB_PORTNAME=${appDBPort}
|
||||||
- DB_DATABASE=${appName}
|
- DB_DATABASE=${appName}
|
||||||
- DB_USERNAME=${appDBUser}
|
- DB_USERNAME=${appDBUser}
|
||||||
- DB_PASSWORD=${appDBPass}
|
- DB_PASSWORD=${appDBPass}
|
||||||
- DB_TIMEZONE=Australia/Sydney
|
- DB_TIMEZONE=Australia/Sydney
|
||||||
- SECRET_ACCESS=${appSecretAccess}
|
- SECRET_ACCESS=${appSecretAccess}
|
||||||
|
volumes:
|
||||||
|
- /etc/timezone:/etc/timezone:ro
|
||||||
|
- /etc/localtime:/etc/localtime:ro
|
||||||
networks:
|
networks:
|
||||||
- app-network
|
- app-network${ appDBHost != 'database' ? '' : `
|
||||||
depends_on:
|
depends_on:
|
||||||
- database
|
- database
|
||||||
|
|
||||||
database:
|
database:
|
||||||
image: mariadb:latest
|
image: mariadb:latest
|
||||||
environment:
|
environment:
|
||||||
MYSQL_DATABASE: ${appName}
|
MYSQL_DATABASE: ${appName}
|
||||||
|
MYSQL_TCP_PORT: ${appDBPort}
|
||||||
MYSQL_USER: ${appDBUser}
|
MYSQL_USER: ${appDBUser}
|
||||||
MYSQL_PASSWORD: ${appDBPass}
|
MYSQL_PASSWORD: ${appDBPass}
|
||||||
MYSQL_ROOT_PASSWORD: ${dbRootPass}
|
MYSQL_ROOT_PASSWORD: ${dbRootPass}
|
||||||
@@ -81,37 +106,43 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- ./mysql:/var/lib/mysql
|
- ./mysql:/var/lib/mysql
|
||||||
- ./startup.sql:/docker-entrypoint-initdb.d/startup.sql:ro
|
- ./startup.sql:/docker-entrypoint-initdb.d/startup.sql:ro
|
||||||
|
- /etc/timezone:/etc/timezone:ro
|
||||||
|
- /etc/localtime:/etc/localtime:ro`}
|
||||||
|
|
||||||
traefik_${appName}:
|
traefik_${appName}:
|
||||||
container_name: ${appName}_traefik
|
container_name: ${appName}_traefik
|
||||||
image: "traefik:v2.4"
|
image: traefik:v2.10
|
||||||
container_name: "traefik"
|
container_name: traefik
|
||||||
command:
|
command:
|
||||||
- "--log.level=ERROR"
|
- --log.level=ERROR
|
||||||
- "--api.insecure=false"
|
- --api.insecure=false
|
||||||
- "--providers.docker=true"
|
- --providers.docker=true
|
||||||
- "--providers.docker.exposedbydefault=false"
|
- --providers.docker.exposedbydefault=false
|
||||||
- "--entrypoints.websecure.address=:443"
|
- --entrypoints.websecure.address=:443
|
||||||
- "--certificatesresolvers.myresolver.acme.tlschallenge=true"
|
- --certificatesresolvers.myresolver.acme.tlschallenge=true
|
||||||
- "--certificatesresolvers.myresolver.acme.email=${supportEmail}"
|
- --certificatesresolvers.myresolver.acme.email=${supportEmail}
|
||||||
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
|
- --certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- 80:80
|
||||||
- "443:443"
|
- 443:443
|
||||||
volumes:
|
volumes:
|
||||||
- "./letsencrypt:/letsencrypt"
|
- ./letsencrypt:/letsencrypt
|
||||||
- "/var/run/docker.sock:/var/run/docker.sock:ro"
|
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||||
|
- /etc/timezone:/etc/timezone:ro
|
||||||
|
- /etc/localtime:/etc/localtime:ro
|
||||||
networks:
|
networks:
|
||||||
- app-network
|
- app-network
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
app-network:
|
app-network:
|
||||||
driver: bridge
|
driver: bridge
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const dockerfile = `
|
const dockerfile = `
|
||||||
FROM node:15
|
FROM node:22-bookworm-slim
|
||||||
WORKDIR "/app"
|
WORKDIR "/app"
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install --production
|
RUN npm install --omit=dev
|
||||||
COPY . /app
|
COPY . /app
|
||||||
EXPOSE ${appPort}
|
EXPOSE ${appPort}
|
||||||
USER node
|
USER node
|
||||||
|
|||||||
Generated
+1092
-3334
File diff suppressed because it is too large
Load Diff
+9
-10
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "chat-server",
|
"name": "chat-server",
|
||||||
"version": "1.0.0",
|
"version": "1.5.5",
|
||||||
"description": "An API centric chat server. Uses Sequelize and mariaDB by default.",
|
"description": "An API centric chat server. Uses Sequelize and mariaDB by default.",
|
||||||
"main": "server/server.js",
|
"main": "server/server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -13,22 +13,21 @@
|
|||||||
"url": "git+https://github.com/krgamestudios/chat-server.git"
|
"url": "git+https://github.com/krgamestudios/chat-server.git"
|
||||||
},
|
},
|
||||||
"author": "Kayne Ruse",
|
"author": "Kayne Ruse",
|
||||||
"license": "ISC",
|
"license": "Zlib",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/krgamestudios/chat-server/issues"
|
"url": "https://github.com/krgamestudios/chat-server/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/krgamestudios/chat-server#readme",
|
"homepage": "https://github.com/krgamestudios/chat-server#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.19.0",
|
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^17.2.3",
|
||||||
"express": "^4.17.1",
|
"express": "^5.2.1",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^9.0.3",
|
||||||
"mariadb": "^2.5.2",
|
"mariadb": "^3.4.5",
|
||||||
"sequelize": "^6.5.0",
|
"sequelize": "^6.37.7",
|
||||||
"socket.io": "^4.0.0"
|
"socket.io": "^4.8.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^2.0.7"
|
"nodemon": "^3.1.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
+28
-15
@@ -1,13 +1,13 @@
|
|||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
const { Op } = require('sequelize');
|
const { Op } = require('sequelize');
|
||||||
const { chatlog, mute } = require('../database/models');
|
const { chatlog, mute, reports } = require('../database/models');
|
||||||
|
|
||||||
const chat = io => {
|
const chat = io => {
|
||||||
io.on('connection', socket => {
|
io.on('connection', socket => {
|
||||||
//middleware
|
//middleware
|
||||||
socket.use((request, next) => {
|
socket.use((request, next) => {
|
||||||
//verify request format
|
//verify request format
|
||||||
if (!['open chat', 'message'].includes(request[0])) {
|
if (!['open chat', 'message', 'report'].includes(request[0])) {
|
||||||
return next(`Invalid request to the chat server ${request[0]}`);
|
return next(`Invalid request to the chat server ${request[0]}`);
|
||||||
}
|
}
|
||||||
return next();
|
return next();
|
||||||
@@ -35,7 +35,7 @@ const chat = io => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//from here, handles all normal messages
|
//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
|
//handle rooms - only in a room if you've opened chat
|
||||||
const newlyOpened = !socket.user.room;
|
const newlyOpened = !socket.user.room;
|
||||||
socket.user.room = socket.user.room || 'general'; //default to general
|
socket.user.room = socket.user.room || 'general'; //default to general
|
||||||
@@ -46,11 +46,8 @@ const chat = io => {
|
|||||||
|
|
||||||
socket.join(socket.user.room);
|
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
|
//log
|
||||||
chatlog.create({
|
const log = await chatlog.create({
|
||||||
notification: true,
|
notification: true,
|
||||||
username: socket.user.username,
|
username: socket.user.username,
|
||||||
text: `${socket.user.username} entered chat`,
|
text: `${socket.user.username} entered chat`,
|
||||||
@@ -58,6 +55,9 @@ const chat = io => {
|
|||||||
emphasis: true
|
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
|
//send backlog to the user
|
||||||
chatlog.findAll({
|
chatlog.findAll({
|
||||||
where: {
|
where: {
|
||||||
@@ -72,7 +72,7 @@ const chat = io => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
order: [
|
order: [
|
||||||
['id', 'DESC']
|
['index', 'DESC']
|
||||||
],
|
],
|
||||||
limit: 50
|
limit: 50
|
||||||
})
|
})
|
||||||
@@ -109,18 +109,18 @@ const chat = io => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//broadcast to this room
|
|
||||||
socket.broadcast.to(socket.user.room).emit('message', { username: socket.user.username, text: message.text });
|
|
||||||
|
|
||||||
//log
|
//log
|
||||||
chatlog.create({
|
const log = await chatlog.create({
|
||||||
username: socket.user.username,
|
username: socket.user.username,
|
||||||
text: message.text,
|
text: message.text,
|
||||||
room: socket.user.room
|
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
|
//broadcast to this room
|
||||||
if (!socket.user) {
|
if (!socket.user) {
|
||||||
return;
|
return;
|
||||||
@@ -137,6 +137,19 @@ const chat = io => {
|
|||||||
emphasis: true
|
emphasis: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('report', info => {
|
||||||
|
//handle reports of malicious content
|
||||||
|
if (!info.index) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//report
|
||||||
|
reports.create({
|
||||||
|
reporter: socket.user.username,
|
||||||
|
chatlogIndex: info.index
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -186,7 +199,7 @@ const executeCommand = (io, socket, command) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case '/mute': {//NOTE: mutes globally, broadcasts only to admin's room
|
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' });
|
socket.emit('message', { emphasis: true, text: '/mute is only available to admins and mods' });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -229,7 +242,7 @@ const executeCommand = (io, socket, command) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case '/unmute': {
|
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' });
|
socket.emit('message', { emphasis: true, text: '/unmute is only available to admins and mods' });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,10 @@ const Sequelize = require('sequelize');
|
|||||||
|
|
||||||
const sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
|
const sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
|
||||||
host: process.env.DB_HOSTNAME,
|
host: process.env.DB_HOSTNAME,
|
||||||
|
port: process.env.DB_PORTNAME,
|
||||||
dialect: 'mariadb',
|
dialect: 'mariadb',
|
||||||
timezone: process.env.DB_TIMEZONE,
|
timezone: process.env.DB_TIMEZONE,
|
||||||
logging: process.env.DB_LOGGING ? console.log : false
|
logging: process.env.DB_LOGGING ? console.log : false
|
||||||
});
|
});
|
||||||
|
|
||||||
sequelize.sync();
|
|
||||||
|
|
||||||
module.exports = sequelize;
|
module.exports = sequelize;
|
||||||
@@ -2,7 +2,7 @@ const Sequelize = require('sequelize');
|
|||||||
const sequelize = require('..');
|
const sequelize = require('..');
|
||||||
|
|
||||||
module.exports = sequelize.define('chatlog', {
|
module.exports = sequelize.define('chatlog', {
|
||||||
id: {
|
index: {
|
||||||
type: Sequelize.INTEGER(11),
|
type: Sequelize.INTEGER(11),
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
autoIncrement: true,
|
autoIncrement: true,
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
|
const chatlog = require('./chatlog');
|
||||||
|
const mute = require('./mute');
|
||||||
|
const reports = require('./reports');
|
||||||
|
|
||||||
|
//relationships
|
||||||
|
reports.belongsTo(chatlog);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
chatlog: require('./chatlog'),
|
chatlog,
|
||||||
mute: require('./mute')
|
mute,
|
||||||
|
reports,
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
const Sequelize = require('sequelize');
|
||||||
|
const sequelize = require('..');
|
||||||
|
|
||||||
|
module.exports = sequelize.define('reports', {
|
||||||
|
index: {
|
||||||
|
type: Sequelize.INTEGER(11),
|
||||||
|
allowNull: false,
|
||||||
|
autoIncrement: true,
|
||||||
|
primaryKey: true,
|
||||||
|
unique: true
|
||||||
|
},
|
||||||
|
|
||||||
|
reporter: {
|
||||||
|
type: 'varchar(320)',
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
|
});
|
||||||
+13
-5
@@ -7,24 +7,31 @@ const app = express();
|
|||||||
const server = require('http').Server(app);
|
const server = require('http').Server(app);
|
||||||
const io = require('socket.io')(server, {
|
const io = require('socket.io')(server, {
|
||||||
cors: {
|
cors: {
|
||||||
origin: '*'
|
origin: process.env.WEB_ORIGIN
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const bodyParser = require('body-parser');
|
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
|
|
||||||
//config
|
//config
|
||||||
app.use(bodyParser.json());
|
app.use(express.json());
|
||||||
app.use(cors());
|
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
|
//database connection
|
||||||
const database = require('./database');
|
const database = require('./database');
|
||||||
|
|
||||||
|
//admin stuff
|
||||||
|
app.use('/admin', require('./admin'));
|
||||||
|
|
||||||
//access the chat
|
//access the chat
|
||||||
require('./chat')(io.of('/chat'));
|
require('./chat')(io.of('/chat'));
|
||||||
|
|
||||||
//error on access
|
//error on access
|
||||||
app.get('*', (req, res) => {
|
app.get('/{*any}', (req, res) => {
|
||||||
res.redirect('https://github.com/krgamestudios/chat-server');
|
res.redirect('https://github.com/krgamestudios/chat-server');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -32,4 +39,5 @@ app.get('*', (req, res) => {
|
|||||||
server.listen(process.env.WEB_PORT || 3300, async (err) => {
|
server.listen(process.env.WEB_PORT || 3300, async (err) => {
|
||||||
await database.sync();
|
await database.sync();
|
||||||
console.log(`listening to localhost:${process.env.WEB_PORT || 3300}`);
|
console.log(`listening to localhost:${process.env.WEB_PORT || 3300}`);
|
||||||
|
console.log(`database located at ${process.env.DB_HOSTNAME || '<default>'}:${process.env.DB_PORTNAME || '<default>'}`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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 chat;
|
||||||
|
CREATE USER 'chat'@'%' IDENTIFIED BY 'blastoise';
|
||||||
|
GRANT ALL PRIVILEGES ON chat.* TO 'chat'@'%';
|
||||||
Reference in New Issue
Block a user