Skip to content

Instantly share code, notes, and snippets.

@ggarber
Created July 15, 2012 22:53
Show Gist options
  • Save ggarber/3119020 to your computer and use it in GitHub Desktop.
Save ggarber/3119020 to your computer and use it in GitHub Desktop.
SIP websockets to TCP proxy
var net = require('net'),
ws = require('ws'),
sip = require('sip');
var wss = new ws.Server({port: 8080});
var LINES_FILTER_REGEX = /^a=candidate.*|^a=crypto.*/;//|^a=ssrc.*/;
wss.on('connection', function(ws) {
console.log('received connection');
var tcpSocket = new net.Socket();
tcpSocket.connect('6050', '193.104.44.133');
tcpSocket.on('data', function(data) {
console.log('TCP received: %s', data);
message = data.toString('utf-8');
//message = message.replace('RTP/AVP', 'RTP/SAVPF');
message = message.replace('TCP', 'WS').replace('tcp', 'ws');
var msg = sip.parse(message);
message = sip.stringify(msg);
console.log('WS sent: %s', message);
ws.send(data);
});
ws.on('message', function(message) {
console.log('WS received: %s', message);
message = message.replace('WS', 'TCP').replace('ws', 'tcp')
.replace('RTP/SAVPF', 'RTP/AVP')
.replace(/df7jal23ls0d.invalid/g, '192.168.1.32');
//message = message.replace(/a=crypto/g, '');
var msg = sip.parse(message)
if (msg.status / 100 == 1) {
return;
}
if (msg.headers.contact) {
// msg.headers.contact[0].uri = msg.headers.contact[0].uri.replace('df7jal23ls0d.invalid', '192.168.1.32');
msg.headers.contact[0].params['reg-id'] = '1';
msg.headers.contact[0].params['+sip.instance'] = '"<urn:uuid:4b1682a8-f968-5701-83fc-7c6741dc6697>"';
msg.headers.supported = 'outbound, path';
}
//Filter SDP lines
lines = msg.content.split('\r\n');
for (var i=0; i<lines.length; i++) {
if (lines[i].match(LINES_FILTER_REGEX)) {
lines.splice(i--, 1);
}
if (lines[i].match(/^a=ssrc.*/)) {
//lines[i] = 'a=ssrc:3589515530 candidate:0 2 tcp 1694498816 192.168.1.34 56466 typ host network_name en1 username HUTQchBG/tGBpWTD password pDEFkVILaaD7qEwvZaLJPuuH generation 0';
}
}
msg.content = lines.join('\r\n');
var data = sip.stringify(msg);
console.log('TCP sent: %s', data);
tcpSocket.write(data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment