immosquare-constants 0.1.11 → 0.1.12

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: 66e6fdddfe8d5b3700f1f14d0b355b9ef0da49174db20ea516fe0aa4eb8cf23c
4
- data.tar.gz: 77d44924b9d4224fe3a7cc8eada1f98a2a0d6c5c16d30359592ea83763a2998f
3
+ metadata.gz: 2801e9a375c22fcb94e0739754e9399630cc8847740bf9013e9514e711aa2455
4
+ data.tar.gz: 93413f0d1b48010f58fb61d04f322d6291612c2e74308a084c24e86423836919
5
5
  SHA512:
6
- metadata.gz: ad8ff8d7eb7b021344d53a6e859a748d667be4f0123fba569c8dbf7a12a7016a937588a316018e207e3880bfc23863b93f8803729c18d6b53de4470c30176825
7
- data.tar.gz: 6d4b3090578cb4af74cb2b3aa686fa90765462503996ceb272b418f5d9d84f212d30aa5bb4b40ea578b62b87b28160c44603126137fda802107d26afc256a104
6
+ metadata.gz: c25c7bf453aef00017391599eaf2bb69f9249663633b304349928f33ccc394e8fc3eabca70fcb7b6d2f3e205f931d4cca633f1cd632fd2ecb05f5230dca26f6f
7
+ data.tar.gz: 84a347e271bb5d35860fd92d5cb3d14029573ddf76d8c9167a048580df954403732f0bd687bfce5a44d200598953044f483d25e395f0ef1fb88e54254afecc84
@@ -1,15 +1,70 @@
1
1
  require "socket"
2
2
  require "net/http"
3
3
  require "uri"
4
+ require "ostruct"
4
5
 
5
6
  module ImmosquareConstants
6
7
  module Ip
7
8
  class << self
8
9
 
9
10
  ##============================================================##
10
- ## On récupère l'IP réelle
11
+ ## Méthode pour récupérer l'IP publique : DEPRECATED
11
12
  ##============================================================##
12
13
  def get_real_ip
14
+ get_public_ip_from_aws
15
+ end
16
+
17
+ ##============================================================##
18
+ ## Méthode pour récupérer les IPs disponibles
19
+ ##============================================================##
20
+ def get_ips(request = nil)
21
+ OpenStruct.new(
22
+ :local => get_local_ip,
23
+ :client => defined?(Rails) && Rails.env.development? && !request.nil? ? get_client_ip(request) : get_public_ip_from_aws
24
+ )
25
+ end
26
+
27
+ private
28
+
29
+ ##============================================================##
30
+ ## On utilise un Socket pour obtenir l'IP locale
31
+ ## en se connectant à au DNS Google
32
+ ##============================================================##
33
+ def get_local_ip
34
+ local_ip = nil
35
+ begin
36
+ socket = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM)
37
+ socket.connect(Addrinfo.tcp("8.8.8.8", 53))
38
+ local_ip = socket.local_address.ip_address
39
+ socket.close
40
+ rescue StandardError => e
41
+ puts("Error getting local IP: #{e.message}")
42
+ ensure
43
+ return local_ip
44
+ end
45
+ end
46
+
47
+ ##============================================================##
48
+ ## On récupère l'IP client avec gestion intelligente des proxies
49
+ ## Hiérarchie de récupération d'IP (du plus fiable au moins fiable) :
50
+ ## 1. HTTP_X_REAL_IP : En-tête personnalisé souvent défini par les load balancers/proxies
51
+ ## 2. request.ip : Méthode Rails intelligente qui analyse X-Forwarded-For, X-Real-IP, etc.
52
+ ## 3. request.remote_ip : IP directe de la connexion (peut être l'IP du proxy)
53
+ ##
54
+ ## Exemple : Client (203.0.113.1) → Proxy (10.0.0.1) → Rails Server
55
+ ## - request.remote_ip retournerait 10.0.0.1 (proxy)
56
+ ## - request.ip retournerait 203.0.113.1 (client réel)
57
+ ## - HTTP_X_REAL_IP pourrait contenir 203.0.113.1 si défini par le proxy
58
+ ##============================================================##
59
+ def get_client_ip(request)
60
+ ip = request.env["HTTP_X_REAL_IP"] || request.ip || request.remote_ip
61
+ ip.nil? ? get_public_ip_from_aws : ip
62
+ end
63
+
64
+ ##============================================================##
65
+ ## On récupère l'IP publique avec fallback (pour production)
66
+ ##============================================================##
67
+ def get_public_ip_from_aws
13
68
  begin
14
69
  begin
15
70
  uri = URI.parse("https://checkip.amazonaws.com/")
@@ -19,8 +74,8 @@ module ImmosquareConstants
19
74
  response.body.strip
20
75
  rescue StandardError => e
21
76
  s = Socket.ip_address_list.find(&:ipv4_private?)
22
- s = Socket.ip_address_list.first if s.blank?
23
- raise("No IP found") if s.blank?
77
+ s = Socket.ip_address_list.first if s.nil?
78
+ raise("No IP found") if s.nil?
24
79
 
25
80
  s.ip_address
26
81
  end
@@ -29,6 +84,7 @@ module ImmosquareConstants
29
84
  end
30
85
  end
31
86
 
87
+
32
88
  end
33
89
  end
34
90
  end
@@ -1,3 +1,3 @@
1
1
  module ImmosquareConstants
2
- VERSION = "0.1.11".freeze
2
+ VERSION = "0.1.12".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.11
4
+ version: 0.1.12
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.6.8
44
+ rubygems_version: 3.6.9
45
45
  specification_version: 4
46
46
  summary: ImmosquareConstants is a comprehensive collection of constants useful for
47
47
  a variety of applications.