-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (129 loc) · 3.57 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
var http = require('http');
var https = require('https');
var os = require('os');
var process = require('process');
var Netmask = require('netmask').Netmask;
const VERSION = "0.0.3";
const CLIENT_NAME = "wrenchmode-express";
const HEROKU_JWT_VAR = "WRENCHMODE_PROJECT_JWT";
function wrenchmodeExpress(options) {
var opts = {
jwt: process.env[HEROKU_JWT_VAR],
forceOpen: false,
ignoreTestMode: true,
disableLocalWrench: false,
statusProtocol: "https",
statusHost: "wrenchmode.com",
statusPath: "/api/projects/status",
checkDelaySecs: 5,
logging: false,
readTimeoutSecs: 3
};
Object.assign(opts, options);
if(!opts.jwt) {
throw new Error("You must set the jwt for the wrenchmodeExpress middleware. Please see the README for more info.");
}
// Set up the periodic status checking
var currentStatus = {
switched: false,
switchUrl: null,
ipWhitelist: []
};
statusCheckLoop(currentStatus, opts);
return function(req, res, next) {
var shouldShowSwitch = currentStatus.switched && !opts.forceOpen && !isIpWhitelisted(req, currentStatus);
if( shouldShowSwitch ) {
res.redirect(302, currentStatus.switchUrl);
} else {
next();
}
};
}
function statusCheckLoop(currentStatus, opts) {
retrieveStatus(opts)
.then(function(response) {
updateStatus(currentStatus, response);
})
.catch(function(error) {
currentStatus.switched = false;
})
.then(function() {
setTimeout(statusCheckLoop.bind(this, currentStatus, opts), opts.checkDelaySecs * 1000);
})
}
function retrieveStatus(opts) {
var options = {
hostname: opts.statusHost,
path: opts.statusPath,
method: 'POST',
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": opts.jwt,
"User-Agent": CLIENT_NAME + "-" + VERSION
}
};
var protocol = opts.statusProtocol === 'http' ? http : https;
return new Promise(function(resolve, reject) {
var req = protocol.request(options, (res) => {
if (res.statusCode !== 200) {
return reject(new Error("HTTP Status: " + res.statusCode));
}
var responseBody = '';
res.on('data', (d) => {
responseBody += d;
});
res.on('end', () => {
var json = null;
try {
json = JSON.parse(responseBody);
} catch (e) {
reject(e);
}
return resolve(json);
});
});
req.setTimeout(opts.readTimeoutSecs * 1000);
req.write(JSON.stringify(buildUpdatePackage()));
req.end();
req.on('error', (e) => {
return reject(e);
});
req.on('timeout', (e) => {
return reject(e);
})
});
}
function updateStatus(currentStatus, serverResponse) {
if(serverResponse.is_switched) {
currentStatus.switched = true;
currentStatus.switchUrl = serverResponse.switch_url;
currentStatus.ipWhitelist = serverResponse.ip_whitelist || [];
} else {
currentStatus.switched = false;
}
}
function buildUpdatePackage() {
return {
hostname: os.hostname(),
ip_address: "127.0.0.1",
pid: process.pid,
client_name: CLIENT_NAME,
client_version: VERSION
}
}
function isIpWhitelisted(req, currentStatus) {
var whitelisted = false;
try {
currentStatus.ipWhitelist.forEach(function(whitelisted_ip) {
var block = new Netmask(whitelisted_ip);
if(block.contains(req.ip)) {
whitelisted = true
}
});
} catch (e) {
// Do nothing? As long as we return true/false, everything should be okay...
}
return whitelisted;
}
module.exports = wrenchmodeExpress;