-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
122 lines (88 loc) · 3.34 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
var sip = require('sip');
var sys = require('sys');
var redis = require('redis');
var tropoapi = require('tropo-webapi');
//Trim leading and trailing whitespace from string values.
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
sip.start({},function(request) {
try {
// Parse the URI in the reuqest header.
var address = sip.parseUri(request.headers.to.uri);
sys.puts('host: ' + address.host)
var siphost = address.host
// Create a redis client to manage the registration
// info.
var client = redis.createClient();
sys.puts(trim(request.method) + ' received.');
// Handle SIP Registrations.
if (trim(request.method) == 'REGISTER') {
var contact = request.headers.contact;
// Store the registration info.
if (Array.isArray(contact)
&& contact.length
&& (+(contact[0].params.expires
|| request.headers.expires || 300)) > 0) {
sys.puts('Registering user ' + request.headers.to.name + ' at ' + contact[0].uri);
client.set(address.user, contact[0].uri);
}
// Remove the registration info.
else {
sys.puts('Logging off user ' + request.headers.to.name);
client.del(address.user);
}
// Build the response.
var response = sip.makeResponse(request, 200, 'OK');
// Send the response to the SIP client
sip.send(response);
}
// Handle SIP Invites.
if (trim(request.method) == 'INVITE') {
sip.send(sip.makeResponse(request, 100, 'Trying'));
// Look up the registration info. for the user being
// called.
address = sip.parseUri(request.uri);
sys.puts('host: ' + address.host);
if (address.host == '127.0.0.1'){ // Our Registrar
client.get(address.user, function(err, contact) {
// If not found or error return 404.
if (err || contact === null) {
// sys.puts('User ' + address.user + ' is not found');
// sip.send(sip.makeResponse(request, 404, 'Not Found'));
sys.puts('Redirecting call to ' + address.user);
var response = sip.makeResponse(request, 302, 'Moved Temporarily');
// response.headers.contact = [ { uri : "sip:194@sip.teleku.com" }]; //works
// response.headers.contact = [ { uri : "sip:chris@127.0.0.1" }]; //works
// response.headers.contact = [ { uri : "sip:9991490318@sip.tropo.com" }];
response.headers.contact = [ {uri: "sip:9991490318@sip.tropo.com?x-numbertodial=" + address.user } ];
sip.send(response);
}
// Otherwise, send redirect with contact URI.
else {
sys.puts('User ' + address.user
+ ' is found at ' + contact);
sys.puts('contact ' + contact);
var response = sip.makeResponse(request, 302, 'Moved Temporarily');
response.headers.contact = [ { uri : contact } ];
sip.send(response);
}
});
// Close the Redis client
client.quit();
} else { // Host other than our Registrar
sys.puts('Routing call to ' + request.uri);
var response = sip.makeResponse(request, 180, 'Ringing');
// response.headers.contact = [{uri: request.uri}];
// var response = sip.makeResponse(request, 100, 'Trying');
// response.headers.contact = [{uri: request.uri}];
sip.send(response);
}
}
}
// Handle exceptions.
catch (e) {
sip.send(sip.makeResponse(request, 500, 'Internal Server Error'));
sys.debug('Exception ' + e + ' at ' + e.stack);
}
});