console_ip_whitelist 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +36 -0
- data/bin/ipwhitelist +21 -0
- data/lib/console_ip_whitelist.rb +7 -0
- data/lib/console_ip_whitelist/engine.rb +46 -0
- data/lib/console_ip_whitelist/middleware.rb +110 -0
- data/lib/console_ip_whitelist/version.rb +3 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 161b072cdb2b8c9fd68096613fe00d764a304ede5e6df7ef73d6d88022b52de1
|
4
|
+
data.tar.gz: 3c41ed813822e98daf8394a50678271572e25e5825b7762ae3377646fcad02a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3dc4e61701bfbe35cc20a49208fe941ebfdb6ef19ede2e82e9fc09d15c36e0253372f1f8bd8a3476ef8c7569e5a89d70d37cd22f0dc50bf1d97c11d2d48e9339
|
7
|
+
data.tar.gz: cadfc3385ced37fa18c10e2c18356c4c16f8fed10b59d838b39c6235ec1d7f4dd9416c6f532f9f9a4f40baca426c06d41b28f439d9d892e48db12c171d114c12
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Murugan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# ConsoleIpWhitelist
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'console_ip_whitelist'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install console_ip_whitelist
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ConsoleIpWhitelist'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
require 'bundler/gem_tasks'
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'test'
|
31
|
+
t.pattern = 'test/**/*_test.rb'
|
32
|
+
t.verbose = false
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
task default: :test
|
data/bin/ipwhitelist
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'yaml'
|
5
|
+
include FileUtils
|
6
|
+
|
7
|
+
ip_address = ARGV[0]
|
8
|
+
path = Pathname.new File.expand_path('../../whitelist.yml', __FILE__)
|
9
|
+
whitelisted_ips = if File.exist?(path)
|
10
|
+
YAML.load_file(path)
|
11
|
+
else
|
12
|
+
[]
|
13
|
+
end
|
14
|
+
|
15
|
+
whitelisted_ips << ip_address
|
16
|
+
whitelisted_ips = whitelisted_ips.uniq.compact
|
17
|
+
|
18
|
+
File.open(path, 'w') {|f| f.write whitelisted_ips.to_yaml }
|
19
|
+
|
20
|
+
puts "Stopping Rails server. Please restart it."
|
21
|
+
`kill -9 $(lsof -i tcp:8080 -t) >/dev/null 2>/dev/null`
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ConsoleIpWhitelist
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace ConsoleIpWhitelist
|
4
|
+
|
5
|
+
initializer "console_ip_whitelist.configure_rails_initialization" do |app|
|
6
|
+
if enable?
|
7
|
+
insert_middleware
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
config.after_initialize do
|
12
|
+
path = Rails.root.join('whitelist.yml')
|
13
|
+
default_whitelist_path = Rails.root.join("default_whitelist.yml")
|
14
|
+
whitelisted_ips = []
|
15
|
+
|
16
|
+
if File.exist?(path)
|
17
|
+
whitelisted_ips = YAML.load_file(path)
|
18
|
+
end
|
19
|
+
|
20
|
+
if File.exist?(default_whitelist_path)
|
21
|
+
whitelisted_ips = whitelisted_ips.concat(YAML.load_file(default_whitelist_path))
|
22
|
+
end
|
23
|
+
|
24
|
+
whitelisted_ips.each do |ip|
|
25
|
+
BetterErrors::Middleware.allow_ip!(ip)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def insert_middleware
|
30
|
+
if defined? BetterErrors::Middleware
|
31
|
+
app.middleware.insert_after BetterErrors::Middleware, ConsoleIpWhitelist::Middleware
|
32
|
+
else
|
33
|
+
app.middleware.use ConsoleIpWhitelist::Middleware
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def enable?
|
38
|
+
!Rails.env.production? and app.config.consider_all_requests_local
|
39
|
+
end
|
40
|
+
|
41
|
+
def app
|
42
|
+
Rails.application
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module ConsoleIpWhitelist
|
2
|
+
class Middleware
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
request = Rack::Request.new(env)
|
9
|
+
@ip = request.ip
|
10
|
+
@app.call(env)
|
11
|
+
rescue Exception => ex
|
12
|
+
if pass_through?
|
13
|
+
@app.call(env)
|
14
|
+
else
|
15
|
+
[200, {"Content-Type" => "text/html"}, [error_message]]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def pass_through?
|
20
|
+
path = Rails.root.join('whitelist.yml')
|
21
|
+
default_whitelist_path = Rails.root.join("default_whitelist.yml")
|
22
|
+
whitelisted_ips = []
|
23
|
+
file_exist = false
|
24
|
+
|
25
|
+
if File.exist?(path)
|
26
|
+
file_exist = true
|
27
|
+
whitelisted_ips = YAML.load_file(path)
|
28
|
+
end
|
29
|
+
|
30
|
+
if File.exist?(default_whitelist_path)
|
31
|
+
file_exist = true
|
32
|
+
whitelisted_ips = whitelisted_ips.concat(YAML.load_file(default_whitelist_path))
|
33
|
+
end
|
34
|
+
|
35
|
+
if file_exist
|
36
|
+
whitelisted_ips.include?(@ip)
|
37
|
+
else
|
38
|
+
true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def error_message
|
43
|
+
<<-HTML
|
44
|
+
<!DOCTYPE html>
|
45
|
+
<html>
|
46
|
+
|
47
|
+
<head>
|
48
|
+
<title>Enhance your error page</title>
|
49
|
+
|
50
|
+
<!-- Expand the number of characters we can use in the document beyond basic ASCII 🎉 -->
|
51
|
+
<meta charset="utf-8">
|
52
|
+
|
53
|
+
<!-- Connect Bootstrap CSS -->
|
54
|
+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
|
55
|
+
|
56
|
+
<!-- Make it responsive to small screens -->
|
57
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
58
|
+
</head>
|
59
|
+
|
60
|
+
<body>
|
61
|
+
<div class="container pt-4 text-center">
|
62
|
+
<div class="row mb-3">
|
63
|
+
<div class="col-md-8 offset-md-2">
|
64
|
+
<div class="alert alert-danger">
|
65
|
+
<p class="h2">
|
66
|
+
Hey you — yes, <span style="text-transform: uppercase;">you</span> — <em>read</em> this message, don't ignore it.
|
67
|
+
</p>
|
68
|
+
</div>
|
69
|
+
</div>
|
70
|
+
</div>
|
71
|
+
|
72
|
+
<div class="row mb-3">
|
73
|
+
<div class="col-md-8 offset-md-2">
|
74
|
+
<h1 class="display-4">Congratulations! There's an error in your code. That means you're making progress!</h1>
|
75
|
+
</div>
|
76
|
+
</div>
|
77
|
+
|
78
|
+
<div class="row mb-3">
|
79
|
+
<div class="col-md-8 offset-md-2">
|
80
|
+
<p class="h3">Now it's time to debug. To get a really helpful error page, copy-paste this command at a terminal prompt:</p>
|
81
|
+
</div>
|
82
|
+
</div>
|
83
|
+
|
84
|
+
<div class="row mb-3">
|
85
|
+
<div class="col-md-8 offset-md-2">
|
86
|
+
<div class="jumbotron py-2 mb-3">
|
87
|
+
<pre style="font-size: 2em;"><code>bin/whitelist #{@ip}</code></pre>
|
88
|
+
</div>
|
89
|
+
</div>
|
90
|
+
</div>
|
91
|
+
|
92
|
+
<div class="row mb-3">
|
93
|
+
<div class="col-md-8 offset-md-2">
|
94
|
+
<p class="h3">and then <span style="text-transform: uppercase;">restart your server</span>. Then, refresh this page. You'll see a supercharged error page. In addition to the high-level error message at the top of the page, you'll have:</p>
|
95
|
+
</div>
|
96
|
+
</div>
|
97
|
+
|
98
|
+
<div class="row mb-3">
|
99
|
+
<div class="col-md-8 offset-md-2">
|
100
|
+
<img src="https://firstdraft.github.io/console_ip_whitelist/better_errors_diagram.png" class="img-fluid">
|
101
|
+
</div>
|
102
|
+
</div>
|
103
|
+
</div>
|
104
|
+
</body>
|
105
|
+
|
106
|
+
</html>
|
107
|
+
HTML
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: console_ip_whitelist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Murugan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
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: better_errors
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: ConsoleIpWhitelist.
|
42
|
+
email:
|
43
|
+
- murugan@firstdraft.com
|
44
|
+
executables:
|
45
|
+
- ipwhitelist
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- MIT-LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- bin/ipwhitelist
|
53
|
+
- lib/console_ip_whitelist.rb
|
54
|
+
- lib/console_ip_whitelist/engine.rb
|
55
|
+
- lib/console_ip_whitelist/middleware.rb
|
56
|
+
- lib/console_ip_whitelist/version.rb
|
57
|
+
homepage: https://github.com/firstdraft
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.7.8
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: ConsoleIpWhitelist.
|
81
|
+
test_files: []
|