ui.php 15 KB

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