-
Notifications
You must be signed in to change notification settings - Fork 0
/
uiserver.js
269 lines (237 loc) · 7.97 KB
/
uiserver.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
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
var net = require('net');
const port = process.argv[2] || 9000;
//port for print client to connect to
const printPort = process.argv[3] || 9001
//tcp server ---
var server = net.createServer();
//emitted when server closes ...not emitted until all connections closes.
server.on('close',function(){
console.log('Server closed !');
});
var imgBuffer;
var completeImage = null;
// emitted when new client connects
server.on('connection',function(socket){
//this property shows the number of characters currently buffered to be written. (Number of characters is approximately equal to the number of bytes to be written, but the buffer may contain strings, and the strings are lazily encoded, so the exact number of bytes is not known.)
console.log('Buffer size : ' + socket.bufferSize);
console.log('---------server details -----------------');
var address = server.address();
var port = address.port;
var family = address.family;
var ipaddr = address.address;
console.log('Server is listening at port ' + port);
console.log('Server ip : ' + ipaddr);
console.log('Server is IP4/IP6 : ' + family);
var lport = socket.localPort;
var laddr = socket.localAddress;
console.log('Server is listening at LOCAL port ' + lport);
console.log('Server LOCAL ip :' + laddr);
console.log('------------remote client info --------------');
var rport = socket.remotePort;
var raddr = socket.remoteAddress;
var rfamily = socket.remoteFamily;
console.log('REMOTE Socket is listening at port ' + rport);
console.log('REMOTE Socket ip : ' + raddr);
console.log('REMOTE Socket is IP4/IP6 : ' + rfamily);
console.log('--------------------------------------------')
server.getConnections(function(error,count){
console.log('Number of concurrent connections to the server : ' + count);
});
socket.setTimeout(800000,function(){
// called after timeout -> same as socket.on('timeout')
// it just tells that soket timed out => its ur job to end or destroy the socket.
// socket.end() vs socket.destroy() => end allows us to send final data and allows some i/o activity to finish before destroying the socket
// whereas destroy kills the socket immediately irrespective of whether any i/o operation is goin on or not...force destry takes place
console.log('Socket timed out');
});
//if no header, expect the first thing received to be a header.
var hasHeader = false;
var bufferIndex = 0;
//total number of bytes for image data
var imgSize = 0;
//any left over bytes from previous image.
var recvBuf="";
socket.on('data',function(data){
if(!hasHeader){
recvBuf += data.toString('latin1');
console.log(recvBuf.length + " " + data.length);
var startIdx = recvBuf.indexOf("image_buffer_size");
//if no proper header found discard other data.
if(startIdx<0){
recvBuf = "";
return;
}
var endIdx = recvBuf.indexOf("\r\n");
if(endIdx<0 || endIdx>startIdx + 40){
recvBuf = "";
console.log("invalid header. expect \\r\\n.");
return;
}
header = recvBuf.slice(startIdx, endIdx);
tokens = header.split(" ");
imgSize = parseInt(tokens[1]);
console.log("expect image size " + imgSize);
hasHeader = true;
data = Buffer.from(recvBuf.slice(endIdx + 2), 'latin1');
bufferIndex = 0;
imgBuffer = Buffer.allocUnsafe(imgSize);
recvBuf = "";
}
if(hasHeader){
endIdx = data.length;
//we are reading into the next image
//or just got extra incorrect data.
var endOfImg = false;
if(bufferIndex + endIdx>=imgSize){
endIdx = imgSize - bufferIndex;
endOfImg = true;
hasHeader = false;
}
data.copy(imgBuffer, bufferIndex, 0, endIdx);
bufferIndex += endIdx;
if(endOfImg){
completeImage = imgBuffer;
recvBuf = data.slice(endIdx).toString("latin1");
console.log("img complete " + bufferIndex);
console.log("extra bytes " + (bufferIndex - imgSize));
bufferIndex = 0;
}
}
});
socket.on('error',function(error){
console.log('TCP server Error : ' + error);
});
socket.on('timeout',function(){
console.log('Socket timed out !');
socket.end('Timed out!');
// can call socket.destroy() here too.
});
socket.on('end',function(data){
var bread = socket.bytesRead;
console.log('socket end Bytes read : ' + bread);
});
socket.on('close',function(error){
var bread = socket.bytesRead;
var bwrite = socket.bytesWritten;
console.log('Bytes read : ' + bread);
console.log('Bytes written : ' + bwrite);
console.log('Socket closed!');
if(error){
console.log('Socket was closed coz of transmission error');
}
});
setTimeout(function(){
var isdestroyed = socket.destroyed;
console.log('Socket destroyed:' + isdestroyed);
socket.destroy();
},1200000);
});
// emits when any error occurs -> calls closed event immediately after this.
server.on('error',function(error){
console.log('Error: ' + error);
});
//emits when server is bound with server.listen
server.on('listening',function(){
console.log('Server is listening!');
});
server.maxConnections = 10;
//static port allocation
server.listen(printPort);
var islistening = server.listening;
if(islistening){
console.log('TCP Server is listening');
}else{
console.log('TCP Server is not listening');
}
setTimeout(function(){
server.close();
},5000000);
//http server ---
// maps file extention to MIME types
const mimeType = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword',
'.eot': 'appliaction/vnd.ms-fontobject',
'.ttf': 'aplication/font-sfnt'
};
// special in memory directory containing machine state
const stateDir = 'state';
var layerCount = 0;
http.createServer(function (req, res) {
console.log(`${req.method} ${req.url}`);
// parse URL
const parsedUrl = url.parse(req.url);
// extract URL path
// Avoid https://en.wikipedia.org/wiki/Directory_traversal_attack
// e.g curl --path-as-is http://localhost:9000/../fileInDanger.txt
// by limiting the path to current directory only
const sanitizePath = path.normalize(parsedUrl.pathname).replace(/^(\.\.[\/\\])+/, '');
let pathname = path.join(__dirname, sanitizePath);
//get root dir of the url and check if it's in memory on on disk.
var firstDir = sanitizePath;
firstDir.toLowerCase();
var tokens = firstDir.split("\\");
console.log(tokens[1]);
if(tokens[1] === stateDir){
if(tokens.length<3){
res.statusCode = 404;
res.end('need a variable name under /state/');
}
var varName = tokens[2];
if(varName === "layer"){
res.setHeader('Content-type', 'text/plain' );
res.end(layerCount.toString());
layerCount = layerCount + 1;
return;
}else if(varName === "scan.png"){
if(completeImage == null){
res.setHeader('Content-type', 'text/plain' );
res.end("scan not available");
return;
}
res.setHeader('Content-type', 'image/png' );
res.end(completeImage);
return;
}
}
fs.exists(pathname, function (exist) {
if(!exist) {
// if the file is not found, return 404
res.statusCode = 404;
res.end(`File ${pathname} not found!`);
return;
}
// if is a directory, then look for index.html
if (fs.statSync(pathname).isDirectory()) {
pathname += '/index.html';
}
// read file from file system
fs.readFile(pathname, function(err, data){
if(err){
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
} else {
// based on the URL path, extract the file extention. e.g. .js, .doc, ...
const ext = path.parse(pathname).ext;
// if the file is found, set Content-type and send data
res.setHeader('Content-type', mimeType[ext] || 'text/plain' );
res.end(data);
}
});
});
}).listen(parseInt(port));
console.log(`Server listening on port ${port}`);