Giter Club home page Giter Club logo

cli's Introduction

goodhosts cli, fork of Lex Toumbourou's project goodhosts

codecov Go Reference Go Report Card

Simple hosts file (/etc/hosts) management in a Go cli. One simple interface for any OS or architecture, script and automate hosts file updates using one simple tool.

Features

  • List, add, remove and check hosts file entries from code or the command-line
    • Remove by IP, Host, or IP/Host
    • check returns proper exit codes for scripting e.g. goodhosts check 10.0.5.12 || echo "Missing hosts entry for 10.0.5.12"
  • Clean hostsfile command will
  • Consolidate duplicate IPs
  • Remove duplicate hosts
  • Alpha sort Hosts
  • Sort IPs
  • Help with OS limitations, e.g. 9 hosts per IP line in windows
  • linux/darwin/windows support
  • Custom hosts file support
  • Backup/Restore
  • Quick inline editor (vim/nano)

Installation

Visit the releases page and download the proper binary for your architecture. Unzip and run in place, put in your system path (linux: /usr/local/bin win: ~/bin) for easier access.

Usage

For full usage directions simply call goodhosts -h

$ goodhosts -h
  NAME:
    goodhosts - manage your hosts file goodly

  USAGE:
     goodosts [global options] command [command options] [arguments...]
  
  COMMANDS:
     add, a         Add an entry to the hostsfile
     backup         Backup hosts file
     check, c       Check if ip or host exists
     clean, cl      Clean the hostsfile by doing: remove dupe IPs, for each IPs remove dupe hosts and sort, sort all IPs, split hosts per OS limitations
     debug, d       Show debug table for hosts file
     edit, e        Open hosts file in an editor, default vim
     list, ls       List all entries in the hostsfile
     remove, rm, r  Remove ip or host(s) if exists
     restore        Restore hosts file from backup
     version
     help, h        Shows a list of commands or help for one command
  
  GLOBAL OPTIONS:
     --file value, -f value  override the default hosts: ${SystemRoot}/System32/drivers/etc/hosts
     --debug, -d             Turn on verbose debug logging (default: false)
     --quiet, -q             Turn on off all logging (default: false)
     --help, -h              show help (default: false)

Each sub-command can be called with a -h option to see detailed help information.

 $ ./goodhosts list -h
 NAME:
    goodhosts list - List all entries in the hostsfile

 USAGE:
    goodhosts list [command options] [arguments...]

 OPTIONS:
    --all       Show all entries in the hosts file including commented lines. (default: false)
    --help, -h  show help (default: false)

License

MIT

cli's People

Contributors

edwardstudy avatar jimmystewpot avatar lextoumbourou avatar luthermonson avatar praveenkumar avatar tomjn avatar yunginnanet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cli's Issues

Breaks Windows hosts file line length rules

Per goodhosts/vagrant#28, Windows only supports 9 aliases per line in the hosts file. While the specifics of what Windows will or won't support don't seem to be super well documented, I also don't see explicit documented support for multiple aliases per line.

In my experience, too many entries on a single line does prevent aliases further down the line from working.

There may be a need to force each entry onto its own line.

Windows 10 Home, v21H1, OS build 19043.1387

Bug: Comment detection on host line is missing

If a line include a comment and we are adding to the same ip new domain they are added after the comment example:

192.168.50.4 vvv # VAGRANT: db53e7b49dc0f3aa85fa47b3d00a40e6 (default) / e605922e-40d0-4629-9db0-8bf0ce7d3670 vvv.test auto.test tideways.vvv.test xhgui.vvv.test

If I do

cli r vvv

The line is:

192.168.50.4 # VAGRANT: db53e7b49dc0f3aa85fa47b3d00a40e6 (default) / e605922e-40d0-4629-9db0-8bf0ce7d3670 vvv.test auto.test tideways.vvv.test xhgui.vvv.test

So basically if we are adding or deleting something this changes will happens anyway with the comment but new stuff will be appended after the comment.

Windows maximum aliases per line

As seen here https://superuser.com/questions/932112/is-there-a-maximum-number-of-hostname-aliases-per-line-in-a-windows-hosts-file, there is a maximum of nine aliases per line in the hosts file on Windows.

I am often beyond that maximum of nine aliases in the VVV environments I use, meaning some of my sites don't work without manually splitting the aliases into multiple lines.

Assuming that I'm not the only one running into this, I would like to request a feature that automatically splits the aliases into multiple lines if there are more than nine.

Thanks!

Q: Maintenance

Hello!

I'm evaluating using this in conjunction with a vagrant project, and I'm wondering what governance of the org is.

I ask because pretty much every vagrant plugin for modifying hosts files has been abandoned or put in a limbo state, even if people were willing to contribute the original author went AWOL with no way of delegating to a new maintainer.

I see this library is based on one such project, and that there are a few go based alternatives that are in similar boats.

