From: Jordan Date: Thu, 13 Jul 2017 00:14:31 +0000 (+0000) Subject: Merge branch 'master' of github.com:Jlavatai/strapp X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=commitdiff_plain;h=c8d3d5f035df084549d35fbf9880f92a5a8d9e00;hp=bfecfec52e8ce28c6eefc336ee42df3263b6cef3 Merge branch 'master' of github.com:Jlavatai/strapp --- diff --git a/client-test.js b/client-test.js index fd84989..a0efaa4 100644 --- a/client-test.js +++ b/client-test.js @@ -1,222 +1,223 @@ -const body = document.createElement('body') -const root = document.createElement('div') -document.title = "Strapp.io Client" -const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] } - -/* TODO: This is duplicated in both client.js and host.js */ -function getPublicKey() { - return new Promise( (resolve, reject) => { - /* Check local storage for public key */ - if (!window.localStorage.getItem('public-key')) { - /* If doesn't exist, generate public and private key pair, store in - local storage */ - crypto.subtle.generateKey( - { name:'RSA-OAEP', - modulusLength: 2048, - publicExponent: new Uint8Array([0x01, 0x00, 0x01]), - hash: {name: "SHA-256"} - }, - true, - ['encrypt', 'decrypt'] - ).then((keyPair) => { - /* TODO: Do we need to store the private key as well? */ - crypto.subtle.exportKey('jwk', keyPair.publicKey) - .then((exportedKey) => { - window.localStorage.setItem('publicKey', exportedKey) - console.log('public key is' + window.localStorage.getItem('publicKey')) - resolve(exportedKey) - }) - - }) - } - else { - resolve(window.localStorage.getItem('publicKey')) - } -}) - -} - -function sendHost(url, data) { - const request = new XMLHttpRequest() - request.open('POST', url, true) - request.setRequestHeader('Content-Type', 'application/json' ) - request.setRequestHeader('X-Strapp-Type', 'ice-candidate-submission') - request.send(data) -} - -/* Poll the server. Send get request, wait for timeout, send another request. -Do this until...? Can be used for either reconnecting or waiting for answer*/ -function requestHostAnswer(url, data) { - return new Promise((resolve, reject) => { - const request = new XMLHttpRequest() - request.open('GET', url, true) - /* But there is no JSON? */ - request.setRequestHeader('Content-Type', 'application/json' ) - request.setRequestHeader('X-Strapp-Type', 'client-sdp-offer') - request.setRequestHeader('X-Strapp-Pubkey', data.pubKey) - request.setRequestHeader('X-Strapp-Offer', JSON.stringify(data.sdp)) - request.onreadystatechange = () => { - if (request.status === 200) { - if(request.readyState === 4) { - console.log('Client: Recieved Answer from Host') - console.log(request) - resolve(request.response) - } - } - else if (request.status === 504) { - console.log('timed out, resending') - resolve(requestHostAnswer(url, data)) - } - else { - reject('server unhandled response of status ' + request.status) - } - } - request.send() - }) -} - -/* Poll server for ice candidates until ice is complete */ -function requestHostICE(cpc, url, pubKey) { - let intervalID = window.setInterval(() => { - if (cpc.iceConnectionState.localeCompare('connected') !== 0 - && cpc.iceConnectionState.localeCompare('completed') !== 0) { - console.log('Client: Polling server begin for intervalID = ' + intervalID) - console.log('Client: Requesting ICE Candidates from server') - const request = new XMLHttpRequest() - request.open('GET', url, true) - request.setRequestHeader('Content-Type', 'application/json' ) - request.setRequestHeader('X-Strapp-Type', 'ice-candidate-request') - request.setRequestHeader('X-client-pubkey', pubKey) - request.onreadystatechange = () => { - if (request.status === 200) { - if(request.readyState === 4) { - console.log('Client: Recieved ICE response from Host') - let response = JSON.parse(request.response) - switch(response['iceState']) { - case "a": - cpc.addIceCandidate(new RTCIceCandidate(response.ice)) - break - case "g": /* Gathering so let interval keep polling */ - break - case "c": /* host iceState == Complete, stop bugging it */ - clearInterval(intervalID) - clearTimeout() - break - default: - console.log('Unhandled iceState in requestHostICE()' + response['iceState']) - break - } - } - } - else { - console.log('server unhandled response of status ' + request.status) - clearInterval(intervalID) - } - } - request.send() - } - else { - clearTimeout() - clearInterval(intervalID) - } - }, 5000) -} - -/* Create and send offer -> Send ICE Candidates -> Poll for ICE Candidates */ -getPublicKey().then((cpk) => { - let dataChannel - console.log('Client: Create and send offer') - const cpc = new RTCPeerConnection(conf) - - cpc.oniceconnectionstatechange = () => { - console.log('iceConnectionState = ' + cpc.iceConnectionState) - } - - cpc.onnegotiationneeded = () => { - console.log('negotiation needed!') - cpc.createOffer().then((offer) => { - return cpc.setLocalDescription(offer) - }) - .then(() => { - console.log('Client: Sending offer to host') - let offer = { - cmd: '> sdp pubKey', - sdp: cpc.localDescription, - pubKey: cpk.n - } - console.log(offer) - return requestHostAnswer(window.location, offer) - }) - .then((serverResponse) => { - const answer = JSON.parse(serverResponse) - console.log('Client: Polling for ICE candidates') - requestHostICE(cpc, window.location, cpk.n) - cpc.setRemoteDescription(answer.sdp) - cpc.onicecandidate = (event) => { - if (event.candidate) { - console.log('Client: Sending ice candidate to host') - sendHost(window.location, JSON.stringify({ - cmd: '> ice pubkey', - ice: event.candidate, - pubKey: cpk.n - })) - } - else { - console.log('Client: No more Ice Candidates to send') - } - } - - - }).catch( (err) => { - console.log('error in sdp handshake: ' + err) - }) - } - /* Start data channel, triggers on negotiation needed */ - dataChannel = cpc.createDataChannel("sendChannel"); - - /* Triggered when Host adds track to peer connection */ - cpc.ontrack = (event) => { - let remoteRTPReceivers = cpc.getReceivers() - let hostScreen - let video = document.querySelector('video') - /* TODO: Audio, video, or other track? */ - console.log(remoteRTPReceivers) - console.log(video) - hostScreen = new MediaStream([remoteRTPReceivers[0].track]) - if(!video.srcObject) { - video.srcObject = hostScreen - } - console.log(hostScreen.getVideoTracks()) - console.log(video.srcObject) - video.onloadedmetadata = () => { - video.play() - } - } - - dataChannel.onmessage = (msg) => { - /* Get mediaStream from host and add it to the video */ - let hostMessage = JSON.parse(msg.data) - console.log('Client: Renego') - cpc.setRemoteDescription(hostMessage.sdp).then(() => { - cpc.createAnswer().then((answer) => { - return cpc.setLocalDescription(answer) - }).then(() => { - dataChannel.send(JSON.stringify({ - "cmd": "> screen dataChannel", - "sdp": cpc.localDescription - })) - }) - }) - - - } - dataChannel.onopen = () => { - document.body.innerHTML = (`
`) - } - -}) -document.addEventListener('DOMContentLoaded', () => { - - document.body.innerHTML = `` - -}); +const body = document.createElement('body') +const root = document.createElement('div') +document.title = "Strapp.io Client" +const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] } + +/* TODO: This is duplicated in both client.js and host.js */ +function getPublicKey() { + return new Promise( (resolve, reject) => { + /* Check local storage for public key */ + if (!window.localStorage.getItem('public-key')) { + /* If doesn't exist, generate public and private key pair, store in + local storage */ + crypto.subtle.generateKey( + { name:'RSA-OAEP', + modulusLength: 2048, + publicExponent: new Uint8Array([0x01, 0x00, 0x01]), + hash: {name: "SHA-256"} + }, + true, + ['encrypt', 'decrypt'] + ).then((keyPair) => { + /* TODO: Do we need to store the private key as well? */ + crypto.subtle.exportKey('jwk', keyPair.publicKey) + .then((exportedKey) => { + window.localStorage.setItem('publicKey', exportedKey) + console.log('public key is' + window.localStorage.getItem('publicKey')) + resolve(exportedKey) + }) + + }) + } + else { + resolve(window.localStorage.getItem('publicKey')) + } +}) + +} + +function sendHost(url, data) { + const request = new XMLHttpRequest() + request.open('POST', url, true) + request.setRequestHeader('Content-Type', 'application/json' ) + request.setRequestHeader('X-Strapp-Type', 'ice-candidate-submission') + request.send(data) +} + +/* Poll the server. Send get request, wait for timeout, send another request. +Do this until...? Can be used for either reconnecting or waiting for answer*/ +function requestHostAnswer(url, data) { + return new Promise((resolve, reject) => { + const request = new XMLHttpRequest() + request.open('GET', url, true) + /* But there is no JSON? */ + request.setRequestHeader('Content-Type', 'application/json' ) + request.setRequestHeader('X-Strapp-Type', 'client-sdp-offer') + request.setRequestHeader('X-Strapp-Pubkey', data.pubKey) + request.setRequestHeader('X-Strapp-Offer', JSON.stringify(data.sdp)) + request.onreadystatechange = () => { + if (request.status === 200) { + if(request.readyState === 4) { + console.log('Client: Recieved Answer from Host') + console.log(request) + resolve(request.response) + } + } + else if (request.status === 504) { + console.log('timed out, resending') + resolve(requestHostAnswer(url, data)) + } + else { + reject('server unhandled response of status ' + request.status) + } + } + request.send() + }) +} + +/* Poll server for ice candidates until ice is complete */ +function requestHostICE(cpc, url, pubKey) { + let intervalID = window.setInterval(() => { + if (cpc.iceConnectionState.localeCompare('connected') !== 0 + && cpc.iceConnectionState.localeCompare('completed') !== 0) { + console.log('Client: Polling server begin for intervalID = ' + intervalID) + console.log('Client: Requesting ICE Candidates from server') + const request = new XMLHttpRequest() + request.open('GET', url, true) + request.setRequestHeader('Content-Type', 'application/json' ) + request.setRequestHeader('X-Strapp-Type', 'ice-candidate-request') + request.setRequestHeader('X-client-pubkey', pubKey) + request.onreadystatechange = () => { + if (request.status === 200) { + if(request.readyState === 4) { + console.log('Client: Recieved ICE response from Host') + let response = JSON.parse(request.response) + switch(response['iceState']) { + case "a": + cpc.addIceCandidate(new RTCIceCandidate(response.ice)) + break + case "g": /* Gathering so let interval keep polling */ + break + case "c": /* host iceState == Complete, stop bugging it */ + clearInterval(intervalID) + clearTimeout() + break + default: + console.log('Unhandled iceState in requestHostICE()' + response['iceState']) + break + } + } + } + else { + console.log('server unhandled response of status ' + request.status) + clearInterval(intervalID) + } + } + request.send() + } + else { + clearTimeout() + clearInterval(intervalID) + } + }, 5000) +} + +/* Create and send offer -> Send ICE Candidates -> Poll for ICE Candidates */ +getPublicKey().then((cpk) => { + let dataChannel + console.log('Client: Create and send offer') + const cpc = new RTCPeerConnection(conf) + + cpc.oniceconnectionstatechange = () => { + console.log('iceConnectionState = ' + cpc.iceConnectionState) + } + + cpc.onnegotiationneeded = () => { + console.log('negotiation needed!') + cpc.createOffer().then((offer) => { + return cpc.setLocalDescription(offer) + }) + .then(() => { + console.log('Client: Sending offer to host') + let offer = { + cmd: '> sdp pubKey', + sdp: cpc.localDescription, + pubKey: cpk.n + } + console.log(offer) + return requestHostAnswer(window.location, offer) + }) + .then((serverResponse) => { + const answer = JSON.parse(serverResponse) + console.log('Client: Polling for ICE candidates') + requestHostICE(cpc, window.location, cpk.n) + cpc.setRemoteDescription(answer.sdp) + cpc.onicecandidate = (event) => { + if (event.candidate) { + console.log('Client: Sending ice candidate to host') + sendHost(window.location, JSON.stringify({ + cmd: '> ice pubkey', + ice: event.candidate, + pubKey: cpk.n + })) + } + else { + console.log('Client: No more Ice Candidates to send') + } + } + + + }).catch( (err) => { + console.log('error in sdp handshake: ' + err) + }) + } + /* Start data channel, triggers on negotiation needed */ + dataChannel = cpc.createDataChannel("sendChannel"); + + /* Triggered when Host adds track to peer connection */ + cpc.ontrack = (event) => { + let remoteRTPReceivers = cpc.getReceivers() + let hostScreen + let video = document.querySelector('video') + /* TODO: Audio, video, or other track? */ + console.log(remoteRTPReceivers) + console.log(video) + hostScreen = new MediaStream([remoteRTPReceivers[0].track]) + if(!video.srcObject) { + video.srcObject = hostScreen + } + console.log(hostScreen.getVideoTracks()) + console.log(video.srcObject) + video.onloadedmetadata = () => { + video.play() + } + } + + dataChannel.onmessage = (msg) => { + /* Get mediaStream from host and add it to the video */ + let hostMessage = JSON.parse(msg.data) + console.log('Client: Renego') + cpc.setRemoteDescription(hostMessage.sdp).then(() => { + cpc.createAnswer().then((answer) => { + return cpc.setLocalDescription(answer) + }).then(() => { + dataChannel.send(JSON.stringify({ + "cmd": "> screen dataChannel", + "sdp": cpc.localDescription + })) + }) + }) + + + } + dataChannel.onopen = () => { + document.body.innerHTML = (`
`) + } + +}) +document.addEventListener('DOMContentLoaded', () => { + + document.body.innerHTML = `` + +}); +>>>>>>> 2fa4d84ec6b04eec92a8a1e76f8b33b2a04f3c58 diff --git a/client.js b/client.js index c1956a7..a4b6953 100644 --- a/client.js +++ b/client.js @@ -1,224 +1,220 @@ -const body = document.createElement('body') -const root = document.createElement('div') -document.title = "Strapp.io Client" -const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] } -let dataChannel -let hostScreen - -/* TODO: This is duplicated in both client.js and host.js */ -function getPublicKey() { - return new Promise( (resolve, reject) => { - /* Check local storage for public key */ - if (!window.localStorage.getItem('public-key')) { - /* If doesn't exist, generate public and private key pair, store in - local storage */ - crypto.subtle.generateKey( - { name:'RSA-OAEP', - modulusLength: 2048, - publicExponent: new Uint8Array([0x01, 0x00, 0x01]), - hash: {name: "SHA-256"} - }, - true, - ['encrypt', 'decrypt'] - ).then((keyPair) => { - /* TODO: Do we need to store the private key as well? */ - crypto.subtle.exportKey('jwk', keyPair.publicKey) - .then((exportedKey) => { - window.localStorage.setItem('publicKey', exportedKey) - console.log('public key is' + window.localStorage.getItem('publicKey')) - resolve(exportedKey) - }) - - }) - } - else { - resolve(window.localStorage.getItem('publicKey')) - } -}) - -} - -function sendHost(url, data) { - const request = new XMLHttpRequest() - request.open('POST', url, true) - request.setRequestHeader('Content-Type', 'application/json' ) - request.setRequestHeader('X-Strapp-Type', 'ice-candidate-submission') - request.send(data) -} - -/* Poll the server. Send get request, wait for timeout, send another request. -Do this until...? Can be used for either reconnecting or waiting for answer*/ -function requestHostAnswer(url, data) { - return new Promise((resolve, reject) => { - const request = new XMLHttpRequest() - request.open('GET', url, true) - /* But there is no JSON? */ - request.setRequestHeader('Content-Type', 'application/json' ) - request.setRequestHeader('X-Strapp-Type', 'client-sdp-offer') - request.setRequestHeader('X-Strapp-Pubkey', data.pubKey) - request.onreadystatechange = () => { - if (request.status === 200) { - if(request.readyState === 4) { - console.log('Client: Recieved Answer from Host') - console.log(request) - resolve(request.response) - } - } - else if (request.status === 504) { - console.log('timed out, resending') - resolve(requestHostAnswer(url, data)) - } - else { - reject('server unhandled response of status ' + request.status) - } - } - request.send(data) - }) -} - -/* Poll server for ice candidates until ice is complete */ -function requestHostICE(cpc, url, pubKey) { - let intervalID = window.setInterval(() => { - if (cpc.iceConnectionState.localeCompare('connected') !== 0 - && cpc.iceConnectionState.localeCompare('completed') !== 0) { - console.log('Client: Polling server begin for intervalID = ' + intervalID) - console.log('Client: Requesting ICE Candidates from server') - const request = new XMLHttpRequest() - request.open('GET', url, true) - request.setRequestHeader('Content-Type', 'application/json' ) - request.setRequestHeader('X-Strapp-Type', 'ice-candidate-request') - request.setRequestHeader('X-client-pubkey', pubKey) - request.onreadystatechange = () => { - if (request.status === 200) { - if(request.readyState === 4) { - console.log('Client: Recieved ICE response from Host') - let response = JSON.parse(request.response) - switch(response['iceState']) { - case "a": - cpc.addIceCandidate(new RTCIceCandidate(response.ice)) - break - case "g": /* Gathering so let interval keep polling */ - break - case "c": /* host iceState == Complete, stop bugging it */ - clearInterval(intervalID) - clearTimeout() - break - default: - console.log('Unhandled iceState in requestHostICE()' + response['iceState']) - break - } - } - } - else { - console.log('server unhandled response of status ' + request.status) - clearInterval(intervalID) - } - } - request.send() - } - else { - clearTimeout() - clearInterval(intervalID) - } - }, 5000) -} - -/* Create and send offer -> Send ICE Candidates -> Poll for ICE Candidates */ -getPublicKey().then((cpk) => { - console.log('Client: Create and send offer') - const cpc = new RTCPeerConnection(conf) - - cpc.oniceconnectionstatechange = () => { - console.log('iceConnectionState = ' + cpc.iceConnectionState) - } - - cpc.onnegotiationneeded = () => { - console.log('negotiation needed!') - cpc.createOffer().then((offer) => { - return cpc.setLocalDescription(offer) - }) - .then(() => { - console.log('Client: Sending offer to host') - let offer = { - cmd: '> sdp pubKey', - sdp: cpc.localDescription, - pubKey: cpk.n - } - return requestHostAnswer(window.location, offer) - }) - .then((serverResponse) => { - const answer = JSON.parse(serverResponse) - console.log('Client: Polling for ICE candidates') - requestHostICE(cpc, window.location, cpk.n) - cpc.setRemoteDescription(answer.sdp) - cpc.onicecandidate = (event) => { - if (event.candidate) { - console.log('Client: Sending ice candidate to host') - sendHost(window.location, JSON.stringify({ - cmd: '> ice pubkey', - ice: event.candidate, - pubKey: cpk.n - })) - } - else { - console.log('Client: No more Ice Candidates to send') - } - } - - - }).catch( (err) => { - console.log('error in sdp handshake: ' + err) - }) - } - /* Start data channel, triggers on negotiation needed */ - dataChannel = cpc.createDataChannel("sendChannel"); - - /* Triggered when Host adds track to peer connection */ - cpc.ontrack = (event) => { - console.log(`track event is ${event}`) - let remoteRTPSenders = cpc.getSenders() - let remoteRTPReceivers = cpc.getReceivers() - console.log(remoteRTPReceivers) - /* Add each remoteRTPSenders.track to own stream */ - let video = document.querySelector('video') - video.autoplay = true - console.log(video) - hostScreen = new MediaStream([remoteRTPReceivers[0].track]) - if(!video.srcObject) { - video.srcObject = hostScreen - } - console.log(hostScreen.getVideoTracks()) - console.log(video.srcObject) - video.play() - video.onloadedmetadata = () => { - video.play() - } - } - - dataChannel.onmessage = (msg) => { - /* Get mediaStream from host and add it to the video */ - let hostMessage = JSON.parse(msg.data) - console.log('Client: Renego') - cpc.setRemoteDescription(hostMessage.sdp).then(() => { - cpc.createAnswer().then((answer) => { - return cpc.setLocalDescription(answer) - }).then(() => { - dataChannel.send(JSON.stringify({ - "cmd": "> screen dataChannel", - "sdp": cpc.localDescription - })) - }) - }) - - - } - dataChannel.onopen = () => { - document.body.innerHTML = (`
`) - } - -}) -document.addEventListener('DOMContentLoaded', () => { - - document.body.innerHTML = `` - -}); +const body = document.createElement('body') +const root = document.createElement('div') +document.title = "Strapp.io Client" +const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] } + +/* TODO: This is duplicated in both client.js and host.js */ +function getPublicKey() { + return new Promise( (resolve, reject) => { + /* Check local storage for public key */ + if (!window.localStorage.getItem('public-key')) { + /* If doesn't exist, generate public and private key pair, store in + local storage */ + crypto.subtle.generateKey( + { name:'RSA-OAEP', + modulusLength: 2048, + publicExponent: new Uint8Array([0x01, 0x00, 0x01]), + hash: {name: "SHA-256"} + }, + true, + ['encrypt', 'decrypt'] + ).then((keyPair) => { + /* TODO: Do we need to store the private key as well? */ + crypto.subtle.exportKey('jwk', keyPair.publicKey) + .then((exportedKey) => { + window.localStorage.setItem('publicKey', exportedKey) + console.log('public key is' + window.localStorage.getItem('publicKey')) + resolve(exportedKey) + }) + + }) + } + else { + resolve(window.localStorage.getItem('publicKey')) + } +}) + +} + +function sendHost(url, data) { + const request = new XMLHttpRequest() + request.open('GET', url, true) + request.setRequestHeader('Content-Type', 'application/json' ) + request.setRequestHeader('x-strapp-type', 'ice-candidate-submission') + request.send(data) +} + +/* Poll the server. Send get request, wait for timeout, send another request. +Do this until...? Can be used for either reconnecting or waiting for answer*/ +function requestHostAnswer(url, data) { + return new Promise((resolve, reject) => { + const request = new XMLHttpRequest() + request.open('GET', url, true) + /* But there is no JSON? */ + request.setRequestHeader('Content-Type', 'application/json' ) + request.setRequestHeader('x-strapp-type', 'client-sdp-offer') + request.setRequestHeader('x-strapp-pubkey', data.pubKey) + request.onreadystatechange = () => { + if (request.status === 200) { + if(request.readyState === 4) { + console.log('Client: Recieved Answer from Host') + console.log(request) + resolve(request.response) + } + } + else if (request.status === 504) { + console.log('timed out, resending') + resolve(requestHostAnswer(url, data)) + } + else { + reject('server unhandled response of status ' + request.status) + } + } + request.send(data) + }) +} + +/* Poll server for ice candidates until ice is complete */ +function requestHostICE(cpc, url, pubKey) { + let intervalID = window.setInterval(() => { + if (cpc.iceConnectionState.localeCompare('connected') !== 0 + && cpc.iceConnectionState.localeCompare('completed') !== 0) { + console.log('Client: Polling server begin for intervalID = ' + intervalID) + console.log('Client: Requesting ICE Candidates from server') + const request = new XMLHttpRequest() + request.open('GET', url, true) + request.setRequestHeader('Content-Type', 'application/json' ) + request.setRequestHeader('x-strapp-Type', 'ice-candidate-request') + request.setRequestHeader('x-client-pubkey', pubKey) + request.onreadystatechange = () => { + if (request.status === 200) { + if(request.readyState === 4) { + console.log('Client: Recieved ICE response from Host') + let response = JSON.parse(request.response) + switch(response['iceState']) { + case "a": + cpc.addIceCandidate(new RTCIceCandidate(response.ice)) + break + case "g": /* Gathering so let interval keep polling */ + break + case "c": /* host iceState == Complete, stop bugging it */ + clearInterval(intervalID) + clearTimeout() + break + default: + console.log('Unhandled iceState in requestHostICE()' + response['iceState']) + break + } + } + } + else { + console.log('server unhandled response of status ' + request.status) + clearInterval(intervalID) + } + } + request.send() + } + else { + clearTimeout() + clearInterval(intervalID) + } + }, 5000) +} + +/* Create and send offer -> Send ICE Candidates -> Poll for ICE Candidates */ +getPublicKey().then((cpk) => { + let dataChannel + console.log('Client: Create and send offer') + const cpc = new RTCPeerConnection(conf) + + cpc.oniceconnectionstatechange = () => { + console.log('iceConnectionState = ' + cpc.iceConnectionState) + } + + cpc.onnegotiationneeded = () => { + console.log('negotiation needed!') + cpc.createOffer().then((offer) => { + return cpc.setLocalDescription(offer) + }) + .then(() => { + console.log('Client: Sending offer to host') + let offer = { + cmd: '> sdp pubKey', + sdp: cpc.localDescription, + pubKey: cpk.n + } + return requestHostAnswer(window.location, offer) + }) + .then((serverResponse) => { + const answer = JSON.parse(serverResponse) + console.log('Client: Polling for ICE candidates') + requestHostICE(cpc, window.location, cpk.n) + cpc.setRemoteDescription(answer.sdp) + cpc.onicecandidate = (event) => { + if (event.candidate) { + console.log('Client: Sending ice candidate to host') + sendHost(window.location, JSON.stringify({ + cmd: '> ice pubkey', + ice: event.candidate, + pubKey: cpk.n + })) + } + else { + console.log('Client: No more Ice Candidates to send') + } + } + + + }).catch( (err) => { + console.log('error in sdp handshake: ' + err) + }) + } + /* Start data channel, triggers on negotiation needed */ + dataChannel = cpc.createDataChannel("sendChannel"); + + /* Triggered when Host adds track to peer connection */ + cpc.ontrack = (event) => { + let remoteRTPReceivers = cpc.getReceivers() + let hostScreen + let video = document.querySelector('video') + /* TODO: Audio, video, or other track? */ + console.log(remoteRTPReceivers) + console.log(video) + hostScreen = new MediaStream([remoteRTPReceivers[0].track]) + if(!video.srcObject) { + video.srcObject = hostScreen + } + console.log(hostScreen.getVideoTracks()) + console.log(video.srcObject) + video.onloadedmetadata = () => { + video.play() + } + } + + dataChannel.onmessage = (msg) => { + /* Get mediaStream from host and add it to the video */ + let hostMessage = JSON.parse(msg.data) + console.log('Client: Renego') + cpc.setRemoteDescription(hostMessage.sdp).then(() => { + cpc.createAnswer().then((answer) => { + return cpc.setLocalDescription(answer) + }).then(() => { + dataChannel.send(JSON.stringify({ + "cmd": "> screen dataChannel", + "sdp": cpc.localDescription + })) + }) + }) + + + } + dataChannel.onopen = () => { + document.body.innerHTML = (`
`) + } + +}) +document.addEventListener('DOMContentLoaded', () => { + + document.body.innerHTML = `` + +}); diff --git a/host-test.js b/host-test.js index 25a7b62..ec53c3d 100644 --- a/host-test.js +++ b/host-test.js @@ -1,233 +1,233 @@ -document.title = "Strapp.io Host" - - -const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] } -const clients = new Map([]) -const iceCandidates = [] -let dataChannel -let screenStream /* TODO: Remove if can access localStreams */ -let tracks = [] -let hpk - -/* TODO: duplicate in both client.js and host.jhs */ -function getPublicKey() { - return new Promise( (resolve, reject) => { - /* Check local storage for public key */ - if (!window.localStorage.getItem('public-key')) { - console.log('public key is undefined') - /* If doesn't exist, generate public and private key pair, store in - local storage */ - crypto.subtle.generateKey( - { name:'RSA-OAEP', - modulusLength: 2048, - publicExponent: new Uint8Array([0x01, 0x00, 0x01]), - hash: {name: "SHA-256"} - }, - true, - ['encrypt', 'decrypt'] - ).then((keyPair) => { - /* TODO: Do we need to store the private key as well? */ - crypto.subtle.exportKey('jwk', keyPair.publicKey) - .then((exportedKey) => { - window.localStorage.setItem('publicKey', exportedKey) - console.log('public key is' + window.localStorage.getItem('publicKey')) - resolve(exportedKey) - }) - - }) - } - else { - resolve(window.localStorage.getItem('publicKey')) - } - }) -} - -function sendClientICE(clientPubKey, hostPubKey, iceCandidate) { - console.log('Host: Allocating ice candidate for client') - console.log(iceCandidate) - iceCandidates.push(JSON.stringify({ - cmd: "< ice pubKey", - ice: iceCandidate, - hostPubKey: hostPubKey, /* TODO: do we need to send this? */ - clientPubKey: clientPubKey, - iceState: "a" - })) -} - -function handleNewClientConnection(pubKey, sdpOffer) { - /* New Client Connection*/ - hpc = new RTCPeerConnection(conf) - console.log('handleNewClientConnections()') - console.log(sdpOffer) - clients.set(pubKey, hpc) - console.log(pubKey) - hpc.setRemoteDescription(sdpOffer) - .then(() => { - hpc.createAnswer().then((answer) => { - return hpc.setLocalDescription(answer) - }) - .then(() => { - - hpc.onicecandidate = (event) => { - if (event.candidate) { - sendClientICE(pubKey, hpk.n, event.candidate) - } - else { - console.log('Host: Finished allocating ICE candidates') - } - } - console.log('Host: Sending answer to Client ') - wsock.send(JSON.stringify({ - cmd: '< sdp pubKey', - sdp: hpc.localDescription, - hostPubKey: hpk.n, - clientPubKey: pubKey - })) - - }).catch((err) => { - console.log(`error in host answer ${err}`) - }) - }).catch((error) => { - console.log(`error in setRemoteDescription ${error}`) - }) - hpc.oniceconnectionstatechange = () => { - console.log('iceConnectionState = ' + hpc.iceConnectionState) - } - hpc.ondatachannel = (evt) => { - dataChannel = evt.channel - dataChannel.onmessage = (msg) => { - let clientMessage = JSON.parse(msg.data) - console.log(`client message is ${clientMessage}`) - hpc.setRemoteDescription(clientMessage.sdp).then(() => { - console.log('should be streaming now') - }) - } - dataChannel.onopen = () => { - /* If !screenStream, gUM */ - screenStream.getTracks().forEach( (track) => { - hpc.addTrack(track, screenStream) - }) - console.log(hpc.getSenders()) - /* Create offer */ - hpc.createOffer().then((offer) => { - return hpc.setLocalDescription(offer) - }).then( () => { - dataChannel.send(JSON.stringify({ - "cmd": "< screen dataChannel", - "sdp": hpc.localDescription, - "pubKey": hpc['hpk'].n - })) - }) - } - } - hpc.onnegotiationneeded = () => { - console.log('negotiation needed') - } - -} - -function handleNewIceCandidate(msg) { - console.log('Host: Adding new ice candidate') - const hpc = clients.get(msg.pubKey) - let candidate = new RTCIceCandidate(msg.ice) - hpc.addIceCandidate(candidate) -} - -function handleIceRequest(msg) { - console.log('Host: Handling ice candidate request') - console.log(iceCandidates) - const hpc = clients.get(msg.pubKey) - const iceCandidate = iceCandidates.pop() - if (iceCandidate !== undefined) { - wsock.send(iceCandidate) - } else { - if (hpc.iceGatheringState.localeCompare('gathering') === 0) { - wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "g"}`) - } - else if (hpc.iceGatheringState.localeCompare('complete') === 0) { - wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "c"}`) - } - - } - -} -if ("WebSocket" in window) { - document.addEventListener('DOMContentLoaded', (event) => { - document.body.innerHTML = '
Choose options for client
' - navigator.mediaDevices.getUserMedia({ - video : { mediaSource: "screen", - width: {max: '1920'}, - height: {max: '1080'}, - frameRate: {max: '10'}} }) - .then(function(mediaStream) { - let video = document.querySelector('video') - screenStream = mediaStream - console.log(mediaStream) - video.srcObject = mediaStream - console.log('Grabbed media') - video.onloadedmetadata = function(e) { - console.log(e) - video.play() - } - }) - .catch(function(err) { - document.body.innerHTML = 'Help me help you. Reload the page and allow screen sharing!' - console.log(err); - }); // always check for errors at the end. - - getPublicKey() - .then((hpkVal) => { - hpk = hpkVal - }) - wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) - wsock.onopen = () => { - console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) - } - - wsock.onmessage = (serverMsg) => { - /* msg is either offer or ice candidate or ice candidate request*/ - - /* What if data null? */ - console.log(`serverMsg = ${serverMsg.data}`) - - let msg = serverMsg.data.split(' ') - let clientID = msg[0] - let type = msg[1] - let data = JSON.parse(msg.splice(2).join()) - - if (clients.has(clientID)) { - switch (type) { - case 'ice-candidate-submission': - handleNewIceCandidate(msg) - break - case 'ice-candidate-request': - handleIceRequest(msg) - break - case 'client-sdp-offer': - //repeatedoffer shouldnt happen here - } - } - else { - switch (type) { - case 'client-sdp-offer': - handleNewClientConnection(clientID, data) - break - case 'ice-candidate-request': - handleIceRequest() - break - case 'ice-candidate-submission': - handleNewIceCandidate - break - } - } - } - - - - }) -} -else { - document.addEventListener('DOMContentLoaded', () => { - document.body.innerHTML = 'Websockets not supported in your browser' - }) -} +document.title = "Strapp.io Host" + + +const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] } +const clients = new Map([]) +const iceCandidates = [] +let dataChannel +let screenStream /* TODO: Remove if can access localStreams */ +let tracks = [] +let hpk + +/* TODO: duplicate in both client.js and host.jhs */ +function getPublicKey() { + return new Promise( (resolve, reject) => { + /* Check local storage for public key */ + if (!window.localStorage.getItem('public-key')) { + console.log('public key is undefined') + /* If doesn't exist, generate public and private key pair, store in + local storage */ + crypto.subtle.generateKey( + { name:'RSA-OAEP', + modulusLength: 2048, + publicExponent: new Uint8Array([0x01, 0x00, 0x01]), + hash: {name: "SHA-256"} + }, + true, + ['encrypt', 'decrypt'] + ).then((keyPair) => { + /* TODO: Do we need to store the private key as well? */ + crypto.subtle.exportKey('jwk', keyPair.publicKey) + .then((exportedKey) => { + window.localStorage.setItem('publicKey', exportedKey) + console.log('public key is' + window.localStorage.getItem('publicKey')) + resolve(exportedKey) + }) + + }) + } + else { + resolve(window.localStorage.getItem('publicKey')) + } + }) +} + +function sendClientICE(clientPubKey, hostPubKey, iceCandidate) { + console.log('Host: Allocating ice candidate for client') + console.log(iceCandidate) + iceCandidates.push(JSON.stringify({ + cmd: "< ice pubKey", + ice: iceCandidate, + hostPubKey: hostPubKey, /* TODO: do we need to send this? */ + clientPubKey: clientPubKey, + iceState: "a" + })) +} + +function handleNewClientConnection(pubKey, sdpOffer) { + /* New Client Connection*/ + hpc = new RTCPeerConnection(conf) + console.log('handleNewClientConnections()') + console.log(sdpOffer) + clients.set(pubKey, hpc) + console.log(pubKey) + hpc.setRemoteDescription(sdpOffer) + .then(() => { + hpc.createAnswer().then((answer) => { + return hpc.setLocalDescription(answer) + }) + .then(() => { + + hpc.onicecandidate = (event) => { + if (event.candidate) { + sendClientICE(pubKey, hpk.n, event.candidate) + } + else { + console.log('Host: Finished allocating ICE candidates') + } + } + console.log('Host: Sending answer to Client ') + wsock.send(JSON.stringify({ + cmd: '< sdp pubKey', + sdp: hpc.localDescription, + hostPubKey: hpk.n, + clientPubKey: pubKey + })) + + }).catch((err) => { + console.log(`error in host answer ${err}`) + }) + }).catch((error) => { + console.log(`error in setRemoteDescription ${error}`) + }) + hpc.oniceconnectionstatechange = () => { + console.log('iceConnectionState = ' + hpc.iceConnectionState) + } + hpc.ondatachannel = (evt) => { + dataChannel = evt.channel + dataChannel.onmessage = (msg) => { + let clientMessage = JSON.parse(msg.data) + console.log(`client message is ${clientMessage}`) + hpc.setRemoteDescription(clientMessage.sdp).then(() => { + console.log('should be streaming now') + }) + } + dataChannel.onopen = () => { + /* If !screenStream, gUM */ + screenStream.getTracks().forEach( (track) => { + hpc.addTrack(track, screenStream) + }) + console.log(hpc.getSenders()) + /* Create offer */ + hpc.createOffer().then((offer) => { + return hpc.setLocalDescription(offer) + }).then( () => { + dataChannel.send(JSON.stringify({ + "cmd": "< screen dataChannel", + "sdp": hpc.localDescription, + "pubKey": hpc['hpk'].n + })) + }) + } + } + hpc.onnegotiationneeded = () => { + console.log('negotiation needed') + } + +} + +function handleNewIceCandidate(msg) { + console.log('Host: Adding new ice candidate') + const hpc = clients.get(msg.pubKey) + let candidate = new RTCIceCandidate(msg.ice) + hpc.addIceCandidate(candidate) +} + +function handleIceRequest(msg) { + console.log('Host: Handling ice candidate request') + console.log(iceCandidates) + const hpc = clients.get(msg.pubKey) + const iceCandidate = iceCandidates.pop() + if (iceCandidate !== undefined) { + wsock.send(iceCandidate) + } else { + if (hpc.iceGatheringState.localeCompare('gathering') === 0) { + wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "g"}`) + } + else if (hpc.iceGatheringState.localeCompare('complete') === 0) { + wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "c"}`) + } + + } + +} +if ("WebSocket" in window) { + document.addEventListener('DOMContentLoaded', (event) => { + document.body.innerHTML = '
Choose options for client
' + navigator.mediaDevices.getUserMedia({ + video : { mediaSource: "screen", + width: {max: '1920'}, + height: {max: '1080'}, + frameRate: {max: '10'}} }) + .then(function(mediaStream) { + let video = document.querySelector('video') + screenStream = mediaStream + console.log(mediaStream) + video.srcObject = mediaStream + console.log('Grabbed media') + video.onloadedmetadata = function(e) { + console.log(e) + video.play() + } + }) + .catch(function(err) { + document.body.innerHTML = 'Help me help you. Reload the page and allow screen sharing!' + console.log(err); + }); // always check for errors at the end. + + getPublicKey() + .then((hpkVal) => { + hpk = hpkVal + }) + wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) + wsock.onopen = () => { + console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) + } + + wsock.onmessage = (serverMsg) => { + /* msg is either offer or ice candidate or ice candidate request*/ + + /* What if data null? */ + console.log(`serverMsg = ${serverMsg.data}`) + + let msg = serverMsg.data.split(' ') + let clientID = msg[0] + let type = msg[1] + let data = JSON.parse(msg.slice(2).join(' ')) + + if (clients.has(clientID)) { + switch (type) { + case 'ice-candidate-submission': + handleNewIceCandidate(msg) + break + case 'ice-candidate-request': + handleIceRequest(msg) + break + case 'client-sdp-offer': + //repeatedoffer shouldnt happen here + } + } + else { + switch (type) { + case 'client-sdp-offer': + handleNewClientConnection(clientID, data) + break + case 'ice-candidate-request': + handleIceRequest() + break + case 'ice-candidate-submission': + handleNewIceCandidate + break + } + } + } + + + + }) +} +else { + document.addEventListener('DOMContentLoaded', () => { + document.body.innerHTML = 'Websockets not supported in your browser' + }) +} diff --git a/host.js b/host.js index 811c467..1e47380 100644 --- a/host.js +++ b/host.js @@ -59,6 +59,7 @@ function handleNewClientConnection(offer) { hpc = new RTCPeerConnection(conf) //console.log(offer) clients.set(offer.pubKey, hpc) + console.log(offer.sdp) hpc.setRemoteDescription(offer.sdp) .then(() => { hpc.createAnswer().then((answer) => { diff --git a/opts.js b/opts.js index 29a378a..d930e79 100644 --- a/opts.js +++ b/opts.js @@ -41,8 +41,8 @@ exports = require('minimist')(process.argv.slice(2), { */ exports['defaults'] = { config: '/etc/strapp.conf:~/.strapp/strapp.conf:./strapp.conf', - 'client-js': './client.js', - 'host-js': './host.js', + 'client-js': './client-test.js', + 'host-js': './host-test.js', tls: true, 'ca-cert': '../certs/cert.pem', 'ca-key': '../certs/key.pem', diff --git a/router.js b/router.js index 8fe5235..2085e9b 100644 --- a/router.js +++ b/router.js @@ -33,7 +33,7 @@ exports = { httpdRoot: undefined, bindJail: undefined, - /** @func + /** @func * @summary Start main HTTP server * @desc starts up an HTTP or HTTPS server used for routing * @arg {Object} conf - object containing configuration properties @@ -116,7 +116,7 @@ exports = { throw new Error(`${path} not found, ${routeName} not bound`) }, - /** @func + /** @func * @summary Router * @desc listens for http client requests and services routes/files * @arg {http.ClientRequest} request @@ -140,8 +140,8 @@ exports = { // that isn't just "give you a route if it's available" on visit /* else if (route.origin == (request.headers['x-forwarded-for'] || request.connection.remoteAddress)) - this.serveHost(response, route, htArgv) - else */ + this.serveHost(response, route, htArgv) */ + else this.serveClient(request, response, route) } /* If it's a valid routename that doesn't exist, make this client a host */ @@ -211,7 +211,7 @@ exports = { this.serveFile(response, bind.path) }, - /** @func + /** @func * @summary Serve a route to an http client * @desc routes may be bound to the filesystem, or to an outgoing host * @arg {http.ClientRequest} request - request from the client @@ -221,7 +221,7 @@ exports = { serveClient: function (request, response, route) { const type = request.headers['x-strapp-type'] const pubKey = request.headers['x-strapp-pubkey'] - dlog(`Client ${type || 'HT'} request routed to ${route.name}`) + dlog(`Client ${type || 'HT GET'} request routed to ${route.name}`) switch (type) { case null: case undefined: @@ -234,9 +234,11 @@ exports = { case 'client-sdp-offer': let data = '' if (pubKey) { + let data = request.headers['x-strapp-offer'] route.pendingResponses.addResponse(pubKey, response) - request.on('data', (chunk) => data += chunk) - request.on('end', () => route.socket.send(`${pubKey} ${type} ${data}`)) + dlog(`${route.origin}=>\n${pubKey}\n${type}`) + dlog(JSON.parse(data)) + route.socket.send(`${pubKey} ${type} ${data}`) } else { response.writeHead(401) @@ -249,7 +251,7 @@ exports = { } }, - /** @func + /** @func * @summary Create a new route for a host * @desc makes a new route for the given route name * @arg {string} routeName - name of the new route @@ -284,13 +286,14 @@ exports = { }) route.pendingResponses.addResponse = function (key, response_p) { let responses = this.get(key) || [] - this.set(key, responses.push(response_p)) + responses.push(response_p) + this.set(key, responses) } this.routes[routeName] = route this.serveHost(response, route, argv) }, - /** @func + /** @Func * @summary Serve a route to an authorized http host * @desc services host application to the client, establishing a socket * @arg {http.ServerResponse} response - response object to use @@ -335,7 +338,7 @@ exports = { * @arg {Object} route - the route over */ hostMessage: function (message, route) { - const argv = message.split(' ') + let argv = message.split(' ') const command = argv[0][0] argv = argv.slice(1) dlog(`Received host message from ${route.name}: ${command}`) @@ -379,7 +382,7 @@ exports = { } }, - /** @func + /** @func * @summary Serve a file to an http client after a request * @desc reads files from the system to be distributed to clients, and * buffers recently accessed files @@ -401,8 +404,8 @@ exports = { response.end() }) }, - - /** @func + + /** @func * @summary Synchronize Reading Multiple Files * @desc reads an array of files into an object, whose keys are the * input filenames, and values are the data read diff --git a/www/Thumbs.db b/www/Thumbs.db deleted file mode 100644 index fa4a059..0000000 Binary files a/www/Thumbs.db and /dev/null differ diff --git a/www/index.html b/www/index.html new file mode 100644 index 0000000..ba9672b --- /dev/null +++ b/www/index.html @@ -0,0 +1,5 @@ + + + Strapp.io /www index +

Index

+