diff --git a/src/config.ts b/src/config.ts index 2bbe46fb96..f7b0095ee7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -56,6 +56,10 @@ interface Source { secretKey: string; }; accesslog?: string; + accesses?: { + enable: boolean; + port: number; + }; twitter?: { consumer_key: string; consumer_secret: string; diff --git a/src/index.ts b/src/index.ts index 54a86e365f..a820c8b757 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,26 +11,22 @@ import * as fs from 'fs'; import * as os from 'os'; import * as cluster from 'cluster'; import * as debug from 'debug'; -import Logger from './utils/logger'; import * as chalk from 'chalk'; //import portUsed = require('tcp-port-used'); import isRoot = require('is-root'); +import { master } from 'accesses'; + +import Logger from './utils/logger'; import ProgressBar from './utils/cli/progressbar'; import EnvironmentInfo from './utils/environmentInfo'; import MachineInfo from './utils/machineInfo'; import DependencyInfo from './utils/dependencyInfo'; -import { path as configPath } from './config'; +import { Config, path as configPath } from './config'; import loadConfig from './config'; const clusterLog = debug('misskey:cluster'); -enum InitResult { - Success, - Warn, - Failure -} - process.title = 'Misskey'; // Start app @@ -51,27 +47,26 @@ function main() { * Init master process */ async function masterMain() { - let initResult: InitResult; + let config: Config; try { // initialize app - initResult = await init(); + config = await init(); } catch (e) { console.error(e); process.exit(1); } - switch (initResult) { - case InitResult.Success: - Logger.info(chalk.green('Successfully initialized :)')); - break; - case InitResult.Warn: - Logger.warn(chalk.yellow('Initialized with some problem(s) :|')); - break; - case InitResult.Failure: - Logger.error(chalk.red('Fatal error occurred during initializing :(')); - process.exit(); - return; + if (config == null) { + Logger.error(chalk.red('Fatal error occurred during initializing :(')); + process.exit(); + } + + Logger.info(chalk.green('Successfully initialized :)')); + + // Init accesses + if (config.accesses && config.accesses.enable) { + master(); } spawnWorkers(() => { @@ -90,9 +85,7 @@ function workerMain() { /** * Init app */ -async function init() { - let warn = false; - +async function init(): Promise { Logger.info('Welcome to Misskey!'); Logger.info(chalk.bold('Misskey ')); Logger.info('Initializing...'); @@ -104,7 +97,7 @@ async function init() { let configLogger = new Logger('Config'); if (!fs.existsSync(configPath)) { configLogger.error('Configuration not found'); - return InitResult.Failure; + return null; } const config = loadConfig(); @@ -114,14 +107,14 @@ async function init() { if (process.platform === 'linux' && !isRoot() && config.port < 1024) { Logger.error('You need root privileges to listen on port below 1024 on Linux'); - return InitResult.Failure; + return null; } // Check if a port is being used /* https://github.com/stdarg/tcp-port-used/issues/3 if (await portUsed.check(config.port)) { Logger.error(`Port ${config.port} is already used`); - return InitResult.Failure; + return null; } */ @@ -133,10 +126,10 @@ async function init() { db.close(); } catch (e) { mongoDBLogger.error(e); - return InitResult.Failure; + return null; } - return warn ? InitResult.Warn : InitResult.Success; + return config; } function spawnWorkers(onComplete: any) { diff --git a/src/server.ts b/src/server.ts index 0274f6c182..97a1dd93dd 100644 --- a/src/server.ts +++ b/src/server.ts @@ -8,6 +8,7 @@ import * as https from 'https'; import * as cluster from 'cluster'; import * as express from 'express'; import * as morgan from 'morgan'; +import Accesses from 'accesses'; import vhost = require('vhost'); import config from './conf'; @@ -20,6 +21,16 @@ app.disable('x-powered-by'); app.set('trust proxy', 'loopback'); // Log +if (config.accesses && config.accesses.enable) { + const accesses = new Accesses({ + appName: 'Misskey', + port: config.accesses.port, + hashIp: true + }); + + app.use(accesses.express); +} + app.use(morgan(process.env.NODE_ENV == 'production' ? 'combined' : 'dev', { // create a write stream (in append mode) stream: config.accesslog ? fs.createWriteStream(config.accesslog) : null