-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxey.js
210 lines (192 loc) · 4.87 KB
/
proxey.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
'use strict';
var http = require('http');
var fs = require('fs');
var url = require('url');
var mimetype = require('./mimetype.js');
function Proxey() {
this.proxyUrl = '/proxy';
this.rootFolder = null;
this.rootDocument = 'index.html';
this.port = 5000;
this.instance = null;
this.vars = {};
this.routes = {};
this.debug = true;
this.charset = 'utf-8'
this.proxy = null;
}
Proxey.prototype.getRoute = function (path) {
return this.routes[path];
};
Proxey.prototype.setConfig = function (config) {
if (config === undefined) {
return;
}
if (config.debug !== undefined) {
this.debug = config.debug;
}
if (config.port) {
this.port = config.port;
}
if (config.proxyUrl) {
this.proxyUrl = config.proxyUrl;
}
if (config.proxy) {
this.proxy = config.proxy;
}
if (config.rootFolder) {
this.rootFolder = config.rootFolder;
}
if (config.rootDocument) {
this.rootDocument = config.rootDocument;
}
if (config.vars) {
this.vars = config.vars;
}
if (config.routes) {
this.routes = config.routes;
if (!this.routes['/']) {
this.routes['/'] = this.rootDocument;
}
}
if (config.charset) {
this.charset = config.charset;
}
};
Proxey.prototype.request = function (req, res, config) {
var joinSplitedUrl = function (arr) {
for (var i = 1; i < arr.length; i++) {
proxyUrl += arr[i];
}
}
var splitedUrl;
var proxyUrl = '';
if (config) {
splitedUrl = req.url.split(config.key);
proxyUrl = config.value;
joinSplitedUrl(splitedUrl);
} else {
splitedUrl = req.url.split(this.proxyUrl + '?url=');
joinSplitedUrl(splitedUrl);
if (splitedUrl.length < 2) {
res.writeHead(400);
res.end('GET: ' + req.url + 'INVALID PROXY URL(400)');
return;
}
}
var parsedUrl = url.parse(decodeURIComponent(proxyUrl));
var dataString = '';
var options = {
hostname: parsedUrl.host,
port: (req.port ? req.port : 80),
path: parsedUrl.path,
method: req.method,
headers: req.headers
};
delete options.headers['host'];
for (var _var in this.vars) {
var key = _var;
var value = this.vars[_var];
if (typeof value == 'string') {
options.headers[key] = value;
} else if (typeof value == 'object') {
if (options.headers[key] !== undefined && options.headers[key].replace(/\_/gim, '') == key) {
var real = value.real || key;
options.headers[real] = value.value;
if (value.real) {
delete options.headers[key];
}
}
}
}
var proxy = http.request(options, function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
dataString += chunk;
});
response.on('end', function () {
res.writeHead(response.statusCode, response.headers);
res.end(dataString);
});
});
if (req.method == 'PUT' || req.method == 'POST') {
var requestData = '';
req.on('data', function(chunk) {
requestData += chunk;
});
req.on('end', function() {
proxy.write(requestData);
proxy.end();
});
}
proxy.end();
};
Proxey.prototype.run = function (config) {
var top = this;
this.setConfig(config);
this.instance = http.createServer(function (req, res) {
var isProxyUrl = function (url) {
if (!top.proxy) {
return false;
}
for (var item in top.proxy) {
if (req.url.indexOf(item) === 0) {
return true;
}
}
return false;
}
var proxyUrlPattern = '^' + top.proxyUrl;
var template = top.getRoute(req.url);
if (template) {
var documentFile = fs.readFileSync(top.rootFolder + '/' + template);
var documentFileMimetype = template.match(/\.[0-9a-z]+$/i);
var mime;
if (!documentFileMimetype) {
mime = 'text/html';
} else {
mime = mimetype.get(documentFileMimetype[0].replace('.', ''));
}
if (mime === undefined) {
mime = 'text/html';
}
mime += ';charset=' + top.charset;
res.writeHead(200, {'Content-Type': mime});
res.end(documentFile);
} else if (req.url.match(new RegExp(proxyUrlPattern, 'g'))) {
top.request(req, res);
} else if (isProxyUrl(req.url)) {
for (var item in top.proxy) {
if (req.url.indexOf(item) === 0) {
top.request(req, res, {key: item, value: top.proxy[item]});
}
}
} else {
try {
var resource = req.url;
if (resource.indexOf('?') != -1) {
resource = resource.split('?')[0];
}
if (resource.indexOf('#') != -1) {
resource = resource.split('#')[0];
}
var resourceFileMimetype = resource.match(/\.[0-9a-z]+$/i);
resourceFileMimetype = resourceFileMimetype[0].replace('.', '');
var mime = mimetype.get(resourceFileMimetype);
var resourceFile = fs.readFileSync(top.rootFolder + resource);
res.writeHead(200, {'Content-Type': mime});
res.end(resourceFile);
} catch (e) {
console.log(e);
res.writeHead(404);
res.end('GET: ' + req.url + 'NOT FOUND(404)');
}
}
}).listen(this.port);
console.log('Running in port: ' + this.port);
};
Proxey.prototype.stop = function () {
proxy.stop();
};
var proxey = new Proxey();
module.exports = proxey;