forked from wavesplatform/WavesGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
104 lines (83 loc) · 3.18 KB
/
server.ts
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
import { createSecureServer } from 'http2';
import { createServer } from 'https';
import { route, parseArguments } from './ts-scripts/utils';
import { readFileSync } from 'fs';
import { serialize, parse as parserCookie } from 'cookie';
import { compile } from 'handlebars';
import { parse } from 'url';
import { TBuild, TConnection, TPlatform } from './ts-scripts/interface';
import { readFile } from 'fs-extra';
import { join } from 'path';
const ip = require('my-local-ip')();
const connectionTypes: Array<TConnection> = ['mainnet', 'testnet'];
const buildTypes: Array<TBuild> = ['dev', 'normal', 'min'];
const privateKey = readFileSync('localhost.key').toString();
const certificate = readFileSync('localhost.crt').toString();
const handler = function (req, res) {
const url = parse(req.url);
if (url.href.includes('/choose/')) {
const [platform, connection, build] = url.href.replace('/choose/', '').split('/');
const cookie = serialize('session', `${platform},${connection},${build}`, {
maxAge: 60 * 60 * 24,
path: '/'
});
res.setHeader('Set-Cookie', cookie);
res.statusCode = 302;
res.setHeader('Location', 'https://localhost:8080');
res.end();
return null;
}
const parsed = parseCookie(req.headers.cookie);
if (!parsed) {
readFile(join(__dirname, 'chooseBuild.hbs'), 'utf8').then((file) => {
res.end(compile(file)({ links: getBuildsLinks(req.headers['user-agent']) }));
});
} else {
route(parsed.connection, parsed.build, parsed.platform)(req, res);
}
};
function createMyServer(port) {
const server = createSecureServer({ key: privateKey, cert: certificate });
server.addListener('request', handler);
server.listen(port);
console.log(`Listen port ${port}...`);
console.log('Available urls:');
console.log(`https://localhost:${port}`);
}
function createSimpleServer({ port = 8000 }) {
const server = createServer({ key: privateKey, cert: certificate });
server.addListener('request', handler);
server.listen(port);
console.log(`Listen port ${port}, for simple server`);
console.log(`https://${ip}:${port}`);
}
createMyServer(8080);
const args = parseArguments() || Object.create(null);
if (args.startSimple) {
createSimpleServer(args);
}
function getBuildsLinks(userAgent: string = ''): Array<{ url: string; text: string }> {
const result = [];
const platform: TPlatform = userAgent.includes('Electron') ? 'desktop' : 'web';
connectionTypes.forEach((connection) => {
buildTypes.forEach((build) => {
result.push({
url: `/choose/${platform}/${connection}/${build}`,
text: `${platform} ${connection} ${build}`
});
});
});
return result;
}
function parseCookie(header = ''): IRequestData {
const [platform, connection, build] = ((parserCookie(header) || Object.create(null)).session || '').split(',');
if (!(build && connection && platform)) {
return null;
}
return { platform, connection, build } as IRequestData;
}
interface IRequestData {
platform: TPlatform;
connection: TConnection;
build: TBuild;
}