Skip to content

Commit

Permalink
[LAN_IP] Allows link-local IP addresses hiding
Browse files Browse the repository at this point in the history
> closes #137
  • Loading branch information
Samuel FORESTIER authored and HorlogeSkynet committed Aug 24, 2023
1 parent f6768f7 commit 12a0bcf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ Below stand further descriptions for each available (default) option :
"max_count": 2,
// Set to `true` if your local network does not honor RFC1918.
"show_global": false,
// Set to `false` to hide link-local IP addresses (see RFC3927).
"show_link_local": true,
// Set to `false` to only display IPv4 LAN addresses.
"ipv6_support": true
},
Expand Down
20 changes: 16 additions & 4 deletions archey/entries/lan_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ def __init__(self, *args, **kwargs):
# Global IP addresses (in RFC1918 terms) will be hidden by default.
show_global = bool(self.options.get("show_global"))

# Link-local IP addresses (in RFC3927 terms) will be shown by default.
show_link_local = bool(self.options.get("show_link_local", True))

self.value = list(
islice(self._lan_ip_addresses_generator(addr_families, show_global), max_count)
islice(
self._lan_ip_addresses_generator(addr_families, show_global, show_link_local),
max_count,
)
)

@staticmethod
def _lan_ip_addresses_generator(addr_families: list, show_global: bool) -> Iterator[str]:
def _lan_ip_addresses_generator(
addr_families: list, show_global: bool, show_link_local: bool
) -> Iterator[str]:
"""Generator yielding local IP address according to passed address families"""
# Loop through all available network interfaces.
for if_name in netifaces.interfaces():
Expand All @@ -57,8 +65,12 @@ def _lan_ip_addresses_generator(addr_families: list, show_global: bool) -> Itera
# IPv6 addresses may contain '%' token separator.
ip_addr = ipaddress.ip_address(if_addr["addr"].split("%")[0])

# Filter out loopback and public IP addresses.
if not ip_addr.is_loopback and (not ip_addr.is_global or show_global):
# Filter out loopback and public/link-local IP addresses (if enabled).
if (
not ip_addr.is_loopback
and (not ip_addr.is_global or show_global)
and (not ip_addr.is_link_local or show_link_local)
):
# Finally, yield the address compressed representation.
yield ip_addr.compressed

Expand Down
51 changes: 51 additions & 0 deletions archey/test/entries/test_archey_lan_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,57 @@ def test_show_global(self, _, __):
["192.168.1.55", "123.45.67.89"],
)

@patch(
"archey.entries.lan_ip.netifaces.interfaces",
return_value=["lo", "en0"],
)
@patch(
"archey.entries.lan_ip.netifaces.ifaddresses",
side_effect=[
{
AF_INET: [
{
"addr": "127.0.0.1",
"netmask": "255.0.0.0",
"peer": "127.0.0.1",
},
],
AF_INET6: [
{
"addr": "::1",
"netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128",
},
],
},
{
AF_INET: [
{
"addr": "192.168.1.55",
"netmask": "255.255.255.0",
"broadcast": "192.168.1.255",
},
{
"addr": "169.254.5.6",
"netmask": "255.255.0.0",
"broadcast": "169.254.255.255",
},
],
AF_INET6: [
{
"addr": "fe80::abcd:ef0:abef:dead",
"netmask": "ffff:ffff:ffff:ffff::/64",
},
],
},
],
)
def test_show_link_local(self, _, __):
"""Test link-local IP addresses hiding"""
self.assertListEqual(
LanIP(options={"show_link_local": False}).value,
["192.168.1.55"],
)

@patch(
"archey.entries.lan_ip.netifaces.interfaces",
return_value=["lo", "en0"],
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"one_line": true,
"max_count": 2,
"show_global": false,
"show_link_local": true,
"ipv6_support": true
},
{
Expand Down

0 comments on commit 12a0bcf

Please sign in to comment.