'use strict' ; import fs from 'fs' ; import deepmerge from 'deepmerge' ; import { IsNullOrEmpty , IsNotNullOrEmpty , getMyExternalIPAddress } from '@Tools/Misc' ; import { httpRequest , httpsRequest } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; export let Config = { 'metaverse' : { 'metaverse-name' : 'Vircadia noobie' , 'metaverse-nick-name' : 'Noobie' , 'metaverse-server-url' : '' , 'default-ice-server-url' : '' , 'dashboard-url' : 'https://dashboard.vircadia.com' } , 'server' : { 'listen-host' : '0.0.0.0' , 'listen-port' : 9400 , 'key-file' : '' , 'cert-file' : '' , 'max-body-size' : 300000 , 'static-base' : '/static' , 'user-config-file' : './iamus.json' , 'server-version' : { 'version-tag' : '1.1.1-20200101-abcdefg' } } , 'auth' : { 'domain-token-expire-hours' : 24 * 365 , 'owner-token-expire-hours' : 24 * 7 } , 'metaverse-server' : { 'http-error-on-failure' : true , 'error-header' : 'x-vircadia-error-handle' , 'metaverse-info-addition-file' : './metaverse_info.json' , 'max-name-length' : 32 , 'session-timeout-minutes' : 5 , 'heartbeat-seconds-until-offline' : 5 * 60 , 'domain-seconds-until-offline' : 10 * 60 , 'domain-seconds-check-if-online' : 2 * 60 , 'handshake-request-expiration-minutes' : 1 , 'connection-request-expiration-minutes' : 60 * 24 * 4 , 'friend-request-expiration-minutes' : 60 * 24 * 4 , 'place-current-timeout-minutes' : 5 , 'place-inactive-timeout-minutes' : 60 , 'place-check-last-activity-seconds' : ( 3 * 60 ) - 5 , 'tokengen_url' : 'METAVERSE_SERVER_URL/static/DomainTokenLogin.html' , 'base-admin-account' : '' , 'fix-domain-network-address' : true , 'allow-temp-domain-creation' : false , 'enable-account-email-verification' : false , 'email-verification-timeout-minutes' : 1440 , 'email-verification-email-body' : 'dist/static/verificationEmail.html' , 'email-verification-from' : '' , 'email-verification-success-redirect' : 'METAVERSE_SERVER_URL/static/verificationEmailSuccess.html' , 'email-verification-failure-redirect' : 'METAVERSE_SERVER_URL/static/verificationEmailFailure.html?r=FAILURE_REASON' } , 'nodemailer-transport-config' : { 'host' : 'SMTP-HOSTNAME' , 'port' : 465 , 'secure' : true , 'auth' : { 'user' : 'SMTP-USER' , 'pass' : 'SMTP-PASSWORD' } } , 'monitoring' : { 'enable' : true , 'history' : true } , 'database' : { 'db-host' : 'localhost' , 'db-port' : 27017 , 'db' : 'tester' , 'db-user' : 'metaverse' , 'db-pw' : 'nooneknowsit' , 'db-authdb' : 'admin' , 'db-connection' : '' } , 'backup' : { "backup-user" : "backuper" , "backup-pw" : "nooneknowsit" , "backup-dir" : "directoryName" , "authenticationDatabase" : "databaseName" } , 'debug' : { 'loglevel' : 'info' , 'log-to-files' : true , 'log-filename' : 'iamus.log' , 'log-directory' : './logs' , 'log-max-size-megabytes' : 100 , 'log-max-files' : 10 , 'log-tailable' : true , 'log-compress' : false , 'log-to-console' : false , 'devel' : false , 'request-detail' : false , 'request-body' : false , 'metaverseapi-response-detail' : false , 'query-detail' : false , 'db-query-detail' : false , 'field-setting' : false } } ; export async function initializeConfiguration ( ) { const envLogLevel = process . env . IAMUS_LOGLEVEL ; if ( IsNotNullOrEmpty ( envLogLevel ) ) Config . debug . loglevel = envLogLevel ; const envHost = process . env . IAMUS_LISTEN_HOST ; if ( IsNotNullOrEmpty ( envHost ) ) Config . server [ 'listen-host' ] = envHost ; const envPort = process . env . IAMUS_LISTEN_PORT ; if ( IsNotNullOrEmpty ( envPort ) ) Config . server [ 'listen-port' ] = Number ( envPort ) ; const envConfigFile = process . env . IAMUS_CONFIG_FILE ; if ( IsNotNullOrEmpty ( envConfigFile ) ) Config . server [ "user-config-file" ] = envConfigFile ; try { const userConfigFile = Config . server [ "user-config-file" ] ; if ( IsNotNullOrEmpty ( userConfigFile ) ) { Logger . debug ( `initializeConfiguration: reading configuration file ${ userConfigFile }` ) ; const userConfig = await readInJSON ( userConfigFile ) ; if ( IsNotNullOrEmpty ( userConfig ) ) { Config = deepmerge ( Config , userConfig ) ; } ; } ; } catch ( e ) { Logger . error ( 'initializeConfiguration: exception adding user config: ' + e ) ; } try { let versionInfo ; for ( const versionFile of [ './VERSION.json' , './dist/VERSION.json' ] ) { if ( fs . existsSync ( versionFile ) ) { versionInfo = await readInJSON ( versionFile ) ; break ; } ; } ; if ( IsNullOrEmpty ( versionInfo ) ) { versionInfo = { 'version-tab' : 'unknown' } ; } ; Config . server [ "server-version" ] = versionInfo ; Logger . debug ( `initializeConfiguration: version info: ${ JSON . stringify ( versionInfo , null , 4 ) }` ) ; } catch ( e ) { Logger . error ( 'initializeConfiguration: exception reading version info: ' + e ) ; } ; if ( IsNullOrEmpty ( Config . metaverse [ "default-ice-server-url" ] ) ) { const myAddr = await getMyExternalIPAddress ( ) ; Logger . debug ( `initializeConfiguration: made ice server addr of ${ myAddr }` ) ; Config . metaverse [ "default-ice-server-url" ] = myAddr ; } ; if ( IsNullOrEmpty ( Config . metaverse [ "metaverse-server-url" ] ) ) { const myAddr = await getMyExternalIPAddress ( ) ; const newUrl = `http://${ myAddr }:${ Config . server [ "listen-port" ] . toString ( ) }/` ; Logger . debug ( `initializeConfiguration: built metaverse url of ${ newUrl }` ) ; Config . metaverse [ "metaverse-server-url" ] = newUrl ; } ; let msu = Config . metaverse [ "metaverse-server-url" ] ; while ( msu . endsWith ( '/' ) ) { msu = msu . slice ( 0 , - 1 ) ; } ; Config . metaverse [ "metaverse-server-url" ] = msu ; const configSubset = { } ; configSubset . metaverse = Config . metaverse ; configSubset . server = Config . server ; configSubset . debug = Config . debug ; const staticBase = Config . server [ 'static-base' ] ; for ( const staticDir of [ '.' + staticBase , './dist' + staticBase ] ) { if ( fs . existsSync ( staticDir ) ) { const configSubsetFilename = staticDir + '/config.json' ; try { fs . writeFileSync ( configSubsetFilename , JSON . stringify ( configSubset ) ) ; Logger . info ( `initializeConfiguration: wrote static config subset to ${ configSubsetFilename }` ) ; } catch ( err ) { Logger . error ( `initializeConfiguration: error writing ${ configSubsetFilename }: ${ err }` ) ; } ; break ; } ; } ; return ; } ; export async function readInJSON ( pFilenameOrURL ) { let configBody ; if ( pFilenameOrURL . startsWith ( 'http://' ) ) { configBody = await httpRequest ( pFilenameOrURL ) ; } else { if ( pFilenameOrURL . startsWith ( 'https://' ) ) { configBody = await httpsRequest ( pFilenameOrURL ) ; } else { try { configBody = fs . readFileSync ( pFilenameOrURL , 'utf-8' ) ; } catch ( err ) { Logger . debug ( `readInJSON: failed read of user config file ${ pFilenameOrURL }: ${ err }` ) ; } ; } ; } ; if ( IsNotNullOrEmpty ( configBody ) ) { return JSON . parse ( configBody ) ; } ; return undefined ; } ; export default Config ;