need to do ICE step
[henge/kiak.git] / host.js
1 document.title = "Strapp.io Host"
2 const conf = {"iceServers": [{ "url": "stun:stun.1.google.com:19302" }] }
3 const clients = new Map([])
4 const hpk = getPublicKey()
5
6 /* TODO: duplicate in both client.js and host.jhs */
7 function getPublicKey() {
8 return new Promise( (resolve, reject) => {
9 /* Check local storage for public key */
10 if (!window.localStorage.getItem('public-key')) {
11 console.log('public key is undefined')
12 /* If doesn't exist, generate public and private key pair, store in
13 local storage */
14 crypto.subtle.generateKey(
15 { name:'RSA-OAEP',
16 modulusLength: 2048,
17 publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
18 hash: {name: "SHA-256"}
19 },
20 true,
21 ['encrypt', 'decrypt']
22 ).then((keyPair) => {
23 /* TODO: Do we need to store the private key as well? */
24 crypto.subtle.exportKey('jwk', keyPair.publicKey)
25 .then((exportedKey) => {
26 window.localStorage.setItem('publicKey', exportedKey)
27 console.log('public key is' + window.localStorage.getItem('publicKey'))
28 resolve(exportedKey)
29 })
30
31 })
32 }
33 else {
34 resolve(window.localStorage.getItem('publicKey'))
35 }
36 })
37 }
38
39
40 function handleNewClientConnection(offer) {
41 /* New Client Connection*/
42 hpc = new RTCPeerConnection(conf)
43 hpc.setRemoteDescription(offer.sdp)
44 .then(() => {
45 hpc.createAnswer().then((answer) => {
46 return hpc.setLocalDescription(answer)
47 })
48 .then(() => {
49 hpk.then(() => {
50 hpc.onicecandidate = (event) => {
51 if (event.candidate) {
52 console.log('Host: Sending ice candidate to client')
53 wsock.send(JSON.stringify({
54 cmd: '< ice pubKey',
55 ice: event.candidate,
56 pubKey: hpk /* TODO: do we need to send this? */
57 }))
58 }
59 else {
60 console.log('Host: Finished setting up ICE candidates')
61 }
62 }
63 console.log('Host: Sending answer to Host')
64 wsock.send(JSON.stringify({
65 cmd: '< sdp pubKey',
66 sdp: hpc.localDescription,
67 pubKey: hpk
68 }))
69 clients.set(offer.pubKey, {
70 hostsdp: hpc.localDescription,
71 clientsdp: hpc.remoteDescription
72 })
73
74 })
75 }).catch((err) => {
76 console.log(`error in host answer ${err}`)
77 })
78 })
79 }
80
81 function handleNewIceCandidate(msg) {
82 const hpc = clients.get(msg.pubKey)
83 let candidate = new RTCIceCandidate(msg.ice)
84 hpc.addIceCandidate(candidate)
85 }
86
87 if ("WebSocket" in window) {
88 document.addEventListener('DOMContentLoaded', (event) => {
89 wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
90 wsock.onopen = () => {
91 console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
92 }
93
94 wsock.onmessage = (serverMsg) => {
95 /* msg is either offer or ice candidate */
96 console.log(serverMsg.data)
97 let msg = JSON.parse(serverMsg.data)
98
99 const clientID = msg.pubKey
100 const msgType = msg.hasOwnProperty('sdp') ? 'o' : 'i'
101 if (clients.has(clientID)) {
102 switch(msgType) {
103 case 'o':
104 console.log('client exist && sending an offer == error')
105 break
106 case 'i':
107 handleNewIceCandidate(msg)
108 break
109 }
110 }
111 else {
112 switch(msgType) {
113 case 'o':
114 handleNewClientConnection(msg)
115 break
116 case 'i':
117 console.log('client !exist && ice candidate === error')
118 break
119 }
120 }
121 }
122 })
123 }
124 else {
125 document.addEventListener('DOMContentLoaded', () => {
126 document.body.innerHTML = 'Websockets not supported in your browser'
127 })
128 }