immosquare-constants 0.1.16 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97b58fa8bdcd04000abd024afae97165b9b11d0e1e38fcb36bf2233af0926895
4
- data.tar.gz: f4e113d541371b985f6c75f58c9bc7964adc75c28880ee24c13347696c6c34c4
3
+ metadata.gz: 6098bdf582ab7d9829fc6e29756d68ef26001a9afdfcfa87b8ad0e3bc3ea2849
4
+ data.tar.gz: 27aef060a5800c610d82e66adefe8fc6337311b6bb66c0fa9188099aec589887
5
5
  SHA512:
6
- metadata.gz: e69deefa3385c84aeb8b666f08cb5a34fe53c114f382cd18c3dfcc2a139a28059489509ec6b3cfc2a94beb66bb34f9ea97065bc022bede2931f203f542c407f5
7
- data.tar.gz: 72cb4e752a34f992c61ce00defa1967b8b0c3efa2092285d38b237e7e1f54a713d088fb1d4801fbab27f0e5a3e1cf8350622b9c1568e9a0433f00fde81237d67
6
+ metadata.gz: fb9cc95875219727e1780a8d5cb4e6dfb511ba17a523302e73530b603039e087689b96e040fef396300e7a61a42eec660027cec3ec727b4c5624ff668fc963a7
7
+ data.tar.gz: 12e6b70b66597ac332c504a760a7747d099b1df954690447abcb0046f92431d018dee74d93b363ea95a5c67d79f863f5812d96bd0401c45181bcda5265bdbb6c
@@ -2,9 +2,11 @@ require "socket"
2
2
  require "net/http"
3
3
  require "uri"
4
4
  require "json"
5
+ require "resolv"
5
6
 
6
7
  module ImmosquareConstants
7
8
  module Ip
9
+ PUBLIC_IP_TIMEOUT = 2
8
10
  ##============================================================##
9
11
  ## Simple class to provide dot notation access to IP addresses
10
12
  ##============================================================##
@@ -69,6 +71,36 @@ module ImmosquareConstants
69
71
  get_public_ip_from_aws
70
72
  end
71
73
 
74
+ ##============================================================##
75
+ ## Get the front IP — DNS resolution of the request host.
76
+ ## Represents the public IPv4 that browsers reach (reverse
77
+ ## proxy like Caddy/Nginx, CDN, load balancer...) before
78
+ ## traffic hits the application server.
79
+ ##
80
+ ## Uses explicit Google/Cloudflare nameservers with a 2s
81
+ ## timeout per server (so up to ~4s total in worst case) to
82
+ ## avoid hangs when the system resolver is misconfigured.
83
+ ## Filters to IPv4 only for consistency with the other IP
84
+ ## helpers in this module.
85
+ ##
86
+ ## Returns nil if request is missing, host is empty or no
87
+ ## IPv4 record is resolved.
88
+ ##============================================================##
89
+ def get_front_ip(request = nil)
90
+ return nil if request.nil?
91
+
92
+ host = request.host
93
+ return nil if host.nil? || host.empty?
94
+
95
+ Resolv::DNS.open(:nameserver_port => [["8.8.8.8", 53], ["1.1.1.1", 53]]) do |dns|
96
+ dns.timeouts = [2, 2]
97
+ dns.getaddresses(host).find {|a| a.is_a?(Resolv::IPv4) }&.to_s
98
+ end
99
+ rescue StandardError => e
100
+ puts("Error getting front IP: #{e.message}")
101
+ nil
102
+ end
103
+
72
104
  ##============================================================##
73
105
  ## Method to get available IP addresses
74
106
  ##============================================================##
@@ -128,14 +160,21 @@ module ImmosquareConstants
128
160
 
129
161
  ##============================================================##
130
162
  ## Get public IP address with fallback
163
+ ##
164
+ ## Callers run inside web requests (Rack middlewares): without
165
+ ## explicit timeouts, Net::HTTP waits up to 60s to connect and
166
+ ## 60s to read, so a slow checkip endpoint would hold a server
167
+ ## thread for two minutes per request.
131
168
  ##============================================================##
132
169
  def get_public_ip_from_aws
133
170
  begin
134
171
  begin
135
- uri = URI.parse("https://checkip.amazonaws.com/")
136
- http = Net::HTTP.new(uri.host, uri.port)
137
- http.use_ssl = true
138
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
172
+ uri = URI.parse("https://checkip.amazonaws.com/")
173
+ http = Net::HTTP.new(uri.host, uri.port)
174
+ http.use_ssl = true
175
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
176
+ http.open_timeout = PUBLIC_IP_TIMEOUT
177
+ http.read_timeout = PUBLIC_IP_TIMEOUT
139
178
  response = http.get(uri.request_uri)
140
179
  raise("No IP found") if !response.is_a?(Net::HTTPSuccess)
141
180
 
@@ -1,3 +1,3 @@
1
1
  module ImmosquareConstants
2
- VERSION = "0.1.16".freeze
2
+ VERSION = "0.1.18".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-constants
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - immosquare
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  requirements: []
44
- rubygems_version: 3.7.2
44
+ rubygems_version: 4.0.21
45
45
  specification_version: 4
46
46
  summary: ImmosquareConstants is a comprehensive collection of constants useful for
47
47
  a variety of applications.