ip_anonymizer 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 +8 -0
- data/.travis.yml +11 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +24 -0
- data/LICENSE.txt +21 -0
- data/README.md +84 -0
- data/Rakefile +19 -0
- data/ip_anonymizer.gemspec +29 -0
- data/lib/ip_anonymizer.rb +28 -0
- data/lib/ip_anonymizer/hash_ip.rb +15 -0
- data/lib/ip_anonymizer/mask_ip.rb +14 -0
- data/lib/ip_anonymizer/version.rb +3 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dca65ff3aed9d29044a1e20925670a110aff00a527071991a5bfa9ba5cb9b269
|
4
|
+
data.tar.gz: ed6c97bd345eae2ac1ecabe9e1cb8db434d79a5df4dc99cdd2e561de91a42db7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 565e3cbbe4d119a859de314d7b658f810ba53e513bb1b92f54652ba2baa936b0dcba8ac83acc761b32b98a04ca62b3b52b192d7ba0a597027a18741c46207fa8
|
7
|
+
data.tar.gz: 9321631e81f1510de578f2b9298f5c1998270e0ee6c938c559ca2309cc8a61d9f55e997187dee36cc61c18c6a167dda9b2754332138e2e471da06ea8ad95b60e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ip_anonymizer (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
benchmark-ips (2.7.2)
|
10
|
+
minitest (5.11.3)
|
11
|
+
rake (12.3.1)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
benchmark-ips
|
18
|
+
bundler
|
19
|
+
ip_anonymizer!
|
20
|
+
minitest
|
21
|
+
rake
|
22
|
+
|
23
|
+
BUNDLED WITH
|
24
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Andrew
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# IP Anonymizer
|
2
|
+
|
3
|
+
:earth_americas: IP address anonymizer for Ruby
|
4
|
+
|
5
|
+
Works with IPv4 and IPv6, and includes middleware for Rails
|
6
|
+
|
7
|
+
## Getting Started
|
8
|
+
|
9
|
+
Add these lines to your application’s Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'ip_anonymizer'
|
13
|
+
```
|
14
|
+
|
15
|
+
There are two strategies for anonymizing IPs.
|
16
|
+
|
17
|
+
### Masking
|
18
|
+
|
19
|
+
This is the approach [Google Analytics uses for IP anonymization](https://support.google.com/analytics/answer/2763052):
|
20
|
+
|
21
|
+
- For IPv4, the last octet is set to 0
|
22
|
+
- For IPv6, the last 80 bits are set to zeros
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
IpAnonymizer.mask_ip("8.8.4.4")
|
26
|
+
# => "8.8.4.0"
|
27
|
+
|
28
|
+
IpAnonymizer.mask_ip("2001:4860:4860:0:0:0:0:8844")
|
29
|
+
# => "2001:4860:4860::"
|
30
|
+
```
|
31
|
+
|
32
|
+
An advantange of this approach is geocoding will still work, only with slightly less accuracy.
|
33
|
+
|
34
|
+
### Hashing
|
35
|
+
|
36
|
+
Transform IP addresses with a keyed hash function (PBKDF2-HMAC-SHA256).
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
IpAnonymizer.hash_ip("8.8.4.4", key: "secret")
|
40
|
+
# => "6.128.151.207"
|
41
|
+
|
42
|
+
IpAnonymizer.hash_ip("2001:4860:4860:0:0:0:0:8844", key: "secret")
|
43
|
+
# => "f6e4:a4fe:32dc:2f39:3e47:84cc:e85e:865c"
|
44
|
+
```
|
45
|
+
|
46
|
+
Be sure to keep the key secret, or else a rainbow table can be constructed.
|
47
|
+
|
48
|
+
## Rails
|
49
|
+
|
50
|
+
Automatically anonymize `request.remote_ip` in Rails.
|
51
|
+
|
52
|
+
For masking, add to `config/application.rb`:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
config.middleware.insert_after ActionDispatch::RemoteIp, IpAnonymizer::MaskIp
|
56
|
+
```
|
57
|
+
|
58
|
+
For hashing, use:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
config.middleware.insert_after ActionDispatch::RemoteIp, IpAnonymizer::HashIp, key: "secret"
|
62
|
+
```
|
63
|
+
|
64
|
+
## History
|
65
|
+
|
66
|
+
View the [changelog](https://github.com/ankane/ip_anonymizer/blob/master/CHANGELOG.md)
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
71
|
+
|
72
|
+
- [Report bugs](https://github.com/ankane/ip_anonymizer/issues)
|
73
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/ip_anonymizer/pulls)
|
74
|
+
- Write, clarify, or fix documentation
|
75
|
+
- Suggest or add new features
|
76
|
+
|
77
|
+
To get started with development and testing:
|
78
|
+
|
79
|
+
```sh
|
80
|
+
git clone https://github.com/ankane/ip_anonymizer.git
|
81
|
+
cd ip_anonymizer
|
82
|
+
bundle install
|
83
|
+
rake test
|
84
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
require "benchmark/ips"
|
4
|
+
|
5
|
+
Rake::TestTask.new(:test) do |t|
|
6
|
+
t.libs << "test"
|
7
|
+
t.libs << "lib"
|
8
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
9
|
+
end
|
10
|
+
|
11
|
+
task default: :test
|
12
|
+
|
13
|
+
task :benchmark do
|
14
|
+
require "ip_anonymizer"
|
15
|
+
Benchmark.ips do |x|
|
16
|
+
x.report("mask_ip") { IpAnonymizer.mask_ip("8.8.4.4") }
|
17
|
+
x.report("hash_ip") { IpAnonymizer.hash_ip("8.8.4.4", key: "secret") }
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "ip_anonymizer/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ip_anonymizer"
|
8
|
+
spec.version = IpAnonymizer::VERSION
|
9
|
+
spec.authors = ["Andrew Kane"]
|
10
|
+
spec.email = ["andrew@chartkick.com"]
|
11
|
+
|
12
|
+
spec.summary = "IP address anonymizer for Ruby"
|
13
|
+
spec.homepage = "https://github.com/ankane/ip_anonymizer"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.required_ruby_version = ">= 2.2.0"
|
24
|
+
|
25
|
+
spec.add_development_dependency "benchmark-ips"
|
26
|
+
spec.add_development_dependency "bundler"
|
27
|
+
spec.add_development_dependency "minitest"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "ipaddr"
|
2
|
+
require "openssl"
|
3
|
+
|
4
|
+
require "ip_anonymizer/hash_ip"
|
5
|
+
require "ip_anonymizer/mask_ip"
|
6
|
+
require "ip_anonymizer/version"
|
7
|
+
|
8
|
+
module IpAnonymizer
|
9
|
+
def self.mask_ip(ip)
|
10
|
+
addr = IPAddr.new(ip)
|
11
|
+
if addr.ipv4?
|
12
|
+
# set last octet to 0
|
13
|
+
addr.mask(24).to_s
|
14
|
+
else
|
15
|
+
# set last 80 bits to zeros
|
16
|
+
addr.mask(48).to_s
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.hash_ip(ip, key:, iterations: 1)
|
21
|
+
addr = IPAddr.new(ip)
|
22
|
+
key_len = addr.ipv4? ? 4 : 16
|
23
|
+
family = addr.ipv4? ? Socket::AF_INET : Socket::AF_INET6
|
24
|
+
|
25
|
+
keyed_hash = OpenSSL::PKCS5.pbkdf2_hmac(addr.to_s, key, iterations, key_len, "sha256")
|
26
|
+
IPAddr.new(keyed_hash.bytes.inject {|a, b| (a << 8) + b }, family).to_s
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module IpAnonymizer
|
2
|
+
class HashIp
|
3
|
+
def initialize(app, key:)
|
4
|
+
@app = app
|
5
|
+
@key = key
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
req = ActionDispatch::Request.new(env)
|
10
|
+
# TODO lazy load, like ActionDispatch::RemoteIp
|
11
|
+
req.remote_ip = IpAnonymizer.hash_ip(req.remote_ip, key: @key)
|
12
|
+
@app.call(req.env)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module IpAnonymizer
|
2
|
+
class MaskIp
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
req = ActionDispatch::Request.new(env)
|
9
|
+
# TODO lazy load, like ActionDispatch::RemoteIp
|
10
|
+
req.remote_ip = IpAnonymizer.mask_ip(req.remote_ip)
|
11
|
+
@app.call(req.env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ip_anonymizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: benchmark-ips
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
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: '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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- andrew@chartkick.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CHANGELOG.md
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- ip_anonymizer.gemspec
|
85
|
+
- lib/ip_anonymizer.rb
|
86
|
+
- lib/ip_anonymizer/hash_ip.rb
|
87
|
+
- lib/ip_anonymizer/mask_ip.rb
|
88
|
+
- lib/ip_anonymizer/version.rb
|
89
|
+
homepage: https://github.com/ankane/ip_anonymizer
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 2.2.0
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.7.6
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: IP address anonymizer for Ruby
|
113
|
+
test_files: []
|