aboutsummaryrefslogtreecommitdiffstats
path: root/cros.js
blob: e37dd38e8f80e9927d7973021462f23de2d80c65 (plain) (blame)
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
// Generated by CoffeeScript 1.3.3
var http, httpProxy, proxyServer;
http = require('http');

httpProxy = require('http-proxy');

proxyServer = function(req, res, proxy) {
  var proxyHost = req.headers.host;

  var cors_headers, header, headers, host, ignore, key, path, port, value, _i, _len, _ref, _ref1, _ref2;
  headers = 'Accept, Accept-Charset, Origin, Accept-Encoding, Accept-Language, Authorization, Content-Length, Content-Type, Host, Proxy-Connection, User-Agent, X-Requested-With';
  if (req.headers['access-control-request-headers']) {
    headers = req.headers['access-control-request-headers'];
  } else {
    _ref = req.headers;
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      header = _ref[_i];
      if (req.indexOf('x-') === 0) {
        headers += ", " + header;
      }
    }
    req.headers.authorization = require('fs').readFileSync(__dirname + '/auth_hack.txt', 'utf8').trim();
  }


  var methods = [
    'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE',
    'REPORT', 'PROPFIND', 'PROPPATCH', 'OPTIONS'
  ];

  console.log('->>>', headers, '<<<---');
  cors_headers = {
    'Access-Control-Allow-Methods': methods.join(', '),
    'Access-Control-Allow-Headers': headers,
    'Access-Control-Allow-Credentials': 'true',
    'Access-Control-Allow-Origin': req.headers.origin
  };
  //cors_headers = {
    //'access-control-allow-methods': methods.join(', '),
    //'access-control-allow-headers': headers,
    //'access-control-allow-credentials': 'true',
    //'access-control-allow-origin': req.headers.origin
  //};

  if (req.method === 'OPTIONS') {
    console.log('responding to OPTIONS request');
    res.writeHead(200, cors_headers);
    console.log(cors_headers);
    return res.end();
  } else {
    for (key in cors_headers) {
      value = cors_headers[key];
      res.setHeader(key, value);
    }

    proxyHost = proxyHost.replace(/(:[0-9]+)/, '');
    proxyHost = proxyHost.split('.');
    proxyHost.splice(proxyHost.length - 2, 1);
    proxyHost = proxyHost.join('.');

    req.headers.host = proxyHost;

    console.log(req.method, req.headers);

    return proxy.proxyRequest(req, res, {
      forward: false,
      host: proxyHost,
      port: 443,
      https: true
    });
  }
};

httpProxy.createServer(proxyServer).listen(process.env.PORT || 9292);