2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
2020-07-01 15:00:40 -03:00
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
|
2019-03-09 16:59:31 -03:00
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
|
2018-04-21 02:59:30 -03:00
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| Licensed under the AGPL-3.0. |
|
2018-11-11 19:09:18 -02:00
|
|
|
| |
|
2018-04-21 02:59:30 -03:00
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
| or under a proprietary license. |
|
|
|
|
| |
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
| |
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
2018-09-11 19:40:53 -03:00
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
2018-04-21 02:59:30 -03:00
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
|
|
|
| "General Bots" is a registered trademark of Pragmatismo.io. |
|
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2018-11-11 19:09:18 -02:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots server core.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2018-09-24 11:04:36 -03:00
|
|
|
|
2022-11-18 22:39:14 -03:00
|
|
|
import express from 'express';
|
|
|
|
import bodyParser from 'body-parser';
|
|
|
|
import https from 'https';
|
|
|
|
import mkdirp from 'mkdirp';
|
|
|
|
import Path from 'path';
|
|
|
|
import * as Fs from 'fs';
|
2022-12-14 08:23:39 -03:00
|
|
|
import { GBLog, IGBCoreService, IGBInstance, IGBPackage } from 'botlib';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { GBAdminService } from '../packages/admin.gbapp/services/GBAdminService.js';
|
|
|
|
import { AzureDeployerService } from '../packages/azuredeployer.gbapp/services/AzureDeployerService.js';
|
|
|
|
import { GBConfigService } from '../packages/core.gbapp/services/GBConfigService.js';
|
|
|
|
import { GBConversationalService } from '../packages/core.gbapp/services/GBConversationalService.js';
|
|
|
|
import { GBCoreService } from '../packages/core.gbapp/services/GBCoreService.js';
|
|
|
|
import { GBDeployer } from '../packages/core.gbapp/services/GBDeployer.js';
|
|
|
|
import { GBImporter } from '../packages/core.gbapp/services/GBImporterService.js';
|
|
|
|
import { GBMinService } from '../packages/core.gbapp/services/GBMinService.js';
|
2022-11-19 23:34:58 -03:00
|
|
|
import auth from 'basic-auth';
|
|
|
|
import child_process from 'child_process';
|
2022-11-18 22:39:14 -03:00
|
|
|
import * as winston from 'winston-logs-display';
|
2022-12-14 08:23:39 -03:00
|
|
|
import { RootData } from './RootData.js';
|
2022-01-23 19:35:20 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
/**
|
|
|
|
* General Bots open-core entry point.
|
|
|
|
*/
|
|
|
|
export class GBServer {
|
2019-05-14 23:02:21 -03:00
|
|
|
public static globals: RootData;
|
|
|
|
|
2018-09-24 15:27:26 -03:00
|
|
|
/**
|
|
|
|
* Program entry-point.
|
|
|
|
*/
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
public static run () {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`The Bot Server is in STARTING mode...`);
|
2019-05-14 23:02:21 -03:00
|
|
|
GBServer.globals = new RootData();
|
2019-06-28 11:17:41 -03:00
|
|
|
GBConfigService.init();
|
2019-03-09 16:59:31 -03:00
|
|
|
const port = GBConfigService.getServerPort();
|
2022-10-28 23:17:35 -03:00
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
if (process.env.TEST_SHELL) {
|
2022-11-06 20:19:05 -03:00
|
|
|
GBLog.info(`Running TEST_SHELL: ${process.env.TEST_SHELL}...`);
|
2022-11-19 23:34:58 -03:00
|
|
|
try {
|
|
|
|
child_process.execSync(process.env.TEST_SHELL);
|
|
|
|
} catch (error) {
|
|
|
|
GBLog.error(`Running TEST_SHELL ERROR: ${error}...`);
|
2022-11-06 20:19:05 -03:00
|
|
|
}
|
|
|
|
}
|
2022-10-28 23:17:35 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const server = express();
|
2022-10-28 23:17:35 -03:00
|
|
|
|
2019-06-05 18:23:31 -03:00
|
|
|
GBServer.globals.server = server;
|
2019-08-22 17:28:11 -03:00
|
|
|
GBServer.globals.appPackages = [];
|
|
|
|
GBServer.globals.sysPackages = [];
|
|
|
|
GBServer.globals.minInstances = [];
|
2020-02-25 10:13:38 -03:00
|
|
|
GBServer.globals.wwwroot = null;
|
2020-02-26 15:20:47 -03:00
|
|
|
GBServer.globals.entryPointDialog = null;
|
2022-11-12 21:33:45 -03:00
|
|
|
GBServer.globals.debuggers = [];
|
2019-08-22 17:28:11 -03:00
|
|
|
|
2019-03-11 19:32:47 -03:00
|
|
|
server.use(bodyParser.json());
|
2019-05-27 08:46:17 -03:00
|
|
|
server.use(bodyParser.urlencoded({ extended: true }));
|
2018-05-11 23:27:00 -03:00
|
|
|
|
2022-12-12 16:10:17 -03:00
|
|
|
process.on('unhandledRejection', (err, p) => {
|
|
|
|
console.log('An unhandledRejection occurred');
|
|
|
|
console.log(`Rejected Promise: ${p}`);
|
|
|
|
console.log(`Rejection: ${err}`);
|
|
|
|
});
|
|
|
|
|
2019-08-26 13:21:52 -03:00
|
|
|
// Creates working directory.
|
|
|
|
|
2022-12-12 16:09:49 -03:00
|
|
|
process.env.PWD = process.cwd();
|
2019-10-17 22:32:32 -03:00
|
|
|
const workDir = Path.join(process.env.PWD, 'work');
|
2022-11-18 22:39:14 -03:00
|
|
|
if (!Fs.existsSync(workDir)) {
|
2019-08-26 13:21:52 -03:00
|
|
|
mkdirp.sync(workDir);
|
2019-10-17 22:32:32 -03:00
|
|
|
}
|
2019-08-26 13:21:52 -03:00
|
|
|
|
2022-01-23 19:35:20 -03:00
|
|
|
const mainCallback = () => {
|
2018-09-09 16:40:04 -03:00
|
|
|
(async () => {
|
|
|
|
try {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Now accepting connections on ${port}...`);
|
2022-09-04 18:50:36 -03:00
|
|
|
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-09 16:40:04 -03:00
|
|
|
// Reads basic configuration, initialize minimal services.
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
const core: IGBCoreService = new GBCoreService();
|
2018-11-27 22:56:11 -02:00
|
|
|
const importer: GBImporter = new GBImporter(core);
|
|
|
|
const deployer: GBDeployer = new GBDeployer(core, importer);
|
2022-12-15 10:56:27 -03:00
|
|
|
const azureDeployer: AzureDeployerService = await AzureDeployerService.createInstance(deployer);
|
2018-11-27 22:56:11 -02:00
|
|
|
const adminService: GBAdminService = new GBAdminService(core);
|
2020-04-02 22:02:50 -03:00
|
|
|
|
2022-02-04 18:50:19 -03:00
|
|
|
if (process.env.NODE_ENV === 'development' && !process.env.BOT_URL) {
|
2019-06-26 13:18:15 -03:00
|
|
|
const proxy = GBConfigService.get('REVERSE_PROXY');
|
|
|
|
if (proxy !== undefined) {
|
|
|
|
GBServer.globals.publicAddress = proxy;
|
2019-10-17 22:32:32 -03:00
|
|
|
} else {
|
2019-06-26 13:18:15 -03:00
|
|
|
// Ensure that local development proxy is setup.
|
|
|
|
|
|
|
|
GBLog.info(`Establishing a development local proxy (ngrok)...`);
|
|
|
|
GBServer.globals.publicAddress = await core.ensureProxy(port);
|
|
|
|
}
|
2020-04-28 20:54:04 -03:00
|
|
|
process.env.BOT_URL = GBServer.globals.publicAddress;
|
2019-05-27 09:14:49 -03:00
|
|
|
} else {
|
2020-04-02 23:52:59 -03:00
|
|
|
const serverAddress = process.env.BOT_URL;
|
2019-05-27 09:14:49 -03:00
|
|
|
GBLog.info(`Defining server address at ${serverAddress}...`);
|
|
|
|
GBServer.globals.publicAddress = serverAddress;
|
|
|
|
}
|
2020-07-26 16:46:37 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
// Creates a boot instance or load it from storage.
|
2019-02-25 08:36:43 -03:00
|
|
|
|
2018-11-28 17:08:06 -02:00
|
|
|
try {
|
|
|
|
await core.initStorage();
|
|
|
|
} catch (error) {
|
2019-03-11 19:32:47 -03:00
|
|
|
GBLog.verbose(`Error initializing storage: ${error}`);
|
2022-11-19 23:34:58 -03:00
|
|
|
GBServer.globals.bootInstance = await core.createBootInstance(
|
|
|
|
core,
|
|
|
|
azureDeployer,
|
|
|
|
GBServer.globals.publicAddress
|
|
|
|
);
|
2018-11-28 17:08:06 -02:00
|
|
|
}
|
|
|
|
|
2019-02-25 09:44:39 -03:00
|
|
|
core.ensureAdminIsSecured();
|
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Deploys system and user packages.
|
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Deploying packages...`);
|
2020-04-13 19:14:55 -03:00
|
|
|
GBServer.globals.sysPackages = await core.loadSysPackages(core);
|
2022-01-03 13:11:21 -03:00
|
|
|
GBLog.info(`Connecting to Bot Storage...`);
|
2018-11-28 17:08:06 -02:00
|
|
|
await core.checkStorage(azureDeployer);
|
2019-08-22 17:28:11 -03:00
|
|
|
await deployer.deployPackages(core, server, GBServer.globals.appPackages);
|
2021-05-18 10:56:04 -03:00
|
|
|
await core.syncDatabaseStructure();
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2022-01-05 15:27:20 -03:00
|
|
|
// Deployment of local applications for the first time.
|
|
|
|
|
|
|
|
if (GBConfigService.get('DISABLE_WEB') !== 'true') {
|
|
|
|
deployer.setupDefaultGBUI();
|
|
|
|
}
|
2021-12-20 20:27:02 -03:00
|
|
|
|
2020-10-26 20:18:38 -03:00
|
|
|
GBLog.info(`Publishing instances...`);
|
2020-12-31 15:36:19 -03:00
|
|
|
const instances: IGBInstance[] = await core.loadAllInstances(
|
2020-10-23 09:55:44 -03:00
|
|
|
core,
|
|
|
|
azureDeployer,
|
|
|
|
GBServer.globals.publicAddress
|
2018-11-28 17:08:06 -02:00
|
|
|
);
|
2020-10-26 20:18:38 -03:00
|
|
|
|
|
|
|
if (instances.length === 0) {
|
|
|
|
const instance = await importer.importIfNotExistsBotPackage(
|
|
|
|
GBConfigService.get('BOT_ID'),
|
|
|
|
'boot.gbot',
|
|
|
|
'packages/boot.gbot',
|
|
|
|
GBServer.globals.bootInstance
|
|
|
|
);
|
|
|
|
await deployer.deployBotFull(instance, GBServer.globals.publicAddress);
|
|
|
|
instances.push(instance);
|
2021-04-17 17:20:44 -03:00
|
|
|
|
2021-11-29 18:39:42 -03:00
|
|
|
// Runs the search even with empty content to create structure.
|
|
|
|
|
2021-04-17 17:20:44 -03:00
|
|
|
await azureDeployer['runSearch'](instance);
|
2020-10-26 20:18:38 -03:00
|
|
|
}
|
|
|
|
|
2020-10-23 09:55:44 -03:00
|
|
|
GBServer.globals.bootInstance = instances[0];
|
2018-11-27 22:56:11 -02:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Builds minimal service infrastructure.
|
2020-04-02 22:02:50 -03:00
|
|
|
|
2020-03-31 09:11:04 -03:00
|
|
|
const conversationalService: GBConversationalService = new GBConversationalService(core);
|
2018-11-27 22:56:11 -02:00
|
|
|
const minService: GBMinService = new GBMinService(core, conversationalService, adminService, deployer);
|
2019-08-22 01:54:30 +00:00
|
|
|
GBServer.globals.minService = minService;
|
2019-08-22 17:28:11 -03:00
|
|
|
await minService.buildMin(instances);
|
2018-11-30 11:55:44 -02:00
|
|
|
|
2022-08-06 19:38:13 -03:00
|
|
|
if (process.env.ENABLE_WEBLOG) {
|
2022-11-19 23:34:58 -03:00
|
|
|
const admins = {
|
|
|
|
admin: { password: process.env.ADMIN_PASS }
|
2022-08-06 19:38:13 -03:00
|
|
|
};
|
|
|
|
|
2022-08-07 11:05:20 -03:00
|
|
|
// ... some not authenticated middlewares
|
2022-08-06 19:38:13 -03:00
|
|
|
|
2022-10-28 23:17:35 -03:00
|
|
|
server.use(async (req, res, next) => {
|
2022-08-07 11:05:20 -03:00
|
|
|
if (req.originalUrl.startsWith('/logs')) {
|
2022-11-19 23:34:58 -03:00
|
|
|
const user = auth(req);
|
2022-08-07 11:05:20 -03:00
|
|
|
if (!user || !admins[user.name] || admins[user.name].password !== user.pass) {
|
|
|
|
res.set('WWW-Authenticate', 'Basic realm="example"');
|
|
|
|
return res.status(401).send();
|
|
|
|
}
|
2022-10-28 23:17:35 -03:00
|
|
|
} else {
|
|
|
|
return next();
|
2022-08-06 19:38:13 -03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-07 11:05:20 -03:00
|
|
|
// If global log enabled, reorders transports adding web logging.
|
2022-08-06 19:38:13 -03:00
|
|
|
|
|
|
|
const loggers = GBLog.getLogger();
|
2022-11-18 22:39:14 -03:00
|
|
|
winston.default(server, loggers[1]);
|
2022-08-06 19:38:13 -03:00
|
|
|
}
|
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`The Bot Server is in RUNNING mode...`);
|
2022-10-28 23:17:35 -03:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Opens Navigator.
|
|
|
|
|
2019-06-28 11:17:41 -03:00
|
|
|
// TODO: Config: core.openBrowserInDevelopment();
|
2018-09-09 16:40:04 -03:00
|
|
|
} catch (err) {
|
2020-07-26 16:46:37 -03:00
|
|
|
GBLog.error(`STOP: ${err.message ? err.message : err} ${err.stack ? err.stack : ''}`);
|
2018-09-24 15:27:26 -03:00
|
|
|
process.exit(1);
|
2018-09-09 16:40:04 -03:00
|
|
|
}
|
2018-09-24 11:04:36 -03:00
|
|
|
})();
|
2022-01-23 19:35:20 -03:00
|
|
|
};
|
2022-12-14 08:23:39 -03:00
|
|
|
// TODO: Move to .gbot folder myown.com pointing to generalbots.ai/myown
|
2022-01-27 17:48:38 -03:00
|
|
|
if (process.env.CERTIFICATE_PFX) {
|
2022-12-14 08:23:39 -03:00
|
|
|
const options1 = {
|
2022-02-20 18:49:32 -03:00
|
|
|
passphrase: process.env.CERTIFICATE_PASSPHRASE,
|
2022-11-18 22:39:14 -03:00
|
|
|
pfx: Fs.readFileSync(process.env.CERTIFICATE_PFX)
|
2022-01-23 19:35:20 -03:00
|
|
|
};
|
2022-12-14 08:23:39 -03:00
|
|
|
const httpsServer = https.createServer(options1, server).listen(port, mainCallback);
|
2022-02-11 12:15:23 -03:00
|
|
|
|
2022-02-20 18:49:32 -03:00
|
|
|
if (process.env.CERTIFICATE2_PFX) {
|
2022-12-14 08:23:39 -03:00
|
|
|
const options2 = {
|
2022-02-20 18:49:32 -03:00
|
|
|
passphrase: process.env.CERTIFICATE2_PASSPHRASE,
|
2022-11-18 22:39:14 -03:00
|
|
|
pfx: Fs.readFileSync(process.env.CERTIFICATE2_PFX)
|
2022-02-20 18:49:32 -03:00
|
|
|
};
|
2022-12-14 08:23:39 -03:00
|
|
|
httpsServer.addContext(process.env.CERTIFICATE2_DOMAIN, options2);
|
2022-02-20 18:49:32 -03:00
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2022-01-23 19:35:20 -03:00
|
|
|
server.listen(port, mainCallback);
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|