Skip to content

Commit

Permalink
add badlist support
Browse files Browse the repository at this point in the history
  • Loading branch information
blocklist committed Aug 2, 2024
1 parent d6c54fc commit bc0bd9c
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 339 deletions.
19 changes: 10 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,30 @@ echo "alias block='cd \$(cat ~/.blocklist) && source \$(cat ~/.blocklist)/blockl
echo "alias block='cd \$(cat ~/.blocklist) && source \$(cat ~/.blocklist)/blocklist/bin/activate'" >> ~/.zshrc
```

# Add domain
# Insert

```
./insert.sh
block
python insert.py
```

Enter the domain name, https:// or http and trailing / are automatically being removed if included.
Insert supports, domains and filepaths.

If you enter a domain name, https:// or http and trailing / are automatically being removed if included.
So https://annoyingtrackers.com/ becomes: annoyingtrackers.com

Hit enter, and the domain will be added to the list.



# Delist a domain
# Badlist

```
./delist.sh
block
python badlist.py
```

- Enter the domain name, https:// or http and trailing / are automatically being removed if included.
- Specify reason

Hit enter, and the domain will be delisted and specification will be added to the list for reference.
Badlist supports filepaths and domains.



Expand Down
61 changes: 37 additions & 24 deletions badfilter.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
import sys

def updateLine(line):
line = line.replace('@@', '')
line = line.replace('^', '^$badfilter')
return line

def checkline(line):
if line.lstrip().startswith("@@"):
return True

return False

def overwrite(filename='blocklist.txt'):
with open(filename,'r+') as file:
lines = file.readlines()
count = 0
for line in lines:
if checkline(line):
lines[count] = updateLine(line)
count = count + 1

with open(filename,'w') as wfile:

from modules.clean import format_record, split_records


def badfilter_records(input_records, blocklist_file='blocklist.txt'):
bad_records = split_records(input_records)

with open(blocklist_file,'r') as f:
formatted_bad_records = []
lines = []

for bad_record in bad_records:
formatted_bad_record = F"{format_record(bad_record)}"
formatted_bad_records.append(formatted_bad_record)

for record in f.readlines():
# Remove newline character
record = record.strip('\n')
if record in formatted_bad_records:
lines.append(F"{record}" + '$badfilter')
formatted_bad_records.remove(record)
print(F"{record} marked as badfilter")
else:
lines.append(record)

for bad_record in formatted_bad_records:
print(F"{bad_record} not been badlisted")

with open(blocklist_file,'w') as wfile:
for line in lines:
wfile.write(line)
wfile.close()
wfile.write(F"{line}\n")


if __name__ == '__main__':
overwrite()
records = sys.argv
records.pop(0) # remove filename from list
if records.__len__() == 0:
print('Set records as badfilter, such as domains, valid adp rules, or full paths to blocklist.txt')
records = input('Enter record(s): ')
badfilter_records(records)
44 changes: 0 additions & 44 deletions delete.sh

This file was deleted.

52 changes: 0 additions & 52 deletions delist.sh

This file was deleted.

40 changes: 0 additions & 40 deletions insert-bulk-by-file.sh

This file was deleted.

36 changes: 0 additions & 36 deletions insert-bulk.sh

This file was deleted.

46 changes: 0 additions & 46 deletions insert-ebay.sh

This file was deleted.

37 changes: 8 additions & 29 deletions insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,27 @@

import pyfiglet

from modules.clean import cleanup_record
from modules.validate import validate_record, is_domain_or_ip
from modules.clean import format_record, split_records
from modules.validate import validate_record
from modules.status import can_be_added


def insert_records(records, blocklist_file='blocklist.txt'):
def insert_records(input_records, blocklist_file='blocklist.txt'):
records = split_records(input_records)

with open(blocklist_file, 'a') as f:
# if submitted as array of records in a list, extract the records
if type(records[0]) == list:
print('Extracting records from list')
records = records[0]

if type(records[0]) == str and ',' in records[0]:
print('Extracting records from comma separated string')
records = records[0].split(',')

for record in records:
record = cleanup_record(record)

if not can_be_added(record):
continue

whitelist = False

# If allow list item, remove the @@ prefix for validations
if record.startswith('@@'):
whitelist = True
record = record[2:]

if '/' in record:
split_record = record.split('/', 1)
if is_domain_or_ip(split_record[0]):
record = f'||{split_record[0]}/{split_record[1]}^'
record = format_record(record)

elif record.startswith('||'):
record = f'{record}^'
elif record.startswith('^') and record.endswith('^'):
record = f'{record}^'
elif record.endswith('.js'):
record = f'^{record}^'
elif is_domain_or_ip(record):
record = f'||{record}^'
if not can_be_added(record):
continue

if not validate_record(record):
continue
Expand Down
Loading

0 comments on commit bc0bd9c

Please sign in to comment.