-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.html
More file actions
255 lines (219 loc) · 7.12 KB
/
client.html
File metadata and controls
255 lines (219 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<html><head>
<style>
* {font-family: "Lucida Console", Monaco, monospace; font-size: 14px;}
input { font-size: 12px; line-height: 14px;}
textarea {width:500px;height:300px;}
div, form { display: none; }
</style>
<script src="https://crypto.stanford.edu/sjcl/sjcl.js"></script>
<title>bitty chat</title>
</head>
<body>
<div id="about"><a href="http://timbauman.github.io/bittychat/">about bitty chat</a></div>
<div id="starting"><button id="new_button">new chat</button> or <input id="remote_code" type="text" placeholder="insert code here" /> <button id="load_button">load existing chat</button></div>
<div id="sharing">
share this URL <input type="text" disabled="disabled" id="url" placeholder="loading..." />
or this code <input type="text" disabled="disabled" id="code" placeholder="loading..." /><br />
</div>
<div id="connecting">waiting for connection...</div>
<form id="chatform">
message: <input type="text" id="message" /><button id="sendButton" type="submit">Send</button><br />
<textarea id="chatbox" disabled="disabled"></textarea>
</form>
<div id="closed">connection lost...</div>
<script>
/* config */
/* brokers chat connections - untrusted */
var URL = 'http://localhost:8000/';
/* stun server */
var servers = {
iceServers: [{ 'url': 'stun:stun.l.google.com:19302' }]
};
/* WebRTC compatability */
var RTCPeerConnection = null;
var getUserMedia = null;
var attachMediaStream = null;
var reattachMediaStream = null;
var webrtcDetectedBrowser = null;
if (navigator.mozGetUserMedia) {
webrtcDetectedBrowser = "firefox";
RTCPeerConnection = mozRTCPeerConnection;
RTCSessionDescription = mozRTCSessionDescription;
RTCIceCandidate = mozRTCIceCandidate;
} else if (navigator.webkitGetUserMedia) {
webrtcDetectedBrowser = "chrome";
RTCPeerConnection = webkitRTCPeerConnection;
} else {
document.body.innerHTML = "your browser doesn't appear to support WebRTC...";
}
performance.now = performance.now || performance.webkitNow;
/* the code */
var options = {
optional: [{ RtpDataChannels: true }]
};
var description = null;
var cands = [];
var sendButton = document.getElementById('sendButton');
var form = document.getElementById('chatform');
var startDiv = document.getElementById('starting');
var sharingDiv = document.getElementById('sharing');
var connectingDiv = document.getElementById('connecting');
document.getElementById('about').style.display = 'block';
var channel;
sendButton.disabled = true;
var k='';
var id='';
var d={};
var pc1 = new RTCPeerConnection(servers, options);
pc1.onicecandidate = function(event) {
if (event.candidate) {
cands.push(event.candidate);
}
};
channel = pc1.createDataChannel("sendDataChannel",
{reliable: false});
channel.onmessage = onReceiveMessageCallback;
channel.onopen = onOpen;
channel.onclose = onClose;
form.onsubmit = send;
if (k === '') {
/* set url*/
startDiv.style.display = 'block';
} else {
connectingDiv.style.display = 'block';
receiveOffer(d);
}
document.getElementById('new_button').onclick = function() {
generateKeys();
pc1.createOffer(createdDesc); /* make offer */
startDiv.style.display = 'none';
sharingDiv.style.display = 'block';
};
document.getElementById('load_button').onclick = function() {
d = JSON.parse(document.getElementById('remote_code').value);
k = d.k;
id = d.id;
receiveOffer(d.d);
};
function generateKeys() {
if (!pc1.localDescription) {
/* not sure if this is secure but it keeps shuffling the key until it gets an offer */
id = sjcl.random.randomWords(1,9)[0].toString();
}
}
function receiveOffer(d) {
console.log(d);
if (d.desc && d.peers) {
pc1.setRemoteDescription(new RTCSessionDescription(d.desc));
var peers = d.peers;
console.log(peers);
for (var i = 0; i < peers.length; i++) {
var peer = peers[i];
var cand = new RTCIceCandidate(peer);
pc1.addIceCandidate(cand);
}
sharingDiv.style.display = 'none';
startDiv.style.display = 'none';
connectingDiv.style.display = 'block';
}
if (!pc1.localDescription) {
/* we must respond */
pc1.createAnswer(createdDesc);
console.log('responding with answer');
}
}
function send() {
var msg = document.getElementById('message');
var m = msg.value;
channel.send(m);
console.log(m);
document.getElementById('chatbox').value += 'me: ' + m + '\n';
msg.value = '';
msg.focus();
return false;
}
window.onbeforeunload = function() {
channel.close();
pc1.close();
pc1 = null;
sendButton.disabled = true;
dataChannelSend.value = "";
dataChannelReceive.value = "";
};
function createdDesc(desc) {
pc1.setLocalDescription(desc);
if (cands.length == 0) {
setTimeout(function() { createdDesc(desc); }, 2000);
return;
}
console.log('description');
var j = JSON.stringify({desc: desc, peers: cands});
console.log(pc1.remoteDescription);
if (pc1.remoteDescription) {
var offer = sjcl.encrypt(k, j + JSON.stringify(sjcl.hash.sha256.hash(j)));
console.log(offer);
sendOffer(offer);
} else {
/* set the key! */
m = (/AES_CM_128_HMAC_SHA1_80 inline:(\S+)/).exec(desc.sdp);
k = m[1];
/* The ([^)]) prevents us from matching the regular expressions */
document.getElementById('url').value =
'data:text/html;,'
+ document.documentElement.innerHTML
.replace(/([^)])k=(''|"")(\W)/, "$1k='" + k + "'$3")
.replace(/([^)])id=(''|"")(\W)/, "$1id='" + id + "'$3")
.replace(/([^)])d={}(\W)/, "$1d=" + j + "$2");
document.getElementById('code').value = JSON.stringify({k:k,id:id,d:{desc: desc, peers: cands}});
/* begin polling for offers */
listenForOffers();
}
}
function sendOffer(offer) {
var req = new XMLHttpRequest();
req.open("get", URL + '?k=' + encodeURIComponent(id) + '&v=' + encodeURIComponent(offer), true);
req.send();
connectingDiv.style.display = 'block';
}
function heardOffer(data) {
if (data && data.trim()) {
var decrypted = sjcl.decrypt(k, data);
hash = JSON.parse(decrypted.substring(decrypted.lastIndexOf('[')));
j = decrypted.substring(0, decrypted.lastIndexOf('['));
if (hash.toString() === sjcl.hash.sha256.hash(j).toString()) {
o = JSON.parse(j);
receiveOffer(o);
}
}
}
function listenForOffers() {
/* this may take many forms! perhaps in the future we will use something kewler than json */
if (!pc1.remoteDescription) {
connectingDiv.style.display = 'block';
var req = new XMLHttpRequest();
req.onload = function(e) {
heardOffer(req.responseText);
};
req.open("get", URL + '?k=' + encodeURIComponent(id), true);
req.send();
setTimeout(listenForOffers, 2000);
}
}
function onReceiveMessageCallback(event) {
document.getElementById('chatbox').value += 'them: ' + event.data + '\n';
}
function onOpen() {
var readyState = channel.readyState;
sendButton.disabled = readyState !== "open";
if (!sendButton.disabled) {
document.title += ' - ' + id;
form.style.display = 'block';
connectingDiv.style.display = 'none';
}
}
function onClose() {
form.style.display = 'none';
document.getElementById('closed').style.display = 'block';
}
</script>
</body></html>