I'd really like to avoid having to maintain a legacy ruby vagrant plugin :( I'd rather write a wrapper around a CLI tool like this. Is there an associated project that relies heavily on this that has some maintainer overlap? Or a process for if the current maintainers no longer use/need the project for me or someone else to contribute fixes?

Crosspost: https://github.com/goodhosts/hostsfile/issues/39

See goodhosts/hostsfile#39

[...]

hosts r ff00::0

Version[s] reproduced with

(Using goodhosts/cli in all cases)

* [x]  Vendored copy that comes with [goodhosts/cli](https://github.com/goodhosts/cli)

* [x]  Latest release

* [x]  Main branch

[...]

removing ip ff00::0
panic: runtime error: slice bounds out of range [5:3]

goroutine 1 [running]:
github.com/goodhosts/hostsfile.(*Hosts).removeByPosition(...)
	/home/kayos/go/pkg/mod/github.com/goodhosts/[email protected]/hosts.go:390
github.com/goodhosts/hostsfile.(*Hosts).RemoveByIp(0xc0002f0770, {0x7fff5401b761?, 0x660254?})
	/home/kayos/go/pkg/mod/github.com/goodhosts/[email protected]/hosts.go:282 +0x1ff
github.com/goodhosts/cli/cmd.processSingleArg(0xc00029fae8?, {0x7fff5401b761, 0x7})
	/home/kayos/Workshop/goodhosts/cli/cmd/remove.go:97 +0xa9
github.com/goodhosts/cli/cmd.remove(0xc0002ee500)
	/home/kayos/Workshop/goodhosts/cli/cmd/remove.go:45 +0x145
github.com/urfave/cli/v2.(*Command).Run(0xc0001aab40, 0xc0002ee340)
	/home/kayos/go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:163 +0x5bb
github.com/urfave/cli/v2.(*App).RunContext(0xc0001a4d00, {0x6c90d8?, 0xc0000140b0}, {0xc000010180, 0x3, 0x3})
	/home/kayos/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:313 +0xb48
github.com/urfave/cli/v2.(*App).Run(...)
	/home/kayos/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:224
main.main()
	/home/kayos/Workshop/goodhosts/cli/main.go:59 +0x414

I believe the issue primarily exists in this tool.

False assumption: IPs appear in a single position

https://github.com/goodhosts/hostsfile/blob/master/hosts.go#L103

The code that adds a host performs a check to see if a line with the IP is already present, and if so, it inspects the hosts on that line and only adds the host if it is not listed.

However, this assumption is not true.

For example, consider this hosts file:

127.0.0.1 tom.test
127.0.0.1 tom.test example.test

Goodhosts will pick up the first line, see that example.test is not present, and add the host, leading to duplication.

Instead, this needs to be a loop, and position := h.getIpPosition(ip) needs to be positions := h.getIpPositions(ip).

Likewise, if an existing entry is present, this code does not remove it, which can lead to hostnames being defined multiple times but with different IPs

Goodhosts issues on OSX

We are getting some crashes on OSX.

==> default: Issue executing goodhosts CLI: panic: runtime error: slice bounds out of range [381:379]
==> default: 
==> default: goroutine 1 [running]:
==> default: github.com/goodhosts/hostsfile.(*Hosts).removeByPosition(...)
==> default: 	/home/runner/go/pkg/mod/github.com/goodhosts/[email protected]/hosts.go:297
==> default: github.com/goodhosts/hostsfile.(*Hosts).combineIp(0xc0000d9ac8, 0xc0000145e0, 0x7)
==> default: 	/home/runner/go/pkg/mod/github.com/goodhosts/[email protected]/hosts.go:285 +0x582
==> default: github.com/goodhosts/hostsfile.(*Hosts).RemoveDuplicateIps(0xc0000d9ac8)
==> default: 	/home/runner/go/pkg/mod/github.com/goodhosts/[email protected]/hosts.go:224 +0x1f3
==> default: github.com/goodhosts/hostsfile.(*Hosts).Clean(0xc0000d9ac8)
==> default: 	/home/runner/go/pkg/mod/github.com/goodhosts/[email protected]/hosts.go:130 +0x40
==> default: github.com/goodhosts/cli/cmd.remove(0xc00001ea00, 0x3, 0x4)
==> default: 	/home/runner/work/cli/cli/cmd/remove.go:75 +0x7f3
==> default: github.com/urfave/cli/v2.(*Command).Run(0xc0000127e0, 0xc00001e840, 0x0, 0x0)
==> default: 	/home/runner/go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:161 +0x4b9
==> default: github.com/urfave/cli/v2.(*App).RunContext(0xc000001200, 0x127d980, 0xc00001a080, 0xc000010050, 0x5, 0x5, 0x0, 0x0)
==> default: 	/home/runner/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:302 +0x790
==> default: github.com/urfave/cli/v2.(*App).Run(...)
==> default: 	/home/runner/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:211
==> default: main.main()
==> default: 	/home/runner/work/cli/cli/main.go:55 +0x48e

Seems the line with the issue is https://github.com/goodhosts/hostsfile/blob/master/hosts.go#L297

Fix BOM

msg="\ufeff# Copyright (c) 1993-2009 Microsoft Corp. # <<< Malformed!"

Goodhosts' rewrite of the hosts file renders MAMP incapable of managing its entries

I am running MAMP Pro and VVV simultaneously, and sometimes concurrently. When MAMP Pro has first crack at the /etc/hosts file, it ends up looking like this:

127.0.0.1	scratch.test	# MAMP PRO - Do NOT remove this entry!
::1		scratch.test	# MAMP PRO - Do NOT remove this entry!
127.0.0.1	clean.test	# MAMP PRO - Do NOT remove this entry!
::1		clean.test	# MAMP PRO - Do NOT remove this entry!
127.0.0.1	cnmd.test	# MAMP PRO - Do NOT remove this entry!
::1		cnmd.test	# MAMP PRO - Do NOT remove this entry!
127.0.0.1	boilerplate.test	# MAMP PRO - Do NOT remove this entry!
::1		boilerplate.test	# MAMP PRO - Do NOT remove this entry!
127.0.0.1	macster.local	# MAMP PRO - Do NOT remove this entry!
::1		macster.local	# MAMP PRO - Do NOT remove this entry!

etc etc. Adding and removing hosts via MAMP Pro works correctly, and shutting down MAMP Pro removes all entries it controls. All fine.

When I fire up MAMP Pro and then fire up VVV, /etc/hosts gets rewritten so that I end up with this

::1 boilerplate.test clean.test cnmd.test macster.local scratch.test # MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!
127.0.0.1 boilerplate.test clean.test cnmd.test macster.local scratch.test # MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!  MAMP PRO - Do NOT remove this entry!
192.168.50.4 vvv vvv.test rfa.test # VAGRANT: 32549be2774dded0939569e71b56d8f1 (default) / 465e8518-e2b5-4ae0-9348-e56e8662ab2d
255.255.255.255 broadcasthost

IOW they have gone from one-per-line pairs to a single entry. This rewrite prevents MAMP from being able to manage its entries.

As technically correct as this may be, this is a bad scene. Is there a way to have GH just do its thing without modifying anything else?

More clean options

The clean subcommand does the following:

Clean the hostsfile by doing: remove dupe IPs, for each IPs remove dupe hosts and sort, sort all IPs, split hosts per OS limitations

However, some of these may be undesirable. For example sorting and merging could interfere with other software that relies on modifying the hosts file.

For example:

::1 localhost testwplocaltest.local www.testwplocaltest.local #Local Site Local Site

Here several items the OS added to the hosts file have been merged with entries added by the WP Engine Local software. If that software was lazy it might remove that entire line when removing that site.

Similarly a group of docker domains got smushed together on the same line with several other domains because they all used 127.0.0.1, and the comments surrounding them have vanished.

So, it would be good to have separate options/flags for:

  • merging entries on to the same line
  • de-duping domains
  • de-duping lines
  • etc

goodhosts stalls when the /etc/hosts file has too many entries

I block malware sites and ads from the internet using a large hosts file (600.000+ lines), redirecting all these bad hosts to 0.0.0.0. When I try to use goodhosts, it just freezes and whatever process I was on won't go on. If I remove most of the entries form /etc/hosts, the process completes without problems. Is this a bug or expected behavior?

Goodhosts check requires a writable hosts file

The check subcommand requires a writable hosts file, but it doesn't write to the hosts file. I should be able to use the check subcommand without needing to use sudo or changing the write permissions of the hosts file if it's readable to all users

❯ goodhosts check vvv.test
Host file not writable. Try running with elevated privileges.
❯ sudo goodhosts check vvv.test
vvv.test exists in hosts file

Add a version subcommand or parameter

It's not possible to check which version of goodhosts is installed via the command, it would be great to be able to run goodhost --version or have the version/commit printed in the help output

Goodhosts don't remove duplicates

We are facing some issues with the vagrant plugin that when we add new hosts, they are added creating things like:

192.168.50.4 vvv vvv.test boilerplate.test woocommerce.test glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test boilerplate.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test tideways.vvv.test xhgui.vvv.test vvv vvv.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test src.wordpress-develop.test build.wordpress-develop.test tideways.vvv.test xhgui.vvv.test vvv vvv.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test src.wordpress-develop.test build.wordpress-develop.test tideways.vvv.test xhgui.vvv.test vvv vvv.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test src.wordpress-develop.test build.wordpress-develop.test tideways.vvv.test xhgui.vvv.test vvv vvv.test woocommerce.test 503702305c22.ngrok.io auto.test glossary.test src.wordpress-develop.test build.wordpress-develop.test tideways.vvv.test xhgui.vvv.test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.