Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

ldlac
Copy link

@ldlac ldlac commented Oct 16, 2024

Support this request #2719

how it should be used python updateHostsFile.py -a -g -i None

I made it that it cannot be combined with -c or -m, should it be?

Copy link

welcome bot commented Oct 16, 2024

Thank you for submitting this pull request! We’ll get back to you as soon as we can!

@@ -259,6 +259,10 @@ def main():
source_data_filename = settings["sourcedatafilename"]
no_unified_hosts = settings["nounifiedhosts"]

settings["targetip"] = (
None if settings["targetip"] == "None" else settings["targetip"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I preferred to go with an explicit None instead of ""

@StevenBlack
Copy link
Owner

Hey @ldlac thank you for this! Looks cool! Looks thisclose 🤏🏻 to mergable, right out of the box, which is awesome 👍🏻

Can I please ask for two additional things?

  • Can you please make -- ip None case insensitive? So -- ip none will work too. I might go as far as suggesting anything with non-numeric first character following --ip should probably work too, but I leave this to you to decide.

  • Can you please add documentation in the Command line options section of readme_template.md file?

Good work!

@ldlac
Copy link
Author

ldlac commented Oct 18, 2024

Good recommendation for the --ip flag

I added an insightful explanation in the readme, let me know what you think

@ldlac
Copy link
Author

ldlac commented Oct 18, 2024

By doing so, we'll break ipv6 no?

@StevenBlack
Copy link
Owner

@ldlac good point.

@ldlac
Copy link
Author

ldlac commented Oct 19, 2024

I changed it to str(settings["targetip"]).lower() == "none" which will make it case insensitive

@StevenBlack
Copy link
Owner

@ldlac thank you very much! All tests pass, and all this behaves exactly as expected.

Except for one thing 😄

With the --ip none flag here are the first 50-lines of what is now not a hosts file (see below ⬇️⬇️)

Observe: we still get that header section with 0.0.0.0 localhost etc.

Expected: (well, I would expect...) no such header, since this is not a hosts file now. So maybe this PR should also bracket the code that writes the header section.

I also now wonder... should the --ip none flag be a super flag, override every other flag, and produce a file named domains.txt imstead of hosts, and additionally strip all comments and blank lines, and sort the domains too?

I'm willing to be convinced otherwise in any and all these things.

Welcome to Open Source 🙃

The first 50 lines of the (misnamed) hosts file produced by the --ip none flag:

# Title: StevenBlack/hosts
#
# This hosts file is a merged collection of hosts from reputable sources,
# with a dash of crowd sourcing via GitHub
#
# Date: 19 October 2024 23:10:22 (UTC)
# Number of unique domains: 117,213
#
# Fetch the latest version of this file: https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
# Project home page: https://github.com/StevenBlack/hosts
# Project releases: https://github.com/StevenBlack/hosts/releases
#
# ===============================================================

127.0.0.1 localhost
127.0.0.1 localhost.localdomain
127.0.0.1 local
255.255.255.255 broadcasthost
::1 localhost
::1 ip6-localhost
::1 ip6-loopback
fe80::1%lo0 localhost
ff00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
0.0.0.0 0.0.0.0

# Custom host records are listed here.


# End of custom host records.
# Start StevenBlack

#=====================================
# Title: Hosts contributed by Steven Black
# http://stevenblack.com

ad-assets.futurecdn.net
ck.getcookiestxt.com
eu1.clevertap-prod.com
wizhumpgyros.com
coccyxwickimp.com
webmail-who-int.000webhostapp.com
010sec.com
01mspmd5yalky8.com
0byv9mgbn0.com
ns6.0pendns.org
dns.0pengl.com

@StevenBlack
Copy link
Owner

@ldlac an idea: we already have internal support for some of what I suggest above ⬆️ maybe the --ip none flag would set these things automatically?

--auto, -a            Run without prompting.

--keepdomaincomments, -k
                        Do not keep domain line comments.

--minimise, -m        Minimise the hosts file ignoring non-necessary lines
                        (empty lines and comments).

--noupdate, -n        Don't update from host data sources.

--skipstatichosts, -s
                        Skip static localhost entries in the final hosts file.

--skipstatichosts, -s
                        Skip static localhost entries in the final hosts file.

@@ -287,19 +302,22 @@ def main():
merge_file = create_initial_file(
nounifiedhosts=no_unified_hosts,
)
remove_old_hosts_file(settings["outputpath"], "hosts", settings["backup"])
Copy link
Author

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"]

@@ -943,14 +966,6 @@ def remove_dups_and_excl(merge_file, exclusion_regexes, output_file=None):
if line and not line.startswith("#"):
settings["exclusions"].append(line)

if not os.path.exists(settings["outputpath"]):
Copy link
Author

Choose a reason for hiding this comment

The 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 remove_old_hosts_file

@@ -906,16 +928,18 @@ def minimise_file(input_file, target_ip, output_file):
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):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it was desired, there was some empty \n with the minimised flag, this will fix those

@ldlac
Copy link
Author

ldlac commented Oct 20, 2024

Thanks for the feedback, I added 3 comments to support my changes to support the domains.txt format

Let me know if you think we should modify the header, I think it's fine even it if it mentions This hosts file is a merged collection ...

There's a few things that still bother me, the blacklist, the custom host file, those could still be pass as arg with the --ip none. If they contain the default content - only comments - these lines will be stripped

Comment on lines +309 to +311
final_file = open(
path_join_robust(settings["outputpath"], options["outputfilename"]), "w+b"
)

Check warning

Code scanning / CodeQL

File is not always closed Warning

File may not be closed if an exception is raised.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants