-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathproxy.js
273 lines (209 loc) · 7.21 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'use strict';
const http = require('http');
const net = require('net');
const https = require('https');
const fs = require('fs');
const os = require('os');
const url = require('url');
const path = require('path');
const request = require('request');
const Cache = require('./cache');
const getLogger = require('./logger');
// To avoid 'DEPTH_ZERO_SELF_SIGNED_CERT' error on some setups
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
exports.log = null;
exports.cache = null;
exports.opts = {};
// Port or socket path of internal MITM server.
let mitmAddress;
// Random header to prevent sending requests in a cycle
const cycleCheckHeader = 'x-npm-proxy-cache-' + Math.round(Math.random() * 10000);
exports.powerup = function(opts) {
exports.opts = opts || {};
const options = {
key: fs.readFileSync(path.join(__dirname, '/../cert/dummy.key'), 'utf8'),
cert: fs.readFileSync(path.join(__dirname, '/../cert/dummy.crt'), 'utf8')
};
this.cache = new Cache({
path: opts.storage, ttl: opts.ttl, friendlyNames: opts.friendlyNames
});
this.log = getLogger(opts);
// Fake https server aka MITM
const mitm = https.createServer(options, exports.httpHandler);
// NOTE: for windows platform user has to specify port, since
// it does not support unix sockets.
if (/^win/i.test(process.platform) && !isNumeric(opts.internalPort)) {
console.error('Error: On Windows platform you have to specify internal port,\n'
+ 'for example `--internal-port 8081`.');
process.exit(1);
}
if (opts.internalPort) {
mitmAddress = opts.internalPort;
} else {
mitmAddress = path.join(os.tmpdir(), 'mitm.sock');
// Cleanup MITM socket for unix platforms
if (fs.existsSync(mitmAddress))
fs.unlinkSync(mitmAddress);
}
mitm.listen(mitmAddress);
// start HTTP server with custom request handler callback function
const server = http.createServer(exports.httpHandler).listen(opts.port, opts.host, function(err) {
if (err) throw err;
exports.log.info('Listening on %s:%s [%d]', opts.host, opts.port, process.pid);
});
// add handler for HTTPS (which issues a CONNECT to the proxy)
server.addListener('connect', this.httpsHandler);
return { httpsServer: mitm, httpServer: server };
};
exports.httpHandler = function(req, res) {
const cache = exports.cache,
log = exports.log,
path = url.parse(req.url).path,
schema = req.client.pair || req.connection.encrypted ? 'https' : 'http',
dest = schema + '://' + req.headers['host'] + path;
if (req.headers[cycleCheckHeader]) {
res.writeHead(502);
res.end('Sending requests to myself. Stopping to prevent cycles.');
return;
}
const params = {
headers: {},
rejectUnauthorized: false,
url: dest
};
params.headers[cycleCheckHeader] = 1;
// Carry following headers down to dest npm repository.
const carryHeaders = ['authorization', 'version', 'referer', 'npm-session', 'user-agent'];
carryHeaders.forEach(function(name) {
params.headers[name] = req.headers[name];
});
if (exports.opts.proxy)
params.proxy = exports.opts.proxy;
// Skipping other than GET methods
// Skipping metadata requests when configured
if (
req.method !== 'GET' ||
(exports.opts.metadataExcluded && req.headers['accept'] === 'application/json')
)
return bypass(req, res, params);
cache.meta(dest, function(err, meta) {
if (err)
throw err;
if (meta.status === Cache.FRESH)
return respondWithCache(dest, cache, meta, res);
const p = cache.getPath(dest);
log.debug('Cache file:', p.rel);
log.warn('direct', dest);
const onResponse = function(err, response) {
// don't save responses with codes other than 200
if (!err && response.statusCode === 200) {
cache.write(dest, r, function(err, meta) {
if (err)
throw err;
respondWithCache(dest, cache, meta, res);
});
} else {
// serve expired cache if user wants so
if (exports.opts.expired && meta.status === Cache.EXPIRED)
return respondWithCache(dest, cache, meta, res);
log.error('An error occcured: "%s", status code "%s"',
err ? err.message : 'Unknown',
response ? response.statusCode : 0
);
// clean old cache
if (meta.status !== Cache.NOT_FOUND)
cache.unlink(dest);
res.end(err ? err.toString() : 'Status ' + response.statusCode + ' returned');
}
};
const r = request(params);
r.on('response', onResponse.bind(null, null));
r.on('error', onResponse.bind(null));
r.on('end', function() {
log.debug('end');
});
});
};
exports.httpsHandler = function(request, socketRequest, bodyhead) {
const log = exports.log,
httpVersion = request['httpVersion'];
log.debug(' = will connect to socket (or port) "%s"', mitmAddress);
// set up TCP connection
const proxySocket = new net.Socket();
proxySocket.connect(mitmAddress, function() {
log.debug('< connected to socket (or port) "%s"', mitmAddress);
log.debug('> writing head of length %d', bodyhead.length);
proxySocket.write(bodyhead);
// tell the caller the connection was successfully established
socketRequest.write('HTTP/' + httpVersion + ' 200 Connection established\r\n\r\n');
});
proxySocket.on('data', function(chunk) {
log.debug('< data length = %d', chunk.length);
socketRequest.write(chunk);
});
proxySocket.on('end', function() {
log.debug('< end');
socketRequest.end();
});
socketRequest.on('data', function(chunk) {
log.debug('> data length = %d', chunk.length);
proxySocket.write(chunk);
});
socketRequest.on('end', function() {
log.debug('> end');
proxySocket.end();
});
proxySocket.on('error', function(err) {
socketRequest.write('HTTP/' + httpVersion + ' 500 Connection error\r\n\r\n');
log.error('< ERR: %s', err.toString());
socketRequest.end();
});
socketRequest.on('error', function(err) {
log.error('> ERR: %s', err.toString());
proxySocket.end();
});
};
function bypass(req, res, params) {
const length = parseInt(req.headers['content-length']);
const log = exports.log;
const onEnd = function(params, res) {
return request(params)
.on('error', function(err) {
log.error('bypass', err);
})
.pipe(res, { end: false });
};
if (!isFinite(length)) {
onEnd(params, res);
return;
}
const raw = new Buffer(length);
let pointer = 0;
req.on('data', function(chunk) {
chunk.copy(raw, pointer);
pointer += chunk.length;
});
req.on('end', function() {
params.method = req.method;
if (raw.length > 0) {
params.body = raw;
}
params.headers = {
'Content-Type': req.headers['content-type']
};
onEnd(params, res);
});
}
function respondWithCache(dest, cache, meta, res) {
const log = exports.log;
log.info('cache', dest);
log.debug('size: %s, type: "%s", ctime: %d', meta.size, meta.type, meta.ctime.valueOf());
res.setHeader('Content-Length', meta.size);
res.setHeader('Content-Type', meta.type);
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Cache-Hit', 'true');
return cache.read(dest).pipe(res);
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}