Skip to content

Commit f8fd025

Browse files
committed
Virtual server folder with keys management
1 parent 931e913 commit f8fd025

File tree

5 files changed

+476
-7
lines changed

5 files changed

+476
-7
lines changed

Diff for: .github/workflows/node.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
runs-on: ${{ matrix.os }}
1515

1616
strategy:
17+
fail-fast: false
1718
matrix:
1819
node-version:
1920
- 14.x

Diff for: config.js.example

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ module.exports = {
1313
'@mod1',
1414
'@mod2',
1515
],
16+
virtualServer: {
17+
enabled: false, // If virtual servers should be used
18+
fileExtensions: [ // Extra files in root of server folder that should be copied to virtual servers
19+
'.json'
20+
],
21+
folders: [ // Extra folders in root of server folder that should be linked to virtual servers
22+
]
23+
},
1624
admins: [], // add steam IDs here to enable #login without password
1725
auth: { // If both username and password is set, HTTP Basic Auth will be used. You may use an array to specify more than one user.
1826
username: '', // Username for HTTP Basic Auth

Diff for: lib/server.js

+55-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var slugify = require('slugify')
55

66
var ArmaServer = require('arma-server')
77

8+
var virtualServer = require('./virtualServer')
89
var config = require('../config.js')
910

1011
var queryInterval = 5000
@@ -134,6 +135,29 @@ Server.prototype.start = function () {
134135
return this
135136
}
136137

138+
var self = this
139+
140+
if (self.config.virtualServer && self.config.virtualServer.enabled) {
141+
virtualServer.create(self.config, self.mods)
142+
.then((serverFolder) => {
143+
self.virtualServerFolder = serverFolder
144+
self.path = serverFolder
145+
self.realStart()
146+
})
147+
.catch((err) => {
148+
console.error('Error creating virtual server folder:', err)
149+
})
150+
} else {
151+
self.path = config.path
152+
self.realStart()
153+
}
154+
}
155+
156+
Server.prototype.realStart = function () {
157+
if (this.instance) {
158+
return this
159+
}
160+
137161
var parameters = this.getParameters()
138162
var server = new ArmaServer.Server({
139163
additionalConfigurationOptions: this.getAdditionalConfigurationOptions(),
@@ -154,7 +178,7 @@ Server.prototype.start = function () {
154178
parameters: parameters,
155179
password: this.password,
156180
passwordAdmin: this.admin_password,
157-
path: this.config.path,
181+
path: this.path,
158182
persistent: this.persistent ? 1 : 0,
159183
platform: this.config.type,
160184
players: this.max_players,
@@ -173,8 +197,13 @@ Server.prototype.start = function () {
173197
self.instance = null
174198

175199
self.stopHeadlessClients()
176-
177-
self.emit('state')
200+
.then(() => {
201+
if (self.virtualServerFolder) {
202+
virtualServer.remove(self.virtualServerFolder)
203+
self.virtualServerFolder = null
204+
}
205+
self.emit('state')
206+
})
178207
})
179208

180209
this.pid = instance.pid
@@ -209,7 +238,7 @@ Server.prototype.startHeadlessClients = function () {
209238
mods: self.mods,
210239
parameters: parameters,
211240
password: self.password,
212-
path: self.config.path,
241+
path: self.path,
213242
platform: self.config.type,
214243
port: self.port
215244
})
@@ -250,10 +279,29 @@ Server.prototype.stop = function (cb) {
250279
}
251280

252281
Server.prototype.stopHeadlessClients = function () {
253-
this.headlessClientInstances.map(function (headlessClientInstance) {
254-
headlessClientInstance.kill()
282+
var self = this
283+
return Promise.all(this.headlessClientInstances.map(function (headlessClientInstance) {
284+
var handled = false
285+
return new Promise(function (resolve, reject) {
286+
headlessClientInstance.on('close', function () {
287+
if (!handled) {
288+
handled = true
289+
resolve()
290+
}
291+
})
292+
293+
setTimeout(function () {
294+
if (!handled) {
295+
handled = true
296+
resolve()
297+
}
298+
}, 5000)
299+
300+
headlessClientInstance.kill()
301+
})
302+
})).then(function () {
303+
self.headlessClientInstances = []
255304
})
256-
this.headlessClientInstances = []
257305
}
258306

259307
Server.prototype.toJSON = function () {

Diff for: lib/virtualServer.js

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
var fs = require('fs')
2+
var fsExtra = require('fs.extra')
3+
var _ = require('lodash')
4+
var glob = require('glob')
5+
var os = require('os')
6+
var path = require('path')
7+
8+
const requiredFileExtensions = [
9+
'.dll',
10+
'.exe',
11+
'.so',
12+
'.txt' // Steam app id
13+
]
14+
15+
const serverFolders = [
16+
'addons',
17+
'aow',
18+
'argo',
19+
'battleye',
20+
'contact',
21+
'csla',
22+
'curator',
23+
'dll',
24+
'dta',
25+
'enoch',
26+
'expansion',
27+
'heli',
28+
'jets',
29+
'kart',
30+
'linux64',
31+
'mark',
32+
'mpmissions',
33+
'orange',
34+
'tacops',
35+
'tank'
36+
]
37+
38+
function copyKeys (config, serverFolder, mods) {
39+
// Copy needed keys, file symlinks on Windows are sketchy
40+
const keysFolder = path.join(serverFolder, 'keys')
41+
return fs.promises.mkdir(keysFolder, { recursive: true })
42+
.then(() => {
43+
const defaultKeysPath = path.join(config.path, 'keys')
44+
const defaultKeysPromise = fs.promises.readdir(defaultKeysPath)
45+
.then((files) => files.filter((file) => path.extname(file) === '.bikey'))
46+
.then((files) => files.map((file) => path.join(defaultKeysPath, file)))
47+
48+
const modKeysPromise = Promise.all(mods.map(mod => {
49+
return new Promise((resolve, reject) => {
50+
const modPath = path.join(config.path, mod)
51+
glob(`${modPath}/**/*.bikey`, function (err, files) {
52+
if (err) {
53+
return reject(err)
54+
}
55+
56+
return resolve(files)
57+
})
58+
})
59+
})).then((modsFiles) => modsFiles.flat())
60+
61+
return Promise.all([defaultKeysPromise, modKeysPromise].map((promise) => {
62+
return promise.then((keyFiles) => {
63+
return Promise.all(keyFiles.map((keyFile) => {
64+
return fs.promises.copyFile(keyFile, path.join(keysFolder, path.basename(keyFile)))
65+
}))
66+
})
67+
})).catch((err) => {
68+
console.error('Error copying keys:', err)
69+
})
70+
})
71+
}
72+
73+
function copyFiles (config, serverFolder) {
74+
const configFileExtensions = (config.virtualServer && config.virtualServer.fileExtensions) || []
75+
const allowedFileExtensions = _.uniq(requiredFileExtensions.concat(configFileExtensions))
76+
77+
return fs.promises.readdir(config.path)
78+
.then((files) => {
79+
// Copy needed files, file symlinks on Windows are sketchy
80+
const serverFiles = files.filter((file) => allowedFileExtensions.indexOf(path.extname(file)) >= 0 || path.basename(file) === 'arma3server' || path.basename(file) === 'arma3server_x64')
81+
return Promise.all(serverFiles.map((file) => {
82+
return fs.promises.copyFile(path.join(config.path, file), path.join(serverFolder, file))
83+
}))
84+
})
85+
}
86+
87+
function createModFolders (config, serverFolder, mods) {
88+
// Create virtual folders from default Arma and mods
89+
const configFolders = (config.virtualServer && config.virtualServer.folders) || []
90+
const serverMods = config.serverMods || []
91+
const symlinkFolders = _.uniq(serverFolders
92+
.concat(mods)
93+
.concat(configFolders)
94+
.concat(serverMods)
95+
.map(function (folder) {
96+
return folder.split(path.sep)[0]
97+
})
98+
)
99+
100+
return Promise.all(symlinkFolders.map((symlink) => {
101+
return fs.promises.access(path.join(config.path, symlink))
102+
.then(() => {
103+
return fs.promises.symlink(path.join(config.path, symlink), path.join(serverFolder, symlink), 'junction')
104+
.catch((err) => {
105+
console.error('Could create symlink for', symlink, 'due to', err)
106+
})
107+
})
108+
.catch(() => {})
109+
}))
110+
}
111+
112+
module.exports.create = function (config, mods) {
113+
return fs.promises.mkdtemp(path.join(os.tmpdir(), 'arma-server-'))
114+
.then((serverFolder) => {
115+
console.log('Created virtual server folder:', serverFolder)
116+
117+
return Promise.all([
118+
copyKeys(config, serverFolder, mods),
119+
copyFiles(config, serverFolder),
120+
createModFolders(config, serverFolder, mods)
121+
]).then(() => {
122+
return serverFolder
123+
})
124+
})
125+
}
126+
127+
module.exports.remove = function (folder, cb) {
128+
if (folder) {
129+
fsExtra.rmrf(folder, function (err) {
130+
if (err) {
131+
console.log('Error removing virtual server folder', err)
132+
}
133+
134+
if (cb) {
135+
cb(err)
136+
}
137+
})
138+
}
139+
}

0 commit comments

Comments
 (0)