From e5612b4f10d7a7d37e3f19ea4c6bbe82853e0310 Mon Sep 17 00:00:00 2001 From: dosse91 Date: Sun, 26 Feb 2017 12:15:51 +0100 Subject: [PATCH] Improved client IP detection; Updated examples --- example1.html | 3 +++ example2.html | 4 +++- example3.html | 8 +++++++- example4.html | 9 ++++++++- getIP.php | 3 +-- speedtest_worker.js | 21 +++++++++++++++++---- 6 files changed, 39 insertions(+), 9 deletions(-) diff --git a/example1.html b/example1.html index 04fee37..dc4d612 100644 --- a/example1.html +++ b/example1.html @@ -5,6 +5,8 @@

Speedtest

+

IP Address

+

Download

Upload

@@ -19,6 +21,7 @@ document.getElementById("download").innerHTML=data[1]+" Mbit/s"; document.getElementById("upload").innerHTML=data[2]+" Mbit/s"; document.getElementById("ping").innerHTML=data[3]+" ms"; + document.getElementById("ip").innerHTML=data[4]; } w.postMessage("start"); //start the speedtest (default params. keep garbage.php and empty.dat in the same directory as the js file) diff --git a/example2.html b/example2.html index a6aec90..5347408 100644 --- a/example2.html +++ b/example2.html @@ -49,13 +49,14 @@
  
ms
+
diff --git a/example3.html b/example3.html index 5cfe182..a1801fd 100644 --- a/example3.html +++ b/example3.html @@ -37,6 +37,10 @@ color:#000000; text-decoration:none; } + #ip{ + margin:0.8em 0; + font-size:1.2em; + } @@ -76,8 +81,9 @@ updateGauge(ggdl, data[1]); updateGauge(ggul, data[2]); updateGauge(ggping, data[3]); + document.getElementById("ip").innerHTML="Your IP: "+data[4]; }.bind(this); - w.postMessage('start {"time_ul":"10", "time_dl":"10", "count_ping":"20", "url_dl":"garbage.php","url_ul":"empty.dat","url_ping":"empty.dat"}'); + w.postMessage('start {"time_ul":"10", "time_dl":"10", "count_ping":"20", "url_dl":"garbage.php","url_ul":"empty.dat","url_ping":"empty.dat","url_getIp":"getIP.php"}'); } function abortTest(){ if(w)w.postMessage("abort"); @@ -161,6 +167,7 @@
+
Start diff --git a/getIP.php b/getIP.php index 11c7163..0b55b77 100644 --- a/getIP.php +++ b/getIP.php @@ -1,4 +1,3 @@ diff --git a/speedtest_worker.js b/speedtest_worker.js index 7799605..e020a1d 100644 --- a/speedtest_worker.js +++ b/speedtest_worker.js @@ -1,10 +1,10 @@ -var testStatus=0,dlStatus="",ulStatus="",pingStatus=""; -var settings={time_ul:15,time_dl:15,count_ping:35,url_dl:"garbage.php",url_ul:"empty.dat",url_ping:"empty.dat"}; +var testStatus=0,dlStatus="",ulStatus="",pingStatus="",clientIp=""; +var settings={time_ul:15,time_dl:15,count_ping:35,url_dl:"garbage.php",url_ul:"empty.dat",url_ping:"empty.dat",url_getIp:"getIP.php"}; var xhr=null; this.addEventListener('message', function(e){ var params=e.data.split(" "); if(params[0]=="status"){ - postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus); + postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus+";"+clientIp); } if(params[0]=="start"&&testStatus==0){ testStatus=1; @@ -13,17 +13,30 @@ this.addEventListener('message', function(e){ if(typeof s.url_dl != "undefined") settings.url_dl=s.url_dl; if(typeof s.url_ul != "undefined") settings.url_ul=s.url_ul; if(typeof s.url_ping != "undefined") settings.url_ping=s.url_ping; + if(typeof s.url_getIp != "undefined") settings.url_getIp=s.url_getIp; if(typeof s.time_dl != "undefined") settings.time_dl=s.time_dl; if(typeof s.time_ul != "undefined") settings.time_ul=s.time_ul; if(typeof s.count_ping != "undefined") settings.count_ping=s.count_ping; }catch(e){} - dlTest(function(){testStatus=2;ulTest(function(){testStatus=3;pingTest(function(){testStatus=4;});});}); + getIp(function(){dlTest(function(){testStatus=2;ulTest(function(){testStatus=3;pingTest(function(){testStatus=4;});});})}); } if(params[0]=="abort"){ try{if(xhr)xhr.abort();}catch(e){} testStatus=5;dlStatus="";ulStatus="";pingStatus=""; } }); +function getIp(done){ + xhr=new XMLHttpRequest(); + xhr.onload=function(){ + clientIp=xhr.responseText; + done(); + } + xhr.onerror=function(){ + done(); + } + xhr.open("GET",settings.url_getIp+"?r="+Math.random(),true); + xhr.send(); +} var dlCalled=false; function dlTest(done){ if(dlCalled) return; else dlCalled=true;