3

我正在尝试创建一个服务于一个目的的最小 SIP 代理:将请求重定向到另一个域。问题是我要重定向到的域需要授权,所以我假设我需要重写一些 SIP 属性,因为 SIP 授权部分基于目标的域名。

我已经尝试发出 302 重定向以及简单地代理和更改每个 SIP 请求的值,但似乎没有一个可以退出。我正在使用 node.js 库(sip.js)并尝试了重定向和代理模块(https://github.com/kirm/sip.js/blob/master/doc/api.markdown)。

我需要如何修改 SIP 数据以将请求重定向到另一个域并启用对另一个域进行身份验证的任何想法?

4

1 回答 1

8

下面是我使用自己的 SIP 服务器使用的基本节点脚本。您需要为自己的测试替换凭据和 IP 地址。

代理脚本不会向客户端发送重定向响应,而是代表客户端向服务器发起新事务。在这种模式下运行的 SIP 服务器更准确地称为背对背用户代理 (B2BUA)。我还没有添加所有需要的功能,例如匹配并将响应传回原始客户端;这涉及到相当多的工作。

var sip = require('sip');
var digest = require('sip/digest');
var util = require('util');
var os = require('os');
var proxy = require('sip/proxy');

var registry = {
  'user': { user: "user", password: "password", realm: "sipserver.com"},
};

function rstring() { return Math.floor(Math.random()*1e6).toString(); }

sip.start({
  address: "192.168.33.116", // If the IP is not specified here the proxy uses a hostname in the Via header which will causes an issue if it's not fully qualified.
  logger: { 
    send: function(message, address) { debugger; util.debug("send\n" + util.inspect(message, false, null)); },
    recv: function(message, address) { debugger; util.debug("recv\n" + util.inspect(message, false, null)); }
  }
},
function(rq) {
  try {
    if(rq.method === 'INVITE') {  

       proxy.send(sip.makeResponse(rq, 100, 'Trying'));

      //looking up user info
      var username = sip.parseUri(rq.headers.to.uri).user;    
      var creds = registry[username];

      if(!creds) {  
        proxy.send(sip.makeResponse(rq, 404, 'User not found'));
      }
      else {
        proxy.send(rq, function(rs) {

            if(rs.status === 401) {

                // Update the original request so that it's not treated as a duplicate.
                rq.headers['cseq'].seq++;
                rq.headers.via.shift ();
                rq.headers['call-id'] = rstring();

                digest.signRequest(creds, rq, rs, creds);

                proxy.send(rq);
            }
        });
      }
    }
    else {
      proxy.send(sip.makeResponse(rq, 405, 'Method Not Allowed'));
    }
  } catch(e) {
    util.debug(e);
    util.debug(e.stack);

   proxy.send(sip.makeResponse(rq, 500, "Server Internal Error"));
  }
});
于 2014-02-11T05:27:25.853 回答