forked from eysp/onlyoffice-ce-docker-license
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
362 lines (326 loc) · 13.9 KB
/
server.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* (c) Copyright Ascensio System SIA 2010-2019
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
'use strict';
const configCommon = require('config');
const config = configCommon.get('services.CoAuthoring');
//process.env.NODE_ENV = config.get('server.mode');
const logger = require('./../../Common/sources/logger');
const co = require('co');
const license = require('./../../Common/sources/license');
const fs = require('fs');
const express = require('express');
const http = require('http');
const urlModule = require('url');
const path = require('path');
const bodyParser = require("body-parser");
const multer = require('multer');
const mime = require('mime');
const apicache = require('apicache');
const docsCoServer = require('./DocsCoServer');
const canvasService = require('./canvasservice');
const converterService = require('./converterservice');
const fileUploaderService = require('./fileuploaderservice');
const wopiClient = require('./wopiClient');
const constants = require('./../../Common/sources/constants');
const utils = require('./../../Common/sources/utils');
const commonDefines = require('./../../Common/sources/commondefines');
const operationContext = require('./../../Common/sources/operationContext');
const tenantManager = require('./../../Common/sources/tenantManager');
const configStorage = configCommon.get('storage');
const cfgWopiEnable = configCommon.get('wopi.enable');
const cfgHtmlTemplate = configCommon.get('wopi.htmlTemplate');
const cfgTokenEnableBrowser = configCommon.get('services.CoAuthoring.token.enable.browser');
const cfgTokenEnableRequestInbox = configCommon.get('services.CoAuthoring.token.enable.request.inbox');
const cfgTokenEnableRequestOutbox = configCommon.get('services.CoAuthoring.token.enable.request.outbox');
const cfgLicenseFile = configCommon.get('license.license_file');
const app = express();
app.disable('x-powered-by');
//path.resolve uses __dirname by default(unexpected path in pkg)
app.set("views", path.resolve(process.cwd(), cfgHtmlTemplate));
app.set("view engine", "ejs");
const server = http.createServer(app);
let licenseInfo, licenseOriginal, updatePluginsTime, userPlugins, pluginsLoaded;
const updatePlugins = (eventType, filename) => {
operationContext.global.logger.info('update Folder: %s ; %s', eventType, filename);
if (updatePluginsTime && 1000 >= (new Date() - updatePluginsTime)) {
return;
}
operationContext.global.logger.info('update Folder true: %s ; %s', eventType, filename);
updatePluginsTime = new Date();
pluginsLoaded = false;
};
const readLicense = function*() {
[licenseInfo, licenseOriginal] = yield* license.readLicense(cfgLicenseFile);
};
const updateLicense = () => {
return co(function*() {
try {
yield* readLicense();
docsCoServer.setLicenseInfo(licenseInfo, licenseOriginal);
operationContext.global.logger.info('End updateLicense');
} catch (err) {
operationContext.global.logger.error('updateLicense error: %s', err.stack);
}
});
};
operationContext.global.logger.warn('Express server starting...');
if (!(cfgTokenEnableBrowser && cfgTokenEnableRequestInbox && cfgTokenEnableRequestOutbox)) {
operationContext.global.logger.warn('Set services.CoAuthoring.token.enable.browser, services.CoAuthoring.token.enable.request.inbox, ' +
'services.CoAuthoring.token.enable.request.outbox in the Document Server config ' +
'to prevent an unauthorized access to your documents and the substitution of important parameters in ONLYOFFICE Document Server requests.');
}
if (!tenantManager.isMultitenantMode()) {
updateLicense();
setInterval(updateLicense, 86400000);
}
if (config.has('server.static_content')) {
const staticContent = config.get('server.static_content');
for (let i in staticContent) {
if (staticContent.hasOwnProperty(i)) {
app.use(i, express.static(staticContent[i]['path'], staticContent[i]['options']));
}
}
}
if (configStorage.has('fs.folderPath')) {
const cfgBucketName = configStorage.get('bucketName');
const cfgStorageFolderName = configStorage.get('storageFolderName');
app.use('/' + cfgBucketName + '/' + cfgStorageFolderName, (req, res, next) => {
const index = req.url.lastIndexOf('/');
if ('GET' === req.method && -1 != index) {
let sendFileOptions = {
root: configStorage.get('fs.folderPath'), dotfiles: 'deny', headers: {
'Content-Disposition': 'attachment'
}
};
const urlParsed = urlModule.parse(req.url);
if (urlParsed && urlParsed.pathname) {
const filename = decodeURIComponent(path.basename(urlParsed.pathname));
sendFileOptions.headers['Content-Type'] = mime.getType(filename);
}
const realUrl = req.url.substring(0, index);
res.sendFile(realUrl, sendFileOptions, (err) => {
if (err) {
operationContext.global.logger.error(err);
res.status(400).end();
}
});
} else {
res.sendStatus(404)
}
});
}
try {
fs.watch(config.get('plugins.path'), updatePlugins);
} catch (e) {
operationContext.global.logger.warn('Failed to subscribe to plugin folder updates. When changing the list of plugins, you must restart the server. https://nodejs.org/docs/latest/api/fs.html#fs_availability');
}
// Если захочется использовать 'development' и 'production',
// то с помощью app.settings.env (https://github.com/strongloop/express/issues/936)
// Если нужна обработка ошибок, то теперь она такая https://github.com/expressjs/errorhandler
docsCoServer.install(server, () => {
operationContext.global.logger.info('Start callbackFunction');
server.listen(config.get('server.port'), () => {
operationContext.global.logger.warn("Express server listening on port %d in %s mode. Version: %s. Build: %s", config.get('server.port'), app.settings.env, commonDefines.buildVersion, commonDefines.buildNumber);
});
app.get('/index.html', (req, res) => {
return co(function*() {
let ctx = new operationContext.Context();
try {
ctx.initFromRequest(req);
let licenseInfo = yield tenantManager.getTenantLicense(ctx);
let buildVersion = commonDefines.buildVersion;
let buildNumber = commonDefines.buildNumber;
let buildDate, packageType, customerId = "", alias = "";
if (licenseInfo) {
buildDate = licenseInfo.buildDate.toISOString();
packageType = licenseInfo.packageType;
customerId = licenseInfo.customerId;
alias = licenseInfo.alias;
}
let output = `Server is functioning normally. Version: ${buildVersion}. Build: ${buildNumber}`;
output += `. Release date: ${buildDate}. Package type: ${packageType}. Customer Id: ${customerId}. Alias: ${alias}`;
res.send(output);
} catch (err) {
ctx.logger.error('index.html error: %s', err.stack);
res.sendStatus(400);
}
});
});
const rawFileParser = bodyParser.raw(
{inflate: true, limit: config.get('server.limits_tempfile_upload'), type: function() {return true;}});
const urleEcodedParser = bodyParser.urlencoded({ extended: false });
let forms = multer();
app.get('/coauthoring/CommandService.ashx', utils.checkClientIp, rawFileParser, docsCoServer.commandFromServer);
app.post('/coauthoring/CommandService.ashx', utils.checkClientIp, rawFileParser,
docsCoServer.commandFromServer);
app.get('/ConvertService.ashx', utils.checkClientIp, rawFileParser, converterService.convertXml);
app.post('/ConvertService.ashx', utils.checkClientIp, rawFileParser, converterService.convertXml);
app.post('/converter', utils.checkClientIp, rawFileParser, converterService.convertJson);
app.get('/FileUploader.ashx', utils.checkClientIp, rawFileParser, fileUploaderService.uploadTempFile);
app.post('/FileUploader.ashx', utils.checkClientIp, rawFileParser, fileUploaderService.uploadTempFile);
app.param('docid', (req, res, next, val) => {
if (constants.DOC_ID_REGEX.test(val)) {
next();
} else {
res.sendStatus(403);
}
});
app.param('index', (req, res, next, val) => {
if (!isNaN(parseInt(val))) {
next();
} else {
res.sendStatus(403);
}
});
//'*' for backward compatible
app.post('/uploadold/:docid*', fileUploaderService.uploadImageFileOld);
app.post('/upload/:docid*', rawFileParser, fileUploaderService.uploadImageFile);
app.post('/downloadas/:docid', rawFileParser, canvasService.downloadAs);
app.post('/savefile/:docid', rawFileParser, canvasService.saveFile);
app.get('/printfile/:docid/:filename', canvasService.printFile);
app.get('/downloadfile/:docid', canvasService.downloadFile);
app.get('/healthcheck', utils.checkClientIp, docsCoServer.healthCheck);
app.get('/baseurl', (req, res) => {
res.send(utils.getBaseUrlByRequest(req));
});
app.get('/robots.txt', (req, res) => {
res.setHeader('Content-Type', 'plain/text');
res.send("User-agent: *\nDisallow: /");
});
app.post('/docbuilder', utils.checkClientIp, rawFileParser, (req, res) => {
converterService.builder(req, res);
});
app.get('/info/info.json', utils.checkClientIp, docsCoServer.licenseInfo);
app.put('/internal/cluster/inactive', utils.checkClientIp, docsCoServer.shutdown);
app.delete('/internal/cluster/inactive', utils.checkClientIp, docsCoServer.shutdown);
if (cfgWopiEnable) {
app.get('/hosting/discovery', utils.checkClientIp, wopiClient.discovery);
app.get('/hosting/capabilities', utils.checkClientIp, wopiClient.collaboraCapabilities);
app.post('/hosting/wopi/:documentType/:mode', urleEcodedParser, forms.none(), utils.lowercaseQueryString, wopiClient.getEditorHtml);
}
app.post('/dummyCallback', utils.checkClientIp, rawFileParser, function(req, res){
let ctx = new operationContext.Context();
ctx.initFromRequest(req);
ctx.logger.debug(`dummyCallback req.body:%s`, req.body);
utils.fillResponseSimple(res, JSON.stringify({error: 0}, "application/json"));
});
const sendUserPlugins = (res, data) => {
pluginsLoaded = true;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(data));
};
app.get('/plugins.json', (req, res) => {
if (userPlugins && pluginsLoaded) {
sendUserPlugins(res, userPlugins);
return;
}
if (!config.has('server.static_content') || !config.has('plugins.uri')) {
res.sendStatus(404);
return;
}
let staticContent = config.get('server.static_content');
let pluginsUri = config.get('plugins.uri');
let pluginsPath = undefined;
let pluginsAutostart = config.get('plugins.autostart');
if (staticContent[pluginsUri]) {
pluginsPath = staticContent[pluginsUri].path;
}
let baseUrl = '../../../..';
utils.listFolders(pluginsPath, true).then((values) => {
return co(function*() {
const configFile = 'config.json';
let stats = null;
let result = [];
for (let i = 0; i < values.length; ++i) {
try {
stats = yield utils.fsStat(path.join(values[i], configFile));
} catch (err) {
stats = null;
}
if (stats && stats.isFile) {
result.push( baseUrl + pluginsUri + '/' + path.basename(values[i]) + '/' + configFile);
}
}
userPlugins = {'url': '', 'pluginsData': result, 'autostart': pluginsAutostart};
sendUserPlugins(res, userPlugins);
});
});
});
app.get('/themes.json', apicache.middleware("5 minutes"), (req, res) => {
return co(function*() {
let themes = [];
let ctx = new operationContext.Context();
try {
ctx.initFromRequest(req);
ctx.logger.info('themes.json start');
if (!config.has('server.static_content') || !config.has('themes.uri')) {
return;
}
let staticContent = config.get('server.static_content');
let themesUri = config.get('themes.uri');
let themesList = [];
for (let i in staticContent) {
if (staticContent.hasOwnProperty(i) && themesUri.startsWith(i)) {
let dir = staticContent[i].path + themesUri.substring(i.length);
themesList = yield utils.listObjects(dir, true);
ctx.logger.debug('themes.json dir:%s', dir);
ctx.logger.debug('themes.json themesList:%j', themesList);
for (let j = 0; j < themesList.length; ++j) {
if (themesList[j].endsWith('.json')) {
let data = yield utils.readFile(themesList[j], true);
themes.push(JSON.parse(data.toString('utf-8')));
}
}
break;
}
}
} catch (err) {
ctx.logger.error('themes.json error:%s', err.stack);
} finally {
if (themes.length > 0) {
res.setHeader('Content-Type', 'application/json');
res.send({"themes": themes});
} else {
res.sendStatus(404);
}
ctx.logger.info('themes.json end');
}
});
});
});
process.on('uncaughtException', (err) => {
operationContext.global.logger.error((new Date).toUTCString() + ' uncaughtException:', err.message);
operationContext.global.logger.error(err.stack);
logger.shutdown(() => {
process.exit(1);
});
});