-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (93 loc) · 2.53 KB
/
server.js
File metadata and controls
109 lines (93 loc) · 2.53 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
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
//-- includes
require('./net.Socket.proto.js');
require('./Date.proto.js');
require('./String.proto.js');
var c = require('./CommandCenter');
var CommandCenter = c.CommandCenter;
var Server = {
ftp: require("net"),
server:null,
ports:[21],
reserver:[this],
configuration: require('./conf.js').conf,
start: function() {
console.log("Starting Server!");
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.on('data', function(command) {
var t = require('./server.js').Server;
command = command.substring(0, command.length-1).toLowerCase();
var data;
if(command.indexOf(' ') > 0) {
data = command.substring(command.indexOf(' ')+1, command.length);
command = command.substring(0, command.indexOf(' '));
}
var l = console.log;
console.log = function(text) {
process.stdout.write("> " + require("util").format.apply(this, arguments) + '\n');
}
switch(command) {
case "quit":
console.log("exiting on admin request");
process.exit();
break;
case "stat":
console.log("Statistics follow: ");
console.log("PID: " + process.pid);
console.log("Memory usage: " + require("util").inspect(process.memoryUsage()));
console.log("Ports reserved: " + t.ports.length);
break;
case "ports": {
console.log("The following ports are reserved/in use by either a data or control socket");
console.log(t.ports);
}
};
console.log = l;
});
this.server = this.ftp.createServer(this.request);
this.server.listen(5000);
},
request: function(socket) {
//console.log("Request recieved");
socket.setEncoding("utf8");
socket.createHistory();
new CommandCenter(socket);
},
getFreePort: function() {
var p;
do {
p = Math.floor(Math.random()*(Math.pow(2, 16)-1024)+1024);
} while(p in this.ports);
return p;
},
portIsReserved: function(port) {
return port in this.ports;
},
reservePort: function(port, reserver) {
console.log("attempt to reserve port " + port);
if(!this.portIsReserved(port)) {
this.ports.push(port);
this.reserver.push(reserver);
return true;
}
return false;
},
releasePort: function(reserver) {
//console.log("releasing port");
var i = this.reserver.indexOf(reserver);
if(i >= 0) {
this.ports.splice(i, 1);
this.reserver.splice(i, 1);
return true;
}
return false;
},
typeToEncoding: function(type) {
type = type.toLowerCase();
if(type.startsWith("a")) return "ASCII";
if(type.startsWith("i")) return null;
return "ASCII";
}
};
exports.Server = Server;
Server.start();