ip_filter 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.rspec +1 -0
  4. data/CHANGELOG +29 -0
  5. data/Gemfile.lock +117 -0
  6. data/LICENSE +20 -0
  7. data/README.rdoc +232 -0
  8. data/Rakefile +6 -0
  9. data/data/geoip/country_code.yml +255 -0
  10. data/data/geoip/country_code3.yml +255 -0
  11. data/data/geoip/country_continent.yml +255 -0
  12. data/data/geoip/country_name.yml +255 -0
  13. data/data/geoip/time_zone.yml +677 -0
  14. data/lib/geoip.rb +559 -0
  15. data/lib/ip_filter.rb +100 -0
  16. data/lib/ip_filter/cache.rb +30 -0
  17. data/lib/ip_filter/cache/dallistore.rb +39 -0
  18. data/lib/ip_filter/cache/redis.rb +26 -0
  19. data/lib/ip_filter/configuration.rb +47 -0
  20. data/lib/ip_filter/controller/geo_ip_lookup.rb +78 -0
  21. data/lib/ip_filter/lookups/base.rb +60 -0
  22. data/lib/ip_filter/lookups/geoip.rb +41 -0
  23. data/lib/ip_filter/providers/max_mind.rb +52 -0
  24. data/lib/ip_filter/providers/s3.rb +51 -0
  25. data/lib/ip_filter/railtie.rb +23 -0
  26. data/lib/ip_filter/request.rb +14 -0
  27. data/lib/ip_filter/results/base.rb +39 -0
  28. data/lib/ip_filter/results/geoip.rb +19 -0
  29. data/lib/ip_filter/version.rb +3 -0
  30. data/spec/cache/dallistore_spec.rb +16 -0
  31. data/spec/cache/redis_spec.rb +56 -0
  32. data/spec/controller/ip_controller_spec.rb +56 -0
  33. data/spec/fixtures/GeoIP.dat +0 -0
  34. data/spec/fixtures/LICENSE.txt +31 -0
  35. data/spec/fixtures/country.dat +0 -0
  36. data/spec/ip_filter_spec.rb +19 -0
  37. data/spec/providers/max_mind_spec.rb +11 -0
  38. data/spec/providers/s3_spec.rb +11 -0
  39. data/spec/spec_helper.rb +40 -0
  40. data/spec/support/enable_dallistore_cache.rb +15 -0
  41. data/spec/support/enable_redis_cache.rb +15 -0
  42. metadata +85 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d1ba59fc5df3909b9e00d5e6d920ed3ed310336
