-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (114 loc) · 3.65 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
const socketIO = require('socket.io');
const fs = require('fs');
const IORouterModule = require('./routing/IORouter');
const IORouter = new IORouterModule();
const app = require('./routing/PathRouter')(saveFile, saveLogData, __dirname);
const readline = require("readline");
const { networkInterfaces } = require('os');
const server = require('http').createServer(app);
const io = socketIO(server, {});
var PORT = process.env.PORT || 80;
var available_adresses = getLocalAddressList();
var IP = "localhost";
const LogSystem = require("./logsystem.js");
const Log = new LogSystem();
const LOG_PATH = __dirname+"/db/logs/log_history.dat";
const default_ip = findValue("-default-ip");
const default_addr_name = findValue("-default-addr-name");
const default_port = findValue("-default-port");
const save_files = (findValue("-save-files")=="true") ? true:false;
const write_log = (findValue("-write-log")=="true") ? true:false;
if(default_port !== ""){
PORT = parseInt(default_port);
}
if(default_ip !== ""){
IP = default_ip;
server.listen(PORT,IP,()=>{
console.log("Server started on http://"+IP+":"+PORT);
});
IORouter.getRouter({save_files:save_files, write_log:write_log}).listen(io, saveFile, saveLogData);
}else if(default_addr_name !== ""){
IP = available_adresses[default_addr_name][0];
server.listen(PORT,IP,()=>{
console.log("Server started on http://"+IP+":"+PORT);
});
IORouter.getRouter({save_files:save_files, write_log:write_log}).listen(io, saveFile, saveLogData);
}else{
inputUserAddress();
}
function saveFile(fileID, file, sender, transport=false){
let fileName = file.name;
let fileData = file.data;
let filePath = `./db/${(transport)?"transport/":""}`;
fs.appendFile(filePath+fileName, fileData,(err)=>{
if(err)
console.log("Something went wrong!");
});
};
function saveLogData(fileID, file, sender, receiver){
let fileName = file.name;
let time = Log.getTime();
console.log(`Saved file : ${fileName} [${time}]`);
Log.writeLog(fileName, "./db/"+fileName, sender, receiver, LOG_PATH, fileID, time, (result)=>{
if(result){
console.log("Transport logged");
}else{
console.log("Transport failed to log");
}
});
}
function inputUserAddress(){
const userInCLI = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log("Available networks:");
Object.keys(available_adresses).map((resultName)=>{
console.log(` - ${resultName}`);
});
userInCLI.question("Select one of above networks to run your program on (to run only on your computer type 'localhost' ):", function(name) {
if(name === 'localhost'){
userInCLI.close();
return;
}
let selection = available_adresses[name];
if(selection)
IP = selection[0];
else{
IP = "localhost";
console.log(`Can't find network with name: ${name}`);
}
userInCLI.close();
});
userInCLI.on("close", function() {
console.log(`Ip address set to ${IP}`);
server.listen(PORT,IP,()=>{
console.log("Server started on http://"+IP+":"+PORT);
});
IORouter.getRouter({save_files:save_files, write_log:write_log}).listen(io, saveFile, saveLogData);
});
}
function findValue(label){
var result = "";
process.argv.forEach((value, idx)=>{
if(value.indexOf(label) !== -1){
result = process.argv[idx+1];
}
})
return result;
}
function getLocalAddressList(){
const nets = networkInterfaces();
const results = Object.create(null);
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
if (net.family === 'IPv4' && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
}
}
return results;
}