-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
182 lines (156 loc) · 4.95 KB
/
app.js
File metadata and controls
182 lines (156 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var bodyParser = require('body-parser');
var Logger = require('./lib/Log');
var Config = require('./lib/Config');
var Server = require('./lib/Server');
var RCON = require('./lib/RCON');
var Query = require('./lib/Query');
var Steam = require('./lib/Steam');
var Scheduler = require('./lib/Scheduler');
var router = express.Router();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/', router);
// Allow Cross Origin
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
global.state = {
WaitForUpdate: false
};
global.timers = {
niceStop: []
};
global.QueryData = null;
global.GameData = {
Players: [],
Tribes: []
};
function checkSecret(req, res, callback) {
Config.Load(function(config) {
if(config.API.Secret == req.params['key']) {
callback()
} else {
res.json({status: false, message: "Access denied, incorrect key"});
}
});
}
// API: Status
app.get('/', function(req, res) {
Config.Load(function(config) {
Server.IsRunning(function(s) {
if(s && s.process && s.process.arguments && s.process.arguments[0]) s.process.arguments[0] = s.process.arguments[0]
.replace(/\?ServerAdminPassword=[a-zA-Z0-9.,\-!$]+/i, "")
.replace(/\?ServerPassword=[a-zA-Z0-9.,\-!$]+/i, "");
if(config && config.Server && config.Server.Params) config.Server.Params = config.Server.Params
.replace(/\?ServerAdminPassword=[a-zA-Z0-9.,\-!$]+/i, "")
.replace(/\?ServerPassword=[a-zA-Z0-9.,\-!$]+/i, "");
try {
res.json({
Server: config.Server,
Running: s
});
} catch(e) {
// Sent data after headers were sent.
}
});
})
});
// API: Query
app.get('/query', function(req, res) {
res.json(global.QueryData);
});
// API: Update
app.get('/update/:key', function (req, res) {
checkSecret(req, res, function() {
Server.Stop(function () {
Logger.log('info', "[Server] Stopping for update");
Steam.Update(function (data) {
Logger.log('info', "[Update] " + (data.success ? "Success" : "Failed"));
Server.Start(function () {
Logger.log('info', "[Server] Started");
});
})
});
});
});
// API: Server Start
app.get('/start/:key', function(req, res) {
checkSecret(req, res, function() {
Server.Start(function (game) {
res.json(game);
});
});
});
// API: Server Nice Stop
app.get('/stop/:message/:key', function(req, res) {
checkSecret(req, res, function() {
Server.StopNice(req.params.message, function () {
});
res.json({status: true, message: req.params.message});
});
});
// API: Server Force Stop
app.get('/force/stop/:key', function(req, res) {
checkSecret(req, res, function() {
Server.Stop(function (status) {
res.json({status: status});
});
});
});
// API: Cancel Stop
app.get('/cancel/stop/:key', function(req, res) {
checkSecret(req, res, function() {
Server.CancelNiceStop(function (response) {
res.json(response);
});
});
});
// API: RCON Command
app.get('/rcon/:command/:key', function(req, res) {
checkSecret(req, res, function() {
RCON.Command(req.params['command'], function (response) {
res.json(response);
});
});
});
// API: Get Players
app.get('/players', function(req, res) {
res.json(global.GameData.Players);
});
// API: Get Tribes
app.get('/tribes', function(req, res) {
res.json(global.GameData.Tribes);
});
// API: Get Schedule
app.get('/scheduler/jobs', function(req, res) {
Scheduler.GetJobs(function(schedule) {
res.json(schedule);
})
});
// catch the uncaught errors that weren't wrapped in a domain or try catch statement
// do not use this in modules, but only in applications, as otherwise we could have multiple of these bound
process.on('uncaughtException', function (err) {
// handle the error safely
Logger.log('error', 'Uncaught exception: ' + err);
})
Config.Init(function() {
Config.Load(function(config) {
server.listen(config.API.Port);
Steam.UpdateSteamCMD(function (success) {
if(success)
{
Logger.log('info', 'Accessible by http://localhost:' + config.API.Port);
Server.Init();
setTimeout(function () {
Scheduler.Init(Server, io);
}, 50);
}
});
});
});