This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make third partiness computation much faster
Based on the method used in Privacy Badger Chrome. Also changes from using hosts (which include port) to hostnames. Fix #696 Auditors: @bbondy
- Loading branch information
1 parent
6b7173b
commit d368f70
Showing
5 changed files
with
5,890 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const punycode = require('punycode') | ||
const publicSuffixes = require('./psl') | ||
|
||
/** | ||
* Returns base domain for specified host based on Public Suffix List. | ||
* @param {string} hostname The name of the host to get the base domain for | ||
*/ | ||
module.exports.getBaseDomain = function (hostname) { | ||
// decode punycode if exists | ||
if (hostname.indexOf('xn--') >= 0) { | ||
hostname = punycode.toUnicode(hostname) | ||
} | ||
|
||
// search through PSL | ||
var prevDomains = [] | ||
var curDomain = hostname | ||
var nextDot = curDomain.indexOf('.') | ||
var tld = 0 | ||
var suffix | ||
|
||
while (true) { | ||
suffix = publicSuffixes[curDomain] | ||
if (typeof suffix !== 'undefined') { | ||
tld = suffix | ||
break | ||
} | ||
|
||
if (nextDot < 0) { | ||
tld = 1 | ||
break | ||
} | ||
|
||
prevDomains.push(curDomain.substring(0, nextDot)) | ||
curDomain = curDomain.substring(nextDot + 1) | ||
nextDot = curDomain.indexOf('.') | ||
} | ||
|
||
while (tld > 0 && prevDomains.length > 0) { | ||
curDomain = prevDomains.pop() + '.' + curDomain | ||
tld-- | ||
} | ||
|
||
return curDomain | ||
} |
Oops, something went wrong.
d368f70
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could still be made faster by caching the most recently-checked domains, i think
d368f70
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please post an issue for that, we should because benchmarks alone do repeated reloading of the same site. Also useful in general.