You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem is that right now, it only supports ipv4, when it should support ipv6 too. the code looks like this:
defconvert_domain_to_ip(
self, domain
) ->ipaddress.IPv4Networkoripaddress.IPv6NetworkorNone:
""" Accepts a domain name or IP address, and returns an IPV4 or IPV6 address """try:
ip_string=socket.gethostbyname(domain)
returnipaddress.ip_address(ip_string)
exceptsocket.gaierroraserr:
logger.warning(f"Unable to lookup domain: {domain} - error: {err}")
This also method right now unfortunately will return a None value when socket.gethostbyname(), which causes a problem later in the code path.
Really we should be using socket.getaddrinfo, which does work with ipv6, but instead of just returning a string representation of an ip address, returns a different object., which we'd need to change. the docs for python are below:
We'd need convert_domain_to_ip to return a string representation of ipv4 addresses AND ipv6 addresses, which involves a bit of work to fetch the string out of the return value of socket.getaddrinfo.
When there is no IPv4 or IPv6 returned, we should probably raise an appropriate exception, so we can catch it early, rather than blindly passing None values along to the caller of the method.
What the code returns
I've put together a quick marimo notebook example of what the socket.getaddrinfo method returns, to give an idea of what datastructures any code would need to fetch the string representation of an IP address out of:
This isn't mega complex, but it's likely a few more lines than we have in the file - the good news it doesn't rely require existing knowledge of the rest of the app, so presumably anyone with a bit of python experience could make a PR with an improved implementation of convert_domain_to_ip.
The text was updated successfully, but these errors were encountered:
We curently have a method in our platform that is responsible for converting domain names to ip address objects in python.
It's linked below:
https://github.com/thegreenwebfoundation/admin-portal/blob/master/apps/greencheck/domain_check.py#L128
The problem is that right now, it only supports ipv4, when it should support ipv6 too. the code looks like this:
This also method right now unfortunately will return a
None
value whensocket.gethostbyname()
, which causes a problem later in the code path.Really we should be using
socket.getaddrinfo
, which does work with ipv6, but instead of just returning a string representation of an ip address, returns a different object., which we'd need to change. the docs for python are below:https://docs.python.org/3.12/library/socket.html#socket.getaddrinfo
We'd need
convert_domain_to_ip
to return a string representation of ipv4 addresses AND ipv6 addresses, which involves a bit of work to fetch the string out of the return value ofsocket.getaddrinfo
.When there is no IPv4 or IPv6 returned, we should probably raise an appropriate exception, so we can catch it early, rather than blindly passing
None
values along to the caller of the method.What the code returns
I've put together a quick marimo notebook example of what the
socket.getaddrinfo
method returns, to give an idea of what datastructures any code would need to fetch the string representation of an IP address out of:https://static.marimo.app/static/get-addrinfo-check-e861
This isn't mega complex, but it's likely a few more lines than we have in the file - the good news it doesn't rely require existing knowledge of the rest of the app, so presumably anyone with a bit of python experience could make a PR with an improved implementation of
convert_domain_to_ip
.The text was updated successfully, but these errors were encountered: