-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdaili_server.js
More file actions
74 lines (71 loc) · 3.26 KB
/
Copy pathdaili_server.js
File metadata and controls
74 lines (71 loc) · 3.26 KB
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
var http = require('http');
var https = require('https')
var url = require('url');
var querystring = require('querystring')
http.createServer(function(request, response) {
var ip = request.headers['x-forwarded-for'] || request.ip || request.connection.remoteAddress || request.socket.remoteAddress || request.connection.socket.remoteAddress || '';
if (ip.split(',').length > 0) {
ip = ip.split(',')[0];
}
response.setHeader("Access-Control-Allow-Origin", "*");
var timeStampStart = Date.now();
var option = {};
option.hostname = 'www.xxx.com';
option.protocol = 'https:'
option.port = 443;
option.method = request.method;
option.path = request.url.split('?')[0];
var reqHeader = request.headers;
reqHeader.host = 'www.xxx.com';
reqHeader.origin = 'https://www.xxx.com';
reqHeader.referer = 'https://www.xxx.com';
option.headers = reqHeader;
if (request.method == 'GET') {
let data = querystring.parse(request.url.split('?')[1]);
var client = https.request(option, (client_res) => {
response.writeHead(client_res.statusCode, client_res.headers);
client_res.on('data', function(d) {
response.write(d);
});
client_res.on('end', function(d) {
var timeStampEnd = Date.now();
console.log("\033[42;30m success \033[40;32m " + option.path + " " + (timeStampEnd - timeStampStart) + "ms " + ip + " \033[0m");
response.end(d);
});
}).on('error', function(e) {
var timeStampEnd = Date.now();
console.log("\033[41;31m error \033[40;31m " + option.path + " " + (timeStampEnd - timeStampStart) + "ms " + ip + " \033[0m");
console.error(e);
})
if (querystring.stringify(data).length > 0) {
client.write(querystring.stringify(data));
}
} else {
var postData = '';
request.on('data', function(chunk) {
postData += chunk;
});
request.on('end', function() {
let data = postData.length > 0 ? querystring.parse(postData) : { 'a': 1 }; //?为啥不穿数据就不往下走了?(现在解决办法是不穿数据(数据为空的)用{a:1}来弥补参数)
var client = https.request(option, (client_res) => {
response.writeHead(client_res.statusCode, client_res.headers);
client_res.on('data', function(d) {
response.write(d);
});
client_res.on('end', function(d) {
var timeStampEnd = Date.now();
console.log("\033[42;30m success \033[40;32m " + option.path + " " + (timeStampEnd - timeStampStart) + "ms " + ip + " \033[0m");
response.end(d);
});
}).on('error', function(e) {
var timeStampEnd = Date.now();
console.log("\033[41;31m error \033[40;31m " + option.path + " " + (timeStampEnd - timeStampStart) + "ms " + ip + " \033[0m");
console.error(e);
})
if (querystring.stringify(data).length > 0) {
client.write(querystring.stringify(data));
}
});
}
}).listen(9999);
console.log('代理地址', 9999);