Skip to content

Commit

Permalink
Merge pull request #19 from versx/develop
Browse files Browse the repository at this point in the history
Release 1.5.1
  • Loading branch information
versx authored Apr 26, 2020
2 parents e7ea997 + 88353e5 commit 1f9c5ce
Show file tree
Hide file tree
Showing 34 changed files with 882 additions and 246 deletions.
1 change: 1 addition & 0 deletions migrations/6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `configs` ADD COLUMN `provider` varchar(64) NOT NULL AFTER `name`;
60 changes: 60 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"moment-timezone": "^0.5.28",
"express-session": "^1.17.1",
"i18n": "^0.9.0",
"moment-timezone": "^0.5.28",
"multer": "^1.4.2",
"mustache": "^4.0.1",
"mustache-express": "^1.3.0",
Expand Down
5 changes: 4 additions & 1 deletion src/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
"port": 9991,
"locale": "en",
"style": "dark",
"logging": true,
"secret": "-/!sup3rr4nd0m70p53cr3t70k3ny0u5h0u1dch4ng3!/-",
"logging": {
"enabled": true,
"max_size": 5
},
"db": {
"host": "127.0.0.1",
"port": 3306,
Expand Down
95 changes: 76 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ const session = require('express-session');
const bodyParser = require('body-parser');
const app = express();
const mustacheExpress = require('mustache-express');
const i18n = require('i18n');

const config = require('./config.json');
const utils = require('./utils.js');
const Device = require('./models/device.js');
const Config = require('./models/config.js');
const Log = require('./models/log.js');
const Migrator = require('./migrator.js');
const ScheduleManager = require('./models/schedule-manager.js');
const apiRoutes = require('./routes/api.js');
Expand All @@ -18,30 +21,74 @@ const timezones = require('../static/data/timezones.json');

// TODO: Create route classes
// TODO: Fix devices scroll with DataTables
// TODO: Secure /api/config/:uuid endpoint with token
// TODO: Provider option to show/hide config options
// TODO: Delete all logs button
// TODO: Secure /api/config endpoint with token
// TODO: Accomodate for # in uuid name
// TODO: Fix schedule end time
// TODO: Center align data in table columns
// TODO: Change require to import

const defaultData = {
title: config.title,
locale: config.locale,
style: config.style == 'dark' ? 'dark' : '',
logging: config.logging
};
const providers = [
{ name: 'GoCheats' },
{ name: 'Kevin' },
];

// Start database migrator
var dbMigrator = new Migrator();
dbMigrator.load();
run();

// Middleware
async function run() {
// Start database migrator
var dbMigrator = new Migrator();
dbMigrator.load();
while (dbMigrator.done === false) {
await utils.snooze(1000);
}
app.listen(config.port, config.interface, () => console.log(`Listening on port ${config.port}...`));
}

i18n.configure({
locales:['en', 'es', 'de'],
directory: path.resolve(__dirname, '../static/locales')
});

// View engine
app.set('view engine', 'mustache');
app.set('views', path.resolve(__dirname, 'views'));
app.engine('mustache', mustacheExpress());

// Static paths
app.use(express.static(path.resolve(__dirname, '../static')));
//app.use('/logs', express.static(path.resolve(__dirname, '../logs')));
app.use('/screenshots', express.static(path.resolve(__dirname, '../screenshots')));

//app.use(express.cookieParser());
app.use(i18n.init);

// register helper as a locals function wrapped as mustache expects
app.use(function (req, res, next) {
// mustache helper
res.locals.__ = function() {
/* eslint-disable no-unused-vars */
return function(text, render) {
return i18n.__.apply(req, arguments);
};
/* eslint-disable no-unused-vars */
};
next();
});

// Default mustache data shared between pages
const defaultData = require('../static/locales/' + config.locale + '.json');
defaultData.title = config.title;
defaultData.locale = config.locale;
defaultData.style = config.style == 'dark' ? 'dark' : '';
defaultData.logging = config.logging.enabled;

i18n.setLocale(config.locale);

// Body parser middlewares
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false, limit: '50mb' })); // for parsing application/x-www-form-urlencoded
//app.use(bodyParser.raw({ type: 'application/x-www-form-urlencoded' }));
app.use(express.static(path.resolve(__dirname, '../static')));
app.use('/screenshots', express.static(path.resolve(__dirname, '../screenshots')));

