Skip to content

Commit

Permalink
Local sources
Browse files Browse the repository at this point in the history
  • Loading branch information
anned20 committed Jun 9, 2019
1 parent 4b7034e commit 77d51da
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 25 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ To install the hosts to your system hosts file with custom sources:
begoneads install --sources https://www.custom.sources/hosts,http://www.and-another.one/hosts
```

To install the hosts to your system hosts file with local sources:

```shell
begoneads install --local-sources path/to/hosts/file,other/path
```

The options `sources` and `local-sources` can be used together

To uninstall the hosts to your system hosts file:

```shell
Expand Down Expand Up @@ -100,8 +108,8 @@ add.Risk | [link](https://github.com/FadeMind/hosts.extras) |
- [X] Windows support
- [X] Custom selection of host files
- [X] Setuptools
- [X] Apply own hosts
- [ ] Systemd integration
- [ ] Apply own hosts
- [ ] Package it for Debian, Arch, CentOS, Fedora, etc.

## Testing
Expand Down
33 changes: 27 additions & 6 deletions begoneads/begoneads.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import sys
import re
import click
from begoneads.collector import Collector
from begoneads.collectors.remote_collector import RemoteCollector
from begoneads.collectors.local_collector import LocalCollector
from begoneads.hostsmanager import HostsManager
from begoneads.exceptions import *
from begoneads.helpers import is_admin
Expand Down Expand Up @@ -35,13 +36,16 @@ def cli():
@cli.command('install', short_help='Install or update BeGoneAds')
@click.option('--sources', is_flag=False, default=','.join(sources),
type=click.STRING, help='Sets sources to fetch from, seperated by ,')
def install(sources):
@click.option('--local-sources', is_flag=False, default='',
type=click.STRING, help='Pass local hosts files to include')
def install(sources, local_sources):
# Check if we have sufficient permissions
if not is_admin(sys.platform.startswith('win')):
raise NotElevatedException(
'This program needs to be run as root to work properly')

sources = [i.strip() for i in sources.split(',')]
local_sources = [i.strip() for i in local_sources.split(',')]

url_pattern = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
Expand All @@ -57,13 +61,30 @@ def install(sources):
if not re.match(url_pattern, source):
raise InvalidSourceException(source)

for source in local_sources:
if not os.path.isfile(source):
raise InvalidSourceException(source)

# Collect hosts for hosts file
collector = Collector(sources)
remote_collector = RemoteCollector(sources)

print('⋯ Collecting and parsing remote hosts')
remote_hosts = remote_collector.get_result()

print('✓ Remote hosts collected')

# Collect local host files
local_collector = LocalCollector(local_sources)

print('⋯ Collecting and parsing local hosts')
local_hosts = local_collector.get_result()

print('⋯ Collecting and parsing hosts')
hosts = collector.get_result()
hosts = "\n".join([
remote_hosts,
local_hosts
])

print('✓ Hosts collected')
print('✓ Local hosts collected')

if sys.platform.startswith('win'):
path = r'c:\windows\system32\drivers\etc\hosts'
Expand Down
Empty file.
22 changes: 8 additions & 14 deletions begoneads/collector.py → begoneads/collectors/base_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from tqdm import tqdm


class Collector(object):
"""A class that collects the remote hosts files
class BaseCollector(object):
"""A base class for all collectors
Attributes:
sources: list
Expand All @@ -14,28 +14,22 @@ def __init__(self, sources):
self.sources = sources

def try_get_sources(self, sources):
"""Try and get each source, don't return them if the request was not succesful"""
"""Try and get each file"""

filtered = []

for source in tqdm(sources):
response = requests.get(source)

if response.status_code >= 200 and response.status_code < 300:
content = str(response.text)

filtered.append(content)
with open(source) as _file:
filtered.append(_file.read())

return filtered

def fix_ips(self, sources):
def fix_ips(self, hosts):
"""Replace all IP addresses with 0.0.0.0"""

pattern = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',
re.MULTILINE)
sources = re.sub(pattern, '0.0.0.0', sources)
hosts = re.sub(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', '0.0.0.0', hosts, 0, re.MULTILINE)

return sources
return hosts

def filter_hosts(self, hosts):
"""Only keep meaningful lines"""
Expand Down
23 changes: 23 additions & 0 deletions begoneads/collectors/local_collector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import requests
import re
from tqdm import tqdm
from begoneads.collectors.base_collector import BaseCollector


class LocalCollector(BaseCollector):
"""A class that collects local host files
Attributes:
sources: list
"""

def try_get_sources(self, sources):
"""Try and get each file"""

filtered = []

for source in tqdm(sources):
with open(source) as _file:
filtered.append(_file.read())

return filtered
27 changes: 27 additions & 0 deletions begoneads/collectors/remote_collector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import requests
import re
from tqdm import tqdm
from begoneads.collectors.base_collector import BaseCollector


class RemoteCollector(BaseCollector):
"""A class that collects the remote host files
Attributes:
sources: list
"""

def try_get_sources(self, sources):
"""Try and get each source, don't return them if the request was not succesful"""

filtered = []

for source in tqdm(sources):
response = requests.get(source)

if response.status_code >= 200 and response.status_code < 300:
content = str(response.text)

filtered.append(content)

return filtered
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collector import Collector
from begoneads.collectors.remote_collector import RemoteCollector
from unittest.mock import patch

collector = Collector()
collector = RemoteCollector([])


def test_try_get_sources():
Expand Down
2 changes: 1 addition & 1 deletion begoneads/hostsmanager_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hostsmanager import HostsManager, start_marker, end_marker
from begoneads.hostsmanager import HostsManager, start_marker, end_marker
from unittest.mock import patch, mock_open

without_begoneads = '''
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def readme():

setup(
name='begoneads',
version='0.0.4',
version='0.0.5',
description='BeGoneAds puts some popular hosts file lists into the hosts file as a adblocker measure.',
long_description=readme(),
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 77d51da

Please sign in to comment.