radames commited on
Commit
7135e39
·
1 Parent(s): 5609d6b

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +62 -0
server.js ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ var fs = require('fs')
2
+
3
+ // Listen on a specific host via the HOST environment variable
4
+ var host = process.env.HOST || '0.0.0.0';
5
+ // Listen on a specific port via the PORT environment variable
6
+ var port = process.env.PORT || 8080;
7
+
8
+ // Grab the blacklist from the command-line so that we can update the blacklist without deploying
9
+ // again. CORS Anywhere is open by design, and this blacklist is not used, except for countering
10
+ // immediate abuse (e.g. denial of service). If you want to block all origins except for some,
11
+ // use originWhitelist instead.
12
+ var originBlacklist = parseEnvList(process.env.CORSANYWHERE_BLACKLIST);
13
+ var originWhitelist = parseEnvList(process.env.CORSANYWHERE_WHITELIST);
14
+ function parseEnvList(env) {
15
+ if (!env) {
16
+ return [];
17
+ }
18
+ return env.split(',');
19
+ }
20
+
21
+ // Set up rate-limiting to avoid abuse of the public CORS Anywhere server.
22
+ var checkRateLimit = require('cors-anywhere/lib/rate-limit')(process.env.CORSANYWHERE_RATELIMIT);
23
+
24
+ if (process.env.KEY || process.env.CERT) {
25
+ var httpsOptions = {
26
+ key: readTLSContent(process.env.KEY),
27
+ cert: readTLSContent(process.env.CERT),
28
+ };
29
+ }
30
+
31
+ function readTLSContent(tls) {
32
+ if (tls.startsWith('-----')) {
33
+ return tls
34
+ } else {
35
+ return fs.readFileSync(tls);
36
+ };
37
+ }
38
+
39
+ var cors_proxy = require('cors-anywhere');
40
+ cors_proxy.createServer({
41
+ originBlacklist: originBlacklist,
42
+ originWhitelist: originWhitelist,
43
+ requireHeader: ['origin', 'x-requested-with'],
44
+ checkRateLimit: checkRateLimit,
45
+ removeHeaders: [
46
+ 'cookie',
47
+ 'cookie2',
48
+ // Strip Heroku-specific headers
49
+ 'x-heroku-queue-wait-time',
50
+ 'x-heroku-queue-depth',
51
+ 'x-heroku-dynos-in-use',
52
+ 'x-request-start',
53
+ ],
54
+ redirectSameOrigin: true,
55
+ httpProxyOptions: {
56
+ // Do not add X-Forwarded-For, etc. headers, because Heroku already adds it.
57
+ xfwd: false,
58
+ },
59
+ httpsOptions: httpsOptions,
60
+ }).listen(port, host, function() {
61
+ console.log('Running CORS Anywhere on ' + host + ':' + port);
62
+ });