internetdata-rails 1.0.0
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +138 -0
- data/lib/internetdata/rails/middleware.rb +95 -0
- data/lib/internetdata/rails/railtie.rb +24 -0
- data/lib/internetdata/rails/selectors.rb +125 -0
- data/lib/internetdata/rails/version.rb +7 -0
- data/lib/internetdata/rails.rb +9 -0
- metadata +86 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e9b45bd517bca21ac54f687db15280c5934aec77f3ef2543ce62bcc4b8edb79a
|
|
4
|
+
data.tar.gz: d67ed585e91d13d68fb4c420c3a7deb111d6af2cc3a0058642c2aebe6697070e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5269587c98b7ff4ab638158b2c7488c2d098233dfada24c7aef5be59bba0e1ca9761b9e33ed647f8f56ebe27069ff3c6c9d14efd7b3be5da6302da2b1f87feff
|
|
7
|
+
data.tar.gz: 2a4587da9487d231ed6a81d250100829f4a6366866431519b76fbb4b1c3677c662788dfed764c8f5b24096bae924a693d71cdb6918c2268bd9a9564d6aed8291
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mslm Dev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# [<img src="https://s3.internetdata.io/internetdata-public/brand/mark.svg" alt="InternetData" height="28"/>](https://internetdata.io/) InternetData Rails Middleware
|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/internetdata-rails)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
The official [Rails](https://rubyonrails.org) middleware for the [InternetData](https://internetdata.io) API.
|
|
7
|
+
|
|
8
|
+
It looks up the visitor behind each request, where they are, which network they come from and whether it's a VPN, a proxy, Tor or a hosting server, and hands the answer to your controllers. Blocking is opt-in.
|
|
9
|
+
|
|
10
|
+
It is Rack middleware, so it works in Sinatra, Hanami, Roda or a bare Rack app too; the Railtie is only what saves a Rails user from inserting it by hand.
|
|
11
|
+
|
|
12
|
+
## Getting Started
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bundle add internetdata-rails
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Requires Ruby 3.3 or newer.
|
|
19
|
+
|
|
20
|
+
Without an API key a lookup is on Unlimited, the free plan: the country, the network and the company behind the address, Tor, relay, anycast, carrier and satellite space and the service flags, with no daily or monthly cap. Every lookup your app makes comes from your server's one address, though, so a busy site will see bursts refused. A paid plan answers every field, VPN, the proxy flags, hosting, CDN and the city among them; create a key in the [console](https://app.internetdata.io). A free account tries every field, 100 lookups a day.
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
# config/application.rb
|
|
24
|
+
config.internetdata = { api_key: ENV["INTERNETDATA_API_KEY"] }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
class HomeController < ApplicationController
|
|
29
|
+
def index
|
|
30
|
+
result = InternetData::Rails.lookup(request)&.result
|
|
31
|
+
render plain: "Hello, visitor from #{result&.location&.fetch("country_name") || "somewhere"}"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
In a non-Rails Rack app, insert it yourself:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
use InternetData::Rails::Middleware, api_key: ENV["INTERNETDATA_API_KEY"]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
By default nothing is blocked. Every request gets an answer and your own code decides what it means: a currency to show, a sign-up to review, a page to keep from some countries.
|
|
43
|
+
|
|
44
|
+
## Blocking
|
|
45
|
+
|
|
46
|
+
Set a `block_condition` and a matching request is answered with `403` and never reaches your controllers.
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
config.internetdata = {
|
|
50
|
+
api_key: ENV["INTERNETDATA_API_KEY"],
|
|
51
|
+
block_condition: { location: { country: %w[CU IR KP] } },
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
A condition is written in the shape of an answer, keyed by the same names the API uses, and only the members you name are considered, so it can refuse on anything in the answer. The country works on every plan, and so do the first two of these:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
{ asn: { type: "hosting" } } # the network's type
|
|
59
|
+
[{ is_anycast: true }, { is_satellite: true }] # anycast and satellite space
|
|
60
|
+
[{ is_hosting: true }, { is_cdn: true }] # traffic from servers, not people
|
|
61
|
+
[{ is_vpn: true }, { is_tor: true }, { is_resproxy: true }] # anonymity: a list is OR
|
|
62
|
+
{ is_vpn: true, vpn: { provider: "nordvpn" } } # one provider
|
|
63
|
+
{ resproxy: { hits: { gte: 5 } } } # a numeric threshold
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Symbol and string keys both work. Values are matched by equality, strings without regard to case. An Array means any-of. A Hash of `gte`/`gt`/`lte`/`lt` compares numbers, and every bound you give must hold, so two of them are a range. Members set to `false` or `nil` are ignored, so a condition states the signals you act on; one that constrains nothing would match every request, and is refused when the middleware is built rather than silently blocking all your traffic.
|
|
67
|
+
|
|
68
|
+
Replace the refusal with `on_blocked`, which returns a Rack triplet:
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
on_blocked: ->(request, lookup) { [303, { "location" => "/unavailable" }, []] }
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Where the client address comes from
|
|
75
|
+
|
|
76
|
+
This is the setting that decides whether any of the above works, and it is the one thing only you can get right.
|
|
77
|
+
|
|
78
|
+
**In Rails**, the default reads `env["action_dispatch.remote_ip"]`, the answer `ActionDispatch::RemoteIp` computed from `X-Forwarded-For` minus your `config.action_dispatch.trusted_proxies`. That is the right fix behind a load balancer: tell Rails which proxies are yours and it resolves the visitor for you. Without it, and behind one, every visitor gets your proxy's country and network, and if it is a hosting address, a hosting rule blocks everyone. `Rack::Request#ip` does *not* read that entry, so reading Rails' own is what makes your `trusted_proxies` mean anything here. **With nothing in front of the app server, that default can be forged**: `RemoteIp` believes `X-Forwarded-For` without checking that the connection came from a proxy, so any visitor picks the address that gets looked up. There, pass `ip_selector: ->(request) { request.ip }`. The middleware warns once when it sees that setup: a request carrying `X-Forwarded-For`, `Client-IP` or `Forwarded` from a public address outside `trusted_proxies`.
|
|
79
|
+
|
|
80
|
+
**In a bare Rack app** there is no such entry and the default falls back to `Rack::Request#ip`. Rack's `ip` returns the connecting address, `REMOTE_ADDR`, unless that is a trusted proxy, which by default means a loopback or private address. Only then does it read the forwarding headers, and it takes the right-most entry that is not a trusted proxy. The test suite pins both. Behind an edge on a public address, or one that appends rather than overwrites, name your edge's header instead:
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
ip_selector: InternetData::Rails.header_ip_selector("CF-Connecting-IP")
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`InternetData::Rails.xff_ip_selector` reads `X-Forwarded-For` directly, and `xff_ip_selector(1)` counts one trusted hop from the right. Anything else, pass your own callable: it receives the Rack request and returns an address.
|
|
87
|
+
|
|
88
|
+
**Behind a proxy that writes only `X-Forwarded-For`, a visitor's own `Forwarded` header names the address.** Rack 3 reads `Forwarded` first, for `Rack::Request#ip` and Rails' `remote_ip` alike, and nginx passes a visitor's copy through untouched, so a request sending `Forwarded: for=1.1.1.1` is looked up as `1.1.1.1`. Clear the header at the proxy, `proxy_set_header Forwarded "";` in nginx, or tell Rack to read `X-Forwarded-For` alone at the top of `config.ru`:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
Rack::Request.forwarded_priority = [:x_forwarded]
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The middleware warns once when a request through a proxy carries both headers. From Rails 8.1.4.1, `RemoteIp` itself refuses a request whose `Forwarded` and `X-Forwarded-For` name different visitors, raising `ActionDispatch::RemoteIp::IpSpoofAttackError`, a 400, and the middleware leaves that to Rails, as your own `remote_ip` call would.
|
|
95
|
+
|
|
96
|
+
If the address resolves to a private one, the middleware says so once. That is expected locally and is the signal to fix your configuration anywhere else.
|
|
97
|
+
|
|
98
|
+
## When a lookup fails
|
|
99
|
+
|
|
100
|
+
The request is let through, and the reason is on `lookup.error`. Our outage should not become yours, so a network failure, a rate limit or a rejected key all fail open. Pass `fail_closed: true` to block instead. Private addresses are answered locally and never fail, so this will not lock you out in development.
|
|
101
|
+
|
|
102
|
+
## Cost and latency
|
|
103
|
+
|
|
104
|
+
Answers are cached for an hour, so a returning visitor costs nothing, and private addresses never leave the process. A cache miss is one request to our API, bounded at 2.5 seconds by default and not retried: on a request path, failing open quickly beats holding a visitor while we try again.
|
|
105
|
+
|
|
106
|
+
Files in `public/`, compiled assets included, never reach the middleware: `ActionDispatch::Static` answers them first, so they cost no lookup. Skip the routes you do not care about:
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
skip: ->(request) { request.path == "/up" || request.path.start_with?("/webhooks") }
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Beyond a few million distinct visitors a day, stop calling the API per request: look addresses up locally in [our databases](https://internetdata.io/databases) instead.
|
|
113
|
+
|
|
114
|
+
## Absent is not false
|
|
115
|
+
|
|
116
|
+
Unlimited, with a key or without one, leaves out `is_vpn`, `is_hosting`, `is_cdn` and the four proxy flags, the detail behind them, and the paid members inside `location`, `asn`, `company` and `abuse`, such as `location["city"]`. A member your plan does not include is `nil`, which means "not in your plan" rather than "checked, and no", and Ruby makes that easy to lose, since `nil` and `false` are both falsy.
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
lookup.result.vpn? # true or false, never nil
|
|
120
|
+
lookup.result.is_vpn # nil when your plan does not include it
|
|
121
|
+
lookup.result.included?(:is_vpn)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
A `block_condition` naming a member your plan does not serve, `location.city` on Unlimited included, can never match, so the middleware warns once instead of failing silently. Pass `on_missing_field: :raise` to make it an error.
|
|
125
|
+
|
|
126
|
+
## Other Libraries
|
|
127
|
+
|
|
128
|
+
There are official InternetData client libraries available for many languages including PHP, Python, Go, Java, and Ruby. See our GitHub at https://github.com/internetdata for more.
|
|
129
|
+
|
|
130
|
+
## About InternetData
|
|
131
|
+
|
|
132
|
+
Geolocation, anonymity, ownership and network data for IP addresses, AS numbers and domains, as an API and as databases you download and query yourself.
|
|
133
|
+
|
|
134
|
+
[<img src="https://s3.internetdata.io/internetdata-public/brand/mark.svg" alt="InternetData" height="64"/>](https://internetdata.io/)
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'rack'
|
|
5
|
+
|
|
6
|
+
module InternetData
|
|
7
|
+
module Rails
|
|
8
|
+
# Rack middleware that looks up the visitor and optionally refuses the
|
|
9
|
+
# request.
|
|
10
|
+
#
|
|
11
|
+
# Being Rack rather than Rails-specific is deliberate: the same object works
|
|
12
|
+
# in Sinatra, Hanami, Roda or a bare Rack app, and the Railtie below is only
|
|
13
|
+
# what saves a Rails user from inserting it by hand.
|
|
14
|
+
#
|
|
15
|
+
# Without a `block_condition` this only enriches the request and never
|
|
16
|
+
# refuses one, leaving the decision to your own controllers. The answer is
|
|
17
|
+
# on `request.env['internetdata']`, or `InternetData::Rails.lookup(request)`.
|
|
18
|
+
class Middleware
|
|
19
|
+
ENV_KEY = 'internetdata'
|
|
20
|
+
|
|
21
|
+
def initialize(app, **options)
|
|
22
|
+
@app = app
|
|
23
|
+
@on_blocked = options.delete(:on_blocked) || method(:refuse)
|
|
24
|
+
# Only the default selector reads Rails' remote_ip, or Rack's ip.
|
|
25
|
+
@check_forgery = !options.key?(:ip_selector)
|
|
26
|
+
@check_passed_through = @check_forgery
|
|
27
|
+
@on_warn = options[:on_warn]
|
|
28
|
+
@core = InternetData::Middleware::Core.new(
|
|
29
|
+
Rails.default_ip_selector, **options
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def call(env)
|
|
34
|
+
request = ::Rack::Request.new(env)
|
|
35
|
+
warn_forgeable(request) if @check_forgery
|
|
36
|
+
warn_passed_through(request) if @check_passed_through
|
|
37
|
+
lookup = @core.evaluate(request)
|
|
38
|
+
return @app.call(env) if lookup.nil?
|
|
39
|
+
|
|
40
|
+
env[ENV_KEY] = lookup
|
|
41
|
+
return @on_blocked.call(request, lookup) if lookup.blocked?
|
|
42
|
+
|
|
43
|
+
@app.call(env)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
# Once: the setup is the same on every request.
|
|
49
|
+
def warn_forgeable(request)
|
|
50
|
+
peer = Rails.forgeable_peer(request)
|
|
51
|
+
return if peer.nil?
|
|
52
|
+
|
|
53
|
+
@check_forgery = false
|
|
54
|
+
message = "a request carrying #{Rails.forwarding_header(request)} came from #{peer}, a public " \
|
|
55
|
+
'address outside config.action_dispatch.trusted_proxies, so Rails takes the visitor ' \
|
|
56
|
+
'from that header, which anyone connecting directly can set. If a proxy sits in front of this app, ' \
|
|
57
|
+
'add its addresses to trusted_proxies; if nothing does, pass ' \
|
|
58
|
+
'ip_selector: ->(request) { request.ip }.'
|
|
59
|
+
say(message)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Once, as above.
|
|
63
|
+
def warn_passed_through(request)
|
|
64
|
+
proxy = Rails.passed_through_forwarded(request)
|
|
65
|
+
return if proxy.nil?
|
|
66
|
+
|
|
67
|
+
@check_passed_through = false
|
|
68
|
+
message = "a request through #{proxy} carries Forwarded beside X-Forwarded-For, and Rack " \
|
|
69
|
+
'reads Forwarded first. A proxy that writes only X-Forwarded-For, as nginx does, ' \
|
|
70
|
+
"passes a visitor's own Forwarded through, and that names the address looked up. " \
|
|
71
|
+
'If yours does, clear the header there (nginx: proxy_set_header Forwarded "";), ' \
|
|
72
|
+
'or set Rack::Request.forwarded_priority = [:x_forwarded] in config.ru.'
|
|
73
|
+
say(message)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def say(message)
|
|
77
|
+
@on_warn ? @on_warn.call(message) : Kernel.warn("[internetdata] #{message}")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def refuse(_request, _lookup)
|
|
81
|
+
body = JSON.generate({ 'error' => 'access denied' })
|
|
82
|
+
[403, { 'content-type' => 'application/json' }, [body]]
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
module_function
|
|
87
|
+
|
|
88
|
+
# What the middleware found out about this visitor, or nil when it has not
|
|
89
|
+
# run for this request or `skip` claimed it.
|
|
90
|
+
def lookup(request)
|
|
91
|
+
env = request.respond_to?(:env) ? request.env : request
|
|
92
|
+
env[Middleware::ENV_KEY]
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module InternetData
|
|
4
|
+
module Rails
|
|
5
|
+
# Inserts the middleware for a Rails app, configured from
|
|
6
|
+
# `config.internetdata`.
|
|
7
|
+
#
|
|
8
|
+
# Placed right after Rails' own RemoteIp, so `request.ip` already means
|
|
9
|
+
# what your `trusted_proxies` say it means, and before the router, so a
|
|
10
|
+
# refusal costs no controller work.
|
|
11
|
+
class Railtie < ::Rails::Railtie
|
|
12
|
+
config.internetdata = ActiveSupport::OrderedOptions.new
|
|
13
|
+
|
|
14
|
+
initializer 'internetdata.middleware' do |app|
|
|
15
|
+
options = app.config.internetdata.to_h
|
|
16
|
+
next if options.delete(:enabled) == false
|
|
17
|
+
|
|
18
|
+
app.middleware.insert_after(
|
|
19
|
+
ActionDispatch::RemoteIp, InternetData::Rails::Middleware, **options
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module InternetData
|
|
4
|
+
module Rails
|
|
5
|
+
# The client-address selectors, bound to a Rack request.
|
|
6
|
+
#
|
|
7
|
+
# There is no portable default, so this is yours to choose - but Rails
|
|
8
|
+
# already has an answer for the common case, and it is the one to reach for
|
|
9
|
+
# first.
|
|
10
|
+
SELECTORS = InternetData::Middleware::Selectors.new do |request|
|
|
11
|
+
InternetData::Middleware::RequestView.new(
|
|
12
|
+
header: ->(name) { request.get_header("HTTP_#{name.upcase.tr('-', '_')}") },
|
|
13
|
+
framework_ip: -> { framework_ip(request) }
|
|
14
|
+
)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
# Rails' own answer where there is one, and Rack's otherwise.
|
|
20
|
+
#
|
|
21
|
+
# `ActionDispatch::RemoteIp` computes the visitor from `X-Forwarded-For`
|
|
22
|
+
# minus your `config.action_dispatch.trusted_proxies` and leaves it on
|
|
23
|
+
# `env['action_dispatch.remote_ip']`. That is the right fix behind a load
|
|
24
|
+
# balancer, so it is read first - `Rack::Request#ip` does NOT consult it,
|
|
25
|
+
# and using Rack's alone would silently ignore the trusted_proxies you
|
|
26
|
+
# configured.
|
|
27
|
+
#
|
|
28
|
+
# **With nothing in front of the app server, Rails' answer can be forged**:
|
|
29
|
+
# `RemoteIp` believes `X-Forwarded-For` from any peer outside
|
|
30
|
+
# trusted_proxies, so a visitor names the address looked up. The middleware
|
|
31
|
+
# says so once when it sees that setup.
|
|
32
|
+
#
|
|
33
|
+
# Outside Rails there is no such entry and this falls back to
|
|
34
|
+
# `Rack::Request#ip`. Under Rack 3 that is the peer itself unless the peer
|
|
35
|
+
# is a trusted proxy (loopback or private by default). Rack 2 returned the
|
|
36
|
+
# left-most `X-Forwarded-For` entry after dropping private and loopback
|
|
37
|
+
# addresses, which is whatever the caller sent: there, behind nothing,
|
|
38
|
+
# prefer {.header_ip_selector} or your own.
|
|
39
|
+
def default_ip_selector
|
|
40
|
+
SELECTORS.default
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @api private
|
|
44
|
+
def framework_ip(request)
|
|
45
|
+
# `to_s` because RemoteIp leaves a lazily-resolving object here, not a
|
|
46
|
+
# String, and a lookup needs the address.
|
|
47
|
+
rails = request.get_header('action_dispatch.remote_ip')
|
|
48
|
+
return rails.to_s unless rails.nil?
|
|
49
|
+
|
|
50
|
+
request.ip
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @api private
|
|
54
|
+
# The peer, when Rails' RemoteIp let a header that peer sent name the
|
|
55
|
+
# visitor: `X-Forwarded-For`, `Client-IP` or a `Forwarded` Rack reads, from
|
|
56
|
+
# a public address outside the proxies RemoteIp trusts, its defaults or
|
|
57
|
+
# trusted_proxies.
|
|
58
|
+
def forgeable_peer(request)
|
|
59
|
+
remote_ip = request.get_header('action_dispatch.remote_ip')
|
|
60
|
+
return nil if remote_ip.nil? || forwarding_header(request).nil?
|
|
61
|
+
|
|
62
|
+
peer = request.get_header('REMOTE_ADDR').to_s
|
|
63
|
+
return nil unless public_address?(peer)
|
|
64
|
+
|
|
65
|
+
# RemoteIp's own list, from the object it leaves in env; Rails has kept
|
|
66
|
+
# it there as @proxies since 4.0.
|
|
67
|
+
proxies = remote_ip.instance_variable_get(:@proxies)
|
|
68
|
+
return nil unless proxies.respond_to?(:any?)
|
|
69
|
+
return nil if proxies.any? { |proxy| proxy === peer } # rubocop:disable Style/CaseEquality
|
|
70
|
+
|
|
71
|
+
peer
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# @api private
|
|
75
|
+
# The first header on the request that can name the visitor, or nil.
|
|
76
|
+
def forwarding_header(request)
|
|
77
|
+
names = { 'X-Forwarded-For' => 'HTTP_X_FORWARDED_FOR', 'Client-IP' => 'HTTP_CLIENT_IP' }
|
|
78
|
+
names['Forwarded'] = 'HTTP_FORWARDED' if forwarded_priority.include?(:forwarded)
|
|
79
|
+
names.find { |_, key| !request.get_header(key).to_s.empty? }&.first
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# @api private
|
|
83
|
+
# The proxy, when a request through it carries `Forwarded` beside
|
|
84
|
+
# `X-Forwarded-For` and Rack reads `Forwarded` first, as Rack 3 does by
|
|
85
|
+
# default, and so does Rails' RemoteIp, which asks Rack. A proxy writing
|
|
86
|
+
# only `X-Forwarded-For`, as nginx does, passes the visitor's own
|
|
87
|
+
# `Forwarded` through, and that names the address looked up.
|
|
88
|
+
def passed_through_forwarded(request)
|
|
89
|
+
return nil unless forwarded_priority.first == :forwarded
|
|
90
|
+
return nil if %w[HTTP_FORWARDED HTTP_X_FORWARDED_FOR].any? { |name| request.get_header(name).to_s.empty? }
|
|
91
|
+
|
|
92
|
+
request.get_header('REMOTE_ADDR').to_s
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# @api private
|
|
96
|
+
# Rack 2 reads only `X-Forwarded-For`, and has no such setting (2.2.24).
|
|
97
|
+
def forwarded_priority
|
|
98
|
+
::Rack::Request.respond_to?(:forwarded_priority) ? ::Rack::Request.forwarded_priority : [:x_forwarded]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# @api private
|
|
102
|
+
def public_address?(ip)
|
|
103
|
+
IPAddr.new(ip)
|
|
104
|
+
!InternetData::Bogon.bogon?(ip)
|
|
105
|
+
rescue IPAddr::Error
|
|
106
|
+
false
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# An address from `X-Forwarded-For`, ignoring Rails' trusted-proxy list.
|
|
110
|
+
#
|
|
111
|
+
# The LEFT-MOST entry (depth 0) is whatever the caller sent, because proxies
|
|
112
|
+
# append to this header. Prefer `trusted_proxies`; reach for this only when
|
|
113
|
+
# you cannot express your topology there.
|
|
114
|
+
def xff_ip_selector(depth = 0)
|
|
115
|
+
SELECTORS.xff(depth)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# An address from a single-value header your edge writes -
|
|
119
|
+
# `header_ip_selector('CF-Connecting-IP')` behind Cloudflare. Falls back to
|
|
120
|
+
# the Rails accessor when the header is absent.
|
|
121
|
+
def header_ip_selector(name)
|
|
122
|
+
SELECTORS.header(name)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'internetdata'
|
|
4
|
+
require 'internetdata/middleware'
|
|
5
|
+
|
|
6
|
+
require_relative 'rails/version'
|
|
7
|
+
require_relative 'rails/selectors'
|
|
8
|
+
require_relative 'rails/middleware'
|
|
9
|
+
require_relative 'rails/railtie' if defined?(::Rails::Railtie)
|
metadata
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: internetdata-rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mslm Dev
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: internetdata
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 2.11.0
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '3'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: 2.11.0
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '3'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: rack
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '2.2'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '2.2'
|
|
46
|
+
description: Looks up the visitor behind each request - location, network, company,
|
|
47
|
+
VPN, proxy, Tor, hosting - and hands the answer to your controllers. Rack middleware,
|
|
48
|
+
so it works in Sinatra, Hanami and Roda too; a Railtie inserts it for you in Rails.
|
|
49
|
+
email:
|
|
50
|
+
- support@internetdata.io
|
|
51
|
+
executables: []
|
|
52
|
+
extensions: []
|
|
53
|
+
extra_rdoc_files: []
|
|
54
|
+
files:
|
|
55
|
+
- LICENSE
|
|
56
|
+
- README.md
|
|
57
|
+
- lib/internetdata/rails.rb
|
|
58
|
+
- lib/internetdata/rails/middleware.rb
|
|
59
|
+
- lib/internetdata/rails/railtie.rb
|
|
60
|
+
- lib/internetdata/rails/selectors.rb
|
|
61
|
+
- lib/internetdata/rails/version.rb
|
|
62
|
+
homepage: https://internetdata.io
|
|
63
|
+
licenses:
|
|
64
|
+
- MIT
|
|
65
|
+
metadata:
|
|
66
|
+
homepage_uri: https://internetdata.io
|
|
67
|
+
source_code_uri: https://github.com/internetdata/sdk-ruby-rails
|
|
68
|
+
rubygems_mfa_required: 'true'
|
|
69
|
+
rdoc_options: []
|
|
70
|
+
require_paths:
|
|
71
|
+
- lib
|
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - ">="
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '3.3'
|
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
requirements: []
|
|
83
|
+
rubygems_version: 3.6.9
|
|
84
|
+
specification_version: 4
|
|
85
|
+
summary: Official Rails and Rack middleware for the InternetData API.
|
|
86
|
+
test_files: []
|