2022-07-11 08:22:41 +01:00
|
|
|
// Bayshore - a Wangan Midnight Maximum Tune 6 private server.
|
|
|
|
// Made with love by Luna, and part of Project Asakura.
|
|
|
|
|
2022-11-19 16:36:08 +00:00
|
|
|
import process from 'process';
|
|
|
|
import * as dotenv from "dotenv";
|
2023-06-21 21:45:16 +02:00
|
|
|
|
|
|
|
if (!process.env.BAYSHORE_NIX)
|
|
|
|
{
|
|
|
|
dotenv.config({path: __dirname + '/.env'});
|
|
|
|
}
|
2022-11-19 16:36:08 +00:00
|
|
|
|
2022-11-19 21:30:19 +00:00
|
|
|
let tracing: any = {};
|
|
|
|
|
2022-11-19 16:36:08 +00:00
|
|
|
if (process.env.OPENTELEMETRY_ENABLED === "true") {
|
2022-11-19 16:38:43 +00:00
|
|
|
console.log('Enabling OpenTelemetry-compatible tracing...');
|
2022-11-19 21:30:19 +00:00
|
|
|
tracing = require('./tracing');
|
|
|
|
tracing.startTracing();
|
2022-11-19 16:36:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 12:31:18 +01:00
|
|
|
import express, { Router } from 'express';
|
2022-07-11 08:22:41 +01:00
|
|
|
import {PrismaClient} from '@prisma/client';
|
2022-07-11 11:45:36 +01:00
|
|
|
import https, {globalAgent} from 'https';
|
2022-07-12 12:17:36 +01:00
|
|
|
import http from 'http';
|
2022-07-11 08:38:33 +01:00
|
|
|
import fs from 'fs';
|
2022-07-11 11:45:36 +01:00
|
|
|
import bodyParser from 'body-parser';
|
2022-07-12 11:42:38 +01:00
|
|
|
import AllnetModule from './allnet';
|
|
|
|
import MuchaModule from './mucha';
|
2022-07-17 15:14:23 +01:00
|
|
|
import { Config } from './config';
|
2022-07-18 11:08:13 +01:00
|
|
|
import * as Sentry from '@sentry/node';
|
|
|
|
import * as Tracing from '@sentry/tracing';
|
2023-01-24 09:40:21 +07:00
|
|
|
import * as common from './modules/util/common';
|
2023-06-21 22:10:22 +02:00
|
|
|
import path from 'path';
|
2022-07-19 23:12:59 +10:00
|
|
|
|
2022-07-11 11:45:36 +01:00
|
|
|
globalAgent.options.keepAlive = true;
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
require('http').globalAgent.options.keepAlive = true;
|
2022-07-11 08:38:33 +01:00
|
|
|
|
2022-07-15 15:39:59 +01:00
|
|
|
export const prisma = new PrismaClient();
|
|
|
|
|
2022-07-12 12:31:18 +01:00
|
|
|
const appRouter = Router();
|
|
|
|
|
2022-11-19 15:41:05 +00:00
|
|
|
const PORT_ALLNET = process.env.ALLNET_PORT !== undefined ? parseInt(process.env.ALLNET_PORT) : 80;
|
|
|
|
const PORT_MUCHA = process.env.MUCHA_PORT !== undefined ? parseInt(process.env.MUCHA_PORT) : 10082;
|
|
|
|
const PORT_BNGI = process.env.SERVICE_PORT !== undefined ? parseInt(process.env.SERVICE_PORT) : 9002;
|
|
|
|
const PORT_API = process.env.API_PORT !== undefined ? parseInt(process.env.API_PORT) : 9003;
|
2022-07-12 11:42:38 +01:00
|
|
|
|
2022-07-11 08:38:33 +01:00
|
|
|
const app = express();
|
2022-09-08 22:18:29 +07:00
|
|
|
const muchaApp = express();
|
|
|
|
const allnetApp = express();
|
|
|
|
|
2022-07-11 11:45:36 +01:00
|
|
|
app.use(bodyParser.raw({
|
2022-11-14 13:29:29 +07:00
|
|
|
type: '*/*',
|
|
|
|
limit: '50mb' // idk.. i got PayloadTooLargeError: request entity too large (adding this solve the problem)
|
2022-07-11 11:45:36 +01:00
|
|
|
}));
|
2022-07-11 08:38:33 +01:00
|
|
|
|
2022-07-18 11:08:13 +01:00
|
|
|
let useSentry = !!Config.getConfig().sentryDsn;
|
|
|
|
if (useSentry) {
|
|
|
|
Sentry.init({
|
|
|
|
dsn: Config.getConfig().sentryDsn,
|
|
|
|
integrations: [
|
|
|
|
new Sentry.Integrations.Http({tracing: true}),
|
|
|
|
new Tracing.Integrations.Express({
|
|
|
|
router: appRouter,
|
|
|
|
})
|
|
|
|
],
|
|
|
|
|
|
|
|
tracesSampleRate: 0.5
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-19 21:30:19 +00:00
|
|
|
if (process.env.OPENTELEMETRY_ENABLED === "true") {
|
|
|
|
tracing.startHttpMetrics([
|
|
|
|
{
|
|
|
|
app,
|
|
|
|
options: {
|
|
|
|
appName: 'service'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
app: muchaApp,
|
|
|
|
options: {
|
|
|
|
appName: 'mucha'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
app: allnetApp,
|
|
|
|
options: {
|
|
|
|
appName: 'allnet'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:08:13 +01:00
|
|
|
if (useSentry) {
|
|
|
|
app.use(Sentry.Handlers.requestHandler());
|
|
|
|
app.use(Sentry.Handlers.tracingHandler());
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:43:39 +01:00
|
|
|
app.use((req, res, next) => {
|
2023-05-27 10:06:29 +07:00
|
|
|
common.writeLog(`[ MAIN] ${req.method} ${req.url}`);
|
2022-07-12 11:42:38 +01:00
|
|
|
next()
|
|
|
|
});
|
|
|
|
|
|
|
|
muchaApp.use((req, res, next) => {
|
2023-05-27 10:06:29 +07:00
|
|
|
common.writeLog(`[ MUCHA] ${req.method} ${req.url}`);
|
2022-07-12 11:42:38 +01:00
|
|
|
next()
|
|
|
|
});
|
|
|
|
|
|
|
|
allnetApp.use((req, res, next) => {
|
2023-05-27 10:06:29 +07:00
|
|
|
common.writeLog(`[ALLNET] ${req.method} ${req.url}`);
|
2022-07-11 13:43:39 +01:00
|
|
|
next()
|
|
|
|
});
|
|
|
|
|
2022-08-15 10:47:34 +07:00
|
|
|
// Get all of the files in the modules directory
|
2023-06-21 22:10:22 +02:00
|
|
|
let dirs = fs.readdirSync(path.join(path.dirname(__filename), 'modules'));
|
2022-08-15 10:47:34 +07:00
|
|
|
// Loop over the files
|
|
|
|
for (let i of dirs)
|
|
|
|
{
|
|
|
|
// If the file is a .js file
|
|
|
|
if (i.endsWith('.js'))
|
|
|
|
{
|
|
|
|
// Require the module file
|
2022-07-11 09:15:30 +01:00
|
|
|
let mod = require(`./modules/${i.substring(0, i.length - 3)}`); // .js extension
|
2022-08-15 10:47:34 +07:00
|
|
|
|
|
|
|
// Create an instance of the module
|
2022-07-11 09:15:30 +01:00
|
|
|
let inst = new mod.default();
|
2022-08-15 10:47:34 +07:00
|
|
|
|
|
|
|
// Register the module with the app
|
2022-07-12 12:31:18 +01:00
|
|
|
inst.register(appRouter);
|
2022-07-11 09:15:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 10:47:34 +07:00
|
|
|
// Host on / and /wmmt6/ path
|
2022-07-12 12:31:18 +01:00
|
|
|
app.use('/', appRouter);
|
|
|
|
app.use('/wmmt6/', appRouter);
|
|
|
|
|
2022-07-11 09:15:30 +01:00
|
|
|
app.all('*', (req, res) => {
|
2023-05-27 10:06:29 +07:00
|
|
|
common.writeLog(`[ MAIN] ${req.method} ${req.url} is unhandled`);
|
2022-07-11 15:19:36 +01:00
|
|
|
res.status(200).end();
|
2022-07-11 09:15:30 +01:00
|
|
|
})
|
|
|
|
|
2022-08-15 10:47:34 +07:00
|
|
|
// Register the ALL.NET / Mucha Server
|
2022-07-12 11:42:38 +01:00
|
|
|
new AllnetModule().register(allnetApp);
|
|
|
|
new MuchaModule().register(muchaApp);
|
|
|
|
|
2022-08-15 10:47:34 +07:00
|
|
|
// Sentry is in use
|
2022-07-18 11:08:13 +01:00
|
|
|
if (useSentry)
|
2022-08-15 10:47:34 +07:00
|
|
|
{
|
2022-09-08 22:18:29 +07:00
|
|
|
// Use the sentry error handler
|
2022-07-18 11:08:13 +01:00
|
|
|
app.use(Sentry.Handlers.errorHandler());
|
2022-08-15 10:47:34 +07:00
|
|
|
}
|
2022-07-18 11:08:13 +01:00
|
|
|
|
2022-08-15 10:47:34 +07:00
|
|
|
// Get the wangan key / certificate file
|
2023-06-21 22:18:00 +02:00
|
|
|
let dataPathBase = process.env.BAYSHORE_NIX ? process.env.BAYSHORE_DATA_PATH : '.';
|
|
|
|
if (!dataPathBase)
|
|
|
|
{
|
|
|
|
throw new Error('Please set BAYSHORE_DATA_PATH.');
|
|
|
|
}
|
|
|
|
|
|
|
|
let key = fs.readFileSync(path.join(dataPathBase, 'server_wangan.key'));
|
|
|
|
let cert = fs.readFileSync(path.join(dataPathBase, 'server_wangan.crt'));
|
2022-07-12 11:42:38 +01:00
|
|
|
|
2022-08-15 10:47:34 +07:00
|
|
|
// Create the (ALL.Net) server
|
2022-07-12 12:17:36 +01:00
|
|
|
http.createServer(allnetApp).listen(PORT_ALLNET, '0.0.0.0', 511, () => {
|
2022-07-12 11:42:38 +01:00
|
|
|
console.log(`ALL.net server listening on port ${PORT_ALLNET}!`);
|
2022-07-17 15:14:23 +01:00
|
|
|
let unix = Config.getConfig().unix;
|
|
|
|
if (unix && process.platform == 'linux') {
|
|
|
|
console.log('Downgrading permissions...');
|
|
|
|
process.setgid!(unix.setgid);
|
2022-07-17 15:18:56 +01:00
|
|
|
process.setuid!(unix.setuid);
|
2022-07-17 15:14:23 +01:00
|
|
|
console.log('Done!');
|
|
|
|
}
|
2022-07-12 11:42:38 +01:00
|
|
|
})
|
|
|
|
|
2022-08-15 10:47:34 +07:00
|
|
|
// Create the mucha server
|
2022-07-12 12:17:36 +01:00
|
|
|
https.createServer({key, cert}, muchaApp).listen(PORT_MUCHA, '0.0.0.0', 511, () => {
|
2022-07-12 11:42:38 +01:00
|
|
|
console.log(`Mucha server listening on port ${PORT_MUCHA}!`);
|
|
|
|
})
|
|
|
|
|
2022-08-15 10:47:34 +07:00
|
|
|
// Create the game server
|
2022-07-12 12:17:36 +01:00
|
|
|
https.createServer({key, cert}, app).listen(PORT_BNGI, '0.0.0.0', 511, () => {
|
2022-07-12 11:42:38 +01:00
|
|
|
console.log(`Game server listening on port ${PORT_BNGI}!`);
|
2022-09-18 13:01:12 +07:00
|
|
|
})
|