Module: ImmosquareConstants::Ip

Defined in:
lib/immosquare-constants/ip.rb

Defined Under Namespace

Classes: IpResult

Class Method Summary collapse

Class Method Details

.get_front_ip(request = nil) ⇒ Object

##

Get the front IP — DNS resolution of the request host. Represents the public IPv4 that browsers reach (reverse proxy like Caddy/Nginx, CDN, load balancer…) before traffic hits the application server.

Uses explicit Google/Cloudflare nameservers with a 2s timeout per server (so up to ~4s total in worst case) to avoid hangs when the system resolver is misconfigured. Filters to IPv4 only for consistency with the other IP helpers in this module.

Returns nil if request is missing, host is empty or no IPv4 record is resolved.

##


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/immosquare-constants/ip.rb', line 88

def get_front_ip(request = nil)
  return nil if request.nil?

  host = request.host
  return nil if host.nil? || host.empty?

  Resolv::DNS.open(:nameserver_port => [["8.8.8.8", 53], ["1.1.1.1", 53]]) do |dns|
    dns.timeouts = [2, 2]
    dns.getaddresses(host).find {|a| a.is_a?(Resolv::IPv4) }&.to_s
  end
rescue StandardError => e
  puts("Error getting front IP: #{e.message}")
  nil
end

.get_ips(request = nil) ⇒ Object

##

Method to get available IP addresses

##


106
107
108
109
110
111
112
# File 'lib/immosquare-constants/ip.rb', line 106

def get_ips(request = nil)
  IpResult.new(
    get_local_ip,
    get_public_ip_from_aws,
    get_client_ip(request)
  )
end

.get_my_ip_from_awsObject

##

Method to get the public IP address used to access the internet

##


69
70
71
# File 'lib/immosquare-constants/ip.rb', line 69

def get_my_ip_from_aws
  get_public_ip_from_aws
end