-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathserver.js
75 lines (61 loc) · 2.17 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
'use strict';
const express = require('express');
const http = require('http');
const path = require('path');
const process = require('child_process');
const bodyParser = require('body-parser');
const Proxy = require('../../lib/proxy');
const Session = require('../../lib/session');
const PROXY_PORT_1 = 1401;
const PROXY_PORT_2 = 1402;
const SERVER_PORT = 1400;
function createSession () {
const session = new Session('test/playground/upload-storage');
session._getIframePayloadScript = () => '';
session._getPayloadScript = () => '';
session.getAuthCredentials = () => ({});
session.handleFileDownload = () => void 0;
session.handlePageError = (ctx, err) => {
console.log(ctx.req.url);
console.log(err);
};
return session;
}
exports.start = sslOptions => {
const app = express();
const appServer = http.createServer(app);
const proxy = new Proxy('localhost', PROXY_PORT_1, PROXY_PORT_2, {
ssl: sslOptions,
developmentMode: true
});
app.use(bodyParser.urlencoded({ extended: true }));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'views/index.html'));
});
app.post('*', (req, res) => {
let url = req.body.url;
if (!url) {
res
.status(403)
.sendFile(path.resolve(__dirname, 'views/403.html'));
}
else {
if (!/^(?:file|https?):\/\//.test(url)) {
const matches = url.match(/^([A-Za-z]:)?(\/|\\)/);
if (matches && matches[0].length === 1)
url = 'file://' + url;
else if (matches && matches[0].length > 1)
url = 'file:///' + url;
else
url = 'http://' + url;
}
res
.status(301)
.set('location', proxy.openSession(url, createSession()))
.end();
}
});
appServer.listen(SERVER_PORT);
console.log('Server listens on port ' + SERVER_PORT);
process.exec('start http://localhost:' + SERVER_PORT);
};