x-real-ip 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b561d1e4fa4fdd9c722ece5e7ce58558e58eb2b9
4
+ data.tar.gz: 637b074b29879b157c8e01ac4faf57378a5b3639
5
+ SHA512:
6
+ metadata.gz: a2827351dd92c5d2c1207d3f8ab30a13d20c1bdb7347315e2215c92f4f948c013c5e2f080eb13b8ddef492d5fb384062709b370ad2079700bfbfe0b04d249f52
7
+ data.tar.gz: 3789046ff9da42656a33f4195beacd21d3c72acee0396f73ee370cb27a793d931bf13a0e37fd088e413d045d0a85dff2ecc062cf84c978a704ce65290baa0256
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ x-real-ip
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p353
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in x-real-ip.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 glebtv
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # X::Real::Ip
2
+
3
+ Replace REMOTE_IP with X-Real-Ip if it's trusted (useful for Nginx)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'x-real-ip'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install x-real-ip
18
+
19
+ ## Usage
20
+
21
+ For rails just add it to Gemfile
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/x-real-ip.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "x-real-ip/version"
2
+ require "rpatricia"
3
+ require "ipaddr"
4
+ require "active_support/core_ext/module/attribute_accessors"
5
+
6
+ module XRealIp
7
+ mattr_accessor :trusted4, :trusted6
8
+ self.trusted4 = Patricia.new()
9
+ %w(10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 127.0.0.0/8).each do |a|
10
+ self.trusted4.add(a)
11
+ end
12
+ self.trusted6 = Patricia.new(:AF_INET6)
13
+ %w(::1/128).each do |a|
14
+ self.trusted6.add(a)
15
+ end
16
+ end
17
+
18
+ require "x-real-ip/railtie" if defined? Rails
19
+
20
+ require "x-real-ip/middleware"
@@ -0,0 +1,53 @@
1
+ module XRealIp
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def is_trusted?(str)
8
+ ip = IPAddr.new(str)
9
+ if ip.ipv4?
10
+ XRealIp.trusted4.include?(ip.to_s)
11
+ else
12
+ XRealIp.trusted6.include?(ip.to_s)
13
+ end
14
+ end
15
+
16
+ def call(env)
17
+ real_ips = env["HTTP_X_REAL_IP"]
18
+ if real_ips
19
+ begin
20
+ proxies = []
21
+ list = real_ips.split(',')
22
+ return @app.call(env) if list.empty?
23
+ list.push env["REMOTE_ADDR"]
24
+ while tmp = list.pop
25
+ addr = tmp
26
+ if is_trusted?(tmp)
27
+ proxies.push tmp
28
+ else
29
+ break
30
+ end
31
+ end
32
+
33
+ if addr
34
+ env["x.proxies"] = proxies.join(',')
35
+ env["REMOTE_ADDR"] = addr
36
+ proto = env["HTTP_X_FORWARDED_PROTO"] || env['HTTP_X_REAL_PROTO']
37
+ if (proto && proto == 'https')
38
+ env["rack.url_scheme"] = 'https'
39
+ end
40
+ end
41
+
42
+ @app.call(env)
43
+ rescue => e
44
+ $stderr.puts "Error in x-real-ip: #{$!}"
45
+ $stderr.puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
46
+ @app.call(env)
47
+ end
48
+ else
49
+ @app.call(env)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,7 @@
1
+ module XRealIp
2
+ class Railtie < Rails::Railtie
3
+ initializer "railtie.configure_rails_initialization" do |app|
4
+ app.config.middleware.use XRealIp::Middleware
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module XRealIp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
5
+
6
+ require 'rack'
7
+ require 'x-real-ip'
8
+
9
+ SUPPORT = File.join(File.dirname(__FILE__), "support")
10
+ Dir["#{SUPPORT}/*.rb"].each { |f| require f }
11
+
12
+ RSpec.configure do |config|
13
+
14
+ end
@@ -0,0 +1,168 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe XRealIp do
5
+ before :each do
6
+ app = lambda { |env| @env = env; [ 200, {}, [] ] }
7
+ @req = Rack::MockRequest.new(XRealIp::Middleware.new(app))
8
+ end
9
+
10
+ context 'no proxy' do
11
+ context 'local ip' do
12
+ context 'http' do
13
+ before :each do
14
+ env = {
15
+ "REMOTE_ADDR" => "127.0.0.1",
16
+ }
17
+ @resp = @req.get("http://example.com/", env)
18
+ end
19
+ it 'sets ip' do
20
+ @env["REMOTE_ADDR"].should eq '127.0.0.1'
21
+ end
22
+ it 'sets proxy ip' do
23
+ @env["x.proxies"].should be_nil
24
+ end
25
+ it 'keeps http' do
26
+ @env["rack.url_scheme"].should eq 'http'
27
+ end
28
+ end
29
+ context 'https' do
30
+ before :each do
31
+ env = {
32
+ "HTTP_X_FORWARDED_PROTO" => "https",
33
+ "REMOTE_ADDR" => "127.0.0.1",
34
+ }
35
+ @resp = @req.get("http://test.com/", env)
36
+ end
37
+ it 'sets ip' do
38
+ @env["REMOTE_ADDR"].should eq '127.0.0.1'
39
+ end
40
+ it 'sets proxy ip' do
41
+ @env["x.proxies"].should be_nil
42
+ end
43
+ it 'keeps http' do
44
+ @env["rack.url_scheme"].should eq 'http'
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ context 'one proxy' do
51
+ context 'local ip' do
52
+ context 'http' do
53
+ before :each do
54
+ env = {
55
+ "HTTP_X_REAL_IP" => "1.1.1.1",
56
+ "REMOTE_ADDR" => "127.0.0.1",
57
+ }
58
+ @resp = @req.get("http://test.com/", env)
59
+ end
60
+ it 'sets ip' do
61
+ @env["REMOTE_ADDR"].should eq '1.1.1.1'
62
+ end
63
+ it 'sets proxy ip' do
64
+ @env["x.proxies"].should eq '127.0.0.1'
65
+ end
66
+ it 'keeps http' do
67
+ @env["rack.url_scheme"].should eq 'http'
68
+ end
69
+ end
70
+ context 'https' do
71
+ before :each do
72
+ env = {
73
+ "HTTP_X_REAL_IP" => "1.1.1.1",
74
+ "HTTP_X_FORWARDED_PROTO" => "https",
75
+ "REMOTE_ADDR" => "127.0.0.1",
76
+ }
77
+ @resp = @req.get("http://test.com/", env)
78
+ end
79
+ it 'sets ip' do
80
+ @env["REMOTE_ADDR"].should eq '1.1.1.1'
81
+ end
82
+ it 'sets proxy ip' do
83
+ @env["x.proxies"].should eq '127.0.0.1'
84
+ end
85
+ it 'sets url scheme to https' do
86
+ @env["rack.url_scheme"].should eq 'https'
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ context '2 proxies' do
93
+ context 'http' do
94
+ before :each do
95
+ env = {
96
+ "HTTP_X_REAL_IP" => "1.1.1.1,10.0.0.1",
97
+ "REMOTE_ADDR" => "127.0.0.1",
98
+ }
99
+ @resp = @req.get("http://test.com/", env)
100
+ end
101
+ it 'sets ip' do
102
+ @env["REMOTE_ADDR"].should eq '1.1.1.1'
103
+ end
104
+ it 'sets proxy ip' do
105
+ @env["x.proxies"].should eq '127.0.0.1,10.0.0.1'
106
+ end
107
+ it 'keeps http' do
108
+ @env["rack.url_scheme"].should eq 'http'
109
+ end
110
+ end
111
+ context 'https' do
112
+ before :each do
113
+ env = {
114
+ "HTTP_X_REAL_IP" => "1.1.1.1,10.0.0.1",
115
+ "HTTP_X_FORWARDED_PROTO" => "https",
116
+ "REMOTE_ADDR" => "127.0.0.1",
117
+ }
118
+ @resp = @req.get("http://test.com/", env)
119
+ end
120
+ it 'sets ip' do
121
+ @env["REMOTE_ADDR"].should eq '1.1.1.1'
122
+ end
123
+ it 'sets proxy ip' do
124
+ @env["x.proxies"].should eq '127.0.0.1,10.0.0.1'
125
+ end
126
+ it 'sets url scheme to https' do
127
+ @env["rack.url_scheme"].should eq 'https'
128
+ end
129
+ end
130
+ end
131
+
132
+ context 'spoofed' do
133
+ context 'one' do
134
+ before :each do
135
+ env = {
136
+ "HTTP_X_REAL_IP" => "1.1.1.1,2.2.2.2",
137
+ "REMOTE_ADDR" => "3.3.3.3",
138
+ }
139
+ @resp = @req.get("http://test.com/", env)
140
+ end
141
+ it 'sets ip' do
142
+ @env["REMOTE_ADDR"].should eq '3.3.3.3'
143
+ end
144
+ it 'doesnt st proxy ip' do
145
+ @env["x.proxies"].should eq ''
146
+ end
147
+ end
148
+ context 'chain' do
149
+ before :each do
150
+ env = {
151
+ "HTTP_X_REAL_IP" => "1.1.1.1,2.2.2.2",
152
+ "REMOTE_ADDR" => "127.0.0.1",
153
+ }
154
+ @resp = @req.get("http://test.com/", env)
155
+ end
156
+ it 'sets ip' do
157
+ @env["REMOTE_ADDR"].should eq '2.2.2.2'
158
+ end
159
+ it 'sets proxy ip' do
160
+ @env["x.proxies"].should eq '127.0.0.1'
161
+ end
162
+ it 'keeps http' do
163
+ @env["rack.url_scheme"].should eq 'http'
164
+ end
165
+ end
166
+ end
167
+
168
+ end
data/x-real-ip.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'x-real-ip/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "x-real-ip"
8
+ spec.version = XRealIp::VERSION
9
+ spec.authors = ["55ideas"]
10
+ spec.email = ["info@55ideas.ru"]
11
+ spec.description = %q{Replace REMOTE_IP with X-Real-Ip if it's trusted (useful for Nginx)}
12
+ spec.summary = %q{Replace REMOTE_IP with X-Real-Ip if it's trusted (useful for Nginx)}
13
+ spec.homepage = "https://github.com/55ideas/x-real-ip"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rpatricia", "~> 1.0"
22
+ spec.add_dependency "rack"
23
+ spec.add_dependency "activesupport"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: x-real-ip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - 55ideas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rpatricia
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Replace REMOTE_IP with X-Real-Ip if it's trusted (useful for Nginx)
98
+ email:
99
+ - info@55ideas.ru
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .ruby-gemset
106
+ - .ruby-version
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/x-real-ip.rb
112
+ - lib/x-real-ip/middleware.rb
113
+ - lib/x-real-ip/railtie.rb
114
+ - lib/x-real-ip/version.rb
115
+ - spec/spec_helper.rb
116
+ - spec/x_real_ip_spec.rb
117
+ - x-real-ip.gemspec
118
+ homepage: https://github.com/55ideas/x-real-ip
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.1.11
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Replace REMOTE_IP with X-Real-Ip if it's trusted (useful for Nginx)
142
+ test_files:
143
+ - spec/spec_helper.rb
144
+ - spec/x_real_ip_spec.rb