-
Notifications
You must be signed in to change notification settings - Fork 3
/
cdn-detector.js
250 lines (218 loc) · 7.39 KB
/
cdn-detector.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// cdn-detector.js
//
// Detects Content Delivery Networks (CDNs) based on the HTTP hostname and
// HTTP response headers.
//
// https://github.com/nicjansma/cdn-detector.js
//
/* eslint-env commonjs, browser, amd */
(function(window) {
"use strict";
// save old CdnDetector object for noConflict()
var root;
var previousObj;
if (typeof window !== "undefined") {
root = window;
previousObj = root.CdnDetector;
} else {
root = {};
}
//
// Imports
//
// Pulls JSON data from the global scope (i.e. if running in a browser) or via
// require() if in Node
//
var headersData = root.CdnDetectorHeaders ?
root.CdnDetectorHeaders : require("../data/headers.json");
var hostnamesData = root.CdnDetectorHostnames ?
root.CdnDetectorHostnames : require("../data/hostnames.json");
var multiHeadersData = root.CdnDetectorMultiHeaders ?
root.CdnDetectorMultiHeaders : require("../data/multi-headers.json");
// model
var self, CdnDetector = self = {};
//
// Functions
//
/**
* Changes the value of CdnDetector back to its original value, returning
* a reference to the CdnDetector object.
*
* @returns {object} Original CdnDetector object
*/
CdnDetector.noConflict = function() {
root.CdnDetector = previousObj;
return CdnDetector;
};
/**
* @typedef CdnDetectorResult
* @type Object
* @property {string} cdn CDN name
* @property {object} [evidence] Evidence why the CDN was detected
* @property {object} [evidence.headers] Header evidence
* @property {string} [evidence.hostname] Hostname evidence
*/
//
// Functions
//
/**
* Detects CDN usage from the HTTP hostname or headers.
*
* @param {string} hostname Hostname
* @param {object} headers Map of HTTP Response headers
*
* @returns {CdnDetectorResult|null} Result, or null if no match
*/
CdnDetector.detect = function(hostname, headers) {
var result = {};
// check hostname first
var hostnameCheck = self.detectFromHostname(hostname);
if (hostnameCheck) {
result.cdn = hostnameCheck.cdn;
result.evidence = result.evidence || {};
result.evidence.hostname = hostnameCheck.evidence;
}
// check HTTP response headers next
var headersCheck = self.detectFromHeaders(headers);
if (headersCheck) {
result.cdn = headersCheck.cdn;
result.evidence = result.evidence || {};
result.evidence.headers = headersCheck.evidence;
}
return result.cdn ? result : null;
};
/**
* @typedef CdnDetectorHostnameResult
* @type Object
* @property {string} cdn CDN name
* @property {string} evidence Evidence why the CDN was detected
*/
/**
* Detects CDN usage from a hostname.
*
* Matches against regular expressions in hostnames.json.
*
* @param {string} hostname Request Host name
*
* @returns {CdnDetectorHostnameResult|null} Host's CDN info, or, null if a CDN wasn't detected
*/
CdnDetector.detectFromHostname = function(hostname) {
if (!hostname) {
// skip falsy (empty, null, etc) hostnames
return null;
}
for (var regexString in hostnamesData) {
if (hostnamesData.hasOwnProperty(regexString)) {
var regex = new RegExp(regexString);
if (regex.test(hostname)) {
return {
cdn: hostnamesData[regexString],
evidence: regexString
};
}
}
}
return null;
};
/**
* @typedef CdnDetectorHeadersResult
* @type Object
* @property {string} cdn CDN name
* @property {string[]} evidence Array of HTTP response headers that matched
*/
/**
* Detects CDN usage from HTTP response headers.
*
* Matches against headers in headers.json and multi-headers.json.
*
* @param {object} headers HTTP Response Headers map
*
* @returns {CdnDetectorHeadersResult|null} Header CDN info, or, null if a CDN wasn't detected
*/
CdnDetector.detectFromHeaders = function(headers) {
if (!headers || headers.length === 0) {
return null;
}
// build return evidence object in case we find something
var result = {
evidence: []
};
// convert all incoming headers to lower case first
var lowerHeaders = {};
for (var headerName in headers) {
if (headers.hasOwnProperty(headerName)) {
lowerHeaders[headerName.toLowerCase()] = headers[headerName];
}
}
var i, data, cdn, match, matches;
// find any matching headers in our data
for (i = 0; i < headersData.length; i++) {
data = headersData[i];
var header = data[0];
match = data[1];
cdn = data[2];
if (typeof lowerHeaders[header] === "string") {
// if there is no match string, we're good
// if there a match string, make sure the header starts with it
if ((!match || match.length === 0)
|| (match.length > 0 && lowerHeaders[header].toLowerCase().indexOf(match) === 0)) {
result.cdn = cdn;
result.evidence.push(header + ": " + (match ? match : "*"));
}
}
}
// find any multi headers (multiple headers need to match)
for (i = 0; i < multiHeadersData.length; i++) {
data = multiHeadersData[i];
cdn = data[0];
matches = data[1];
var matchesAll = true;
var matchEvidence = [];
// loop through, looking to see if this request matches
// all headers in the set
for (match in matches) {
if (matches.hasOwnProperty(match)) {
var matchValue = matches[match];
if (typeof lowerHeaders[match] !== "string") {
matchesAll = false;
break;
}
if (lowerHeaders[match].toLowerCase().indexOf(matchValue) !== 0) {
matchesAll = false;
break;
}
// add to our evidence pile
matchEvidence.push(match + ": " + (matchValue ? matchValue : "*"));
}
}
// we matched all headers
if (matchesAll) {
result.cdn = cdn;
result.evidence = result.evidence.concat(matchEvidence);
}
}
return result.cdn ? result : null;
};
//
// Export to the appropriate location
//
if (typeof define === "function" && define.amd) {
//
// AMD / RequireJS
//
define([], function() {
return CdnDetector;
});
} else if (typeof module !== "undefined" && module.exports) {
//
// Node.js
//
module.exports = CdnDetector;
} else if (typeof root !== "undefined") {
//
// Browser Global
//
root.CdnDetector = CdnDetector;
}
}(typeof window !== "undefined" ? window : undefined));