Skip to content

Commit

Permalink
vendor/aicp: Toggle-able adblock hosts file [2/3]
Browse files Browse the repository at this point in the history
Regenerate with ./adaway/generate.sh

Change-Id: Ia389457746f9af1060a1c1ae62af0cb9ca643a71
  • Loading branch information
SpiritCroc authored and semdoc committed Apr 26, 2020
1 parent 8161296 commit 9867edd
Show file tree
Hide file tree
Showing 6 changed files with 15,321 additions and 0 deletions.
2 changes: 2 additions & 0 deletions adaway/.hosts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
54 changes: 54 additions & 0 deletions adaway/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3

from datetime import datetime
import sys

def write_header(f, sources):
f.write("# This hosts file has been generated on:\n# {}\n# This file is generated from the following sources:\n".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
for s in sources:
f.write("# {}\n".format(s))
f.write("\n127.0.0.1 localhost\n::1 localhost\n\n")

def read_redirect(line):
source = None
target = None
for l in line.split(" "):
if l.startswith("#"):
# Ignore comments
return None
if len(l) > 0:
if source == None:
source = l
else:
target = l
return (source, target)
return None

if __name__ == "__main__":
source_dir = sys.argv[1]
outfile = sys.argv[2]
sources = sys.argv[3:]
redirects = []
source_urls = []
with open("{}/sources.txt".format(source_dir), "r") as fin:
for line in fin:
line = line.split("\n")[0]
if len(line) > 0:
source_urls.append(line)
with open(outfile, 'w') as fout:
write_header(fout, source_urls)
# Read hosts sources
for infile in sources:
with open(infile, 'r') as fin:
for line in fin:
line = line.split('\n')[0]
redirect = read_redirect(line)
if redirect != None:
redirects.append(redirect)
# Remove duplicates and sort it
redirects = sorted(list(set(redirects)))
# Write it
for redirect in redirects:
if redirect[1] == "localhost":
continue
fout.write("127.0.0.1 {}\n".format(redirect[1]))
24 changes: 24 additions & 0 deletions adaway/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

mydir="$(dirname "$(realpath "$0")")"

hosts_dir="$mydir/.hosts/"
hosts_out="$mydir/../prebuilt/common/etc/hosts.aicp_adblock"

download_hosts() {
url="$1"
outfile="$2"
wget "$url" -O "$outfile"
}


rm "$hosts_dir"/*

i=0

while read l; do
((i++))
download_hosts "$l" "$hosts_dir/hosts$i.txt"
done < "$mydir/sources.txt"

python3 "$mydir/generate.py" "$mydir" "$hosts_out" "$hosts_dir"/*
2 changes: 2 additions & 0 deletions adaway/sources.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://adaway.org/hosts.txt
https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext
4 changes: 4 additions & 0 deletions config/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,9 @@ PRODUCT_COPY_FILES += \
vendor/aicp/prebuilt/common/lib/libsketchology_native.so:$(TARGET_COPY_OUT_SYSTEM)/lib/libsketchology_native.so \
vendor/aicp/prebuilt/common/lib64/libsketchology_native.so:$(TARGET_COPY_OUT_SYSTEM)/lib64/libsketchology_native.so

# Ad-block hosts
PRODUCT_COPY_FILES += \
vendor/aicp/prebuilt/common/etc/hosts.aicp_adblock:system/etc/hosts.aicp_adblock

# Disable vendor restrictions
PRODUCT_RESTRICT_VENDOR_FILES := false
Loading

0 comments on commit 9867edd

Please sign in to comment.