-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
316 lines (254 loc) · 12.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Copyright 2022 Nikita Petko <petko@vmminfra.net>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
File Name: Main.ts
Description: The main entry point for the application.
Written by: Nikita Petko
*/
////////////////////////////////////////////////////////////////////////////////////////////////////
// Top Level Declarations
////////////////////////////////////////////////////////////////////////////////////////////////////
import 'source-map-support/register';
// eslint-disable-next-line @typescript-eslint/no-var-requires
new (require('prom-client').Gauge)({
name: 'server_info',
help: 'Information about the server.',
labelNames: ['hostname', 'app_name', 'version', 'node_version', 'platform', 'architecture', 'environment'],
}).set(
{
// eslint-disable-next-line @typescript-eslint/no-var-requires
hostname: require('os').hostname(),
// eslint-disable-next-line @typescript-eslint/no-var-requires
app_name: require('../package.json').name,
// eslint-disable-next-line @typescript-eslint/no-var-requires
version: `v${require('../package.json').version}`,
environment: process.env.NODE_ENV || 'development',
// eslint-disable-next-line @typescript-eslint/no-var-requires
node_version: require('process').version,
// eslint-disable-next-line @typescript-eslint/no-var-requires
platform: require('os').platform(),
// eslint-disable-next-line @typescript-eslint/no-var-requires
architecture: require('os').arch(),
},
1,
);
import './import_handler';
import './stdin_handler';
import '@lib/environment/register';
import dotenvLoader from '@lib/environment/dotenv_loader';
dotenvLoader.reloadEnvironment();
import googleAnalytics from '@lib/utility/google_analytics';
googleAnalytics.initialize();
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Primary Declarations
////////////////////////////////////////////////////////////////////////////////////////////////////
import web from '@lib/setup';
import setupLogger from '@lib/loggers/setup_logger';
import webEnvironment from '@lib/environment/web_environment';
import entrypointLogger from '@lib/loggers/entrypoint_logger';
import sentryEnvironment from '@lib/environment/sentry_environment';
////////////////////////////////////////////////////////////////////////////////////////////////////
// Type Declarations
////////////////////////////////////////////////////////////////////////////////////////////////////
import startupOptions from '@lib/setup/options/startup_options';
////////////////////////////////////////////////////////////////////////////////////////////////////
// Middleware
////////////////////////////////////////////////////////////////////////////////////////////////////
import errorMiddleware from '@lib/middleware/error_middleware';
import configMiddleware from '@lib/middleware/config_middleware';
import loggingMiddleware from '@lib/middleware/logging_middleware';
import metricsMiddleware from '@lib/middleware/metrics_middleware';
import overrideMiddleware from '@lib/middleware/override_middleware';
import cidrCheckMiddleware from '@lib/middleware/cidr_check_middleware';
import healthcheckMiddleware from '@lib/middleware/healthcheck_middleware';
import crawlerCheckMiddleware from '@lib/middleware/crawler_check_middleware';
import reverseProxyMiddleware from '@lib/middleware/reverse_proxy_middleware';
import testExceptionMiddleware from '@lib/middleware/test_exception_middleware';
import corsApplicationMiddleware from '@lib/middleware/cors_application_middleware';
import sendAxiosRequestMiddleware from '@lib/middleware/send_axios_request_middleware';
import loadBalancerInfoMiddleware from '@lib/middleware/load_balancer_info_middleware';
import hardcodedResponseMiddleware from '@lib/middleware/hardcoded_response_middleware';
import hostnameResolutionMiddleware from '@lib/middleware/hostname_resolution_middleware';
import denyLoopbackAttackMiddleware from '@lib/middleware/deny_loopback_attack_middleware';
import wanAddressApplicationMiddleware from '@lib/middleware/wan_address_application_middleware';
import denyLocalAreaNetworkAccessMiddleware from '@lib/middleware/deny_local_area_network_access_middleware';
////////////////////////////////////////////////////////////////////////////////////////////////////
// Third Party Declarations
////////////////////////////////////////////////////////////////////////////////////////////////////
import * as fs from 'fs';
import * as path from 'path';
import express from 'express';
import * as Sentry from '@sentry/node';
import * as bodyParser from 'body-parser';
import * as Prometheus from 'prom-client';
import * as Tracing from '@sentry/tracing';
import environment from '@mfdlabs/environment';
Error.stackTraceLimit = Infinity;
// We want to try and not hard code these values.
// In the future we should have an environment variable for the passphrase
// and maybe the certificate stuff as well.
const settings = {} as startupOptions;
googleAnalytics.fireServerEventGA4('Server', 'Start');
const proxyServer = express();
if (sentryEnvironment.singleton.sentryEnabled) {
entrypointLogger.debug('Sentry enabled, using %s as DSN.', sentryEnvironment.singleton.sentryClientDsn);
Sentry.init({
dsn: sentryEnvironment.singleton.sentryClientDsn,
attachStacktrace: true,
tracesSampleRate: 1.0,
sampleRate: 1.0,
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({
app: proxyServer,
}),
],
});
Tracing.addExtensionMethods();
}
Prometheus.collectDefaultMetrics({ register: Prometheus.register });
// Make sure it uses body-parser.raw() to parse the body as a Buffer.
proxyServer.use(
bodyParser.raw({
inflate: true,
limit: '5Gb',
type: () => true,
}),
);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Middleware
////////////////////////////////////////////////////////////////////////////////////////////////////
// Note: For middleware, please make it a lambda then invoke it, as this will make it so it doesn't capture
// Express's function context and cause the `this` keyword to be either undefined or not have the correct
// members.
entrypointLogger.information('Loading middleware...');
if (sentryEnvironment.singleton.sentryEnabled) {
proxyServer.use(Sentry.Handlers.requestHandler());
proxyServer.use(Sentry.Handlers.tracingHandler());
}
proxyServer.use(overrideMiddleware.invoke.bind(overrideMiddleware));
proxyServer.use(reverseProxyMiddleware.invoke.bind(reverseProxyMiddleware));
proxyServer.use(loggingMiddleware.invoke.bind(loggingMiddleware));
proxyServer.use(cidrCheckMiddleware.invoke.bind(cidrCheckMiddleware));
proxyServer.use(crawlerCheckMiddleware.invoke.bind(crawlerCheckMiddleware));
proxyServer.use(loadBalancerInfoMiddleware.invoke.bind(loadBalancerInfoMiddleware));
proxyServer.use(metricsMiddleware.invoke.bind(metricsMiddleware));
proxyServer.use(configMiddleware.invoke.bind(configMiddleware));
proxyServer.use(testExceptionMiddleware.invoke.bind(testExceptionMiddleware));
proxyServer.use(healthcheckMiddleware.invoke.bind(healthcheckMiddleware));
proxyServer.use(wanAddressApplicationMiddleware.invoke.bind(wanAddressApplicationMiddleware));
proxyServer.use(hostnameResolutionMiddleware.invoke.bind(hostnameResolutionMiddleware));
proxyServer.use(denyLocalAreaNetworkAccessMiddleware.invoke.bind(denyLocalAreaNetworkAccessMiddleware));
proxyServer.use(denyLoopbackAttackMiddleware.invoke.bind(denyLoopbackAttackMiddleware));
proxyServer.use(corsApplicationMiddleware.invoke.bind(corsApplicationMiddleware));
proxyServer.use(hardcodedResponseMiddleware.invoke.bind(hardcodedResponseMiddleware));
proxyServer.use(sendAxiosRequestMiddleware.invoke.bind(sendAxiosRequestMiddleware));
////////////////////////////////////////////////////////////////////////////////////////////////////
if (webEnvironment.singleton.logStartupInfo)
web.overrideLoggers(setupLogger.information.bind(setupLogger), setupLogger.error.bind(setupLogger));
web.configureServer({
app: proxyServer,
noETag: true,
noXPowerBy: true,
rawBufferRequest: false,
routingOptions: {
caseSensitive: true, // We want to be case sensitive for our routes, so that /a and /A are different
strict: true, // We want /a and /a/ to be treated as different routes
},
trustProxy: false,
});
if (sentryEnvironment.singleton.sentryEnabled) {
proxyServer.use(Sentry.Handlers.errorHandler());
}
proxyServer.use(errorMiddleware.invoke.bind(errorMiddleware));
////////////////////////////////////////////////////////////////////////////////////////////////////
// Settings
////////////////////////////////////////////////////////////////////////////////////////////////////
if (webEnvironment.singleton.enableSecureServer) {
entrypointLogger.information('Loading TLS settings...');
if (webEnvironment.singleton.enableTLSv2) {
entrypointLogger.information('TLSv2 is enabled.');
settings.tlsV2 = true;
}
settings.tls = true;
settings.tlsPort = webEnvironment.singleton.securePort;
if (!fs.existsSync(webEnvironment.singleton.sslBaseDirectory)) {
entrypointLogger.error(
'The SSL base directory does not exist. Please make sure it exists and is readable. Path: %s',
webEnvironment.singleton.sslBaseDirectory,
);
throw new Error(`The SSL base directory "${webEnvironment.singleton.sslBaseDirectory}" does not exist.`);
}
const fullyQualifiedCertificatePath = path.join(
webEnvironment.singleton.sslBaseDirectory,
webEnvironment.singleton.sslCertificateFileName,
);
const fullyQualifiedKeyPath = path.join(
webEnvironment.singleton.sslBaseDirectory,
webEnvironment.singleton.sslKeyFileName,
);
if (!fs.existsSync(fullyQualifiedCertificatePath)) {
entrypointLogger.error(
'The SSL certificate file does not exist. Please make sure it exists and is readable. Path: %s',
fullyQualifiedCertificatePath,
);
throw new Error(`The SSL certificate file "${fullyQualifiedCertificatePath}" does not exist.`);
}
if (!fs.existsSync(fullyQualifiedKeyPath)) {
entrypointLogger.error(
'The SSL key file does not exist. Please make sure it exists and is readable. Path: %s',
fullyQualifiedKeyPath,
);
throw new Error(`The SSL key file "${fullyQualifiedKeyPath}" does not exist.`);
}
settings.baseTlsDirectory = webEnvironment.singleton.sslBaseDirectory;
settings.cert = webEnvironment.singleton.sslCertificateFileName;
settings.key = webEnvironment.singleton.sslKeyFileName;
if (webEnvironment.singleton.sslKeyPassphrase !== null) {
entrypointLogger.information('TLS key passphrase is enabled.');
settings.passphrase = webEnvironment.singleton.sslKeyPassphrase;
}
if (webEnvironment.singleton.sslCertificateChainFileName !== null) {
entrypointLogger.information('TLS certificate chain is enabled.');
const fullyQualifiedCertificateChainPath = path.join(
webEnvironment.singleton.sslBaseDirectory,
webEnvironment.singleton.sslCertificateChainFileName,
);
if (!fs.existsSync(fullyQualifiedCertificateChainPath)) {
entrypointLogger.error(
'The SSL certificate chain file does not exist. Please make sure it exists and is readable. Path: %s',
fullyQualifiedCertificateChainPath,
);
throw new Error(`The SSL certificate chain file "${fullyQualifiedCertificateChainPath}" does not exist.`);
}
settings.chain = webEnvironment.singleton.sslCertificateChainFileName;
}
}
settings.insecure = true;
settings.insecurePort = webEnvironment.singleton.insecurePort;
settings.bind = webEnvironment.singleton.bindAddressIPv4;
web.startServer({
app: proxyServer,
...settings,
});
// This is a temporary fix for the issue where the server is not able to start when
// running in a Docker container on IPv6.
if (!webEnvironment.singleton.disableIPv6 && !environment.isDocker()) {
entrypointLogger.information('Starting IPv6 server...');
settings.bind = webEnvironment.singleton.bindAddressIPv6;
web.startServer({
app: proxyServer,
...settings,
});
}