Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
///
import * as node_server from '../node/server';
import * as node_socket from '../node/socket';
import * as session from '../session';
import * as wrtc from 'wrtc';
const SERVER_ADDRESS = '0.0.0.0';
const SERVER_PORT = 9999;
const getter = new wrtc.RTCPeerConnection();
const giver = new wrtc.RTCPeerConnection();
getter.onicecandidate = (event: any) => {
if (event.candidate) {
console.info('getter candidate', event.candidate.candidate);
giver.addIceCandidate(event.candidate);
}
}
giver.onicecandidate = (event: any) => {
if (event.candidate) {
console.info('giver candidate', event.candidate.candidate);
getter.addIceCandidate(event.candidate);
}
}
// Create a SocksSession in response to each new datachannel.
// NOTE: because onopen datachannel events don't fire for the
function createPeerConnection(sessionID, callbackType, callback)
{
var pc = new RTCPeerConnection
(
{iceServers: [{url: 'stun:'+stun_server}]},
{optional: [{DtlsSrtpKeyAgreement: true}]}
);
pc.addEventListener('icecandidate', function(event)
{
// There's a candidate, ignore it
if(event.candidate)
return;
// There's no candidate, send the full SDP
var type = this.localDescription.type;
var sdp = this.localDescription.sdp;
if(type == callbackType)
function createPeerConnection(sessionID, callbackType, callback)
{
var pc = new RTCPeerConnection
(
{iceServers: [{url: 'stun:'+stun_server}]},
{optional: [{DtlsSrtpKeyAgreement: true}]}
);
pc.addEventListener('icecandidate', function(event)
{
// There's a candidate, ignore it
if(event.candidate)
return;
// There's no candidate, send the full SDP
var type = this.localDescription.type;
var sdp = this.localDescription.sdp;
if(type == callbackType)
break
}
// Route session descriptions to peers.
case "OFFER":
case "ANSWER": {
const {
src,
payload: { sdp },
} = message
const peer = this.peers[src] || this.addPeer(src)
let description: RTCSessionDescription
try {
description = new RTCSessionDescription(sdp)
} catch (e) {
throw new Error("Invalid SDP")
}
peer.setRemoteDescription(description)
if (message.type === "OFFER") {
const sdp = await peer.answer()
this.onPeerSDP(sdp, src)
}
break
}
case "KEEP_ALIVE": {
this.transport.send({
peer.addIceCandidate(ice);
break;
}
// Route session descriptions to peers.
case "OFFER":
case "ANSWER": {
var _src = message.src,
sdp = message.payload.sdp;
var _peer = _this._peers[_src] || _this._addPeer(_src);
var description = void 0;
try {
description = new wrtc.RTCSessionDescription(sdp);
} catch (e) {
throw new Error("Invalid SDP");
}
_peer.setRemoteDescription(description);
if (message.type === "OFFER") {
_peer.answer();
}
break;
}
default:
break;
}
};
if (that.iceGatheringState === 'complete') {
var nullCandidate = new RTCIceCandidate();
nullCandidate.candidate = null;
that.onicecandidate(nullCandidate);
} else {
checkIceState();
}
}, 1000).unref();
})(); // miserable hack, waiting for https://github.com/js-platform/node-webrtc/issues/44
};
util.inherits(RTCPeerConnection, webrtc.RTCPeerConnection);
GLOBAL.RTCPeerConnection = RTCPeerConnection;
GLOBAL.RTCIceCandidate = webrtc.RTCIceCandidate;
GLOBAL.RTCSessionDescription = webrtc.RTCSessionDescription;
GLOBAL.DataChannel = webrtc.DataChannel;
var CustomWebSocket = function(url) {
if (!(this instanceof WebSocket)) {
return new CustomWebSocket(url);
}
var websocketClient = new WebSocketClient();
var that = this;
websocketClient.on('connect', function(connection) {
connection.on('message', function(msg) {
msg.data = msg.utf8Data;
that.onmessage(msg);
});
that.send = function(msg) {
connection.send(msg);
};
});
function clientStreamDescription(streamSource, description) {
const signal = JSON.parse(description);
if (signal.sdp) {
streamSource.implementation.peerConnection.setRemoteDescription(new webrtc.RTCSessionDescription(signal.sdp)).then(() => {
return streamSource.implementation.peerConnection.createAnswer();
}).then((desc) => {
return streamSource.implementation.peerConnection.setLocalDescription(desc);
}).then(() => {
streamSource.server_stream_description(JSON.stringify({"sdp": streamSource.implementation.peerConnection.localDescription}));
}).catch((error) => {
console.error(error);
streamSource.client.close();
});
} else if (signal.candidate) {
streamSource.implementation.peerConnection.addIceCandidate(new webrtc.RTCIceCandidate(signal.candidate)).catch((error) => {
console.error("Error: Failure during addIceCandidate()", error);
streamSource.client.close();
});
}
if (signal.sdp) {
streamSource.implementation.peerConnection.setRemoteDescription(new webrtc.RTCSessionDescription(signal.sdp)).then(() => {
return streamSource.implementation.peerConnection.createAnswer();
}).then((desc) => {
return streamSource.implementation.peerConnection.setLocalDescription(desc);
}).then(() => {
streamSource.server_stream_description(JSON.stringify({"sdp": streamSource.implementation.peerConnection.localDescription}));
}).catch((error) => {
console.error(error);
streamSource.client.close();
});
} else if (signal.candidate) {
streamSource.implementation.peerConnection.addIceCandidate(new webrtc.RTCIceCandidate(signal.candidate)).catch((error) => {
console.error("Error: Failure during addIceCandidate()", error);
streamSource.client.close();
});
}
}
value: function _addPeer(id) {
var _this2 = this;
if (this._peers[id]) {
throw new Error(
"RTCPeer with id " + id + " already exists."
);
}
var peer = new RTCPeer({
pc: new wrtc.RTCPeerConnection(RTC_PEER_CONNECTION_OPTIONS),
onChannel: this._onPeerChannel,
onClose: function onClose() {
return _this2._onPeerClose(id);
},
onSDP: function onSDP(sdp) {
return _this2._onPeerSDP(sdp, id);
},
onICE: function onICE(ice) {
return _this2._onPeerICE(ice, id);
}
});
this._peers[id] = peer;
return peer;
}