rack-ip_address_restriction 0.1.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/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +35 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rack/ip_address_restriction.rb +69 -0
- data/lib/rack/ip_address_restriction/version.rb +5 -0
- data/rack-ip_address_restriction.gemspec +26 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a6b4729f793969057737ee297d919565300a2c8e
|
4
|
+
data.tar.gz: efa1b52bc8caad25404cb1b1ab520214f5cac461
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 095cf3f21059a7b199ae0cbd3a997bf94a93d27589cdb516d7a16f555672c57cbbba0b9936ecf4d0769929033d6d35637a52ee59867543e52230a83a7cd03c0d
|
7
|
+
data.tar.gz: 812f4e63ccf7ab33c9b7306198cbe90d4829cab4cbdcc1f87bf08692e95f7873765b8e7fd59a7a51f839a03a45b6e50796e93ab8d349533793087f37e30a1260
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Rack::IpAddressRestriction
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'rack-ip_address_restriction'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rack-ip_address_restriction
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Allow access to / from 127.0.0.1 or 192.168.1.0/24:
|
22
|
+
|
23
|
+
```
|
24
|
+
use Rack::IpAddressRestriction, '/' => %w(127.0.0.1 192.168.1.0/24)
|
25
|
+
```
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rack-ip_address_restriction.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rack/ip_address_restriction"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'rack/ip_address_restriction/version'
|
2
|
+
require 'ipaddr'
|
3
|
+
require 'uri'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
module Rack
|
7
|
+
class IpAddressRestriction
|
8
|
+
DEFAULT_MAPPING = { '/' => %w(127.0.0.1) }.freeze
|
9
|
+
private_constant :DEFAULT_MAPPING
|
10
|
+
|
11
|
+
def initialize(app, options = {})
|
12
|
+
@app = app
|
13
|
+
options = DEFAULT_MAPPING if options.empty?
|
14
|
+
@mapping = create_mapping(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
if allow?(env)
|
19
|
+
@app.call(env)
|
20
|
+
else
|
21
|
+
[403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def create_mapping(config)
|
28
|
+
mapping = config.map do |location, ip_masks|
|
29
|
+
host, path = parse_location(location)
|
30
|
+
raise ArgumentError, 'paths need to start with /' if path[0] != '/'
|
31
|
+
path_prefix = Pathname.new(path).cleanpath.to_s
|
32
|
+
|
33
|
+
ip_masks = ip_masks.map { |addr| addr.is_a?(IPAddr) ? addr : IPAddr.new(addr) }
|
34
|
+
|
35
|
+
[host, path_prefix, ip_masks]
|
36
|
+
end
|
37
|
+
mapping.sort_by { |(host, path_prefix, _)| [host ? -host.size : (-1.0 / 0.0), -path_prefix.size] }
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse_location(location)
|
41
|
+
uri = URI.parse(location)
|
42
|
+
if uri.host
|
43
|
+
[uri.host, uri.path]
|
44
|
+
else
|
45
|
+
[nil, location]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def allow?(env)
|
50
|
+
return true unless ip_masks = ip_masks_for_current_path(env)
|
51
|
+
|
52
|
+
request = Request.new(env)
|
53
|
+
ip_masks.any? { |addr| addr.include?(IPAddr.new(request.ip)) }
|
54
|
+
end
|
55
|
+
|
56
|
+
def ip_masks_for_current_path(env)
|
57
|
+
path = Pathname.new(env["PATH_INFO"].to_s).cleanpath.to_s
|
58
|
+
|
59
|
+
@mapping.each do |host, path_prefix, ip_masks|
|
60
|
+
next if host && ![env['HTTP_HOST'], env['SERVER_NAME']].include?(host)
|
61
|
+
next if path !~ %r{\A#{Regexp.quote(path_prefix)}(.*)\z}i || !($1.empty? || $1[0] == '/')
|
62
|
+
|
63
|
+
return ip_masks
|
64
|
+
end
|
65
|
+
|
66
|
+
nil
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/ip_address_restriction/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rack-ip_address_restriction"
|
8
|
+
spec.version = Rack::IpAddressRestriction::VERSION
|
9
|
+
spec.authors = ["koshigoe"]
|
10
|
+
spec.email = ["koshigoeb@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Restrict access by IP Address.}
|
13
|
+
spec.description = %q{Inspired by Rack::Access provided by https://github.com/rack/rack-contrib.}
|
14
|
+
spec.homepage = "https://github.com/koshigoe/rack-ip_address_restriction"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "rack"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "rack-contrib"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-ip_address_restriction
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- koshigoe
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-contrib
|
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
|
+
description: Inspired by Rack::Access provided by https://github.com/rack/rack-contrib.
|
84
|
+
email:
|
85
|
+
- koshigoeb@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- lib/rack/ip_address_restriction.rb
|
99
|
+
- lib/rack/ip_address_restriction/version.rb
|
100
|
+
- rack-ip_address_restriction.gemspec
|
101
|
+
homepage: https://github.com/koshigoe/rack-ip_address_restriction
|
102
|
+
licenses: []
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.5.1
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Restrict access by IP Address.
|
124
|
+
test_files: []
|