mirror of
https://github.com/zero-peak/ZeroOmega.git
synced 2025-01-22 15:08:12 -05:00
Use publicsuffixlist for base domain extraction. Fix #276.
This commit is contained in:
parent
5a7e365896
commit
0ae801e716
@ -18,6 +18,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ipv6": "beaugunderson/javascript-ipv6",
|
"ipv6": "beaugunderson/javascript-ipv6",
|
||||||
|
"tldjs": "^1.5.2",
|
||||||
"uglify-js": "^2.4.15"
|
"uglify-js": "^2.4.15"
|
||||||
},
|
},
|
||||||
"browser": {
|
"browser": {
|
||||||
|
@ -39,19 +39,23 @@ class AttachedCache
|
|||||||
|
|
||||||
exports.AttachedCache = AttachedCache
|
exports.AttachedCache = AttachedCache
|
||||||
|
|
||||||
exports.getBaseDomain = (domain) ->
|
tld = require('tldjs')
|
||||||
return domain if domain.indexOf(':') > 0 # IPv6
|
|
||||||
|
exports.isIp = (domain) ->
|
||||||
|
return true if domain.indexOf(':') > 0 # IPv6
|
||||||
lastCharCode = domain.charCodeAt(domain.length - 1)
|
lastCharCode = domain.charCodeAt(domain.length - 1)
|
||||||
return domain if 48 <= lastCharCode <= 57 # IP address ending with number.
|
return true if 48 <= lastCharCode <= 57 # IP address ending with number.
|
||||||
segments = domain.split('.')
|
return false
|
||||||
if segments.length <= 2
|
|
||||||
return domain
|
exports.getBaseDomain = (domain) ->
|
||||||
if segments[0] == 'www'
|
return domain if exports.isIp(domain)
|
||||||
segments.shift()
|
return tld.getDomain(domain) ? domain
|
||||||
len = segments.length
|
|
||||||
if len <= 2
|
exports.wildcardForDomain = (domain) ->
|
||||||
return segments.join('.')
|
return domain if exports.isIp(domain)
|
||||||
if segments[len - 2].length <= 2
|
return '*.' + exports.getBaseDomain(domain)
|
||||||
return segments[len - 3] + '.' + segments[len - 2] + '.' + segments[len - 1]
|
|
||||||
else
|
Url = require('url')
|
||||||
return segments[len - 2] + '.' + segments[len - 1]
|
exports.wildcardForUrl = (url) ->
|
||||||
|
domain = Url.parse(url).hostname
|
||||||
|
return exports.wildcardForDomain(domain)
|
||||||
|
@ -10,19 +10,13 @@ describe 'getBaseDomain', ->
|
|||||||
getBaseDomain('example.com').should.equal('example.com')
|
getBaseDomain('example.com').should.equal('example.com')
|
||||||
getBaseDomain('e.test').should.equal('e.test')
|
getBaseDomain('e.test').should.equal('e.test')
|
||||||
getBaseDomain('a.b').should.equal('a.b')
|
getBaseDomain('a.b').should.equal('a.b')
|
||||||
it 'should ignore the leading www with domains with two or more levels', ->
|
it 'should treat two-segment TLD as one component', ->
|
||||||
getBaseDomain('www.example.com').should.equal('example.com')
|
|
||||||
getBaseDomain('www.e.test').should.equal('e.test')
|
|
||||||
getBaseDomain('www.a.b').should.equal('a.b')
|
|
||||||
it 'should assume two-segment TLD if len(second segment from last) <= 2', ->
|
|
||||||
getBaseDomain('images.google.co.uk').should.equal('google.co.uk')
|
getBaseDomain('images.google.co.uk').should.equal('google.co.uk')
|
||||||
getBaseDomain('images.google.co.jp').should.equal('google.co.jp')
|
getBaseDomain('images.google.co.jp').should.equal('google.co.jp')
|
||||||
getBaseDomain('ab.de.ef.test').should.equal('de.ef.test')
|
getBaseDomain('example.com.cn').should.equal('example.com.cn')
|
||||||
it 'should assume one-segment TLD and keep two segments as base otherwise', ->
|
it 'should not mistake short domains with two-segment TLDs', ->
|
||||||
getBaseDomain('subdomain.example.com').should.equal('example.com')
|
getBaseDomain('a.bc.com').should.equal('bc.com')
|
||||||
getBaseDomain('some.site.example.net').should.equal('example.net')
|
getBaseDomain('i.t.co').should.equal('t.co')
|
||||||
getBaseDomain('some.site.abc.test').should.equal('abc.test')
|
|
||||||
getBaseDomain('ab.de.efg.test').should.equal('efg.test')
|
|
||||||
it 'should not try to modify IP address literals', ->
|
it 'should not try to modify IP address literals', ->
|
||||||
getBaseDomain('127.0.0.1').should.equal('127.0.0.1')
|
getBaseDomain('127.0.0.1').should.equal('127.0.0.1')
|
||||||
getBaseDomain('[::1]').should.equal('[::1]')
|
getBaseDomain('[::1]').should.equal('[::1]')
|
||||||
|
Loading…
Reference in New Issue
Block a user