-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
155 lines (132 loc) · 3.2 KB
/
server.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
var http = require('http');
var basicAuth = require('./basic-auth.js');
var g_cmds = {};
var cmdId = 0;
var cmdParams = {};
function SendResponse(res, status, txt) {
res.writeHead(status, {});
res.end(txt);
}
function sendDataToClient(response, result) {
console.info("send data to user.");
if (typeof(result) === "string") {
result = JSON.parse(result);
}
response.writeHead(result.code, result.headers);
if (result.isText) {
response.end(result.body);
} else {
response.end(new Buffer(result.body, "base64"));
}
}
function checkClientCmd(name, res) {
res.phTimeCount--;
if (res.phTimeCount <= 0) {
res.writeHead(206, {});
res.end();
return;
}
if (g_cmds[name] && g_cmds[name].length) {
var cmd = g_cmds[name].shift();
if ((Date.now() - cmd.time) > 100000) { //100s,放弃命令
console.info("skip old cmd");
checkClientCmd(name, res);
return;
}
console.info("send data to client");
res.end(JSON.stringify(cmd));
return;
} else {
setTimeout(function() {
checkClientCmd(name, res);
}, 100);
}
}
var server = http.createServer(function(request, response) {
var body = "";
console.log(request.method, request.headers.host, request.url);
//console.log(request.headers);
request.on('data', function(chunk) {
console.info("on data");
body += chunk;
});
request.on('end', function() {
if (request.url == "/favicon.ico") {
response.writeHead(404, {});
response.end();
}
//body = Buffer.concat(body);
// /__ph_cmd/name/cmd
if (request.url.indexOf("/__ph_cmd/") === 0) {
var cmds = request.url.split("/");
if (cmds.length < 4) {
response.writeHead(500, {});
response.end("error cmd. /__ph_cmd/name/cmd");
return;
}
if(cmds[3] === "noop") {
response.end("ok");
return;
}
if (cmds[3] === "result") {
var cmdId = cmds[4];
var cmd = cmdParams[cmdId];
sendDataToClient(cmd.res, body);
return;
}
if (cmds[3] === "get") {
response.phTimeCount = 600;
checkClientCmd(cmds[2], response);
return;
}
return;
}
var user = basicAuth(request);
console.log('auth', user);
if (!user || !user.name || !user.pass) {
response.writeHead(401, {
'WWW-Authenticate': 'Basic realm=Authorization Required'
});
response.end('WWW-Authenticate');
return;
}
var name = user.name;
var port = parseInt(user.pass);
if (isNaN(port)) {
response.writeHead(500, {});
response.end("host error: port must int " + user.pass);
return;
}
request.headers["url"] = request.url;
request.headers["method"] = request.method;
request.headers["body"] = body.toString();
request.headers["port"] = port;
sendToClient(name, request.headers, response);
});
});
var sendToClient = function(name, headers, res) {
//console.info(name,wsClients);
delete headers["host"];
delete headers["connection"];
delete headers["authorization"];
//request.headers["ReceptionTime"] = Date.now();
if (!g_cmds[name]) {
g_cmds[name] = [];
}
cmdId++;
if (cmdId > 10000) {
cmdId = 1;
}
g_cmds[name].push({
headers: headers,
cmdId: cmdId,
time: Date.now(),
cmd: "get"
});
cmdParams[cmdId] = {
cmdId: cmdId,
time: Date.now(),
res: res
};
};
server.listen(process.env.VCAP_APP_PORT || 8888);