-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
137 lines (100 loc) · 3.23 KB
/
index.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
var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;
var fs = require('fs');
var http = require('http');
var https = require('https');
var os = require('os');
var ifaces = os.networkInterfaces();
var privateKey = fs.readFileSync('serve/key.pem', 'utf8');
var certificate = fs.readFileSync('serve/cert.pem', 'utf8');
var sslCredentials = {key: privateKey, cert: certificate};
var httpServer = http.createServer(app);
var httpsServer = https.createServer(sslCredentials, app);
httpServer.close();
httpsServer.close();
var exports = module.exports = {};
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/', function(req, res, next) { res.send('Welcome to OfficeX'); });
/*app.get('/send', function(req, res, next) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('demo/playb.html', function (err, data){
if(err) {
throw error;
}
else {
res.write(data);
}
res.end();
});
});*/
var host = "127.0.0.1";
var port = 9001;
var Sport = 9000;
var path = '/officeConn';
console.log(ifaces);
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
console.log("");
console.log("Welcome to the Chat Sandbox");
console.log("");
console.log("Test the chat interface from this device at : ", "https://localhost:" +Sport);
console.log("");
console.log("And access the chat sandbox from another device through LAN using any of the IPS:");
console.log("Important: Node.js needs to accept inbound connections through the Host Firewall");
console.log("");
if (alias >= 1) {
console.log("Multiple ipv4 addreses were found ... ");
// this single interface has multiple ipv4 addresses
console.log(ifname + ':' + alias, "https://"+ iface.address + ":"+Sport);
} else {
// this interface has only one ipv4 adress
console.log(ifname, "https://"+ iface.address + ":"+Sport);
}
++alias;
});
});
app.get('/send', function(req, res, next) {
res.render('demo/play', {
user: 'collins',
host: host,
port: Sport,
path: path
});
});
app.get('/recieve', function(req, res, next) {
res.render('demo/play', {
user: 'derrick',
host: host,
port: Sport,
path: path
});
});
var options = {
debug: 3,
ssl: sslCredentials
}
var connServer = ExpressPeerServer(httpsServer, options);
app.use(path, connServer);
connServer.on('connection', function(id) {
console.log('New connection with ' + id);
});
connServer.on('disconnect', function (id) {
console.log('disconnect with id ' + id);
});
// Allow access from all the devices of the network (as long as connections are allowed by the firewall)
var LANAccess = "0.0.0.0";
// For http
httpServer.listen(port, LANAccess);
// For https
httpsServer.listen(Sport, LANAccess);
exports.closeOfficeX = function(){
httpServer.close();
httpsServer.close();
};