Socket connection now works

- Pairing a new device works

(I did a lot since the last commit)
This commit is contained in:
2021-11-14 01:42:21 +01:00
parent 28e85ea730
commit 19b7c05d75
72 changed files with 5861 additions and 23719 deletions

View File

@@ -7,14 +7,15 @@ import * as figlet from 'figlet';
import * as mongoose from 'mongoose';
import { exit } from 'process';
import * as winston from 'winston';
import { createServer } from 'http';
import { config } from './config';
import { GetBeat, GetBeatStats } from './endpoints/beat';
import { getNotification } from './endpoints/notification';
import { GetPhone, PostPhone } from './endpoints/phone';
import { DeleteUser, GetUser, LoginRabbitUser, LoginUser, MW_User, PatchUser, PostUser, Resource, Topic, VHost } from './endpoints/user';
import { DeleteUser, GetUser, LoginUser, MW_User, PatchUser, PostUser } from './endpoints/user';
import { hashPassword, randomPepper, randomString } from './lib/crypto';
import { RabbitMQ } from './lib/rabbit';
import { SocketManager } from './lib/socketio';
import { UserType } from './models/user/user.interface';
import { User } from './models/user/user.model';
@@ -27,7 +28,6 @@ export const JWT_SECRET = process.env.JWT_SECRET || "";
export const IS_DEBUG = process.env.DEBUG == 'true';
export let logger: winston.Logger;
export let rabbitmq: RabbitMQ;
async function run() {
const { combine, timestamp, label, printf, prettyPrint } = winston.format;
@@ -108,10 +108,8 @@ async function run() {
await User.create({
name: 'admin',
password: await hashPassword(randomPassword + salt + randomPepper()),
brokerToken: randomString(16),
salt,
createdAt: Date.now(),
lastLogin: 0,
lastLogin: new Date(0),
type: UserType.ADMIN
});
logger.info("===================================================");
@@ -124,14 +122,23 @@ async function run() {
/**
* HTTP server
*/
logger.debug("Preparing HTTP server ...")
const app = express();
app.use(express.json());
const server = createServer(app);
app.use(cors());
app.options('*', cors());
app.use(express.json());
app.use(bodyParser.json({ limit: '5kb' }));
app.use((req, res, next) => {
res.on('finish', () => {
const done = Date.now();
// Censor any user passwords
if (req.body.password != null) {
req.body.password = "***********";
}
logger.debug(`${req.method} - ${req.url} ${JSON.stringify(req.body)} -> ${res.statusCode}`);
});
next();
@@ -141,10 +148,6 @@ async function run() {
// User authentication
app.post('/user/login', (req, res) => LoginUser(req, res));
app.get('/user/rabbitlogin', (req, res) => LoginRabbitUser(req, res));
app.get('/user/vhost', (req, res) => VHost(req, res));
app.get('/user/resource', (req, res) => Resource(req, res));
app.get('/user/topic', (req, res) => Topic(req, res));
// CRUD user
app.get('/user/notification', MW_User, (req, res) => getNotification(req, res)); // Notifications
@@ -163,16 +166,11 @@ async function run() {
app.get('/beat/', MW_User, (req, res) => GetBeat(req, res));
app.get('/beat/stats', MW_User, (req, res) => GetBeatStats(req, res));
app.listen(config.http.port, config.http.host, () => {
const socketManager = new SocketManager(server);
server.listen(config.http.port, config.http.host, () => {
logger.info(`HTTP server is running at ${config.http.host}:${config.http.port}`);
});
/**
* Message broker
*/
rabbitmq = new RabbitMQ();
await rabbitmq.init();
logger.info("Connected with message broker.");
}
run();