maiha-hosts_access 0.1 → 0.2
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.
- data/README +10 -1
- data/init.rb +1 -4
- data/lib/hosts_access.rb +66 -21
- metadata +2 -2
data/README
CHANGED
@@ -6,7 +6,8 @@ HostsAccess
|
|
6
6
|
|
7
7
|
Usage
|
8
8
|
=====
|
9
|
-
config/environment.rb
|
9
|
+
config/environment.rb (in Rails)
|
10
|
+
config/init.rb (in Merb)
|
10
11
|
|
11
12
|
# specify hosts to allow access
|
12
13
|
HostsAccess.allow = ['127.0.0.1', 'ac.wota.jp']
|
@@ -20,6 +21,14 @@ Note
|
|
20
21
|
All accesses are allowed if no rules are given
|
21
22
|
|
22
23
|
|
24
|
+
Environment
|
25
|
+
===========
|
26
|
+
|
27
|
+
tested on
|
28
|
+
|
29
|
+
* Rails 1.2
|
30
|
+
* Merb 1.0.3
|
31
|
+
|
23
32
|
|
24
33
|
Copyright (c) 2008 maiha@wota.jp, released under the MIT license
|
25
34
|
|
data/init.rb
CHANGED
data/lib/hosts_access.rb
CHANGED
@@ -1,44 +1,89 @@
|
|
1
1
|
require 'resolv'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
######################################################################
|
4
|
+
### core_ext
|
5
|
+
unless Module.respond_to?(:mattr_accessor)
|
6
|
+
require File.dirname(__FILE__) + '/../core_ext/module/attribute_accessors'
|
7
|
+
end
|
8
|
+
|
9
|
+
unless [].respond_to?(:extract_options!)
|
10
|
+
require File.dirname(__FILE__) + '/../core_ext/array/extract_options'
|
11
|
+
class Array #:nodoc:
|
12
|
+
include ActiveSupport::CoreExtensions::Array::ExtractOptions
|
9
13
|
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
10
17
|
|
18
|
+
######################################################################
|
19
|
+
### HostsAccess
|
20
|
+
|
21
|
+
module HostsAccess
|
11
22
|
mattr_accessor :allow
|
12
23
|
mattr_accessor :allow_controller
|
13
24
|
|
14
|
-
module InstanceMethods
|
15
25
|
private
|
16
26
|
def hosts_access
|
17
|
-
if allow.blank?
|
18
|
-
|
19
|
-
return true
|
27
|
+
if HostsAccess.allow.blank?
|
28
|
+
return hosts_access_allowed("not restricted (no rules)")
|
20
29
|
end
|
21
30
|
|
22
31
|
if allow_controller
|
23
|
-
Array(allow_controller).each do |name|
|
24
|
-
if name == controller_name
|
25
|
-
logger.info "[HostsAccess] OK: #{controller_name} controller is not restricted"
|
26
|
-
return true
|
27
|
-
end
|
32
|
+
Array(HostsAccess.allow_controller).each do |name|
|
33
|
+
return hosts_access_allowed("#{controller_name} controller is not restricted") if name == controller_name
|
28
34
|
end
|
29
35
|
end
|
30
36
|
|
31
|
-
Array(allow).each do |host|
|
37
|
+
Array(HostsAccess.allow).each do |host|
|
32
38
|
address = host
|
33
39
|
address = Resolv.getaddress(host) rescue host unless /\A[\d\.]+\Z/ === host
|
34
|
-
if request.remote_ip == address
|
35
|
-
logger.info "[HostsAccess] OK: #{host} is allowed"
|
36
|
-
return true
|
37
|
-
end
|
40
|
+
return hosts_access_allowed("#{host} is allowed") if request.remote_ip == address
|
38
41
|
end
|
39
42
|
|
40
|
-
|
43
|
+
return hosts_access_denied("#{request.remote_ip} is denied")
|
44
|
+
end
|
45
|
+
|
46
|
+
def hosts_access_allowed(reason)
|
47
|
+
logger.info "[HostsAccess] OK: #{reason}"
|
48
|
+
return true
|
49
|
+
end
|
50
|
+
|
51
|
+
def hosts_access_denied(reason)
|
52
|
+
logger.info "[HostsAccess] NG: #{reason}"
|
41
53
|
return false
|
42
54
|
end
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
######################################################################
|
59
|
+
### for Merb
|
60
|
+
if defined?(Merb::Plugins)
|
61
|
+
module HostsAccess
|
62
|
+
def logger
|
63
|
+
Merb.logger
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
Merb::BootLoader.after_app_loads do
|
68
|
+
Application.class_eval do
|
69
|
+
include HostsAccess
|
70
|
+
before :hosts_access
|
71
|
+
|
72
|
+
private
|
73
|
+
def hosts_access_denied(reason)
|
74
|
+
super
|
75
|
+
throw :halt
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
######################################################################
|
82
|
+
### for Rails
|
83
|
+
|
84
|
+
if defined?(ActionController::Base)
|
85
|
+
ActionController::Base.class_eval do
|
86
|
+
include HostsAccess
|
87
|
+
before_filter :hosts_access
|
43
88
|
end
|
44
89
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maiha-hosts_access
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.2"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maiha
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-12-09 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|