ui.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="shortcut icon" href="favicon.ico">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no" />
  6. <meta charset="UTF-8" />
  7. <script type="text/javascript" src="speedtest.js"></script>
  8. <script type="text/javascript">
  9. function I(i){return document.getElementById(i);}
  10. //LIST OF TEST SERVERS. See documentation for details if needed
  11. <?php
  12. $mode=getenv("MODE");
  13. if($mode=="standalone" || $mode=="backend"){ ?>
  14. var SPEEDTEST_SERVERS=[];
  15. <?php } else { ?>
  16. var SPEEDTEST_SERVERS= <?= file_get_contents('/servers.json') ?: '[]' ?>;
  17. <?php }
  18. if ($mode=="dual"){ ?>
  19. // add own server in dual mode
  20. SPEEDTEST_SERVERS.unshift({
  21. "name":"This Server",
  22. "server":document.location.href+"backend/",
  23. "id":1,
  24. "dlURL":"garbage.php",
  25. "ulURL":"empty.php",
  26. "pingURL":"empty.php",
  27. "getIpURL":"getIP.php",
  28. "pingT":-1
  29. })
  30. <?php } ?>
  31. //INITIALIZE SPEED TEST
  32. var s=new Speedtest(); //create speed test object
  33. <?php if(getenv("TELEMETRY")=="true"){ ?>
  34. s.setParameter("telemetry_level","basic");
  35. <?php } ?>
  36. <?php if(getenv("DISABLE_IPINFO")=="true"){ ?>
  37. s.setParameter("getIp_ispInfo",false);
  38. <?php } ?>
  39. <?php if(getenv("DISTANCE")){ ?>
  40. s.setParameter("getIp_ispInfo_distance","<?=getenv("DISTANCE") ?>");
  41. <?php } ?>
  42. //SERVER AUTO SELECTION
  43. function initServers(){
  44. if(SPEEDTEST_SERVERS.length==0){ //standalone installation
  45. //just make the UI visible
  46. I("loading").className="hidden";
  47. I("serverArea").style.display="none";
  48. I("testWrapper").className="visible";
  49. initUI();
  50. }else{ //multiple servers
  51. var noServersAvailable=function(){
  52. I("message").innerHTML="No servers available";
  53. }
  54. var runServerSelect=function(){
  55. s.selectServer(function(server){
  56. if(server!=null){ //at least 1 server is available
  57. I("loading").className="hidden"; //hide loading message
  58. //populate server list for manual selection
  59. for(var i=0;i<SPEEDTEST_SERVERS.length;i++){
  60. if(SPEEDTEST_SERVERS[i].pingT==-1) continue;
  61. var option=document.createElement("option");
  62. option.value=i;
  63. option.textContent=SPEEDTEST_SERVERS[i].name;
  64. if(SPEEDTEST_SERVERS[i]===server) option.selected=true;
  65. I("server").appendChild(option);
  66. }
  67. //show test UI
  68. I("testWrapper").className="visible";
  69. initUI();
  70. }else{ //no servers are available, the test cannot proceed
  71. noServersAvailable();
  72. }
  73. });
  74. }
  75. if(typeof SPEEDTEST_SERVERS === "string"){
  76. //need to fetch list of servers from specified URL
  77. s.loadServerList(SPEEDTEST_SERVERS,function(servers){
  78. if(servers==null){ //failed to load server list
  79. noServersAvailable();
  80. }else{ //server list loaded
  81. SPEEDTEST_SERVERS=servers;
  82. runServerSelect();
  83. }
  84. });
  85. }else{
  86. //hardcoded server list
  87. s.addTestPoints(SPEEDTEST_SERVERS);
  88. runServerSelect();
  89. }
  90. }
  91. }
  92. var meterBk=/Trident.*rv:(\d+\.\d+)/i.test(navigator.userAgent)?"#EAEAEA":"#80808040";
  93. var dlColor="#6060AA",
  94. ulColor="#616161";
  95. var progColor=meterBk;
  96. //CODE FOR GAUGES
  97. function drawMeter(c,amount,bk,fg,progress,prog){
  98. var ctx=c.getContext("2d");
  99. var dp=window.devicePixelRatio||1;
  100. var cw=c.clientWidth*dp, ch=c.clientHeight*dp;
  101. var sizScale=ch*0.0055;
  102. if(c.width==cw&&c.height==ch){
  103. ctx.clearRect(0,0,cw,ch);
  104. }else{
  105. c.width=cw;
  106. c.height=ch;
  107. }
  108. ctx.beginPath();
  109. ctx.strokeStyle=bk;
  110. ctx.lineWidth=12*sizScale;
  111. ctx.arc(c.width/2,c.height-58*sizScale,c.height/1.8-ctx.lineWidth,-Math.PI*1.1,Math.PI*0.1);
  112. ctx.stroke();
  113. ctx.beginPath();
  114. ctx.strokeStyle=fg;
  115. ctx.lineWidth=12*sizScale;
  116. ctx.arc(c.width/2,c.height-58*sizScale,c.height/1.8-ctx.lineWidth,-Math.PI*1.1,amount*Math.PI*1.2-Math.PI*1.1);
  117. ctx.stroke();
  118. if(typeof progress !== "undefined"){
  119. ctx.fillStyle=prog;
  120. ctx.fillRect(c.width*0.3,c.height-16*sizScale,c.width*0.4*progress,4*sizScale);
  121. }
  122. }
  123. function mbpsToAmount(s){
  124. return 1-(1/(Math.pow(1.3,Math.sqrt(s))));
  125. }
  126. function format(d){
  127. d=Number(d);
  128. if(d<10) return d.toFixed(2);
  129. if(d<100) return d.toFixed(1);
  130. return d.toFixed(0);
  131. }
  132. //UI CODE
  133. var uiData=null;
  134. function startStop(){
  135. if(s.getState()==3){
  136. //speed test is running, abort
  137. s.abort();
  138. data=null;
  139. I("startStopBtn").className="";
  140. I("server").disabled=false;
  141. initUI();
  142. }else{
  143. //test is not running, begin
  144. I("startStopBtn").className="running";
  145. I("shareArea").style.display="none";
  146. I("server").disabled=true;
  147. s.onupdate=function(data){
  148. uiData=data;
  149. };
  150. s.onend=function(aborted){
  151. I("startStopBtn").className="";
  152. I("server").disabled=false;
  153. updateUI(true);
  154. if(!aborted){
  155. //if testId is present, show sharing panel, otherwise do nothing
  156. try{
  157. var testId=uiData.testId;
  158. if(testId!=null){
  159. var shareURL=window.location.href.substring(0,window.location.href.lastIndexOf("/"))+"/results/?id="+testId;
  160. I("resultsImg").src=shareURL;
  161. I("resultsURL").value=shareURL;
  162. I("testId").innerHTML=testId;
  163. I("shareArea").style.display="";
  164. }
  165. }catch(e){}
  166. }
  167. };
  168. s.start();
  169. }
  170. }
  171. //this function reads the data sent back by the test and updates the UI
  172. function updateUI(forced){
  173. if(!forced&&s.getState()!=3) return;
  174. if(uiData==null) return;
  175. var status=uiData.testState;
  176. I("ip").textContent=uiData.clientIp;
  177. I("dlText").textContent=(status==1&&uiData.dlStatus==0)?"...":format(uiData.dlStatus);
  178. drawMeter(I("dlMeter"),mbpsToAmount(Number(uiData.dlStatus*(status==1?oscillate():1))),meterBk,dlColor,Number(uiData.dlProgress),progColor);
  179. I("ulText").textContent=(status==3&&uiData.ulStatus==0)?"...":format(uiData.ulStatus);
  180. drawMeter(I("ulMeter"),mbpsToAmount(Number(uiData.ulStatus*(status==3?oscillate():1))),meterBk,ulColor,Number(uiData.ulProgress),progColor);
  181. I("pingText").textContent=format(uiData.pingStatus);
  182. I("jitText").textContent=format(uiData.jitterStatus);
  183. }
  184. function oscillate(){
  185. return 1+0.02*Math.sin(Date.now()/100);
  186. }
  187. //update the UI every frame
  188. window.requestAnimationFrame=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||(function(callback,element){setTimeout(callback,1000/60);});
  189. function frame(){
  190. requestAnimationFrame(frame);
  191. updateUI();
  192. }
  193. frame(); //start frame loop
  194. //function to (re)initialize UI
  195. function initUI(){
  196. drawMeter(I("dlMeter"),0,meterBk,dlColor,0);
  197. drawMeter(I("ulMeter"),0,meterBk,ulColor,0);
  198. I("dlText").textContent="";
  199. I("ulText").textContent="";
  200. I("pingText").textContent="";
  201. I("jitText").textContent="";
  202. I("ip").textContent="";
  203. }
  204. </script>
  205. <style type="text/css">
  206. html,body{
  207. border:none; padding:0; margin:0;
  208. background:#FFFFFF;
  209. color:#202020;
  210. }
  211. body{
  212. text-align:center;
  213. font-family:"Roboto",sans-serif;
  214. }
  215. h1{
  216. color:#404040;
  217. }
  218. #loading{
  219. background-color:#FFFFFF;
  220. color:#404040;
  221. text-align:center;
  222. }
  223. span.loadCircle{
  224. display:inline-block;
  225. width:2em;
  226. height:2em;
  227. vertical-align:middle;
  228. background:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAP1BMVEUAAAB2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZyFzwnAAAAFHRSTlMAEvRFvX406baecwbf0casimhSHyiwmqgAAADpSURBVHja7dbJbQMxAENRahnN5lkc//5rDRAkDeRgHszXgACJoKiIiIiIiIiIiIiIiIiIiIj4HHspsrpAVhdVVguzrA4OWc10WcEqpwKbnBo0OU1Q5NSpsoJFTgOecrrdEag85DRgktNqfoEdTjnd7hrEHMEJvmRUYJbTYk5Agy6nau6Abp5Cm7mDBtRdPi9gyKdU7w4p1fsLvyqs8hl4z9/w3n/Hmr9WoQ65lAU4d7lMYOz//QboRR5jBZibLMZdAR6O/Vfa1PlxNr3XdS3HzK/HVPRu/KnLs8iAOh993VpRRERERMT/fAN60wwWaVyWwAAAAABJRU5ErkJggg==');
  229. background-size:2em 2em;
  230. margin-right:0.5em;
  231. animation: spin 0.6s linear infinite;
  232. }
  233. @keyframes spin{
  234. 0%{transform:rotate(0deg);}
  235. 100%{transform:rotate(359deg);}
  236. }
  237. #startStopBtn{
  238. display:inline-block;
  239. margin:0 auto;
  240. color:#6060AA;
  241. background-color:rgba(0,0,0,0);
  242. border:0.15em solid #6060FF;
  243. border-radius:0.3em;
  244. transition:all 0.3s;
  245. box-sizing:border-box;
  246. width:8em; height:3em;
  247. line-height:2.7em;
  248. cursor:pointer;
  249. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  250. }
  251. #startStopBtn:hover{
  252. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  253. }
  254. #startStopBtn.running{
  255. background-color:#FF3030;
  256. border-color:#FF6060;
  257. color:#FFFFFF;
  258. }
  259. #startStopBtn:before{
  260. content:"Start";
  261. }
  262. #startStopBtn.running:before{
  263. content:"Abort";
  264. }
  265. #serverArea{
  266. margin-top:1em;
  267. }
  268. #server{
  269. font-size:1em;
  270. padding:0.2em;
  271. }
  272. #test{
  273. margin-top:2em;
  274. margin-bottom:12em;
  275. }
  276. div.testArea{
  277. display:inline-block;
  278. width:16em;
  279. height:12.5em;
  280. position:relative;
  281. box-sizing:border-box;
  282. }
  283. div.testArea2{
  284. display:inline-block;
  285. width:14em;
  286. height:7em;
  287. position:relative;
  288. box-sizing:border-box;
  289. text-align:center;
  290. }
  291. div.testArea div.testName{
  292. position:absolute;
  293. top:0.1em; left:0;
  294. width:100%;
  295. font-size:1.4em;
  296. z-index:9;
  297. }
  298. div.testArea2 div.testName{
  299. display:block;
  300. text-align:center;
  301. font-size:1.4em;
  302. }
  303. div.testArea div.meterText{
  304. position:absolute;
  305. bottom:1.55em; left:0;
  306. width:100%;
  307. font-size:2.5em;
  308. z-index:9;
  309. }
  310. div.testArea2 div.meterText{
  311. display:inline-block;
  312. font-size:2.5em;
  313. }
  314. div.meterText:empty:before{
  315. content:"0.00";
  316. }
  317. div.testArea div.unit{
  318. position:absolute;
  319. bottom:2em; left:0;
  320. width:100%;
  321. z-index:9;
  322. }
  323. div.testArea2 div.unit{
  324. display:inline-block;
  325. }
  326. div.testArea canvas{
  327. position:absolute;
  328. top:0; left:0; width:100%; height:100%;
  329. z-index:1;
  330. }
  331. div.testGroup{
  332. display:block;
  333. margin: 0 auto;
  334. }
  335. #shareArea{
  336. width:95%;
  337. max-width:40em;
  338. margin:0 auto;
  339. margin-top:2em;
  340. }
  341. #shareArea > *{
  342. display:block;
  343. width:100%;
  344. height:auto;
  345. margin: 0.25em 0;
  346. }
  347. #privacyPolicy{
  348. position:fixed;
  349. top:2em;
  350. bottom:2em;
  351. left:2em;
  352. right:2em;
  353. overflow-y:auto;
  354. width:auto;
  355. height:auto;
  356. box-shadow:0 0 3em 1em #000000;
  357. z-index:999999;
  358. text-align:left;
  359. background-color:#FFFFFF;
  360. padding:1em;
  361. }
  362. a.privacy{
  363. text-align:center;
  364. font-size:0.8em;
  365. color:#808080;
  366. padding: 0 3em;
  367. }
  368. div.closePrivacyPolicy {
  369. width: 100%;
  370. text-align: center;
  371. }
  372. div.closePrivacyPolicy a.privacy {
  373. padding: 1em 3em;
  374. }
  375. @media all and (max-width:40em){
  376. body{
  377. font-size:0.8em;
  378. }
  379. }
  380. div.visible{
  381. animation: fadeIn 0.4s;
  382. display:block;
  383. }
  384. div.hidden{
  385. animation: fadeOut 0.4s;
  386. display:none;
  387. }
  388. @keyframes fadeIn{
  389. 0%{
  390. opacity:0;
  391. }
  392. 100%{
  393. opacity:1;
  394. }
  395. }
  396. @keyframes fadeOut{
  397. 0%{
  398. display:block;
  399. opacity:1;
  400. }
  401. 100%{
  402. display:block;
  403. opacity:0;
  404. }
  405. }
  406. @media all and (prefers-color-scheme: dark){
  407. html,body,#loading{
  408. background:#202020;
  409. color:#F4F4F4;
  410. color-scheme:dark;
  411. }
  412. h1{
  413. color:#E0E0E0;
  414. }
  415. a{
  416. color:#9090FF;
  417. }
  418. #privacyPolicy{
  419. background:#000000;
  420. }
  421. #resultsImg{
  422. filter: invert(1);
  423. }
  424. }
  425. </style>
  426. <title><?= getenv('TITLE') ?: 'LibreSpeed' ?></title>
  427. </head>
  428. <body onload="initServers()">
  429. <h1><?= getenv('TITLE') ?: 'LibreSpeed' ?></h1>
  430. <div id="loading" class="visible">
  431. <p id="message"><span class="loadCircle"></span>Selecting a server...</p>
  432. </div>
  433. <div id="testWrapper" class="hidden">
  434. <div id="startStopBtn" onclick="startStop()"></div><br/>
  435. <?php if(getenv("TELEMETRY")=="true"){ ?>
  436. <a class="privacy" href="#" onclick="I('privacyPolicy').style.display=''">Privacy</a>
  437. <?php } ?>
  438. <div id="serverArea">
  439. Server: <select id="server" onchange="s.setSelectedServer(SPEEDTEST_SERVERS[this.value])"></select>
  440. </div>
  441. <div id="test">
  442. <div class="testGroup">
  443. <div class="testArea2">
  444. <div class="testName">Ping</div>
  445. <div id="pingText" class="meterText" style="color:#AA6060"></div>
  446. <div class="unit">ms</div>
  447. </div>
  448. <div class="testArea2">
  449. <div class="testName">Jitter</div>
  450. <div id="jitText" class="meterText" style="color:#AA6060"></div>
  451. <div class="unit">ms</div>
  452. </div>
  453. </div>
  454. <div class="testGroup">
  455. <div class="testArea">
  456. <div class="testName">Download</div>
  457. <canvas id="dlMeter" class="meter"></canvas>
  458. <div id="dlText" class="meterText"></div>
  459. <div class="unit">Mbit/s</div>
  460. </div>
  461. <div class="testArea">
  462. <div class="testName">Upload</div>
  463. <canvas id="ulMeter" class="meter"></canvas>
  464. <div id="ulText" class="meterText"></div>
  465. <div class="unit">Mbit/s</div>
  466. </div>
  467. </div>
  468. <div id="ipArea">
  469. <span id="ip"></span>
  470. </div>
  471. <div id="shareArea" style="display:none">
  472. <h3>Share results</h3>
  473. <p>Test ID: <span id="testId"></span></p>
  474. <input type="text" value="" id="resultsURL" readonly="readonly" onclick="this.select();this.focus();this.select();document.execCommand('copy');alert('Link copied')"/>
  475. <img src="" id="resultsImg" />
  476. </div>
  477. </div>
  478. <a href="https://github.com/librespeed/speedtest">Source code</a>
  479. </div>
  480. <div id="privacyPolicy" style="display:none">
  481. <h2>Privacy Policy</h2>
  482. <p>This HTML5 speed test server is configured with telemetry enabled.</p>
  483. <h4>What data we collect</h4>
  484. <p>
  485. At the end of the test, the following data is collected and stored:
  486. <ul>
  487. <li>Test ID</li>
  488. <li>Time of testing</li>
  489. <li>Test results (download and upload speed, ping and jitter)</li>
  490. <li>IP address</li>
  491. <li>ISP information</li>
  492. <li>Approximate location (inferred from IP address, not GPS)</li>
  493. <li>User agent and browser locale</li>
  494. <li>Test log (contains no personal information)</li>
  495. </ul>
  496. </p>
  497. <h4>How we use the data</h4>
  498. <p>
  499. Data collected through this service is used to:
  500. <ul>
  501. <li>Allow sharing of test results (sharable image for forums, etc.)</li>
  502. <li>To improve the service offered to you (for instance, to detect problems on our side)</li>
  503. </ul>
  504. No personal information is disclosed to third parties.
  505. </p>
  506. <h4>Your consent</h4>
  507. <p>
  508. By starting the test, you consent to the terms of this privacy policy.
  509. </p>
  510. <h4>Data removal</h4>
  511. <p>
  512. If you want to have your information deleted, you need to provide either the ID of the test or your IP address. This is the only way to identify your data, without this information we won't be able to comply with your request.<br/><br/>
  513. Contact this email address for all deletion requests: <a href="mailto:<?=getenv("EMAIL") ?>"><?=getenv("EMAIL") ?></a>.
  514. </p>
  515. <br/><br/>
  516. <div class="closePrivacyPolicy">
  517. <a class="privacy" href="#" onclick="I('privacyPolicy').style.display='none'">Close</a>
  518. </div>
  519. <br/>
  520. </div>
  521. </body>
  522. </html>