Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix domain extraction when --dns flag is used #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,10 @@ def extract_headers(headers):

def top_level(url, fix_protocol=True):
"""Extract the top level domain from an URL."""
ext = tld.get_tld(url, fix_protocol=fix_protocol)
toplevel = '.'.join(urlparse(url).netloc.split('.')[-2:]).split(
ext)[0] + ext
res = tld.get_tld(url, fix_protocol=fix_protocol, as_object=True)
toplevel = res.domain + '.' + res.tld
return toplevel


def is_proxy_list(v, proxies):
if os.path.isfile(v):
with open(v, 'r') as _file:
Expand Down
19 changes: 19 additions & 0 deletions test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest
import tld.exceptions
from core.utils import top_level

def test_top_level_with_https_url():
assert top_level('https://google.co.uk') == 'google.co.uk'
assert top_level('https://google.com') == 'google.com'

def test_top_level_with_one_level_domain():
assert top_level('google.co.uk') == 'google.co.uk'
assert top_level('google.com') == 'google.com'

def test_top_level_with_second_level_domain():
assert top_level('123.google.co.uk') == 'google.co.uk'
assert top_level('123.google.com') == 'google.com'

def test_top_level_with_wrong_domain():
with pytest.raises(tld.exceptions.TldDomainNotFound):
top_level('google.co.uk2')