-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshove_it_in_your_pi-hole.py
executable file
·159 lines (126 loc) · 4.96 KB
/
shove_it_in_your_pi-hole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
prog_ver = "0.0.4"
prog_desc ='''Takes a list of domains, one per line, with # as the comment char
and convert it into a format that can be directly added to the pi-hole software
pi-hole is a DNS server implementation that blocks advertisers based on DNS list
pi-hole: https://github.com/pi-hole/pi-hole
© 2022 GI_Jack. Distrbuted under the GPLv3
https://www.gnu.org/licenses/gpl-3.0.txt
'''
defaults = {
'null_addr' : "0.0.0.0",
'title' : "YourTitleHere/Hosts",
'description' : "X's list of blocked hosts for Y Reasons"
}
output_header = '''# Title: +TITLE+
# Description: +DESCRIPTION+
#
# Generated with:
# https://github.com/GIJack/stalkerware-urls/blob/shove_it_in_your_pi-hole/shove_it_in_your_pi-hole.py
# pi-hole: https://github.com/pi-hole/pi-hole
'''
import sys
import argparse
class color:
reset='\033[0m'
bold='\033[01m'
red='\033[31m'
yellow='\033[93m'
def exit_with_error(exit_code,message):
'''print error message to STDERR and exit with code'''
out_message = "shoveit_in_your_pihole.py: " + color.red + color.bold + "ERROR: " + color.reset + message
print(out_message,file=sys.stderr)
exit(exit_code)
def print_version_and_exit():
out_message = prog_ver + "\t shove_it_in_your_pi-hole.py \t ©copyright 2022 GI_Jack, GPLv3"
print(out_message)
sys.exit(4)
def message(message):
'''print formated message'''
out_message = "shoveit_in_your_pihole.py: " + message
print(out_message)
def warn(message):
'''print a warning message'''
out_message = "shoveit_in_your_pihole.py: " + color.yellow + color.bold + "WARN: " + color.reset + message
print(out_message,file=sys.stderr)
def strip_comments(in_lines):
'''strip out comments and return list of lines. should be already converted to string with decode()'''
comment = "#"
out_lines = []
for line in in_lines:
line = line.strip()
if not line.startswith(comment) and line != "":
out_lines.append(line.split(comment)[0])
return out_lines
def convert_to_pihole(in_lines):
'''convert each line to pi-hole format'''
out_lines = []
for line in in_lines:
line = defaults['null_addr'] + " " + line
out_lines.append(line)
out_lines = "\n".join(out_lines)
return out_lines
def get_lines_from_file(input_file):
'''read the input file and return the lines as a list'''
in_obj = open(input_file,'r+')
file_lines = in_obj.read()
in_obj.close()
file_lines = file_lines.split()
return file_lines
def write_output(out_lines,out_file):
'''Write Output to File. takes two parameters, out_lines, and out_file'''
try:
out_obj = open(out_file,'w')
except:
exit_with_error(1,"Could not write to output file: " + out_file + " Please check directory exists and permissions allow writing")
file_out = output_header + '\n' + out_lines
out_obj.write(file_out)
out_obj.close()
def main():
## Parse input
WARNS = 0
parser = argparse.ArgumentParser(description=prog_desc,epilog="\n\n",add_help=False,formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("input_file_list", nargs="*" , help="Input file(s) to Convert")
parser.add_argument("-?","--help" , help="Show This Help Message",action="help")
parser.add_argument("-v","--version" , help="Show version and quit",action="store_true")
parser.add_argument("-o","--output" , help="Output file. default block_list.pi_hosts",type=str)
args = parser.parse_args()
# parse file header
global output_header
output_header = output_header.replace('+TITLE+',defaults['title'])
output_header = output_header.replace('+DESCRIPTION+',defaults['description'])
## Sanity checks, and variable proccessing
if args.version == True:
print_version_and_exit()
out_file = None
if args.input_file_list == []:
exit_with_error(2,"No input file(s) given, see --help")
if args.output == None:
out_file = "block_list.pi_hosts"
else:
out_file = args.output
## Alright, lets roll
message("Proccessing file(s): " + " ".join(args.input_file_list))
message("Writing Output to: " + out_file)
out_lines = []
for in_file in args.input_file_list:
# Read from the input file
try:
in_lines = get_lines_from_file(in_file)
except:
warn("Could not read file: " + in_file)
WARNS += 1
continue
# Strip comments
out_lines += strip_comments(in_lines)
line_count = len(out_lines)
# Convert and add to list
out_lines = convert_to_pihole(out_lines)
# Write output
write_output(out_lines,out_file)
# Generate done message
out_msg = "DONE, proccessed " + str(line_count) + " domains"
if WARNS > 0:
out_msg += ", " + str(WARNS) + " File(s) could not be read"
message(out_msg)
main()