-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
177 lines (135 loc) · 4.57 KB
/
server.js
File metadata and controls
177 lines (135 loc) · 4.57 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
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
173
174
175
176
177
/*
* Copyright (c) 2015 Interactive Intelligence
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
var express = require('express')
var app = express();
var fs = require('fs');
var cors = require('cors');
var path = require('path');
//var email = require('emailjs');
var request = require('request');
var util = require('util');
var os = require('os');
var sip = require('sip');
var http = require('http');
var bodyParser = require('body-parser');
var email = require('emailjs/email');
app.use(express.static('public'));
app.use(cors())
app.set('port', (process.env.PORT || 8080))
app.use(bodyParser.json());
var messageTimer = null
console.log("app starting")
//Generates a random string, used in the options message
function rstring() { return Math.floor(Math.random()*1e6).toString(); }
var config = require('./configuration.json');
var devices = {};
sip.start({}, function(rq) {});
//timeout in the sip library is 120000 and is not configurable, lets set our poll higher than that so we don't have more than 1 ping at a time
pollDevices();
messageTimer = setInterval(pollDevices, 121000);
function pollDevices(){
for(var i=0; i< config.servers.length; i++){
var server = config.servers[i];
sendOptions(server.ip, server.name)
}
}
//sends an outbound email for status changes
function sendEmail(name, data) {
if( config.notifications.email.configuration.host == null || config.notifications.email.configuration.host === ""){
return;
}
var emailConfig = config.notifications.email.configuration;
var server = email.server.connect(emailConfig);
var sendParams = config.notifications.email.message;
sendParams.subject = "SIP Stack for " + name + " is " + data.status;
sendParams.text = "Server: " + name + "\nIP: " + data.ip + "\nStatus: " + data.status
console.log("sending email to " + sendParams.to)
server.send(sendParams, function(err, message) {
if(err){
console.log("ERROR sending email: " + err)
}else{
console.log("email sent")
}
});
}
//makes the outbound POST for a webhook when the status changes.
function sendWebHook(name, data) {
if( config.notifications.webhook.host == null || config.notifications.webhook.host === ""){
return;
}
data.name = name;
// An object of options to indicate where to post to
var post_options = {
host: config.notifications.webhook.host,
port: config.notifications.webhook.port,
path: config.notifications.webhook.path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': JSON.stringify(data).length
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
});
// post the data
console.log("calling webhook");
post_req.write(JSON.stringify(data));
post_req.end();
}
//handles sending an options message to a server
function sendOptions(baseIp, name){
ip = "sip:" + baseIp + ":5060";
sip.send({
method: 'OPTIONS',
uri: ip,
headers: {
to: {uri: ip},
from: {uri: 'sip:test@test', params: {tag: rstring()}},
'call-id': rstring(),
cseq: {method: 'OPTIONS', seq: Math.floor(Math.random() * 1e5)},
'content-type': 'application/sdp',
}
},
function(rs) {
var status = {
ip: baseIp
}
if(rs.status == 200){
status.status = "up"
console.log(name + " UP")
}
else{
status.status = "down"
console.log(name + " DOWN")
}
var oldData = devices[name];
if(oldData != null && oldData.status != status.status){
sendWebHook(name, status);
sendEmail(name, status);
}else if(oldData == null){
sendWebHook(name, status);
sendEmail(name, status);
}
devices[name] = status;
});
}
app.listen(app.get('port'), function() {
console.log("SIP Verification app is running at localhost:" + app.get('port'))
})
app.get('/devices', function(request, response){
response.send(devices);
})
app.get('/', function(request, response){
response.sendFile(path.join(__dirname,"index.html"));
})
app.post('/webhooktest', function(req, res){
console.log('webhook received: ' + JSON.stringify(req.body));
res.send();
})