forked from mozilla/persona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router
executable file
·166 lines (141 loc) · 5.02 KB
/
router
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
#!/usr/bin/env node
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const
fs = require('fs'),
path = require('path'),
url = require('url'),
http = require('http'),
urlparse = require('urlparse'),
express = require('express');
const
metrics = require('../lib/metrics.js'),
wsapi = require('../lib/wsapi.js'),
config = require('../lib/configuration.js'),
heartbeat = require('../lib/heartbeat.js'),
logger = require('../lib/logging.js').logger,
forward = require('../lib/http_forward').forward,
shutdown = require('../lib/shutdown');
var app = undefined;
app = express.createServer();
logger.info("router server starting up");
// verify that we have a keysigner configured
if (!config.get('keysigner_url')) {
logger.error('missing required configuration - url for the keysigner (KEYSIGNER_URL in env)');
process.exit(1);
}
// verify that we have a dbwriter configured
if (!config.get('dbwriter_url')) {
logger.error('missing required configuration - url for the dbwriter (DBWRITER_URL in env)');
process.exit(1);
}
// verify that we have a browserid configured
if (!config.get('browserid_url')) {
logger.error('missing required configuration - url for browserid (BROWSERID_URL in env)');
process.exit(1);
}
// NOTE: ordering of middleware registration is important in this file, it is the
// order in which middleware will be invoked as requests are processed.
// #1 - Setup health check / heartbeat middleware.
// Depends on positive health checks from browserid and static processes
// This is in front of logging on purpose. see issue #537
var browserid_url = urlparse(config.get('browserid_url')).validate().normalize().originOnly();
var static_url = urlparse(config.get('static_url')).validate().normalize().originOnly();
heartbeat.setup(app, {
dependencies: [browserid_url, static_url]
});
// #2 - logging! all requests other than __heartbeat__ are logged
app.use(express.logger({
format: config.get('express_log_format'),
stream: {
write: function(x) {
logger.info(typeof x === 'string' ? x.trim() : x);
}
}
}));
// limit all content bodies to 10kb, at which point we'll forcefully
// close down the connection.
app.use(express.limit("10kb"));
var statsd_config = config.get('statsd');
if (statsd_config && statsd_config.enabled) {
var logger_statsd = require("connect-logger-statsd");
app.use(logger_statsd({
host: statsd_config.hostname || "localhost",
port: statsd_config.port || 8125,
prefix: statsd_config.prefix || "browserid.router."
}));
}
// Add Strict-Transport-Security headers if we're serving over SSL
if (config.get('scheme') == 'https') {
app.use(function(req, res, next) {
// expires in 30 days, include subdomains like www
res.setHeader("Strict-Transport-Security", "max-age=2592000; includeSubdomains");
next();
});
}
// redirect requests to the "verifier" processes
if (config.get('verifier_url')) {
var verifier_url = urlparse(config.get('verifier_url')).validate().normalize();
// support requests coming into the verifier hostname as well as those with /verify in the path,
// iff the verifier is configured explicitly with a distinct hostname
var verifier_host;
if (config.get('public_verifier_url') !== config.get('public_url')) {
verifier_host = urlparse(config.get('public_verifier_url')).validate().host;
}
app.use(function(req, res, next) {
if (/^\/verify$/.test(req.url) ||
(req.headers.host && verifier_host && req.headers.host === verifier_host))
{
forward(
verifier_url, req, res,
function(err) {
if (err) {
logger.error("error forwarding request:", err);
}
});
} else {
return next();
}
});
}
// #10 if the BROWSERID_FAKE_VERIFICATION env var is defined, we'll include
// fake_verification.js. This is used during testing only and should
// never be included in a production deployment
if (process.env['BROWSERID_FAKE_VERIFICATION']) {
app.use(function(req, res, next) {
if (url.parse(req.url).pathname == '/wsapi/fake_verification') {
forward(
browserid_url+req.url, req, res,
function(err) {
if (err) {
logger.error("error forwarding request:", err);
}
});
} else {
return next();
}
});
}
// handle /wsapi reads/writes
var dbwriter_url = urlparse(config.get('dbwriter_url')).validate().normalize().originOnly();
wsapi.routeSetup(app, {
read_url: browserid_url,
write_url: dbwriter_url
});
//catch-all
app.use(function(req, res, next) {
// log metrics
if (req.url === '/sign_in') metrics.userEntry(req);
forward(
static_url+req.url, req, res,
function(err) {
if (err) {
logger.error("error forwarding request:", err);
}
});
});
var bindTo = config.get('bind_to');
app.listen(bindTo.port, bindTo.host, function(conn) {
logger.info("running on http://" + app.address().address + ":" + app.address().port);
});