2023-02-21 10:21:56 -08:00
|
|
|
import { Container } from 'typedi';
|
2023-01-04 02:38:48 -08:00
|
|
|
import { readFile } from 'fs/promises';
|
|
|
|
import type { Server } from 'http';
|
|
|
|
import express from 'express';
|
|
|
|
import compression from 'compression';
|
2023-09-01 04:54:35 -07:00
|
|
|
import isbot from 'isbot';
|
2023-09-07 05:44:19 -07:00
|
|
|
import { LoggerProxy as Logger } from 'n8n-workflow';
|
2023-09-01 04:54:35 -07:00
|
|
|
|
2023-01-04 02:38:48 -08:00
|
|
|
import config from '@/config';
|
2023-08-01 08:32:30 -07:00
|
|
|
import { N8N_VERSION, inDevelopment, inTest } from '@/constants';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
|
2023-01-04 02:38:48 -08:00
|
|
|
import * as Db from '@/Db';
|
2023-08-07 08:03:21 -07:00
|
|
|
import { N8nInstanceType } from '@/Interfaces';
|
2023-01-04 02:38:48 -08:00
|
|
|
import type { IExternalHooksClass } from '@/Interfaces';
|
|
|
|
import { ExternalHooks } from '@/ExternalHooks';
|
2023-08-01 08:32:30 -07:00
|
|
|
import { send, sendErrorResponse, ServiceUnavailableError } from '@/ResponseHelper';
|
2023-08-11 00:18:33 -07:00
|
|
|
import { rawBodyReader, bodyParser, corsMiddleware } from '@/middlewares';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { TestWebhooks } from '@/TestWebhooks';
|
2023-01-04 02:38:48 -08:00
|
|
|
import { WaitingWebhooks } from '@/WaitingWebhooks';
|
2023-08-01 08:32:30 -07:00
|
|
|
import { webhookRequestHandler } from '@/WebhookHelpers';
|
2023-08-07 08:03:21 -07:00
|
|
|
import { generateHostInstanceId } from './databases/utils/generators';
|
2023-09-07 05:44:19 -07:00
|
|
|
import { OrchestrationService } from './services/orchestration.service';
|
2023-01-04 02:38:48 -08:00
|
|
|
|
|
|
|
export abstract class AbstractServer {
|
2023-02-10 06:02:47 -08:00
|
|
|
protected server: Server;
|
|
|
|
|
2023-08-01 08:32:30 -07:00
|
|
|
readonly app: express.Application;
|
2023-01-04 02:38:48 -08:00
|
|
|
|
|
|
|
protected externalHooks: IExternalHooksClass;
|
|
|
|
|
2023-02-21 10:21:56 -08:00
|
|
|
protected activeWorkflowRunner: ActiveWorkflowRunner;
|
2023-01-04 02:38:48 -08:00
|
|
|
|
|
|
|
protected protocol: string;
|
|
|
|
|
|
|
|
protected sslKey: string;
|
|
|
|
|
|
|
|
protected sslCert: string;
|
|
|
|
|
|
|
|
protected timezone: string;
|
|
|
|
|
|
|
|
protected restEndpoint: string;
|
|
|
|
|
|
|
|
protected endpointWebhook: string;
|
|
|
|
|
|
|
|
protected endpointWebhookTest: string;
|
|
|
|
|
|
|
|
protected endpointWebhookWaiting: string;
|
|
|
|
|
2023-04-24 06:12:00 -07:00
|
|
|
protected instanceId = '';
|
|
|
|
|
2023-08-01 08:32:30 -07:00
|
|
|
protected webhooksEnabled = true;
|
|
|
|
|
|
|
|
protected testWebhooksEnabled = false;
|
2023-01-04 02:38:48 -08:00
|
|
|
|
2023-08-07 08:03:21 -07:00
|
|
|
readonly uniqueInstanceId: string;
|
|
|
|
|
|
|
|
constructor(instanceType: N8nInstanceType = 'main') {
|
2023-01-04 02:38:48 -08:00
|
|
|
this.app = express();
|
|
|
|
this.app.disable('x-powered-by');
|
|
|
|
|
|
|
|
this.protocol = config.getEnv('protocol');
|
|
|
|
this.sslKey = config.getEnv('ssl_key');
|
|
|
|
this.sslCert = config.getEnv('ssl_cert');
|
|
|
|
|
|
|
|
this.timezone = config.getEnv('generic.timezone');
|
|
|
|
|
|
|
|
this.restEndpoint = config.getEnv('endpoints.rest');
|
|
|
|
this.endpointWebhook = config.getEnv('endpoints.webhook');
|
|
|
|
this.endpointWebhookTest = config.getEnv('endpoints.webhookTest');
|
|
|
|
this.endpointWebhookWaiting = config.getEnv('endpoints.webhookWaiting');
|
2023-08-07 08:03:21 -07:00
|
|
|
|
|
|
|
this.uniqueInstanceId = generateHostInstanceId(instanceType);
|
2023-01-04 02:38:48 -08:00
|
|
|
}
|
|
|
|
|
2023-08-01 08:32:30 -07:00
|
|
|
async configure(): Promise<void> {
|
|
|
|
// Additional configuration in derived classes
|
|
|
|
}
|
|
|
|
|
2023-02-10 06:02:47 -08:00
|
|
|
private async setupErrorHandlers() {
|
2023-01-04 02:38:48 -08:00
|
|
|
const { app } = this;
|
|
|
|
|
|
|
|
// Augment errors sent to Sentry
|
|
|
|
const {
|
|
|
|
Handlers: { requestHandler, errorHandler },
|
|
|
|
} = await import('@sentry/node');
|
|
|
|
app.use(requestHandler());
|
|
|
|
app.use(errorHandler());
|
2023-02-10 06:02:47 -08:00
|
|
|
}
|
|
|
|
|
2023-08-01 08:32:30 -07:00
|
|
|
private setupCommonMiddlewares() {
|
2023-01-04 02:38:48 -08:00
|
|
|
// Compress the response data
|
2023-08-01 08:32:30 -07:00
|
|
|
this.app.use(compression());
|
2023-01-04 02:38:48 -08:00
|
|
|
|
2023-08-01 08:32:30 -07:00
|
|
|
// Read incoming data into `rawBody`
|
2023-08-11 00:18:33 -07:00
|
|
|
this.app.use(rawBodyReader);
|
2023-01-04 02:38:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private setupDevMiddlewares() {
|
|
|
|
this.app.use(corsMiddleware);
|
|
|
|
}
|
|
|
|
|
2023-02-10 06:02:47 -08:00
|
|
|
protected setupPushServer() {}
|
|
|
|
|
2023-01-04 02:38:48 -08:00
|
|
|
private async setupHealthCheck() {
|
2023-05-10 01:27:04 -07:00
|
|
|
// health check should not care about DB connections
|
2023-01-04 02:38:48 -08:00
|
|
|
this.app.get('/healthz', async (req, res) => {
|
2023-05-10 01:27:04 -07:00
|
|
|
res.send({ status: 'ok' });
|
|
|
|
});
|
2023-01-04 02:38:48 -08:00
|
|
|
|
2023-05-10 01:27:04 -07:00
|
|
|
const { connectionState } = Db;
|
|
|
|
this.app.use((req, res, next) => {
|
|
|
|
if (connectionState.connected) {
|
|
|
|
if (connectionState.migrated) next();
|
|
|
|
else res.send('n8n is starting up. Please wait');
|
|
|
|
} else sendErrorResponse(res, new ServiceUnavailableError('Database is not ready!'));
|
2023-01-04 02:38:48 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (config.getEnv('executions.mode') === 'queue') {
|
2023-09-07 05:44:19 -07:00
|
|
|
// will start the redis connections
|
|
|
|
await Container.get(OrchestrationService).init(this.uniqueInstanceId);
|
2023-01-04 02:38:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 01:27:04 -07:00
|
|
|
async init(): Promise<void> {
|
|
|
|
const { app, protocol, sslKey, sslCert } = this;
|
2023-01-04 02:38:48 -08:00
|
|
|
|
|
|
|
if (protocol === 'https' && sslKey && sslCert) {
|
|
|
|
const https = await import('https');
|
2023-02-10 06:02:47 -08:00
|
|
|
this.server = https.createServer(
|
2023-01-04 02:38:48 -08:00
|
|
|
{
|
|
|
|
key: await readFile(this.sslKey, 'utf8'),
|
|
|
|
cert: await readFile(this.sslCert, 'utf8'),
|
|
|
|
},
|
|
|
|
app,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const http = await import('http');
|
2023-02-10 06:02:47 -08:00
|
|
|
this.server = http.createServer(app);
|
2023-01-04 02:38:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const PORT = config.getEnv('port');
|
|
|
|
const ADDRESS = config.getEnv('listen_address');
|
|
|
|
|
2023-02-10 06:02:47 -08:00
|
|
|
this.server.on('error', (error: Error & { code: string }) => {
|
2023-01-04 02:38:48 -08:00
|
|
|
if (error.code === 'EADDRINUSE') {
|
|
|
|
console.log(
|
|
|
|
`n8n's port ${PORT} is already in use. Do you have another instance of n8n running already?`,
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-10 06:02:47 -08:00
|
|
|
await new Promise<void>((resolve) => this.server.listen(PORT, ADDRESS, () => resolve()));
|
2023-01-04 02:38:48 -08:00
|
|
|
|
2023-07-25 09:17:34 -07:00
|
|
|
this.externalHooks = Container.get(ExternalHooks);
|
|
|
|
this.activeWorkflowRunner = Container.get(ActiveWorkflowRunner);
|
|
|
|
|
2023-05-10 01:27:04 -07:00
|
|
|
await this.setupHealthCheck();
|
|
|
|
|
|
|
|
console.log(`n8n ready on ${ADDRESS}, port ${PORT}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
async start(): Promise<void> {
|
2023-08-01 08:32:30 -07:00
|
|
|
if (!inTest) {
|
|
|
|
await this.setupErrorHandlers();
|
|
|
|
this.setupPushServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setupCommonMiddlewares();
|
|
|
|
|
|
|
|
// Setup webhook handlers before bodyParser, to let the Webhook node handle binary data in requests
|
|
|
|
if (this.webhooksEnabled) {
|
|
|
|
// Register a handler for active webhooks
|
|
|
|
this.app.all(
|
|
|
|
`/${this.endpointWebhook}/:path(*)`,
|
|
|
|
webhookRequestHandler(Container.get(ActiveWorkflowRunner)),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Register a handler for waiting webhooks
|
|
|
|
this.app.all(
|
|
|
|
`/${this.endpointWebhookWaiting}/:path/:suffix?`,
|
|
|
|
webhookRequestHandler(Container.get(WaitingWebhooks)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.testWebhooksEnabled) {
|
|
|
|
const testWebhooks = Container.get(TestWebhooks);
|
|
|
|
|
|
|
|
// Register a handler for test webhooks
|
|
|
|
this.app.all(`/${this.endpointWebhookTest}/:path(*)`, webhookRequestHandler(testWebhooks));
|
|
|
|
|
|
|
|
// Removes a test webhook
|
|
|
|
// TODO UM: check if this needs validation with user management.
|
|
|
|
this.app.delete(
|
|
|
|
`/${this.restEndpoint}/test-webhook/:id`,
|
|
|
|
send(async (req) => testWebhooks.cancelTestWebhook(req.params.id)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-01 04:54:35 -07:00
|
|
|
// Block bots from scanning the application
|
|
|
|
const checkIfBot = isbot.spawn(['bot']);
|
|
|
|
this.app.use((req, res, next) => {
|
|
|
|
const userAgent = req.headers['user-agent'];
|
2023-09-12 10:57:25 -07:00
|
|
|
if (userAgent && checkIfBot(userAgent)) {
|
2023-09-01 04:54:35 -07:00
|
|
|
Logger.info(`Blocked ${req.method} ${req.url} for "${userAgent}"`);
|
|
|
|
res.status(204).end();
|
|
|
|
} else next();
|
|
|
|
});
|
|
|
|
|
2023-01-04 02:38:48 -08:00
|
|
|
if (inDevelopment) {
|
|
|
|
this.setupDevMiddlewares();
|
|
|
|
}
|
|
|
|
|
2023-08-11 00:18:33 -07:00
|
|
|
// Setup body parsing middleware after the webhook handlers are setup
|
|
|
|
this.app.use(bodyParser);
|
2023-08-01 08:32:30 -07:00
|
|
|
|
2023-01-04 02:38:48 -08:00
|
|
|
await this.configure();
|
|
|
|
|
2023-08-01 08:32:30 -07:00
|
|
|
if (!inTest) {
|
|
|
|
console.log(`Version: ${N8N_VERSION}`);
|
2023-01-04 02:38:48 -08:00
|
|
|
|
2023-08-01 08:32:30 -07:00
|
|
|
const defaultLocale = config.getEnv('defaultLocale');
|
|
|
|
if (defaultLocale !== 'en') {
|
|
|
|
console.log(`Locale: ${defaultLocale}`);
|
|
|
|
}
|
2023-01-04 02:38:48 -08:00
|
|
|
|
2023-08-01 08:32:30 -07:00
|
|
|
await this.externalHooks.run('n8n.ready', [this, config]);
|
|
|
|
}
|
2023-01-04 02:38:48 -08:00
|
|
|
}
|
|
|
|
}
|