}, function(rq) { if(rq.method === 'REGISTER') { var user = sip.parseUri(rq.headers.to.uri).user; contacts[user] = rq.headers.contact; var rs = sip.makeResponse(rq, 200, 'Ok'); rs.headers.to.tag = Math.floor(Math.random() * 1e6); // Notice _proxy.send_ not sip.send proxy.send(rs); } else { var user = sip.parseUri(rq.uri).user; if(contacts[user] && Array.isArray(contacts[user]) && contacts[user].length > 0) { rq.uri = contacts[user][0].uri; proxy.send(sip.makeResponse(rq, 100, 'Trying')); proxy.send(rq); } else { proxy.send(sip.makeResponse(rq, 404, 'Not Found')); } } });
function(rq) { try { if(rq.method === 'REGISTER') { //looking up user info var username = sip.parseUri(rq.headers.to.uri).user; var userinfo = registry[username]; if(!userinfo) { // we don't know this user and answer with a challenge to hide this fact var session = {realm: realm}; sip.send(digest.challenge({realm: realm}, sip.makeResponse(rq, 401, 'Authentication Required'))); } else { userinfo.session = userinfo.session || {realm: realm}; if(!digest.authenticateRequest(userinfo.session, rq, {user: username, password: userinfo.password})) { sip.send(digest.challenge(userinfo.session, sip.makeResponse(rq, 401, 'Authentication Required'))); } else { userinfo.contact = rq.headers.contact; var rs = sip.makeResponse(rq, 200, 'Ok'); rs.headers.contact = rq.headers.contact; sip.send(rs); } } } else if(rq.method === 'INVITE') { var username = sip.parseUri(rq.uri).user; var userinfo = registry[username] if(userinfo && Array.isArray(userinfo.contact) && userinfo.contact.length > 0) { var rs = sip.makeResponse(rq, 302, 'Moved'); rs.headers.contact = userinfo.contact; sip.send(rs); } else { sip.send(sip.makeResponse(rq, 404, 'Not Found')); } } else { sip.send(sip.makeResponse(rq, 405, 'Method Not Allowed')); } } catch(e) { util.debug(e); util.debug(e.stack); sip.send(sip.makeResponse(rq, 500, "Server Internal Error")); } });
// Initiating a call example. Note: we use bogus sdp, so no real rtp session will be established. var sip = require('sip'); var util = require('util'); var os = require('os'); console.log(process.argv); console.log(process.argv[2]); if(process.argv < 3 || !sip.parseUri(process.argv[2])) { console.error('usage node make_call.js _uri_'); process.exit(1); } var dialogs = {}; function rstring() { return Math.floor(Math.random()*1e6).toString(); } //starting stack sip.start({}, function(rq) { if(rq.headers.to.params.tag) { // check if it's an in dialog request var id = [rq.headers['call-id'], rq.headers.to.params.tag, rq.headers.from.params.tag].join(':'); if(dialogs[id]) dialogs[id](rq); else sip.send(sip.makeResponse(rq, 481, "Call doesn't exists")); } else sip.send(sip.makeResponse(rq, 405, 'Method not allowed'));
function getUser(sipuri){ var user = sip.parseUri(sipuri).user; return (user); }