-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
executable file
·160 lines (126 loc) · 4.26 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
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env node
var http = require("http");
var fs = require("fs");
var https = require("https");
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
app.use(bodyParser.json());
var _configuration = {};
var _availableIpAddresses = [];
function determineIpAddress(){
var os = require('os');
var ifaces = os.networkInterfaces();
var ip = '';
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;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
console.log(ifname + ':' + alias, iface.address);
} else {
// this interface has only one ipv4 adress
console.log(ifname, iface.address);
_availableIpAddresses.push (iface.address);
}
++alias;
if(ifname === "en0" || ifname == "eth0" || ifname == "eth1" || ifname == "Ethernet"){
ip = iface.address;
}
});
});
//ip = '172.17.179.40'
return ip;
}
function loadConfigurationFile(callback){
fs.readFile( __dirname + '/config.json', function (err, data) {
if (err) {
_configuration = {
edgeServer: {
ip: ""
},
stations: [
],
callSimulator: {
concurrentCalls: "10",
interval: "10",
calls: [
{
to: "555",
fromNumber: "555-555-5555",
fromName: "John Doe"
}
]
}
};
}else{
console.log("Got configuration");
console.log(data.toString());
_configuration = JSON.parse(data.toString());
}
callback(_configuration);
});
}
var ipAddress = determineIpAddress();
var sipEngine = require('./lib/sip_engine.js')(ipAddress);
var simulator = require('./lib/simulator.js')(sipEngine, ipAddress);
//sipEngine.makeCall('sip:555@morbo.dev2000.com', "Kevin Glinski", '8723000')
//sipEngine.registerPhones([100], '172.19.33.192');
loadConfigurationFile(function(config){
simulator.start(config);
});
app.get('/status', function (req, res) {
res.setHeader('content-type', 'application/javascript');
var status = simulator.status();
status.simIp = ipAddress;
res.send(status);
});
app.post('/configuration', function (req, res) {
console.log("new configuration");
console.log(req.body);
_configuration = req.body;
fs.writeFileSync( __dirname + '/config.json', JSON.stringify(_configuration));
res.send("");
});
app.get('/configuration', function (req, res) {
res.setHeader('content-type', 'application/javascript');
res.send(_configuration);
});
app.get('/startsimulation', function (req, res) {
simulator.start(_configuration);
res.send();
});
app.get('/stopsimulation', function (req, res) {
simulator.stop(_configuration);
res.send();
});
app.all('/setip/:ip', function (req, res) {
var ip = req.params.ip;
ipAddress = ip;
simulator.setIp(ip);
sipEngine.setIp(ip);
res.send();
});
app.get('/allips', function (req, res) {
res.send(_availableIpAddresses);
});
app.all('/dial/:number', function (req, res) {
var number = req.params.number;
console.log(_configuration);
var to = "sip:" + number + "@" + _configuration.edgeServer.ip + ":5060";
sipEngine.makeCall( to ,
"Simulator",
"sip:" + number +"@" +ipAddress + ":5060");
res.send("");
});
app.use(express.static(__dirname + '/public'));
var httpServer = http.createServer(app);
var port = 8888;
console.log("starting on " + port);
var open = require('open');
open('http://localhost:'+port);
httpServer.listen(port);