retropilot-server/src/server/router/api/devices.js

105 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-03-20 17:59:37 -06:00
import bodyParser from 'body-parser';
2022-01-12 08:02:30 -07:00
import crypto from 'crypto';
import dirTree from 'directory-tree';
2022-03-20 17:59:37 -06:00
import express from 'express';
2022-03-02 19:29:13 -07:00
import log4js from 'log4js';
2022-01-07 18:35:55 -07:00
2022-03-22 07:03:17 -06:00
import { isAuthenticated } from '../../middlewares/authentication';
2022-03-20 17:59:37 -06:00
import deviceController from '../../controllers/devices';
import { MutateDevice } from '../../schema/routes/devices';
2022-03-02 19:29:13 -07:00
2022-03-22 09:14:08 -06:00
const logger = log4js.getLogger();
2022-03-02 19:29:13 -07:00
2022-03-22 07:03:17 -06:00
// /api/devices
2022-01-12 08:02:30 -07:00
const router = express.Router();
2022-03-20 17:59:37 -06:00
2022-03-22 07:03:17 -06:00
router.get('/', isAuthenticated, async (req, res) => {
2022-01-07 18:35:55 -07:00
const dongles = await deviceController.getDevices(req.account.id);
2022-01-07 18:35:55 -07:00
return res.json({ success: true, data: dongles });
});
2022-01-12 08:02:30 -07:00
/*
{
2022-03-20 17:59:37 -06:00
version: "1.0"
2fa: {
tokenProvided: false,
token: 000000
unixTime: 00000
},
modifications: {
nicname: x
publicKey: x
}
2022-01-12 08:02:30 -07:00
}
*/
2022-03-22 07:03:17 -06:00
router.put('/:dongle_id/', [isAuthenticated, bodyParser.json()], async (req, res) => {
2022-01-12 08:02:30 -07:00
const { body } = req;
2022-03-21 17:38:56 -06:00
logger.info(MutateDevice.isValid(body));
2022-03-20 17:59:37 -06:00
// TODO: response?
return res.json({ success: true });
2022-01-12 08:02:30 -07:00
});
2022-03-22 07:03:17 -06:00
router.get('/:dongle_id/drives/:drive_identifier/segment', isAuthenticated, async (req, res) => {
2022-01-07 18:35:55 -07:00
const dongleId = req.params.dongle_id;
const accountId = req.account.id;
const isUserAuthorised = await deviceController.isUserAuthorised(dongleId, accountId);
2022-01-06 13:07:25 -07:00
2022-03-22 07:03:17 -06:00
// TODO reduce data returned
2022-01-07 18:35:55 -07:00
if (isUserAuthorised.success === false || isUserAuthorised.data.authorised === false) {
return res.json({ success: false, msg: isUserAuthorised.msg });
}
const dongleIdHash = crypto.createHmac('sha256', process.env.APP_SALT).update(req.params.dongle_id).digest('hex');
const driveIdentifierHash = crypto.createHmac('sha256', process.env.APP_SALT).update(req.params.drive_identifier).digest('hex');
2022-01-06 13:07:25 -07:00
const directoryTree = dirTree(`${process.env.STORAGE_PATH + req.params.dongle_id}/${dongleIdHash}/${driveIdentifierHash}/${req.params.drive_identifier}`);
2022-01-06 13:07:25 -07:00
2022-01-07 18:35:55 -07:00
return res.json({ success: true, msg: 'ok', data: directoryTree });
});
2022-01-06 13:07:25 -07:00
2022-03-22 07:03:17 -06:00
router.get('/:dongle_id/drives/:deleted', isAuthenticated, async (req, res) => {
2022-01-07 18:35:55 -07:00
const dongleId = req.params.dongle_id;
const accountId = req.account.id;
const isUserAuthorised = await deviceController.isUserAuthorised(dongleId, accountId);
2022-03-22 07:03:17 -06:00
// TODO reduce data returned
2022-01-07 18:35:55 -07:00
if (isUserAuthorised.success === false || isUserAuthorised.data.authorised === false) {
return res.json({ success: false, msg: isUserAuthorised.msg });
}
const dongles = await deviceController.getDrives(req.params.dongle_id, req.params.deleted === 'true', true);
return res.json({ success: true, data: dongles });
});
2022-03-22 07:03:17 -06:00
router.get('/:dongle_id/bootlogs', isAuthenticated, async (req, res) => {
2022-01-07 18:35:55 -07:00
const dongleId = req.params.dongle_id;
const accountId = req.account.id;
const isUserAuthorised = await deviceController.isUserAuthorised(dongleId, accountId);
2022-03-22 07:03:17 -06:00
// TODO reduce data returned
2022-01-07 18:35:55 -07:00
if (isUserAuthorised.success === false || isUserAuthorised.data.authorised === false) {
return res.json({ success: false, msg: isUserAuthorised.msg });
}
const bootlogs = await deviceController.getBootlogs(req.params.dongle_id);
return res.json({ success: true, data: bootlogs });
});
2022-03-22 07:03:17 -06:00
router.get('/:dongle_id/crashlogs', isAuthenticated, async (req, res) => {
2022-01-07 18:35:55 -07:00
const dongleId = req.params.dongle_id;
const accountId = req.account.id;
const isUserAuthorised = await deviceController.isUserAuthorised(dongleId, accountId);
2022-03-22 07:03:17 -06:00
// TODO reduce data returned
2022-01-07 18:35:55 -07:00
if (isUserAuthorised.success === false || isUserAuthorised.data.authorised === false) {
return res.json({ success: false, msg: isUserAuthorised.msg });
}
2022-01-12 08:02:30 -07:00
const crashlogs = await deviceController.getCrashlogs(req.params.dongle_id);
2022-01-07 18:35:55 -07:00
2022-01-12 08:02:30 -07:00
return res.json({ success: true, data: crashlogs });
2022-01-07 18:35:55 -07:00
});
2022-01-12 08:02:30 -07:00
export default router;