Skip to content

Commit

Permalink
Virtual server folder with keys management
Browse files Browse the repository at this point in the history
  • Loading branch information
Dahlgren committed Jun 21, 2020
1 parent babe25e commit 7a44f62
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 9 deletions.
129 changes: 120 additions & 9 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var _ = require('lodash')
var events = require('events')
var fs = require('fs')
var fsExtra = require('fs.extra')
var Gamedig = require('gamedig')
var glob = require('glob')
var path = require('path')
var slugify = require('slugify')

var ArmaServer = require('arma-server')
Expand Down Expand Up @@ -134,6 +137,104 @@ Server.prototype.start = function () {
return this
}

var self = this

const requiredFileExtensions = ['.dll', '.exe', '.so', '.txt']
const serverFolders = [
'addons',
'argo',
'battleye',
'contact',
'curator',
'dll',
'dta',
'enoch',
'expansion',
'gm',
'heli',
'jets',
'kart',
'mark',
'mpmissions',
'orange',
'tacops',
'tank'
]
const symlinkFolders = serverFolders.concat(self.mods).concat(config.serverMods)

return fs.promises.mkdtemp(path.join(self.config.path, 'arma-server-'))
.then((serverFolder) => {
self.virtualServerFolder = serverFolder
console.log('Created virtual server folder:', serverFolder)

return fs.promises.readdir(self.config.path)
.then((files) => {
// Copy needed files, file symlinks on Windows are sketchy
const serverFiles = files.filter((file) => requiredFileExtensions.indexOf(path.extname(file)) >= 0 || path.basename(file) === 'arma3server')
return Promise.all(serverFiles.map((file) => {
return fs.promises.copyFile(path.join(self.config.path, file), path.join(serverFolder, file))
}))
})
.then(() => {
// Create virtual folders from default Arma and mods
return Promise.all(symlinkFolders.map((symlink) => {
return fs.promises.access(path.join(self.config.path, symlink))
.then(() => {
return fs.promises.symlink(path.join(self.config.path, symlink), path.join(serverFolder, symlink), 'junction')
})
.catch((err) => {
console.error('Could create symlink for', symlink, 'due to', err)
})
}))
})
.then(() => {
// Copy needed keys, file symlinks on Windows are sketchy
const keysFolder = path.join(serverFolder, 'keys')
return fs.promises.mkdir(keysFolder, { recursive: true })
.then(() => {
const defaultKeysPath = path.join(self.config.path, 'keys')
const defaultKeysPromise = fs.promises.readdir(defaultKeysPath)
.then((files) => files.filter((file) => path.extname(file) === '.bikey'))
.then((files) => files.map((file) => path.join(defaultKeysPath, file)))

const modKeysPromise = Promise.all(self.mods.map(mod => {
return new Promise((resolve, reject) => {
const modPath = path.join(this.config.path, mod)
glob(`${modPath}/**/*.bikey`, function (err, files) {
if (err) {
return reject(err)
}

return resolve(files)
})
})
})).then((modsFiles) => modsFiles.flat())

return Promise.all([defaultKeysPromise, modKeysPromise].map((promise) => {
return promise.then((keyFiles) => {
return Promise.all(keyFiles.map((keyFile) => {
return fs.promises.copyFile(keyFile, path.join(keysFolder, path.basename(keyFile)))
}))
})
})).catch((err) => {
console.error('Error copying keys:', err)
})
})
})
.then(() => {
self.realStart(serverFolder)
})
.catch((err) => {
console.error('Error creating virtual server folder:', err)
})
})
}

Server.prototype.realStart = function (path) {
if (this.instance) {
return this
}

var parameters = this.getParameters()
var server = new ArmaServer.Server({
additionalConfigurationOptions: this.getAdditionalConfigurationOptions(),
Expand All @@ -154,7 +255,7 @@ Server.prototype.start = function () {
parameters: parameters,
password: this.password,
passwordAdmin: this.admin_password,
path: this.config.path,
path: path,
persistent: this.persistent ? 1 : 0,
platform: this.config.type,
players: this.max_players,
Expand Down Expand Up @@ -196,8 +297,18 @@ Server.prototype.start = function () {
self.instance = null

self.stopHeadlessClients()

self.emit('state')
.then(() => {
if (self.virtualServerFolder) {
fsExtra.rmrf(self.virtualServerFolder, function (err) {
if (err) {
console.log('Error removing virtual server folder', err)
}
})
self.virtualServerFolder = null
}

self.emit('state')
})
})

instance.on('error', function (err) {
Expand All @@ -210,14 +321,14 @@ Server.prototype.start = function () {
self.queryStatus()
}, queryInterval)

this.startHeadlessClients()
this.startHeadlessClients(path)

this.emit('state')

return this
}

Server.prototype.startHeadlessClients = function () {
Server.prototype.startHeadlessClients = function (path) {
var parameters = this.getParameters()
var self = this
var headlessClientInstances = _.times(this.number_of_headless_clients, function (i) {
Expand All @@ -228,7 +339,7 @@ Server.prototype.startHeadlessClients = function () {
mods: self.mods,
parameters: parameters,
password: self.password,
path: self.config.path,
path: path,
platform: self.config.type,
port: self.port
})
Expand Down Expand Up @@ -293,9 +404,9 @@ Server.prototype.stop = function (cb) {
}

Server.prototype.stopHeadlessClients = function () {
this.headlessClientInstances.map(function (headlessClientInstance) {
headlessClientInstance.kill()
})
return Promise.all(this.headlessClientInstances.map(function (headlessClientInstance) {
return headlessClientInstance.kill()
}))
}

Server.prototype.toJSON = function () {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"filesize": "^3.1.0",
"fs.extra": "~1.3.2",
"gamedig": "^0.2.30",
"glob": "^7.1.6",
"jquery": "^3.5.0",
"jquery.iframe-transport": "^1.0.0",
"ladda": "1.0.5",
Expand Down

0 comments on commit 7a44f62

Please sign in to comment.