Esempio n. 1
0
def uninstall():
    # 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')

    if sys.platform.startswith('win'):
        path = r'c:\windows\system32\drivers\etc\hosts'
    else:
        path = '/etc/hosts'

    print('⋯ Parse current hosts file')
    hosts_manager = HostsManager(path)

    print('⋯ Removing BeGoneAds')
    hosts_manager.remove_begoneads()

    print('⋯ Saving')
    hosts_manager.commit()

    print('✓ BeGoneAds uninstalled')
Esempio n. 2
0
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(',')]

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

    url_pattern = re.compile(
        r'^(?:http|ftp)s?://'  # http:// or https://
        # domain...
        r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
        r'localhost|'  # localhost...
        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'  # ...or ip
        r'(?::\d+)?'  # optional port
        r'(?:/?|[/?]\S+)$', re.IGNORECASE
    )

    for source in 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
    remote_collector = RemoteCollector(sources)

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

    print('✓ Remote hosts collected')

    local_hosts = ""

    if local_sources:
        # Collect local host files
        local_collector = LocalCollector(local_sources)

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

        print('✓ Local hosts collected')

    hosts = "\n".join([
        remote_hosts,
        local_hosts
    ])

    if sys.platform.startswith('win'):
        path = r'c:\windows\system32\drivers\etc\hosts'
    else:
        path = '/etc/hosts'

    print('⋯ Parse current hosts file')
    hosts_manager = HostsManager(path)

    print('⋯ Applying new hosts')
    hosts_manager.apply_hosts(hosts)

    print('⋯ Saving')
    hosts_manager.commit()

    print('✓ Hosts applied')