-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
70 lines (65 loc) · 2.34 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
var sip = require('sip'),
proxy = require('sip/proxy'),
digest = require('sip/digest');
var Proxy = function(conf){
var digest_context = {realm: conf.digest_context}; // sauce
var twilio_sip_uri = conf.sip_uri; // eg. foo.sip.twilio.com
var getPassword = conf.getPassword; // function that returns the password for a username
var onCancel = conf.onCancel; // function that returns call-id on cancel
var update_request_uri = function(rq){
var uri = sip.parseUri(rq.uri);
uri.host = twilio_sip_uri;
rq.uri = sip.stringifyUri(uri);
return rq;
}
var get_request_username = function(rq){
return sip.parseUri(rq.headers.from.uri).user;
}
proxy.start({
logger: {
recv: function(m) {
if (m.method == "CANCEL" && onCancel) {
onCancel(null, m.headers['call-id']);
}
},
}
}, function(rq) {
try {
rq = update_request_uri(rq);
switch(rq.method) {
case "REGISTER":
case "SUBSCRIBE":
proxy.send(sip.makeResponse(rq, 403, 'Forbidden'));
break;
case "INVITE":
proxy.send(sip.makeResponse(rq, 100, 'Trying'));
var username = get_request_username(rq);
getPassword(username, function(err, password){
if (err) {
proxy.send(sip.makeResponse(rq, err.code || 500, err.toString()));
} else {
var authenticated = digest.authenticateRequest(digest_context, rq, {
user: username,
password: password
});
if (!authenticated) {
var resp = sip.makeResponse(rq, 407, 'Proxy Authentication Required');
var challenge = digest.challenge(digest_context, resp);
proxy.send(challenge);
} else {
proxy.send(rq);
}
}
});
break;
default:
proxy.send(sip.makeResponse(rq, 100, 'Trying'));
proxy.send(rq);
break;
}
} catch (err) {
proxy.send(sip.makeResponse(rq, 500, "Server Internal Error"));
}
});
}
module.exports = Proxy;