This repository has been archived by the owner on Jan 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
proxy.js
189 lines (152 loc) · 6.95 KB
/
proxy.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
/*───────────────────────────────────────────────────────────────────────────*\
│ Copyright (C) 2014 eBay Software Foundation │
│ │
│hh ,'""`. │
│ / _ _ \ Licensed under the Apache License, Version 2.0 (the "License"); │
│ |(@)(@)| you may not use this file except in compliance with the License. │
│ ) __ ( You may obtain a copy of the License at │
│ /,'))((`.\ │
│(( (( )) )) http://www.apache.org/licenses/LICENSE-2.0 │
│ `\ `)(' /' │
│ │
│ Unless required by applicable law or agreed to in writing, software │
│ distributed under the License is distributed on an "AS IS" BASIS, │
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
│ See the License for the specific language governing permissions and │
│ limitations under the License. │
\*───────────────────────────────────────────────────────────────────────────*/
'use strict';
var Url = require('url');
var Http = require('http');
var Hapi = require('hapi');
var Hoek = require('hoek');
var Wreck = require('wreck');
var Https = require('https');
var Util = require('../util');
require('tls').SLAB_BUFFER_SIZE = 200 * 1024;
Http.globalAgent.maxSockets = Https.globalAgent.maxSockets = Infinity;
Https.globalAgent.ciphers = 'ALL:!aNULL:!eNULL:!DH:!ECDH:!MD5';
var proto = {
proxy: function (req, reply) {
var self, registry, removeAuth, start;
self = this;
registry = this.registry;
removeAuth = !this.isFirst;
start = Date.now();
reply.proxy({
mapUri: function (request, callback) {
var resource, options;
resource = Url.resolve(registry, request.url.path.substring(1));
options = Url.parse(resource);
options.method = request.method.toLowerCase();
options.rejectUnauthorized = false;
// Copy headers, deleting host. The correct host header (different from
// this host) is automatically added by node http internals.
options.headers = Hoek.clone(request.raw.req.headers);
delete options.headers.host;
// HACK: Only ever send auth to first server.
// This is a unique situation where, for writes we ONLY go to a single
// repo so this is a noop, but for GETs we fallback to others. If we
// fallback, we don't want our Basic auth headers sent to downstream
// services since 1) they won't be of any use, and 2) they expose us.
if (removeAuth) {
delete options.headers.authorization;
}
callback(null, options.format(), options.headers);
},
onResponse: function (error, res, request, reply) {
var good, end;
if (good = request.server.plugins.good) {
// The good plugin is available, so report custom `proxy` events.
// NOTE: using the `proxy` event type with reporters that cannot
// handle arbitrary event types will fail with errors.
end = Date.now();
good.monitor.emit('report', 'proxy', {
timestamp: end,
event: 'proxy',
duration: end - start,
method: request.method,
registry: Url.parse(registry).host,
path: request.path,
statusCode: res ? res.statusCode : -1
});
}
self._onResponse.apply(self, arguments);
},
timeout: 20000
});
},
_onResponse: function (error, res, request, reply) {
var resume, self, response;
if (error) {
// Ensure any necessary socket cleanup is done.
res && res.socket && res.socket.destroy();
this._onError(error);
return;
}
if (!this.isLast && res.statusCode === 404) {
// This particular response has been deemed unnecessary
// so discard it and move on.
res.socket && res.socket.destroy();
this.onComplete();
return;
}
self = this;
response = request.raw.res;
response.once('error', this._onError.bind(this));
response.once('finish', this.onComplete);
Wreck.read(res, { json: true }, function (err, payload) {
var response;
if (err) {
// Actual parsing error, so fail immediately.
// The server returned an invalid response
// so the client should be notified.
err = Hapi.error.wrap(err);
err.output.headers['x-registry'] = self.registry;
reply(err);
return;
}
response = reply(payload).code(res.statusCode);
response.header('x-registry', self.registry);
Object.keys(res.headers)
.filter(Util.isValidHeader)
.forEach(function (key) {
response.header(key, res.headers[key]);
});
});
},
_onError: function (err) {
// TODO: Log error even though we ignore it?
// If primary registry is down, always fail. This is for the scenario
// in which there may be naming collisions and falling back might return
// a node module different than the desired/intended one.
this.onComplete(this.isFirst ? err : null);
},
get isFirst() {
return this.index === 0;
},
get isLast() {
return this.index === this.registries.length - 1;
}
};
exports.create = function (item, index, items) {
return Object.create(proto, {
registry: {
value: item,
enumerable: true
},
index: {
value: index,
enumerable: true
},
registries: {
value: items,
enumerable: true
},
onComplete: {
value: undefined,
writable: true,
enumerable: true
}
});
};