簡體   English   中英

有沒有辦法檢查用戶是否通過Tor使用JavaScript連接到網站?

[英]Is there a way to check if a user is connected to a site through Tor using JavaScript?

如果用戶通過Tor連接,我希望能夠顯示消息或等。 這不需要是完全安全的,它應該是客戶端。 如有必要,它可以使用外部API或站點來獲取IP地址或類似內容。

我通過使用此功能檢查Tor的概率:

  • performance.now()(在Tor中總是四舍五入到100.0)(Date.now()也被舍入)
  • navigator.hardwareConcurrency(總是1 in Tor)
  • devicePixelRatio(Tor中始終為1)

例:

var isTor = true;

if (navigator.hardwareConcurrency && navigator.hardwareConcurrency !== 1) {
    //This may not happen in Tor Browser because in Tor hardwareConcurrency is always 1
    isTor = false;
}

if (window.devicePixelRatio && devicePixelRatio !== 1) {
    //This may not happen in Tor Browser because in Tor devicePixelRatio is always 1
    isTor = false;
}

checkTor_byCurTime()
//Do something...

checkTor_byCurTime();
//Do something again...

checkTor_byCurTime();
// <<< Here you can use isTor var >>>

function checkTor_byCurTime() {
    if (performance.now() % 100 !== 0) {
        //This may not happen in Tor Browser but in other browsers it happens almost every time
        isTor = false;
    }
}

如果您不需要同步檢查,還可以添加setTimeout調用:

setTimeout(checkTor_byCurTime, 1);
setTimeout(checkTor_byCurTime, 2);
setTimeout(checkTor_byCurTime, 3);
setTimeout(checkTor_byCurTime, 5);
setTimeout(checkTor_byCurTime, 7);
setTimeout(checkTor_byCurTime, 11);
setTimeout(checkTor_byCurTime, 13);

這是使用函數的polyfill:

if (!window.performance) window.performance = {};
if (!performance.now) performance.now = function() { return Date.now(); };
if (!Date.now()) Date.now = function() { return (new Date).getTime(); };

是一個JavaScript文件,您可以加載以查看訪問者是否使用Tor。 它會將window._isTor設置為truefalse具體取決於加載腳本的遠程IP是否來自Tor出口節點。

像這樣將它加載到您的頁面上(域支持TLS 1.0 - TLS 1.2連接,無SSL):

<script src="//openinternet.io/tor/istor.js"></script>

檢查:

<script>
    if (typeof window._isTor != 'undefined' && window._isTor == true) {
        // VISITOR IS USING TOR - code here
    } else {
        // not using Tor or the value was undefined
    }
</script>

將它放在<head>標簽中(會減慢頁面加載時間)或推遲加載它並在完成后檢查值。

我將提供腳本的源代碼(PHP),如果您願意,可以動態生成該JavaScript。

我還計划盡可能長時間保持該URL的活躍性 - 但不保證可用性,可靠性,或者它將保持多年。

編輯:應該注意的是,有一段時間,Tor默認啟用NoScript,這可能會阻止從另一個域加載的JS,因此這可能會也可能不會。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM