-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
91 lines (75 loc) · 2.38 KB
/
index.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
var
Resource = require('deployd/lib/resource'),
querystring = require('querystring'),
util = require('util'),
url = require('url');
function Proxy(name, options) {
Resource.apply(this, arguments);
}
util.inherits(Proxy, Resource);
module.exports = Proxy;
Proxy.prototype.clientGeneration = true;
Proxy.prototype.handle = function (ctx, next) {
if(ctx.req && ctx.req.method !== 'GET') return next();
var urlObj = url.parse(this.config.url + ctx.url);
urlObj.query = querystring.parse(url.parse(ctx.req.url).query);
var paramsObj = querystring.parse(this.config.params);
for (var property in paramsObj)
urlObj.query[property] = paramsObj[property];
requestOptions = url.parse(url.format(urlObj));
requestOptions.method = ctx.req.method;
if (ctx.req.headers && ctx.req.headers.accept) {
requestOptions.headers = requestOptions.headers || {};
requestOptions.headers.Accept = ctx.req.headers.accept;
}
if (this.config.username) {
requestOptions.headers = requestOptions.headers || {};
requestOptions.headers.Authorization = 'Basic ' + new Buffer(this.config.username + ':' + this.config.password).toString('base64');
}
var client;
if (requestOptions.protocol === 'https:')
client = require('https');
else
client = require('http');
var proxy_request = client.request(requestOptions, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
ctx.res.write(chunk);
});
res.on('end', function (chunk) {
ctx.res.end();
});
});
proxy_request.on('error', function (err) {
console.error(err);
// Bad request
ctx.res.statusCode = 400;
ctx.res.end();
});
ctx.req.addListener('data', function(chunk) {
proxy_request.write(chunk, 'binary');
});
ctx.req.addListener('end', function() {
proxy_request.end();
});
};
Proxy.basicDashboard = {
settings: [
{
name : 'url',
type : 'text',
description : 'the url'
}, {
name : 'username',
type : 'text',
description : 'HTTP username'
}, {
name : 'password',
type : 'text',
description : 'HTTP password'
}, {
name : 'params',
type : 'text',
description : 'Extra query string params'
}]
};