Skip to content

Instantly share code, notes, and snippets.

@WhiteyDude
Last active July 22, 2023 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhiteyDude/893acc13b0eb5ab8b76769126d614542 to your computer and use it in GitHub Desktop.
Save WhiteyDude/893acc13b0eb5ab8b76769126d614542 to your computer and use it in GitHub Desktop.
const Srf = require('drachtio-srf');
const srf = new Srf();
const rtpengineClient = require('rtpengine-client').Client;
const { v4: uuid } = require('uuid');
const sdpTransform = require('sdp-transform');
const config = {
'drachtio': {
"host": "drachtio",
"port": 9022,
"secret": "cymru"
},
'rtpengine': {
'host': 'rtpengine',
'port': 22222
}
}
const rtpengine = new rtpengineClient()
srf.connect(config['drachtio'], (err, srf) => {
if (err) return console.log(`error connecting to drachtio: ${err}`);
});
function createDummyUA(legASdp) {
return new Promise((resolve, reject) => {
let ua = {}
ua.toTag = uuid()
let tempSdp = sdpTransform.parse(legASdp)
tempSdp.origin.sessionVersion += 1
tempSdp.media = tempSdp.media.map((item) => {
//item['direction'] = 'inactive'
item.connection.ip = '127.0.0.1'
item.port = 31337
return item
});
ua.sdp = sdpTransform.write(tempSdp)
console.log(ua)
resolve(ua)
});
}
srf.invite(async(req, res) => {
console.log(`[${req.get('call-id')}] Sending rtpengine offer, invite SDP`)//, req.body)
// Take our Leg A offer and rewrite it to suit the media proxy
let rewrittenOffer = await rtpengine.offer(config['rtpengine'], {
'sdp': req.body,
'call-id': req.get('call-id'),
'from-tag': req.getParsedHeader('From')['params']['tag'],
//'media-echo': 'backwards',
'replace': ['origin', 'session connection'] // IP hiding
})
console.log(`[${req.get('call-id')}] Got response from rtpengine, creating dummy UA for answer`)//, rewrittenOffer.sdp)
// Create a dummy UA (leg B) with our media proxy offer
let dummyUA = await createDummyUA(rewrittenOffer.sdp)
// Prepare an answer response for leg A
console.log(`[${req.get('call-id')}] Sending answer to rtpengine for parsing`)//, rewrittenOffer.sdp)
let answer = await rtpengine.answer(config['rtpengine'], {
'sdp': dummyUA.sdp,
'call-id': req.get('call-id'),
'to-tag': dummyUA.toTag,
'from-tag': req.getParsedHeader('From')['params']['tag'],
'replace': ['origin', 'session connection'] // IP hiding
})
// Send our prepared answer to leg A, allowing it to connect to the media session
let uas = await srf.createUAS(req, res, {
localSdp: answer.sdp
})
uas.on('destroy', async () => {
console.log("Leg A destroyed")
let rtpResult = await rtpengine.query(config['rtpengine'], {
'call-id': req.get('call-id')
})
console.log(`[${req.get('call-id')}] Call query result:`)
console.dir(rtpResult, { depth: null });
await rtpengine.delete(config['rtpengine'], {
'call-id': req.get('call-id'),
'from-tag': req.getParsedHeader('From')['params']['tag']
})
});
console.log(`[${req.get('call-id')}] Dialog created, playing test media`)
let playMediaTest = await rtpengine.playMedia(config['rtpengine'], {
'call-id': req.get('call-id'),
'from-tag': req.getParsedHeader('From')['params']['tag'],
//'flags': ['all'],
'file': '/test.mp3'
})
if (playMediaTest['duration']) {
await new Promise(resolve => setTimeout(resolve, playMediaTest['duration']));
}
uas.destroy()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment