forked from mekazombify/Hack4Good2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMAServer.js
172 lines (161 loc) · 4.37 KB
/
BMAServer.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
/*
Borrow My Angel Server
Frank Giddens
November 2, 2018
November 4, 2018
*/
/*
TO DO LIST
Account Creation and Authentication
Route People in Need to an Available Angel to Communicate
Integrate with an Internet-based Text Chat (Ideally Voice)
Manage Angel Availability
Maintain and Provide Searchable List of Local Resources
Maintain and Provide a BMA Blog
Provide Ability to Accept Donations Securely
Maintain and Provide Angel Training Resources
Monero Implementation
Write Presentation
*/
/*
Presentation Notes
*/
"use strict";
let fs = require("fs");
let os = require("os");
let url = require("url");
let http = require("http");
let child = require("child_process");
let io = require("socket.io");
let server;
let listener;
let port = 8080;
let waitingType = "PIN";
let waiting = [];
let getIPAddress = function(){
let address = "127.0.0.1";
let interfaces = os.networkInterfaces();
for(let devName in interfaces){
let iface = interfaces[devName];
for(let i = 0; i < iface.length; i++){
let alias = iface[i];
if(alias.family === "IPv4" && alias.address !== "127.0.0.1" && !alias.internal){
address = alias.address
}
}
}
return address;
};
let handleRequest = function(req, res){
let pathname = url.parse(req.url).pathname;
if(pathname.indexOf(".") === -1){
pathname += ".html";
}
if(pathname.indexOf("/") === 0){
pathname = pathname.substr(1);
}
if(pathname === ".html"){
pathname = "index.html";
}
fs.readFile(pathname, function(err, data){
if(err){
res.writeHead(404, {'Content-Type': 'text/html'});
}
else{
switch(pathname.substr(pathname.lastIndexOf(".") + 1)){
case "css":
res.writeHead(200, {'Content-Type': 'text/css'});
res.write(data.toString());
break;
case "html":
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data.toString());
break;
case "js":
res.writeHead(200, {'Content-Type': 'text/js'});
res.write(data.toString());
break;
}
}
res.end();
});
};
let init = function(){
server = http.createServer(handleRequest);
server.listen(port, getIPAddress());
listener = io.listen(server);
listener.sockets.on("connection", function(socket){
socket.on("typeCheck", function(data){
if(waiting.length == 0){
waitingType = data.userType;
waiting[waiting.length] = socket.id;
}
else{
if(data.userType == waitingType){
waiting[waiting.length] = socket.id;
}
else{
socket.emit("conn", {"other" : waiting[0]});
listener.to(waiting[0]).emit("conn", {"other" : socket.id});
waiting.shift();
}
}
});
socket.on("message", function(data){
listener.to(data.target).emit("message", {"mes" : data.mes});
});
socket.on("signupAngel", function(data){
let angelData = data;
angelData.userType = "Angel";
angelData.verified = false;
angelData.texts = 0;
angelData.minutes = 0;
angelData.training = {};
angelData.training.createAccount = true;
if(fs.existsSync("users/" + angelData.userName + ".json")){
socket.emit("signupReceived", {"success" : false});
}
else{
fs.writeFileSync("users/" + angelData.userName + ".json", JSON.stringify(angelData));
socket.emit("signupReceived", {"success" : true});
}
});
socket.on("signupPIN", function(data){
let pinData = data;
pinData.userType = "PIN";
if(fs.existsSync("users/" + pinData.userName + ".json")){
socket.emit("signupReceived", {"success" : false});
}
else{
fs.writeFileSync("users/" + pinData.userName + ".json", JSON.stringify(pinData));
socket.emit("signupReceived", {"success" : true, "user" : pinData.userName});
}
});
socket.on("login", function(data){
let successfulLogin = true;
let tempData;
if(fs.existsSync("users/" + data.user + ".json")){
tempData = fs.readFileSync("users/" + data.user + ".json");
tempData = JSON.parse(tempData);
if(tempData.password != data.pass){
successfulLogin = false;
}
}
else{
successfulLogin = false;
}
if(successfulLogin){
switch(tempData.userType){
case "Admin": socket.emit("AdminLogin", {"user" : tempData.userName}); break;
case "Angel": socket.emit("AngelLogin", {"user" : tempData.userName}); break;
case "PIN": socket.emit("PINLogin", {"user" : tempData.userName}); break;
}
}
else{
socket.emit("LoginError", {});
}
});
});
console.log("Server active at " + getIPAddress() + ":" + port);
};
init();