Skip to content

Commit

Permalink
Resolve 'localhost' to loopback IP addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
smortex committed Feb 16, 2023
1 parent 7c7b309 commit 8d55c7f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/riemann/tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ def run
def tick; end

def with_each_address(host, &block)
addresses = Resolv::DNS.new.getaddresses(host)
addresses = if host == 'localhost'
loopback_addresses
else
Resolv::DNS.new.getaddresses(host)
end

if addresses.empty?
host = host[1...-1] if host[0] == '[' && host[-1] == ']'
addresses << IPAddr.new(host)
Expand All @@ -105,6 +110,10 @@ def with_each_address(host, &block)
end
end

def loopback_addresses
Socket.ip_address_list.select { |address| address.ipv6_loopback? || address.ipv4_loopback? }.map(&:ip_address)
end

def endpoint_name(address, port)
if address.ipv6?
"[#{address}]:#{port}"
Expand Down

0 comments on commit 8d55c7f

Please sign in to comment.