This repository has been archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
net.js
272 lines (257 loc) · 7.88 KB
/
net.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
var http = require('http');
var https = require('https');
var url = require("url");
var pass = require('stream').PassThrough;
var getHeader = function (req) {
var ret = {};
for (var i in req.headers) {
if (!/host|connection|Access-|origin|referer|user-agent|__user|__path|__url|__method|__headers/i.test(i)) {
ret[i] = req.headers[i];
}
}
var headers = req.headers["__headers"];
if (headers) {
headers = JSON.parse(headers);
for (var key in headers) {
ret[key] = headers[key];
}
}
return ret
};
var filterResHeader = function (headers) {
var ret = {};
for (var i in headers) {
if (!/Access-/i.test(i)) {
ret[i] = headers[i];
}
}
return ret;
};
var getPath = function (req) {
var url = req.url;
if (url.substr(0, 7).toLowerCase() === 'http://') {
var i = url.indexOf('/', 7);
if (i !== -1) {
url = url.substr(i);
}
}
return url;
};
function getHost(req) {
var url = req.headers["__url"];
url = url.replace(/^(http:\/\/|https:\/\/)/i, "");
var arr = url.split(":");
return arr[0];
}
function getPort(req) {
var url = req.headers["__url"];
var defaultPort;
if (req.headers["__url"].toLowerCase().startsWith("https://")) {
defaultPort = 443;
} else {
defaultPort = 80;
}
url = url.replace(/^(http:\/\/|https:\/\/)/i, "");
var arr = url.split(":");
return arr.length > 1 ? arr[1] : defaultPort;
}
var onProxy = function (req, res) {
handleHeader(req, res);
if (req.method == "OPTIONS") {
return;
}
if (!req.headers["__url"]) {
mock(req, res);
} else {
proxy(req, res);
}
};
function mock(req, res) {
var obj = url.parse(mockUrl);
var path = req.headers.path || url.parse(req.url).path;
var opt = {
host: obj.hostname,
path: (obj.pathname == "/" ? "" : obj.pathname) + path,
method: req.method,
headers: getHeader(req),
port: obj.port ? obj.port : 80,
}
if (opt.headers["content-length"]) {
delete opt.headers["content-length"]
}
var req2 = http.request(opt, function (res2) {
if (!realUrl || res2.headers["__finish"] == "0") {
res.writeHead(res2.statusCode, filterResHeader(res2.headers));
res2.pipe(res);
res2.on('end', function () {
});
} else {
var headers = getHeader(req);
var objUrl = url.parse(realUrl);
var request1, opt1;
if (objUrl.protocol == "http:") {
opt1 = {
host: objUrl.hostname,
path: (objUrl.pathname == "/" ? "" : objUrl.pathname) + path,
method: req.method,
port: objUrl.port ? objUrl.port : 80,
headers: headers
}
request1 = http.request;
} else {
opt1 = {
host: objUrl.hostname,
path: (objUrl.pathname == "/" ? "" : objUrl.pathname) + path,
method: req.method,
port: objUrl.port ? objUrl.port : 443,
headers: headers,
rejectUnauthorized: false,
requestCert: true,
}
request1 = https.request;
}
var req3 = request1(opt1, function (res3) {
res.writeHead(res3.statusCode, filterResHeader(res3.headers));
res3.pipe(res);
res3.on('end', function () {
});
})
if (/POST|PUT/i.test(req.method)) {
stream.pipe(req3);
} else {
req3.end();
}
req3.on('error', function (err) {
res.end(err.stack);
});
}
});
var stream;
if (/POST|PUT/i.test(req.method)) {
stream = new pass();
req.pipe(stream);
req.pipe(req2);
} else {
req2.end();
}
req2.on('error', function (err) {
res.end(err.stack);
});
}
function handleHeader(req, res) {
if (!req.headers.origin) {
return;
}
res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE");
res.setHeader("Access-Control-Allow-Credentials", "true");
if (req.headers["access-control-request-headers"]) {
res.setHeader("Access-Control-Allow-Headers", req.headers["access-control-request-headers"])
}
res.setHeader("Access-Control-Expose-Headers", "connection,content-length,date,x-powered-by,content-encoding,server,etag,accept-ranges,allow,content-language,set-cookie");
if (req.method == "OPTIONS") {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end();
return;
}
}
function redirect(res, bHttps, opt, location) {
var urlRedirect = location;
if (urlRedirect.startsWith("/")) {
urlRedirect = (bHttps ? "https://" : "http://") + opt.host + urlRedirect;
}
var objUrl = url.parse(urlRedirect);
var request1, opt1;
if (objUrl.protocol == "http:") {
opt1 = {
host: objUrl.hostname,
path: objUrl.path,
method: "GET",
port: objUrl.port ? objUrl.port : 80,
headers: opt.headers
}
request1 = http.request;
bHttps = false;
} else {
opt1 = {
host: objUrl.hostname,
path: objUrl.path,
method: "GET",
port: objUrl.port ? objUrl.port : 443,
headers: opt.headers,
rejectUnauthorized: false,
requestCert: true,
}
request1 = https.request;
bHttps = true;
}
var req3 = request1(opt1, function (res3) {
if (res3.statusCode == 302) {
redirect(res, bHttps, opt, res3.headers.location)
} else {
res.writeHead(res3.statusCode, filterResHeader(res3.headers));
res3.pipe(res);
res3.on('end', function () {
});
}
})
req3.end();
req3.on('error', function (err) {
res.end(err.stack);
});
}
function proxy(req, res) {
var bHttps = false;
if (req.headers["__url"].toLowerCase().startsWith("https://")) {
bHttps = true;
}
var opt, request;
if (bHttps) {
opt = {
host: getHost(req),
path: req.headers["__path"],
method: req.headers["__method"],
headers: getHeader(req),
port: getPort(req),
rejectUnauthorized: false,
requestCert: true,
};
request = https.request;
} else {
opt = {
host: getHost(req),
path: req.headers["__path"],
method: req.headers["__method"],
headers: getHeader(req),
port: getPort(req)
};
request = http.request;
}
var req2 = request(opt, function (res2) {
if (res2.statusCode == 302) {
redirect(res, bHttps, opt, res2.headers.location)
} else {
res.writeHead(res2.statusCode, filterResHeader(res2.headers));
res2.pipe(res);
res2.on('end', function () {
});
}
});
if (/POST|PUT/i.test(req.method)) {
req.pipe(req2);
} else {
req2.end();
}
req2.on('error', function (err) {
res.end(err.stack);
});
}
var arguments = process.argv.splice(2);
var mockUrl = arguments[0];
var realUrl = arguments[1];
var server = http.createServer(onProxy);
server.listen(36742);
console.log(arguments.length > 0 ? "内网测试,Mock数据正待命运行中!" : "正在运行中,请用SBDoc的接口测试页面进行内网测试!");