-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support alternative raw without 0.0.0.0 #2756
base: master
Are you sure you want to change the base?
Changes from all commits
4a06f1d
f452749
5da4bea
cbe9f36
a552043
41f6207
560a8a6
a577961
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ myhosts | |
blacklist | ||
whitelist | ||
hosts-* | ||
domains.txt | ||
/web.config | ||
/__pycache__ | ||
/node_modules/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,8 @@ | |
|
||
# Project Settings | ||
BASEDIR_PATH = os.path.dirname(os.path.realpath(__file__)) | ||
HOSTS_FILENAME = "hosts" | ||
DOMAINS_FILENAME = "domains.txt" | ||
|
||
|
||
def get_defaults(): | ||
|
@@ -234,6 +236,7 @@ | |
options = vars(parser.parse_args()) | ||
|
||
options["outputpath"] = path_join_robust(BASEDIR_PATH, options["outputsubfolder"]) | ||
options["outputfilename"] = HOSTS_FILENAME | ||
options["freshen"] = not options["noupdate"] | ||
|
||
settings = get_defaults() | ||
|
@@ -259,6 +262,14 @@ | |
source_data_filename = settings["sourcedatafilename"] | ||
no_unified_hosts = settings["nounifiedhosts"] | ||
|
||
settings["targetip"] = ( | ||
None if str(settings["targetip"]).lower() == "none" else settings["targetip"] | ||
) | ||
if settings["targetip"] is None: | ||
settings["skipstatichosts"] = True | ||
settings["keepdomaincomments"] = False | ||
options["outputfilename"] = DOMAINS_FILENAME | ||
|
||
update_sources = prompt_for_update(freshen=settings["freshen"], update_auto=auto) | ||
if update_sources: | ||
update_all_sources(source_data_filename, settings["hostfilename"]) | ||
|
@@ -287,19 +298,23 @@ | |
merge_file = create_initial_file( | ||
nounifiedhosts=no_unified_hosts, | ||
) | ||
remove_old_hosts_file(settings["outputpath"], "hosts", settings["backup"]) | ||
remove_old_hosts_file( | ||
settings["outputpath"], options["outputfilename"], settings["backup"] | ||
) | ||
|
||
final_file = open( | ||
path_join_robust(settings["outputpath"], options["outputfilename"]), "w+b" | ||
) | ||
Comment on lines
+305
to
+307
Check warning Code scanning / CodeQL File is not always closed Warning
File may not be closed if an exception is raised.
|
||
temp_file = tempfile.NamedTemporaryFile() | ||
remove_dups_and_excl(merge_file, exclusion_regexes, temp_file) | ||
|
||
if settings["compress"]: | ||
final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b") | ||
compressed_file = tempfile.NamedTemporaryFile() | ||
remove_dups_and_excl(merge_file, exclusion_regexes, compressed_file) | ||
compress_file(compressed_file, settings["targetip"], final_file) | ||
compress_file(temp_file, settings["targetip"], final_file) | ||
elif settings["minimise"]: | ||
final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b") | ||
minimised_file = tempfile.NamedTemporaryFile() | ||
remove_dups_and_excl(merge_file, exclusion_regexes, minimised_file) | ||
minimise_file(minimised_file, settings["targetip"], final_file) | ||
minimise_file(temp_file, settings["targetip"], final_file) | ||
else: | ||
final_file = remove_dups_and_excl(merge_file, exclusion_regexes) | ||
shutil.copy(temp_file.name, final_file.name) | ||
temp_file.close() | ||
|
||
number_of_rules = settings["numberofrules"] | ||
output_subfolder = settings["outputsubfolder"] | ||
|
@@ -853,12 +868,16 @@ | |
---------- | ||
input_file : file | ||
The file object that contains the hostnames that we are reducing. | ||
target_ip : str | ||
target_ip : str | None | ||
The target IP address. | ||
output_file : file | ||
The file object that will contain the reduced hostnames. | ||
""" | ||
|
||
if target_ip is None: | ||
print("Compress file is not supported with targetip to None") | ||
return | ||
|
||
input_file.seek(0) # reset file pointer | ||
write_data(output_file, "\n") | ||
|
||
|
@@ -893,7 +912,7 @@ | |
---------- | ||
input_file : file | ||
The file object that contains the hostnames that we are reducing. | ||
target_ip : str | ||
target_ip : str | None | ||
The target IP address. | ||
output_file : file | ||
The file object that will contain the reduced hostnames. | ||
|
@@ -906,16 +925,18 @@ | |
for line in input_file.readlines(): | ||
line = line.decode("UTF-8") | ||
|
||
if line.startswith(target_ip): | ||
lines.append(line[: line.find("#")].strip() + "\n") | ||
if target_ip is None or line.startswith(target_ip): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it was desired, there was some empty |
||
minimised_line = line[: line.find("#")].strip() + "\n" | ||
if minimised_line != "\n": | ||
lines.append(minimised_line) | ||
|
||
for line in lines: | ||
write_data(output_file, line) | ||
|
||
input_file.close() | ||
|
||
|
||
def remove_dups_and_excl(merge_file, exclusion_regexes, output_file=None): | ||
def remove_dups_and_excl(merge_file, exclusion_regexes, output_file): | ||
""" | ||
Remove duplicates and remove hosts that we are excluding. | ||
|
||
|
@@ -929,8 +950,7 @@ | |
exclusion_regexes : list | ||
The list of regex patterns used to exclude domains. | ||
output_file : file | ||
The file object in which the result is written. If None, the file | ||
'settings["outputpath"]' will be created. | ||
The file object in which the result is written. | ||
""" | ||
|
||
number_of_rules = settings["numberofrules"] | ||
|
@@ -943,14 +963,6 @@ | |
if line and not line.startswith("#"): | ||
settings["exclusions"].append(line) | ||
|
||
if not os.path.exists(settings["outputpath"]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed output_file to required, and I made a few changes here, the file is already ensure created by |
||
os.makedirs(settings["outputpath"]) | ||
|
||
if output_file is None: | ||
final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b") | ||
else: | ||
final_file = output_file | ||
|
||
merge_file.seek(0) # reset file pointer | ||
hostnames = {"localhost", "localhost.localdomain", "local", "broadcasthost"} | ||
exclusions = settings["exclusions"] | ||
|
@@ -969,7 +981,7 @@ | |
|
||
# Testing the first character doesn't require startswith | ||
if line[0] == "#" or re.match(r"^\s*$", line[0]): | ||
write_data(final_file, line) | ||
write_data(output_file, line) | ||
continue | ||
if "::1" in line: | ||
continue | ||
|
@@ -995,15 +1007,14 @@ | |
break | ||
|
||
if normalized_rule and (hostname not in hostnames) and write_line: | ||
write_data(final_file, normalized_rule) | ||
write_data(output_file, normalized_rule) | ||
hostnames.add(hostname) | ||
number_of_rules += 1 | ||
|
||
settings["numberofrules"] = number_of_rules | ||
merge_file.close() | ||
|
||
if output_file is None: | ||
return final_file | ||
return output_file | ||
|
||
|
||
def normalize_rule(rule, target_ip, keep_domain_comments): | ||
|
@@ -1014,7 +1025,7 @@ | |
---------- | ||
rule : str | ||
The rule whose spelling and spacing we are standardizing. | ||
target_ip : str | ||
target_ip : str | None | ||
The target IP address for the rule. | ||
keep_domain_comments : bool | ||
Whether or not to keep comments regarding these domains in | ||
|
@@ -1048,7 +1059,10 @@ | |
and spacing reformatted. | ||
""" | ||
|
||
rule = "%s %s" % (target_ip, extracted_hostname) | ||
if target_ip is None: | ||
rule = extracted_hostname | ||
else: | ||
rule = "%s %s" % (target_ip, extracted_hostname) | ||
|
||
if keep_domain_comments and extracted_suffix: | ||
if not extracted_suffix.strip().startswith("#"): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made few changes here since I introduced options["outputfilename"]