4
+ data.tar.gz: 4f970604687e630ae7bcaace9aa8cf53bf1245bd
5
+ SHA512:
6
+ metadata.gz: ccffe9b88880990d11a0196437b7972f2f8cb024454166799909ab13d76ab2d56d444c151a77bf7d2933525ffade02031ab42c22654475021dc29412ffd40c4a
7
+ data.tar.gz: 0d97e6c882d15a45b0a979cdc4ffe91732c8400177aef2b3b5ed2f05761ce1e154db193426c988d445a25cf746339059b3ce917fe010dbd57258ba47eded0854
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ rdoc/*
3
+ *.gem
4
+ .bundle
5
+ spec/log/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format Fuubar
@@ -0,0 +1,29 @@
1
+ IpFilter
2
+
3
+ v0.7.2
4
+
5
+ All cached objects return an OpenStruct.
6
+
7
+ v0.7.1
8
+
9
+ Fix bad ip adress definition (remote_ip = nil)
10
+
11
+ v0.7.0
12
+
13
+ Add missing tests
14
+ Enhance Configuration handling
15
+ Clean debug messages
16
+
17
+ v0.6.0
18
+
19
+ Fix bug with cache
20
+
21
+ v0.0.4
22
+ ============================================
23
+ Add Redis handling for caching.
24
+ Add S3 Download/Upload for GeoipDat files
25
+ Use geoipupdate to update GeoIp databases
26
+
27
+ v0.0.3
28
+ ============================================
29
+ Add IP range support - Thanks Ihor!
@@ -0,0 +1,117 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ip_filter (0.8.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ actionpack (4.2.3)
10
+ actionview (= 4.2.3)
11
+ activesupport (= 4.2.3)
12
+ rack (~> 1.6)
13
+ rack-test (~> 0.6.2)
14
+ rails-dom-testing (~> 1.0, >= 1.0.5)
15
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
16
+ actionview (4.2.3)
17
+ activesupport (= 4.2.3)
18
+ builder (~> 3.1)
19
+ erubis (~> 2.7.0)
20
+ rails-dom-testing (~> 1.0, >= 1.0.5)
21
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
22
+ activesupport (4.2.3)
23
+ i18n (~> 0.7)
24
+ json (~> 1.7, >= 1.7.7)
25
+ minitest (~> 5.1)
26
+ thread_safe (~> 0.3, >= 0.3.4)
27
+ tzinfo (~> 1.1)
28
+ aws-sdk (2.1.2)
29
+ aws-sdk-resources (= 2.1.2)
30
+ aws-sdk-core (2.1.2)
31
+ jmespath (~> 1.0)
32
+ aws-sdk-resources (2.1.2)
33
+ aws-sdk-core (= 2.1.2)
34
+ aws-sdk-v1 (1.64.0)
35
+ json (~> 1.4)
36
+ nokogiri (>= 1.4.4)
37
+ builder (3.2.2)
38
+ dalli (2.7.4)
39
+ diff-lcs (1.2.5)
40
+ erubis (2.7.0)
41
+ fakeredis (0.5.0)
42
+ redis (~> 3.0)
43
+ fuubar (2.0.0)
44
+ rspec (~> 3.0)
45
+ ruby-progressbar (~> 1.4)
46
+ i18n (0.7.0)
47
+ jmespath (1.0.2)
48
+ multi_json (~> 1.0)
49
+ json (1.8.3)
50
+ loofah (2.0.2)
51
+ nokogiri (>= 1.5.9)
52
+ mini_portile (0.6.2)
53
+ minitest (5.7.0)
54
+ multi_json (1.11.1)
55
+ nokogiri (1.6.6.2)
56
+ mini_portile (~> 0.6.0)
57
+ rack (1.6.4)
58
+ rack-test (0.6.3)
59
+ rack (>= 1.0)
60
+ rails-deprecated_sanitizer (1.0.3)
61
+ activesupport (>= 4.2.0.alpha)
62
+ rails-dom-testing (1.0.6)
63
+ activesupport (>= 4.2.0.beta, < 5.0)
64
+ nokogiri (~> 1.6.0)
65
+ rails-deprecated_sanitizer (>= 1.0.1)
66
+ rails-html-sanitizer (1.0.2)
67
+ loofah (~> 2.0)
68
+ railties (4.2.3)
69
+ actionpack (= 4.2.3)
70
+ activesupport (= 4.2.3)
71
+ rake (>= 0.8.7)
72
+ thor (>= 0.18.1, < 2.0)
73
+ rake (10.4.2)
74
+ redis (3.2.1)
75
+ rspec (3.3.0)
76
+ rspec-core (~> 3.3.0)
77
+ rspec-expectations (~> 3.3.0)
78
+ rspec-mocks (~> 3.3.0)
79
+ rspec-core (3.3.1)
80
+ rspec-support (~> 3.3.0)
81
+ rspec-expectations (3.3.0)
82
+ diff-lcs (>= 1.2.0, < 2.0)
83
+ rspec-support (~> 3.3.0)
84
+ rspec-mocks (3.3.1)
85
+ diff-lcs (>= 1.2.0, < 2.0)
86
+ rspec-support (~> 3.3.0)
87
+ rspec-rails (3.3.2)
88
+ actionpack (>= 3.0, < 4.3)
89
+ activesupport (>= 3.0, < 4.3)
90
+ railties (>= 3.0, < 4.3)
91
+ rspec-core (~> 3.3.0)
92
+ rspec-expectations (~> 3.3.0)
93
+ rspec-mocks (~> 3.3.0)
94
+ rspec-support (~> 3.3.0)
95
+ rspec-support (3.3.0)
96
+ ruby-progressbar (1.7.5)
97
+ thor (0.19.1)
98
+ thread_safe (0.3.5)
99
+ tzinfo (1.2.2)
100
+ thread_safe (~> 0.1)
101
+
102
+ PLATFORMS
103
+ ruby
104
+
105
+ DEPENDENCIES
106
+ actionpack
107
+ aws-sdk
108
+ aws-sdk-v1
109
+ dalli
110
+ fakeredis
111
+ fuubar
112
+ ip_filter!
113
+ rspec
114
+ rspec-rails
115
+
116
+ BUNDLED WITH
117
+ 1.10.6
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-11 Alex Reisner
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.
@@ -0,0 +1,232 @@
1
+ = IpFilter
2
+
3
+ IpFilter is IP filtering solution for Ruby. Common use case for this is DRM.
4
+ Uses a GeoIP (or Geo::IP) database to determine the request ip's location.
5
+
6
+
7
+ == Compatibility
8
+
9
+ * Supports Ruby version: 1.9.2 to 2.2.0
10
+ * Supports cache store: Memcache, Redis.
11
+ * Supports Rails 3.
12
+
13
+
14
+ == Install
15
+
16
+ === As a Gem
17
+
18
+ Add to your Gemfile:
19
+
20
+ gem "ip_filter"
21
+
22
+ and run at the command prompt:
23
+
24
+ bundle install
25
+
26
+
27
+ === Or As a Plugin
28
+
29
+ At the command prompt:
30
+
31
+ rails plugin install git://github.com/chtrinh/ip_filter.git
32
+
33
+
34
+ == Typical Rails Configuration
35
+
36
+ # config/initializers/ip_filter.rb
37
+
38
+ IpFilter.configure do |config|
39
+ # Location of GeoIP database files.
40
+ config.data_folder = "lib/assets/"
41
+
42
+ # Level of filtering : Country, city...
43
+ config.geoip_level = 'country'
44
+
45
+ # Type of ip country code to compare by.
46
+ config.ip_code_type = "country_code2"
47
+
48
+ # Accepts a proc that must return an array with corresponding format as :ip_code_type
49
+ config.ip_codes = Proc.new { ["FI", "US", "GB"] }
50
+
51
+ # Accepts a proc that must return an array of IPs or IP range to
52
+ config.ip_whitelist = Proc.new { ["22.33.11.0/24", "2.3.1.4"] }
53
+
54
+ # Exception to throw when IP is NOT allowed.
55
+ # Accepts a Proc for fine-grain control of the appropriate response.
56
+ config.ip_exception = Proc.new { raise Api::Exception::AuthorizationError.new("You region is blocked!") }
57
+
58
+ # Cache object (Memcache or Redis).
59
+ config.cache = Rails.cache
60
+
61
+ ## MaxMind binary require a configuration file.
62
+ # If empty,
63
+ # Configuration path for geoipupdate binary
64
+ config.geoipupdate_config = '/usr/local/etc/GeoIP.conf'
65
+
66
+ ## S3 credentials ##
67
+ # if access_key_id is nil, S3 isn't loaded.
68
+ config.access_key_id = nil
69
+
70
+ # S3 Secret API key
71
+ config.secret_access_key = nil
72
+
73
+ # S3 bucket name
74
+ config.bucket_name = 'ottsm-geoip'
75
+ end
76
+
77
+ === Default Values
78
+
79
+ +data_folder+ : "/tmp/geoip"
80
+
81
+ +geo_ip_dat+ : data/GeoIP.dat
82
+
83
+ +geoip_level+ : :country
84
+
85
+ +update_method+ : Proc {}
86
+
87
+ +ip_code_type+ : nil
88
+ +ip_codes+ : Proc {}
89
+
90
+ +ip_whitelist+ : Proc {}
91
+
92
+ +ip_exception+ : Proc {}
93
+
94
+ +allow_loopback+ : true
95
+
96
+ +cache+ : nil
97
+
98
+ +cache_prefix+ : 'ip_filter:'
99
+
100
+ +geoipupdate_config+ : '/usr/local/etc/GeoIP.conf'
101
+
102
+ +s3_access_key_id+ : nil
103
+
104
+ +s3_secret_access_key+ : nil
105
+
106
+ +s3_bucket_name+ : 'ip_filter-geoip'
107
+
108
+ +refresh_delay+ : 86400
109
+
110
+ == Whitelist Ip
111
+
112
+ See above configuration.
113
+
114
+ == Validate IP
115
+
116
+ Add the following method to any controller to validate the <tt>Request#ip</tt>.
117
+ <tt>IpFilter::Configuration.ip_exception</tt> is a +Proc+ which is called when ip is NOT within the specified region/country code and NOT in IP whitelist. You can pass in filter options similar to those in +before_filter+.
118
+
119
+ validate_ip
120
+
121
+ You can also pass a block (with filter options) to be executed when validation of ip fails like so (defaults to <tt>IpFilter::Configuration.ip_exception</tt> proc if no block present):
122
+
123
+ validate_ip do
124
+ raise Exception.new "Sorry access not granted!"
125
+ end
126
+
127
+ By default, loopback/private network addresses are allowed. You can disable this in like so:
128
+
129
+ #config/initializers/ip_filter.rb
130
+
131
+ IpFilter.configure do |config|
132
+ config.allow_loopback = false
133
+ end
134
+
135
+
136
+ == Skip validation of IP
137
+
138
+ Add the following method to any controller that contains <tt>validate_ip</tt> to skip over the
139
+ validation.
140
+
141
+ skip_validate_ip
142
+
143
+ Internally it is just <tt>skip_before_filter</tt> so filter options can be pass to it as well.
144
+
145
+ skip_validate_ip :only => [:index, :show], :if => lambda { account.roles == "admin" }
146
+
147
+
148
+ == Request Geocoding by IP Address
149
+
150
+ IpFilter adds a +location+ method to the standard <tt>Rack::Request</tt> object so you can easily look up the location of any HTTP request by IP address. For example, in a Rails controller:
151
+
152
+ # returns IpFilter::Result object
153
+ result = request.location
154
+
155
+ See "Advanced Geocoding" below for more information about IpFilter::Result objects.
156
+
157
+
158
+ == Advanced Geocoding
159
+
160
+ So far we have looked at shortcuts for assigning geocoding results to object attributes.
161
+
162
+ Every <tt>IpFilter::Result</tt> object, +result+, provides the following data:
163
+
164
+ * <tt>result.ip</tt> - string
165
+ * <tt>result.country_code</tt> - string
166
+ * <tt>result.country_code2</tt> - string
167
+ * <tt>result.country_code3</tt> - string
168
+ * <tt>result.country_name</tt> - string
169
+ * <tt>result.continent_code</tt> - string
170
+
171
+ == Update Geoip database files
172
+
173
+ To update Geoip database file, you just have to instantiate :
174
+
175
+ IpFilter::Providers::MaxMind.new
176
+
177
+ This will update files located in <tt>IpFilter::Configuration.data_folder</tt>
178
+
179
+ == S3
180
+
181
+ The database files could also be uploaded or downloaded from S3.
182
+
183
+ Upload to S3 :
184
+
185
+ IpFilter.s3.upload!
186
+
187
+ Download from s3 :
188
+
189
+ IpFilter.s3.download!
190
+
191
+ These actions could be done exclusively if you provide you access key ID
192
+ and your secret access key in IpFilter configuration.
193
+
194
+ == Caching
195
+
196
+ It's easy to cache ip results with IpFilter, just configure a cache store:
197
+
198
+ IpFilter.configure do |config|
199
+ config.cache = Rails.cache
200
+ end
201
+
202
+ Currently only Memcache and redis are supported.
203
+
204
+ You can also set a custom prefix to be used for cache keys:
205
+
206
+ IpFilter.configure do |config|
207
+ config.cache_prefix = "whatever:"
208
+ end
209
+
210
+ By default the prefix is <tt>ip_filter:</tt>
211
+
212
+ <i>Before you implement caching in your app please be sure that doing so does not violate the Terms of Service for your geocoding service.</i>
213
+
214
+
215
+ == Use Outside of Rails
216
+
217
+ You can use IpFilter outside of Rails by calling the <tt>IpFilter.search</tt> method:
218
+
219
+ results = IpFilter.search("147.243.3.83")
220
+
221
+ This returns an array of <tt>IpFilter::Result</tt> objects. Please see above and in the code for details.
222
+
223
+ == Contributors
224
+
225
+ Jeremy Le Massu
226
+
227
+ Based on Chris Trinh gem, helped by Ihor Ratsyborynskyy.
228
+
229
+ == Acknowledgements
230
+
231
+ The architecture/design of this gem is heavily based off of Geocoder (by Alex Reisner https://github.com/alexreisner/geocoder).
232
+ Whereas Geocoder was built with 3rd party API resources in mind, IpFilter uses a local database file (.dat) similar to Geoip (by Clifford Heath https://github.com/cjheath/geoip).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,255 @@
1
+ # Ordered list of the ISO3166 2-character country codes, ordered by GeoIP ID
2
+ ---
3
+ - --
4
+ - AP
5
+ - EU
6
+ - AD
7
+ - AE
8
+ - AF
9
+ - AG
10
+ - AI
11
+ - AL
12
+ - AM
13
+ - AN
14
+ - AO
15
+ - AQ
16
+ - AR
17
+ - AS
18
+ - AT
19
+ - AU
20
+ - AW
21
+ - AZ
22
+ - BA
23
+ - BB
24
+ - BD
25
+ - BE
26
+ - BF
27
+ - BG
28
+ - BH
29
+ - BI
30
+ - BJ
31
+ - BM
32
+ - BN
33
+ - BO
34
+ - BR
35
+ - BS
36
+ - BT
37
+ - BV
38
+ - BW
39
+ - BY
40
+ - BZ
41
+ - CA
42
+ - CC
43
+ - CD
44
+ - CF
45
+ - CG
46
+ - CH
47
+ - CI
48
+ - CK
49
+ - CL
50
+ - CM
51
+ - CN
52
+ - CO
53
+ - CR
54
+ - CU
55
+ - CV
56
+ - CX
57
+ - CY
58
+ - CZ
59
+ - DE
60
+ - DJ
61
+ - DK
62
+ - DM
63
+ - DO
64
+ - DZ
65
+ - EC
66
+ - EE
67
+ - EG
68
+ - EH
69
+ - ER
70
+ - ES
71
+ - ET
72
+ - FI
73
+ - FJ
74
+ - FK
75
+ - FM
76
+ - FO
77
+ - FR
78
+ - FX
79
+ - GA
80
+ - GB
81
+ - GD
82
+ - GE
83
+ - GF
84
+ - GH
85
+ - GI
86
+ - GL
87
+ - GM
88
+ - GN
89
+ - GP
90
+ - GQ
91
+ - GR
92
+ - GS
93
+ - GT
94
+ - GU
95
+ - GW
96
+ - GY
97
+ - HK
98
+ - HM
99
+ - HN
100
+ - HR
101
+ - HT
102
+ - HU
103
+ - ID
104
+ - IE
105
+ - IL
106
+ - IN
107
+ - IO
108
+ - IQ
109
+ - IR
110
+ - IS
111
+ - IT
112
+ - JM
113
+ - JO
114
+ - JP
115
+ - KE
116
+ - KG
117
+ - KH
118
+ - KI
119
+ - KM
120
+ - KN
121
+ - KP
122
+ - KR
123
+ - KW
124
+ - KY
125
+ - KZ
126
+ - LA
127
+ - LB
128
+ - LC
129
+ - LI
130
+ - LK
131
+ - LR
132
+ - LS
133
+ - LT
134
+ - LU
135
+ - LV
136
+ - LY
137
+ - MA
138
+ - MC
139
+ - MD
140
+ - MG
141
+ - MH
142
+ - MK
143
+ - ML
144
+ - MM
145
+ - MN
146
+ - MO
147
+ - MP
148
+ - MQ
149
+ - MR
150
+ - MS
151
+ - MT
152
+ - MU
153
+ - MV
154
+ - MW
155
+ - MX
156
+ - MY
157
+ - MZ
158
+ - NA
159
+ - NC
160
+ - NE
161
+ - NF
162
+ - NG
163
+ - NI
164
+ - NL
165
+ - "NO"
166
+ - NP
167
+ - NR
168
+ - NU
169
+ - NZ
170
+ - OM
171
+ - PA
172
+ - PE
173
+ - PF
174
+ - PG
175
+ - PH
176
+ - PK
177
+ - PL
178
+ - PM
179
+ - PN
180
+ - PR
181
+ - PS
182
+ - PT
183
+ - PW
184
+ - PY
185
+ - QA
186
+ - RE
187
+ - RO
188
+ - RU
189
+ - RW
190
+ - SA
191
+ - SB
192
+ - SC
193
+ - SD
194
+ - SE
195
+ - SG
196
+ - SH
197
+ - SI
198
+ - SJ
199
+ - SK
200
+ - SL
201
+ - SM
202
+ - SN
203
+ - SO
204
+ - SR
205
+ - ST
206
+ - SV
207
+ - SY
208
+ - SZ
209
+ - TC
210
+ - TD
211
+ - TF
212
+ - TG
213
+ - TH
214
+ - TJ
215
+ - TK
216
+ - TM
217
+ - TN
218
+ - TO
219
+ - TL
220
+ - TR
221
+ - TT
222
+ - TV
223
+ - TW
224
+ - TZ
225
+ - UA
226
+ - UG
227
+ - UM
228
+ - US
229
+ - UY
230
+ - UZ
231
+ - VA
232
+ - VC
233
+ - VE
234
+ - VG
235
+ - VI
236
+ - VN
237
+ - VU
238
+ - WF
239
+ - WS
240
+ - YE
241
+ - YT
242
+ - RS
243
+ - ZA
244
+ - ZM
245
+ - ME
246
+ - ZW
247
+ - A1
248
+ - A2
249
+ - O1
250
+ - AX
251
+ - GG
252
+ - IM
253
+ - JE
254
+ - BL
255
+ - MF