forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (162 loc) · 4.85 KB
/
index.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
module.exports = function (kbnServer, server, config) {
let _ = require('lodash');
let fs = require('fs');
let Boom = require('boom');
let Hapi = require('hapi');
let parse = require('url').parse;
let format = require('url').format;
let getDefaultRoute = require('./getDefaultRoute');
server = kbnServer.server = new Hapi.Server();
const shortUrlLookup = require('./short_url_lookup')(server);
// Create a new connection
var connectionOptions = {
host: config.get('server.host'),
port: config.get('server.port'),
state: {
strictHeader: false
},
routes: {
cors: config.get('server.cors'),
payload: {
maxBytes: config.get('server.maxPayloadBytes')
}
}
};
// enable tls if ssl key and cert are defined
if (config.get('server.ssl.key') && config.get('server.ssl.cert')) {
connectionOptions.tls = {
key: fs.readFileSync(config.get('server.ssl.key')),
cert: fs.readFileSync(config.get('server.ssl.cert')),
// The default ciphers in node 0.12.x include insecure ciphers, so until
// we enforce a more recent version of node, we craft our own list
// @see https://github.com/nodejs/node/blob/master/src/node_constants.h#L8-L28
ciphers: [
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-ECDSA-AES256-GCM-SHA384',
'DHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES128-SHA256',
'DHE-RSA-AES128-SHA256',
'ECDHE-RSA-AES256-SHA384',
'DHE-RSA-AES256-SHA384',
'ECDHE-RSA-AES256-SHA256',
'DHE-RSA-AES256-SHA256',
'HIGH',
'!aNULL',
'!eNULL',
'!EXPORT',
'!DES',
'!RC4',
'!MD5',
'!PSK',
'!SRP',
'!CAMELLIA'
].join(':'),
// We use the server's cipher order rather than the client's to prevent
// the BEAST attack
honorCipherOrder: true
};
}
server.connection(connectionOptions);
// provide a simple way to expose static directories
server.decorate('server', 'exposeStaticDir', function (routePath, dirPath) {
this.route({
path: routePath,
method: 'GET',
handler: {
directory: {
path: dirPath,
listing: true,
lookupCompressed: true
}
},
config: {auth: false}
});
});
// provide a simple way to expose static files
server.decorate('server', 'exposeStaticFile', function (routePath, filePath) {
this.route({
path: routePath,
method: 'GET',
handler: {
file: filePath
},
config: {auth: false}
});
});
// helper for creating view managers for servers
server.decorate('server', 'setupViews', function (path, engines) {
this.views({
path: path,
isCached: config.get('optimize.viewCaching'),
engines: _.assign({ jade: require('jade') }, engines || {})
});
});
server.decorate('server', 'redirectToSlash', function (route) {
this.route({
path: route,
method: 'GET',
handler: function (req, reply) {
return reply.redirect(format({
search: req.url.search,
pathname: req.url.pathname + '/',
}));
}
});
});
// attach the app name to the server, so we can be sure we are actually talking to kibana
server.ext('onPreResponse', function (req, reply) {
let response = req.response;
if (response.isBoom) {
response.output.headers['kbn-name'] = kbnServer.name;
response.output.headers['kbn-version'] = kbnServer.version;
} else {
response.header('kbn-name', kbnServer.name);
response.header('kbn-version', kbnServer.version);
}
return reply.continue();
});
server.route({
path: '/',
method: 'GET',
handler: function (req, reply) {
return reply.view('rootRedirect', {
hashRoute: `${config.get('server.basePath')}/app/kibana`,
defaultRoute: getDefaultRoute(kbnServer),
});
}
});
server.route({
method: 'GET',
path: '/{p*}',
handler: function (req, reply) {
let path = req.path;
if (path === '/' || path.charAt(path.length - 1) !== '/') {
return reply(Boom.notFound());
}
return reply.redirect(format({
search: req.url.search,
pathname: path.slice(0, -1),
}))
.permanent(true);
}
});
server.route({
method: 'GET',
path: '/goto/{urlId}',
handler: async function (request, reply) {
const url = await shortUrlLookup.getUrl(request.params.urlId);
reply().redirect(config.get('server.basePath') + url);
}
});
server.route({
method: 'POST',
path: '/shorten',
handler: async function (request, reply) {
const urlId = await shortUrlLookup.generateUrlId(request.payload.url);
reply(urlId);
}
});
return kbnServer.mixin(require('./xsrf'));
};