-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.html
116 lines (105 loc) · 4.4 KB
/
index.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
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="PeerFast is the first P2P Internet Speed Test">
<meta name="keywords" content="peer,peer-to-peer,speedtest,speed,test,internet,fast.com,speedtest.net,p2p">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/g/normalize@3,skeleton@2">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/fontawesome/4/css/font-awesome.min.css">
<script src="https://cdn.jsdelivr.net/npm/webtorrent@0/webtorrent.min.js"></script>
<title>PeerFast - P2P Fast.com Clone</title>
<style>
@media (prefers-color-scheme: dark) {
html {
background-color: #222;
}
body,
.button {
color: #ddd;
}
}
</style>
</head>
<body style="text-align: center">
<header style="margin: 20px 0 40px">
<h1 style="margin-bottom: -10px">PeerFast</h1>
<small>P2P Fast.com Clone</small>
</header>
<h2 id="starting">Test is starting</h2>
<div id="counters" style="visibility: hidden; display: none">
<h3>Speed in bytes: <small id="speedBytes"></small></h3>
<h3>Speed in bits: <small id="speedBits"></small></h3>
<h4 id="reload">Please wait while the test finishes</h4>
</div>
<footer style="margin-top: 50px">
<a class="button" href="https://github.com/DiegoRBaquero/PeerFast/issues" target="_blank"><i class="fa fa-github"></i> Suggest a feature / Report a bug <i class="fa fa-comment"></i></a><br>
<b>PeerFast is an Open-Source project by <a href="https://diegorbaquero.com" target="_blank">DiegoRBaquero</a></b><br>
Powered by <a href="https://github.com/webtorrent/webtorrent" target="_blank">WebTorrent</a><br>
</footer>
<script>
var client = new WebTorrent()
var maxSpeed = 0
var speedBytes = document.querySelector('#speedBytes')
var speedBits = document.querySelector('#speedBits')
var counters = document.querySelector('#counters')
var starting = document.querySelector('#starting')
var updateInteval
var stopTimeout
var speeds = []
// Sintel, a free, Creative Commons movie
var torrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&dn=sintel.mp4&tr=wss://tracker.btorrent.xyz&tr=wss://tracker.openwebtorrent.com&ws=https://webtorrent.io/torrents/sintel-1024-surround.mp4&ws=https://webseed.btorrent.xyz/sintel-1024-surround.mp4'
client.add(torrentId, function (torrent) {
counters.removeAttribute('style')
starting.parentNode.removeChild(starting)
var updateSpeed = function () {
var speed = torrent.downloadSpeed
speeds.push(speed)
if(speeds.length > 50) {
var shouldStop = true
for(i = speeds.length - 50; i < speeds.length; i++) {
if (speeds[i] >= maxSpeed) {
shouldStop = false
break
}
}
if (shouldStop) {
console.debug('No faster speed in last 5 seconds')
return stop()
}
}
maxSpeed = Math.max(maxSpeed, speed)
speedBytes.innerText = prettyBytes(maxSpeed)
speedBits.innerText = prettyBytes(maxSpeed*8, true)
}
updateInteval = setInterval(updateSpeed, 100)
})
stopTimeout = setTimeout(stop, 30000)
function stop () {
console.debug("Destroying everything")
clearInterval(updateInteval)
clearTimeout(stopTimeout)
client.torrents[0].destroy(function () {
client.destroy(function (err) {
if (err) console.error(err)
})
})
document.querySelector('#reload').innerHTML = '<a href="#reload" class="button button-primary" onclick="location.reload()">Test again</a>'
}
function prettyBytes (num, bits) {
var exponent, unit, units
if (isNaN(num)) {
return ''
}
if (bits) units = [ 'b', 'kb', 'Mb', 'Gb', 'Tb' ]
else units = [ 'B', 'kB', 'MB', 'GB', 'TB' ]
if (num < 1) {
return ''
}
exponent = Math.min(Math.floor(Math.log(num) / 6.907755278982137), 8)
num = (num / Math.pow(1000, exponent)).toFixed(1) * 1
unit = units[ exponent ]
return num +' ' + unit + (bits ? 'ps' : '/s')
}
</script>
</body>
</html>