-
Notifications
You must be signed in to change notification settings - Fork 0
/
fshare.html
108 lines (95 loc) · 3.74 KB
/
fshare.html
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
<html><head>
<title>Share my browser</title></head>
<!--meta name="viewport" content="width=device-width"-->
<script src="https://pi.pe/iot/js/sha256.js"></script>
<script src="https://pi.pe/iot/js/jquery-1.8.3.js"></script>
<script src="https://pi.pe/iot/js/phono.sdp.js"></script>
<script src="https://pi.pe/iot/js/pipeDb.js"></script>
<script src="https://pi.pe/iot/js/pipeDuct.js"></script>
<script src="https://pi.pe/iot/js/qrcode.js"></script>
<script type="text/javascript">
var id = undefined;
var duct = null;
var pipe;
var sha256 = Sha256;
function createHexString(arr) {
var result = "";
var z;
for (var i = 0; i < arr.length; i++) {
var str = arr[i].toString(16);
z = 2 - str.length + 1;
str = Array(z).join("0") + str;
result += str;
}
return result.toUpperCase();
}
function mkNonce() {
var buf = new Uint8Array(16);
window.crypto.getRandomValues(buf);
var ret = createHexString(buf);
return ret;
}
function gotId(liid) {
id = liid;
duct = new PipeDuct(id);
duct.setOnDataChannel(onNewDc);
var myid = id + ":" + mkNonce();
console.log("mk qr of " + myid);
var qrcode = new QRCode(document.getElementById("qr"), {
width: 400,
height: 400,
correctLevel: QRCode.CorrectLevel.M
});
duct.connect().then(function (d) {
console.log("connected as " + id);
qrcode.makeCode(myid);
});
}
function onDcMessage(evt) {
//console.log(evt.data);
var req = JSON.parse(evt.data);
if (req.action === "list") {
// hack - we should be doing a load of checks first
duct.addRemote(null, function() {
var rep = {status: "ok"};
pipe.send(JSON.stringify(rep));
$('#result').html("<H3>Added friend's phone...</H3>");
});
}
if (req.action === "introduce") {
//{id: fing, action: "introduce", cert: cert};
var printncert = {finger: req.id, cert: req.cert, tag: req.tag};
dcb = function() {
var rep = {status: "ok"};
console.log("added friend print" + req.id);
pipe.send(JSON.stringify(rep));
$('#result').html("<H3>Introducing you to your friends smartphone .</H3>");
}
console.log("adding friend print" + req.id);
$('#result').html("<H3>Adding friend's device...</H3>");
PipeDb.dbAddPrint(printncert, dcb);
}
if (req.action === "accepted") {
$('#result').html("<H3>Secure channel to lending device set up</H3>");
pipe.close();
}
}
function onNewDc(channel) {
console.log("New DC " + channel.label);
$('#qr').hide();
$('#result').html("<H3>Scan ok - connecting to friend's phone...</H3>");
channel.onmessage = onDcMessage;
pipe = channel;
}
$(document).ready(function() {
PipeDb.whoAmI(gotId, function(err) {
console.log("could not create identity " + err)
});
});
</script>
<h1>Share this device by scanning it with another.</h1>
<div id="qr" class="qr">
</div>
<div id="result"> Get scanned by a friend's smartphone to join their network.</div>
</body>
</html>