// Sessions middleware
app.use(session({
Expand Down Expand Up @@ -74,12 +121,14 @@ app.get(['/', '/index'], async function(req, res) {
var configs = await Config.getAll();
var schedules = ScheduleManager.getAll();
var metadata = await Migrator.getEntries();
var logsSize = Log.getTotalSize();
var data = defaultData;
data.metadata = metadata;
data.devices = devices.length;
data.configs = configs.length;
data.schedules = Object.keys(schedules).length;
data.username = username;
data.logs_size = utils.formatBytes(logsSize);
res.render('index', data);
}
});
Expand Down Expand Up @@ -110,6 +159,8 @@ app.get('/devices', function(req, res) {
});

app.get('/device/new', async function(req, res) {
var data = defaultData;
data.configs = await Config.getAll();
res.render('device-new', defaultData);
});

Expand Down Expand Up @@ -173,7 +224,9 @@ app.get('/config/assign/:uuid', async function(req, res) {
});

app.get('/config/new', function(req, res) {
res.render('config-new', defaultData);
var data = defaultData;
data.providers = providers;
res.render('config-new', data);
});

app.get('/config/edit/:name', async function(req, res) {
Expand All @@ -183,6 +236,11 @@ app.get('/config/edit/:name', async function(req, res) {
data.title = config.title;
data.old_name = name;
data.name = c.name;
data.providers = providers;
data.providers.forEach(function(provider) {
provider.selected = provider.name === c.provider;
});
data.gocheats_selected = c.provider === data.providers[0].name;
data.backend_url = c.backendUrl;
data.data_endpoints = c.dataEndpoints;
data.token = c.token;
Expand Down Expand Up @@ -280,9 +338,8 @@ app.get('/settings', function(req, res) {
data.languages.forEach(function(locale) {
locale.selected = locale.name === config.locale;
});
data.logging = config.logging ? 'checked' : '';
data.logging = config.logging.enabled ? 'checked' : '';
data.max_size = config.logging.max_size;
console.log('Settings:', data);
res.render('settings', data);
});

app.listen(config.port, config.interface, () => console.log(`Listening on port ${config.port}...`));
});
8 changes: 5 additions & 3 deletions src/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const migrationsDir = path.resolve(__dirname, '../migrations');

class Migrator {
constructor() {
this.done = false;
}
async load() {
var count = 1;
Expand Down Expand Up @@ -123,11 +124,12 @@ class Migrator {
process.exit(-1);
});
console.log('[DBController] Migration successful');
if (newVersion === toVersion) {
console.log('[DBController] Migration done');
}
this.migrate(newVersion, toVersion);
}
if (fromVersion === toVersion) {
console.log('[DBController] Migration done');
this.done = true;
}
}
backup() {
// TODO: Migrator backup
Expand Down
22 changes: 13 additions & 9 deletions src/models/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
const query = require('../db.js');

class Config {
constructor(name, backendUrl, dataEndpoints, token, heartbeatMaxTime, minDelayLogout,
constructor(name, provider, backendUrl, dataEndpoints, token, heartbeatMaxTime, minDelayLogout,
accountManager, deployEggs, nearbyTracker, autoLogin, isDefault) {
this.name = name;
this.provider = provider;
this.backendUrl = backendUrl;
this.dataEndpoints = dataEndpoints;
this.token = token;
Expand All @@ -23,7 +24,7 @@ class Config {
}
static async getByName(name) {
var sql = `
SELECT backend_url, data_endpoints, token, heartbeat_max_time, min_delay_logout,
SELECT backend_url, provider, data_endpoints, token, heartbeat_max_time, min_delay_logout,
account_manager, deploy_eggs, nearby_tracker, auto_login, is_default
FROM configs
WHERE name = ?
Expand All @@ -36,6 +37,7 @@ class Config {
var c = result[0];
var data = new Config(
name,
c.provider,
c.backend_url,
c.data_endpoints,
c.token,
Expand All @@ -49,13 +51,13 @@ class Config {
);
return data;
}
static async create(name, backendUrl, dataEndpoints, token, heartbeatMaxTime, minDelayLogout,
static async create(name, provider, backendUrl, dataEndpoints, token, heartbeatMaxTime, minDelayLogout,
accountManager, deployEggs, nearbyTracker, autoLogin, isDefault) {
var sql = `
INSERT INTO configs (name, backend_url, data_endpoints, token, heartbeat_max_time, min_delay_logout,
INSERT INTO configs (name, provider, backend_url, data_endpoints, token, heartbeat_max_time, min_delay_logout,
account_manager, deploy_eggs, nearby_tracker, auto_login, is_default)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
var args = [name, backendUrl, dataEndpoints, token, heartbeatMaxTime, minDelayLogout,
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
var args = [name, provider, backendUrl, dataEndpoints, token, heartbeatMaxTime, minDelayLogout,
accountManager, deployEggs, nearbyTracker, autoLogin, isDefault];
var result = await query(sql, args);
return result.affectedRows === 1;
Expand All @@ -68,7 +70,7 @@ class Config {
}
static async getDefault() {
var sql = `
SELECT name, backend_url, data_endpoints, token, heartbeat_max_time, min_delay_logout,
SELECT name, provider, backend_url, data_endpoints, token, heartbeat_max_time, min_delay_logout,
account_manager, deploy_eggs, nearby_tracker, auto_login, is_default
FROM configs
WHERE is_default = 1
Expand All @@ -80,6 +82,7 @@ class Config {
var c = result[0];
var data = new Config(
c.name,
c.provider,
c.backend_url,
c.data_endpoints,
c.token,
Expand All @@ -95,7 +98,7 @@ class Config {
}
static async setDefault(name) {
var sql = `
UPDATE dcm.configs
UPDATE configs
SET is_default = IF(name = ?, 1, 0);`;
var args = [name];
var result = await query(sql, args);
Expand All @@ -104,11 +107,12 @@ class Config {
async save(oldName) {
var sql = `
UPDATE configs
SET name=?, backend_url=?, data_endpoints=?, token=?, heartbeat_max_time=?, min_delay_logout=?,
SET name=?, provider=?, backend_url=?, data_endpoints=?, token=?, heartbeat_max_time=?, min_delay_logout=?,
account_manager=?, deploy_eggs=?, nearby_tracker=?, auto_login=?, is_default=?
WHERE name=?`;
var args = [
this.name,
this.provider,
this.backendUrl,
this.dataEndpoints,
this.token,
Expand Down
Loading

0 comments on commit 1f9c5ce

Please sign in to comment.