immosquare-constants 0.1.12 → 0.1.13
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5073fe427e2b2b4c43e1152815e581dd34e1ad8b212c0fb5331a3d9b17c95c28
|
4
|
+
data.tar.gz: 774752277082db562cf31143d55e5df66456148dc261ad820bf4ea25b16a3fd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dfa8a292ab75f8ed0f5b18f7360fb6b93eded2aaf2dd9ad01d308e5bcecd8b9ddf7c1f645c702a5310376ae31ec5a82a72808c2d35fe08f97f2c0839e36fc87
|
7
|
+
data.tar.gz: 685f991fa70989e6560332590d1d9309ca5b1df1e099751ccb0ec5a76514bbd4c0103b3ad60add4b736249d2db7ce84af60a8890d22d25dd883c77f7092b7573
|
@@ -1,34 +1,78 @@
|
|
1
1
|
require "socket"
|
2
2
|
require "net/http"
|
3
3
|
require "uri"
|
4
|
-
require "
|
4
|
+
require "json"
|
5
5
|
|
6
6
|
module ImmosquareConstants
|
7
7
|
module Ip
|
8
|
+
##============================================================##
|
9
|
+
## Simple class to provide dot notation access to IP addresses
|
10
|
+
##============================================================##
|
11
|
+
class IpResult
|
12
|
+
|
13
|
+
attr_reader :local, :client
|
14
|
+
|
15
|
+
def initialize(local_ip, client_ip)
|
16
|
+
@local = local_ip
|
17
|
+
@client = client_ip
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_json(*)
|
21
|
+
{
|
22
|
+
:local => @local,
|
23
|
+
:client => @client
|
24
|
+
}.to_json(*)
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_hash
|
28
|
+
{
|
29
|
+
:local => @local,
|
30
|
+
:client => @client
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
"local: #{@local}, client: #{@client}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def inspect
|
39
|
+
"#<ImmosquareConstants::Ip::IpResult local: #{@local.inspect}, client: #{@client.inspect}>"
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_a
|
43
|
+
[@local, @client]
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_h
|
47
|
+
to_hash
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
8
52
|
class << self
|
9
53
|
|
10
54
|
##============================================================##
|
11
|
-
##
|
55
|
+
## Method to get the public IP address used to access the internet
|
12
56
|
##============================================================##
|
13
|
-
def
|
57
|
+
def get_my_ip_from_aws
|
14
58
|
get_public_ip_from_aws
|
15
59
|
end
|
16
60
|
|
17
61
|
##============================================================##
|
18
|
-
##
|
62
|
+
## Method to get available IP addresses
|
19
63
|
##============================================================##
|
20
64
|
def get_ips(request = nil)
|
21
|
-
|
22
|
-
|
23
|
-
|
65
|
+
IpResult.new(
|
66
|
+
get_local_ip,
|
67
|
+
get_client_ip(request)
|
24
68
|
)
|
25
69
|
end
|
26
70
|
|
27
71
|
private
|
28
72
|
|
29
73
|
##============================================================##
|
30
|
-
##
|
31
|
-
##
|
74
|
+
## Use a Socket to get the local IP address
|
75
|
+
## by connecting to Google's DNS server
|
32
76
|
##============================================================##
|
33
77
|
def get_local_ip
|
34
78
|
local_ip = nil
|
@@ -45,24 +89,33 @@ module ImmosquareConstants
|
|
45
89
|
end
|
46
90
|
|
47
91
|
##============================================================##
|
48
|
-
##
|
49
|
-
##
|
50
|
-
## 1. HTTP_X_REAL_IP :
|
51
|
-
## 2. request.ip :
|
52
|
-
## 3. request.remote_ip :
|
92
|
+
## Get client IP with intelligent proxy handling
|
93
|
+
## IP retrieval hierarchy (from most reliable to least reliable):
|
94
|
+
## 1. HTTP_X_REAL_IP : Custom header often set by load balancers/proxies
|
95
|
+
## 2. request.ip : Rails intelligent method that analyzes X-Forwarded-For, X-Real-IP, etc.
|
96
|
+
## 3. request.remote_ip : Direct connection IP (might be proxy IP)
|
53
97
|
##
|
54
|
-
##
|
55
|
-
## - request.remote_ip
|
56
|
-
## - request.ip
|
57
|
-
## - HTTP_X_REAL_IP
|
98
|
+
## Example: Client (203.0.113.1) → Proxy (10.0.0.1) → Rails Server
|
99
|
+
## - request.remote_ip would return 10.0.0.1 (proxy)
|
100
|
+
## - request.ip would return 203.0.113.1 (real client)
|
101
|
+
## - HTTP_X_REAL_IP might contain 203.0.113.1 if set by proxy
|
58
102
|
##============================================================##
|
59
|
-
def get_client_ip(request)
|
60
|
-
|
61
|
-
|
103
|
+
def get_client_ip(request = nil)
|
104
|
+
begin
|
105
|
+
raise("No request provided") if request.nil?
|
106
|
+
|
107
|
+
ip = request.env["HTTP_X_REAL_IP"] || request.ip || request.remote_ip
|
108
|
+
raise("No IP found") if ip.nil?
|
109
|
+
raise("IP is localhost (127.0.0.1)") if ip == "127.0.0.1"
|
110
|
+
|
111
|
+
ip
|
112
|
+
rescue StandardError => e
|
113
|
+
get_public_ip_from_aws
|
114
|
+
end
|
62
115
|
end
|
63
116
|
|
64
117
|
##============================================================##
|
65
|
-
##
|
118
|
+
## Get public IP address with fallback (for production)
|
66
119
|
##============================================================##
|
67
120
|
def get_public_ip_from_aws
|
68
121
|
begin
|
@@ -212,6 +212,7 @@ module ImmosquareConstants
|
|
212
212
|
##============================================================##
|
213
213
|
def native_name_for_locale(locale)
|
214
214
|
begin
|
215
|
+
return nil if locale.nil? || locale.to_s.strip.empty?
|
215
216
|
key = locale.to_s.split("-").map.with_index {|part, index| index == 0 ? part.downcase : part.upcase }.join("-").to_sym
|
216
217
|
LOCALES[key]
|
217
218
|
rescue StandardError
|