mirror of
https://github.com/octoleo/hosts.git
synced 2024-11-25 14:17:48 +00:00
Handle multiple hostnames with block
and unblock
.
This commit is contained in:
parent
ab2a193371
commit
ada8cb6019
14
README.md
14
README.md
@ -46,7 +46,7 @@ A package for Arch users is also [available in the AUR](https://aur.archlinux.or
|
|||||||
Usage:
|
Usage:
|
||||||
hosts [<search string>]
|
hosts [<search string>]
|
||||||
hosts add <ip> <hostname> [<comment>]
|
hosts add <ip> <hostname> [<comment>]
|
||||||
hosts block <hostname>
|
hosts block <hostname>...
|
||||||
hosts disable (<ip> | <hostname> | <search string>)
|
hosts disable (<ip> | <hostname> | <search string>)
|
||||||
hosts disabled
|
hosts disabled
|
||||||
hosts edit
|
hosts edit
|
||||||
@ -57,7 +57,7 @@ Usage:
|
|||||||
hosts search <search string>
|
hosts search <search string>
|
||||||
hosts show (<ip> | <hostname> | <search string>)
|
hosts show (<ip> | <hostname> | <search string>)
|
||||||
hosts remove (<ip> | <hostname> | <search string>) [--force]
|
hosts remove (<ip> | <hostname> | <search string>) [--force]
|
||||||
hosts unblock <hostname>
|
hosts unblock <hostname>...
|
||||||
hosts --auto-sudo
|
hosts --auto-sudo
|
||||||
hosts -h | --help
|
hosts -h | --help
|
||||||
hosts --version
|
hosts --version
|
||||||
@ -113,11 +113,11 @@ Description:
|
|||||||
|
|
||||||
```text
|
```text
|
||||||
Usage:
|
Usage:
|
||||||
hosts block <hostname>
|
hosts block <hostname>...
|
||||||
|
|
||||||
Description:
|
Description:
|
||||||
Block a given hostname by adding new entries assigning it to `127.0.0.1`
|
Block one or more hostnames by adding new entries assigned to \`127.0.0.1\`
|
||||||
for IPv4 and both `fe80::1%lo0` and `::1` for IPv6.
|
for IPv4 and both \`fe80::1%lo0\` and \`::1\` for IPv6.
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Blocklists
|
#### Blocklists
|
||||||
@ -262,10 +262,10 @@ Description:
|
|||||||
|
|
||||||
```text
|
```text
|
||||||
Usage:
|
Usage:
|
||||||
hosts unblock <hostname>
|
hosts unblock <hostname>...
|
||||||
|
|
||||||
Description:
|
Description:
|
||||||
Unblock a given hostname by removing its entries from the hosts file.
|
Unblock one or more hostnames by removing the entries from the hosts file.
|
||||||
```
|
```
|
||||||
|
|
||||||
### `hosts version`
|
### `hosts version`
|
||||||
|
30
hosts
30
hosts
@ -611,7 +611,7 @@ Version: ${_VERSION}
|
|||||||
Usage:
|
Usage:
|
||||||
${_ME} [<search string>]
|
${_ME} [<search string>]
|
||||||
${_ME} add <ip> <hostname> [<comment>]
|
${_ME} add <ip> <hostname> [<comment>]
|
||||||
${_ME} block <hostname>
|
${_ME} block <hostname>...
|
||||||
${_ME} disable (<ip> | <hostname> | <search string>)
|
${_ME} disable (<ip> | <hostname> | <search string>)
|
||||||
${_ME} disabled
|
${_ME} disabled
|
||||||
${_ME} edit
|
${_ME} edit
|
||||||
@ -622,7 +622,7 @@ Usage:
|
|||||||
${_ME} search <search string>
|
${_ME} search <search string>
|
||||||
${_ME} show (<ip> | <hostname> | <search string>)
|
${_ME} show (<ip> | <hostname> | <search string>)
|
||||||
${_ME} remove (<ip> | <hostname> | <search string>) [--force]
|
${_ME} remove (<ip> | <hostname> | <search string>) [--force]
|
||||||
${_ME} unblock <hostname>
|
${_ME} unblock <hostname>...
|
||||||
${_ME} --auto-sudo
|
${_ME} --auto-sudo
|
||||||
${_ME} -h | --help
|
${_ME} -h | --help
|
||||||
${_ME} --version
|
${_ME} --version
|
||||||
@ -754,10 +754,10 @@ add() {
|
|||||||
|
|
||||||
desc "block" <<HEREDOC
|
desc "block" <<HEREDOC
|
||||||
Usage:
|
Usage:
|
||||||
$_ME block <hostname>
|
$_ME block <hostname>...
|
||||||
|
|
||||||
Description:
|
Description:
|
||||||
Block a given hostname by adding new entries assigning it to \`127.0.0.1\`
|
Block one or more hostnames by adding new entries assigned to \`127.0.0.1\`
|
||||||
for IPv4 and both \`fe80::1%lo0\` and \`::1\` for IPv6.
|
for IPv4 and both \`fe80::1%lo0\` and \`::1\` for IPv6.
|
||||||
HEREDOC
|
HEREDOC
|
||||||
block() {
|
block() {
|
||||||
@ -769,10 +769,13 @@ block() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
"${_ME}" add 127.0.0.1 "${1}"
|
for __hostname in "${@}"
|
||||||
|
do
|
||||||
|
"${_ME}" add 127.0.0.1 "${__hostname}"
|
||||||
# block IPv6
|
# block IPv6
|
||||||
"${_ME}" add "fe80::1%lo0" "${1}"
|
"${_ME}" add "fe80::1%lo0" "${__hostname}"
|
||||||
"${_ME}" add "::1" "${1}"
|
"${_ME}" add "::1" "${__hostname}"
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# --------------------------------------------------------------------- disable
|
# --------------------------------------------------------------------- disable
|
||||||
@ -1204,10 +1207,10 @@ show() {
|
|||||||
|
|
||||||
desc "unblock" <<HEREDOC
|
desc "unblock" <<HEREDOC
|
||||||
Usage:
|
Usage:
|
||||||
$_ME unblock <hostname>
|
$_ME unblock <hostname>...
|
||||||
|
|
||||||
Description:
|
Description:
|
||||||
Unblock a given hostname by removing its entries from the hosts file.
|
Unblock one or more hostnames by removing the entries from the hosts file.
|
||||||
HEREDOC
|
HEREDOC
|
||||||
unblock() {
|
unblock() {
|
||||||
_verify_write_permissions "$@"
|
_verify_write_permissions "$@"
|
||||||
@ -1218,10 +1221,13 @@ unblock() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
"${_ME}" remove 127.0.0.1 "${1}" --force
|
for __hostname in "${@}"
|
||||||
|
do
|
||||||
|
"${_ME}" remove 127.0.0.1 "${__hostname}" --force
|
||||||
# unblock IPv6
|
# unblock IPv6
|
||||||
"${_ME}" remove "fe80::1%lo0" "${1}" --force
|
"${_ME}" remove "fe80::1%lo0" "${__hostname}" --force
|
||||||
"${_ME}" remove "::1" "${1}" --force
|
"${_ME}" remove "::1" "${__hostname}" --force
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
@ -25,7 +25,7 @@ load test_helper
|
|||||||
printf "\${status}: %s\\n" "${status}"
|
printf "\${status}: %s\\n" "${status}"
|
||||||
printf "\${output}: '%s'\\n" "${output}"
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
[[ "${lines[0]}" == "Usage:" ]]
|
[[ "${lines[0]}" == "Usage:" ]]
|
||||||
[[ "${lines[1]}" == " hosts block <hostname>" ]]
|
[[ "${lines[1]}" == " hosts block <hostname>..." ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
# `hosts block <hostname>` #################################################
|
# `hosts block <hostname>` #################################################
|
||||||
@ -61,6 +61,47 @@ load test_helper
|
|||||||
[[ "${lines[5]}" == "::1 example.com" ]]
|
[[ "${lines[5]}" == "::1 example.com" ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# `hosts block <hostname> <hostname2>` ########################################
|
||||||
|
|
||||||
|
@test "\`block <hostname> <hostname2>\` exits with status 0." {
|
||||||
|
run "${_HOSTS}" block example.com example2.com
|
||||||
|
printf "\${status}: %s\\n" "${status}"
|
||||||
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
|
[[ ${status} -eq 0 ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "\`block <hostname> <hostname2>\` adds entries to the hosts file." {
|
||||||
|
_original="$(cat "${HOSTS_PATH}")"
|
||||||
|
|
||||||
|
run "${_HOSTS}" block example.com example2.com
|
||||||
|
printf "\${status}: %s\\n" "${status}"
|
||||||
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
|
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||||
|
_compare '127.0.0.1 example.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
||||||
|
_compare '127.0.0.1 example2.com' "$(sed -n '11p' "${HOSTS_PATH}")"
|
||||||
|
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||||
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "127.0.0.1 example.com" ]]
|
||||||
|
[[ "$(sed -n '14p' "${HOSTS_PATH}")" == "127.0.0.1 example2.com" ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "\`block <hostname> <hostname2>\` prints feedback." {
|
||||||
|
run "${_HOSTS}" block example.com example2.com
|
||||||
|
printf "\${status}: %s\\n" "${status}"
|
||||||
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
|
[[ "${lines[0]}" == "Added:" ]]
|
||||||
|
[[ "${lines[1]}" == "127.0.0.1 example.com" ]]
|
||||||
|
[[ "${lines[2]}" == "Added:" ]]
|
||||||
|
[[ "${lines[3]}" == "fe80::1%lo0 example.com" ]]
|
||||||
|
[[ "${lines[4]}" == "Added:" ]]
|
||||||
|
[[ "${lines[5]}" == "::1 example.com" ]]
|
||||||
|
[[ "${lines[6]}" == "Added:" ]]
|
||||||
|
[[ "${lines[7]}" == "127.0.0.1 example2.com" ]]
|
||||||
|
[[ "${lines[8]}" == "Added:" ]]
|
||||||
|
[[ "${lines[9]}" == "fe80::1%lo0 example2.com" ]]
|
||||||
|
[[ "${lines[10]}" == "Added:" ]]
|
||||||
|
[[ "${lines[11]}" == "::1 example2.com" ]]
|
||||||
|
}
|
||||||
|
|
||||||
# help ########################################################################
|
# help ########################################################################
|
||||||
|
|
||||||
@test "\`help block\` exits with status 0." {
|
@test "\`help block\` exits with status 0." {
|
||||||
@ -73,5 +114,5 @@ load test_helper
|
|||||||
printf "\${status}: %s\\n" "${status}"
|
printf "\${status}: %s\\n" "${status}"
|
||||||
printf "\${output}: '%s'\\n" "${output}"
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
[[ "${lines[0]}" == "Usage:" ]]
|
[[ "${lines[0]}" == "Usage:" ]]
|
||||||
[[ "${lines[1]}" == " hosts block <hostname>" ]]
|
[[ "${lines[1]}" == " hosts block <hostname>..." ]]
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ load test_helper
|
|||||||
printf "\${status}: %s\\n" "${status}"
|
printf "\${status}: %s\\n" "${status}"
|
||||||
printf "\${output}: '%s'\\n" "${output}"
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
[[ "${lines[0]}" == "Usage:" ]]
|
[[ "${lines[0]}" == "Usage:" ]]
|
||||||
[[ "${lines[1]}" == " hosts unblock <hostname>" ]]
|
[[ "${lines[1]}" == " hosts unblock <hostname>..." ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
# `hosts unblock <invalid>` ############################################
|
# `hosts unblock <invalid>` ############################################
|
||||||
@ -133,6 +133,99 @@ Removed:
|
|||||||
[[ "${output}" == "${_expected}" ]]
|
[[ "${output}" == "${_expected}" ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# `hosts unblock <hostname> <hostname2>` ######################################
|
||||||
|
|
||||||
|
@test "\`unblock <hostname> <hostname2>\` exits with status 0." {
|
||||||
|
{
|
||||||
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||||
|
run "${_HOSTS}" block example.com
|
||||||
|
run "${_HOSTS}" block example2.com
|
||||||
|
run "${_HOSTS}" add 127.0.0.1 example.net
|
||||||
|
}
|
||||||
|
|
||||||
|
run "${_HOSTS}" unblock example.com example2.com
|
||||||
|
printf "\${status}: %s\\n" "${status}"
|
||||||
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
|
[[ ${status} -eq 0 ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "\`unblock <hostname> <hostname2>\` updates the hosts file." {
|
||||||
|
{
|
||||||
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||||
|
run "${_HOSTS}" block example.com
|
||||||
|
run "${_HOSTS}" block example2.com
|
||||||
|
run "${_HOSTS}" add 127.0.0.1 example.net
|
||||||
|
}
|
||||||
|
_original="$(cat "${HOSTS_PATH}")"
|
||||||
|
|
||||||
|
run "${_HOSTS}" unblock example.com example2.com
|
||||||
|
printf "\${status}: %s\\n" "${status}"
|
||||||
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
|
_compare "${_original}" "$(cat "${HOSTS_PATH}")"
|
||||||
|
[[ "$(sed -n '11p' "${HOSTS_PATH}")" == "0.0.0.0 example.com" ]]
|
||||||
|
[[ "$(sed -n '12p' "${HOSTS_PATH}")" == "127.0.0.1 example.net" ]]
|
||||||
|
[[ "$(sed -n '13p' "${HOSTS_PATH}")" == "" ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "\`unblock <hostname> <hostname2>\` removes all matches." {
|
||||||
|
{
|
||||||
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||||
|
run "${_HOSTS}" block example.com
|
||||||
|
run "${_HOSTS}" block example2.com
|
||||||
|
run "${_HOSTS}" add 127.0.0.1 example.net
|
||||||
|
}
|
||||||
|
_original="$(cat "${HOSTS_PATH}")"
|
||||||
|
|
||||||
|
run "${_HOSTS}" unblock example.com example2.com
|
||||||
|
printf "\${status}: %s\\n" "${status}"
|
||||||
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
|
_expected="\
|
||||||
|
##
|
||||||
|
# Host Database
|
||||||
|
#
|
||||||
|
# localhost is used to configure the loopback interface
|
||||||
|
# when the system is booting. Do not change this entry.
|
||||||
|
##
|
||||||
|
127.0.0.1 localhost
|
||||||
|
255.255.255.255 broadcasthost
|
||||||
|
::1 localhost
|
||||||
|
fe80::1%lo0 localhost
|
||||||
|
0.0.0.0 example.com
|
||||||
|
127.0.0.1 example.net"
|
||||||
|
_compare "'${_expected}'" "'$(cat "${HOSTS_PATH}")'"
|
||||||
|
[[ "$(cat "${HOSTS_PATH}")" != "${_original}" ]]
|
||||||
|
[[ "$(cat "${HOSTS_PATH}")" == "${_expected}" ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "\`unblock <hostname> <hostname2>\` prints feedback." {
|
||||||
|
{
|
||||||
|
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||||
|
run "${_HOSTS}" block example.com
|
||||||
|
run "${_HOSTS}" block example2.com
|
||||||
|
run "${_HOSTS}" add 127.0.0.1 example.net
|
||||||
|
}
|
||||||
|
|
||||||
|
run "${_HOSTS}" unblock example.com example2.com
|
||||||
|
printf "\${status}: %s\\n" "${status}"
|
||||||
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
|
_expected="\
|
||||||
|
Removed:
|
||||||
|
127.0.0.1 example.com
|
||||||
|
Removed:
|
||||||
|
fe80::1%lo0 example.com
|
||||||
|
Removed:
|
||||||
|
::1 example.com
|
||||||
|
Removed:
|
||||||
|
127.0.0.1 example2.com
|
||||||
|
Removed:
|
||||||
|
fe80::1%lo0 example2.com
|
||||||
|
Removed:
|
||||||
|
::1 example2.com"
|
||||||
|
_compare "'${output}'" "'${_expected}'"
|
||||||
|
diff <(echo "${output}" ) <(echo "${_expected}")
|
||||||
|
[[ "${output}" == "${_expected}" ]]
|
||||||
|
}
|
||||||
|
|
||||||
# help ########################################################################
|
# help ########################################################################
|
||||||
|
|
||||||
@test "\`help unblock\` exits with status 0." {
|
@test "\`help unblock\` exits with status 0." {
|
||||||
@ -145,5 +238,5 @@ Removed:
|
|||||||
printf "\${status}: %s\\n" "${status}"
|
printf "\${status}: %s\\n" "${status}"
|
||||||
printf "\${output}: '%s'\\n" "${output}"
|
printf "\${output}: '%s'\\n" "${output}"
|
||||||
[[ "${lines[0]}" == "Usage:" ]]
|
[[ "${lines[0]}" == "Usage:" ]]
|
||||||
[[ "${lines[1]}" == " hosts unblock <hostname>" ]]
|
[[ "${lines[1]}" == " hosts unblock <hostname>..." ]]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user