-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { format as formatUrl } from 'url'; | ||
import Hapi from 'hapi'; | ||
|
||
// If a redirect port is specified, we need to start a server a non-ssl | ||
// server at this port, and redirect all requests to the ssl port. | ||
export default function (kbnServer, server, config) { | ||
const isSslEnabled = config.get('server.ssl.enabled'); | ||
const portToRedirectFrom = config.get('server.ssl.redirectHttpFromPort'); | ||
|
||
// Both ssl and port to redirect from must be specified | ||
if (!isSslEnabled || portToRedirectFrom === undefined) { | ||
return; | ||
} | ||
|
||
const host = config.get('server.host'); | ||
const sslPort = config.get('server.port'); | ||
|
||
const redirectServer = new Hapi.Server(); | ||
|
||
redirectServer.connection({ | ||
host, | ||
port: portToRedirectFrom | ||
}); | ||
|
||
redirectServer.ext('onRequest', (req, reply) => { | ||
reply.redirect(formatUrl({ | ||
protocol: 'https', | ||
hostname: host, | ||
port: sslPort, | ||
pathname: req.url.pathname, | ||
search: req.url.search, | ||
})); | ||
}); | ||
|
||
redirectServer.start((err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
} |