ui.php 15 KB

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