maxmind-db 1.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +4 -0
- data/LICENSE-APACHE +202 -0
- data/LICENSE-MIT +17 -0
- data/README.dev.md +30 -0
- data/README.md +54 -0
- data/Rakefile +12 -0
- data/bin/mmdb-benchmark.rb +61 -0
- data/lib/maxmind/db.rb +274 -0
- data/lib/maxmind/db/decoder.rb +232 -0
- data/lib/maxmind/db/errors.rb +8 -0
- data/lib/maxmind/db/file_reader.rb +37 -0
- data/lib/maxmind/db/memory_reader.rb +24 -0
- data/lib/maxmind/db/metadata.rb +61 -0
- data/maxmind-db.gemspec +19 -0
- data/test/data/LICENSE +4 -0
- data/test/data/MaxMind-DB-spec.md +558 -0
- data/test/data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
- data/test/data/README.md +4 -0
- data/test/data/bad-data/README.md +7 -0
- data/test/data/bad-data/libmaxminddb/libmaxminddb-offset-integer-overflow.mmdb +0 -0
- data/test/data/bad-data/maxminddb-golang/cyclic-data-structure.mmdb +0 -0
- data/test/data/bad-data/maxminddb-golang/invalid-bytes-length.mmdb +1 -0
- data/test/data/bad-data/maxminddb-golang/invalid-data-record-offset.mmdb +0 -0
- data/test/data/bad-data/maxminddb-golang/invalid-map-key-length.mmdb +0 -0
- data/test/data/bad-data/maxminddb-golang/invalid-string-length.mmdb +1 -0
- data/test/data/bad-data/maxminddb-golang/metadata-is-an-uint128.mmdb +1 -0
- data/test/data/bad-data/maxminddb-golang/unexpected-bytes.mmdb +0 -0
- data/test/data/perltidyrc +12 -0
- data/test/data/source-data/GeoIP2-Anonymous-IP-Test.json +41 -0
- data/test/data/source-data/GeoIP2-City-Test.json +12852 -0
- data/test/data/source-data/GeoIP2-Connection-Type-Test.json +102 -0
- data/test/data/source-data/GeoIP2-Country-Test.json +11347 -0
- data/test/data/source-data/GeoIP2-DensityIncome-Test.json +14 -0
- data/test/data/source-data/GeoIP2-Domain-Test.json +452 -0
- data/test/data/source-data/GeoIP2-Enterprise-Test.json +673 -0
- data/test/data/source-data/GeoIP2-ISP-Test.json +12585 -0
- data/test/data/source-data/GeoIP2-Precision-Enterprise-Test.json +1598 -0
- data/test/data/source-data/GeoIP2-User-Count-Test.json +2824 -0
- data/test/data/source-data/GeoLite2-ASN-Test.json +37 -0
- data/test/data/source-data/README +15 -0
- data/test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
- data/test/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
- data/test/data/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
- data/test/data/test-data/GeoIP2-City-Test.mmdb +0 -0
- data/test/data/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
- data/test/data/test-data/GeoIP2-Country-Test.mmdb +0 -0
- data/test/data/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
- data/test/data/test-data/GeoIP2-Domain-Test.mmdb +0 -0
- data/test/data/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
- data/test/data/test-data/GeoIP2-ISP-Test.mmdb +0 -0
- data/test/data/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
- data/test/data/test-data/GeoIP2-User-Count-Test.mmdb +0 -0
- data/test/data/test-data/GeoLite2-ASN-Test.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
- data/test/data/test-data/MaxMind-DB-test-nested.mmdb +0 -0
- data/test/data/test-data/README.md +26 -0
- data/test/data/test-data/maps-with-pointers.raw +0 -0
- data/test/data/test-data/write-test-data.pl +620 -0
- data/test/data/tidyall.ini +5 -0
- data/test/mmdb_util.rb +24 -0
- data/test/test_decoder.rb +241 -0
- data/test/test_reader.rb +415 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '0783d0fa6b13e4fa43dd70dd24c876452e1d5911a8de8296669bd4f806e38ec9'
|
4
|
+
data.tar.gz: 22720864310267dc946e90f1b6b7fb12ee506e2959414b7214539479401a24a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2705b919ec933748987b7495ce750361eb62643e8f5cf8ef142215af61c4f6f6b539cdec47976b58346cad0748241d362aee16f054a14a9986cdcee8c7c534ff
|
7
|
+
data.tar.gz: a7f994be60c64df42176ef98131433812a739eef6533fafde8a867eeb8e097b18127e276cb49eaf42d5d93f84ef807958b4f4cf84d5c6276b49fc56b0decd298
|
data/CHANGELOG.md
ADDED
data/LICENSE-APACHE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/LICENSE-MIT
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
2
|
+
this software and associated documentation files (the "Software"), to deal in
|
3
|
+
the Software without restriction, including without limitation the rights to
|
4
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
5
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
6
|
+
so, subject to the following conditions:
|
7
|
+
|
8
|
+
The above copyright notice and this permission notice shall be included in all
|
9
|
+
copies or substantial portions of the Software.
|
10
|
+
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
12
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
13
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
14
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
15
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
16
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
17
|
+
SOFTWARE.
|
data/README.dev.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# How to release
|
2
|
+
* Update changelog and set release date
|
3
|
+
* Bump version in `maxmind-db.gemspec`
|
4
|
+
* Commit: `git commit -m v1.0.0`
|
5
|
+
* Push: `git push`
|
6
|
+
* Tag: `git tag -a v1.0.0 -m v1.0.0`
|
7
|
+
* Push tag: `git push --tags`
|
8
|
+
* Create `.gem` file: `gem build maxmind-db.spec`
|
9
|
+
* Complete prerequisites (see below)
|
10
|
+
* Upload to rubygems.org: `gem push <.gem>`
|
11
|
+
|
12
|
+
|
13
|
+
# Prerequisites
|
14
|
+
|
15
|
+
## Step 1
|
16
|
+
Sign up for an account at rubygems.org if you don't have one.
|
17
|
+
|
18
|
+
Enable multi factor authentication.
|
19
|
+
|
20
|
+
|
21
|
+
## Step 2
|
22
|
+
Ask someone who is an owner of the gem to add you as one.
|
23
|
+
|
24
|
+
They do this by using the `gem owner` command
|
25
|
+
([docs](https://guides.rubygems.org/command-reference/#gem-owner)).
|
26
|
+
|
27
|
+
|
28
|
+
## Step 3
|
29
|
+
Go to your rubygems.org profile and find the curl command to run to
|
30
|
+
download your API key.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# MaxMind DB Reader Ruby API
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
This is the Ruby API for reading [MaxMind
|
6
|
+
DB](https://maxmind.github.io/MaxMind-DB/) files. MaxMind DB is a binary
|
7
|
+
file format that stores data indexed by IP address subnets (IPv4 or IPv6).
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require 'maxmind/db'
|
13
|
+
|
14
|
+
reader = MaxMind::DB.new('GeoIP2-City.mmdb', mode: MaxMind::DB::MODE_MEMORY)
|
15
|
+
|
16
|
+
record = reader.get('1.1.1.1')
|
17
|
+
if record.nil?
|
18
|
+
puts '1.1.1.1 was not found in the database'
|
19
|
+
else
|
20
|
+
puts record['country']['iso_code']
|
21
|
+
puts record['country']['names']['en']
|
22
|
+
end
|
23
|
+
|
24
|
+
reader.close
|
25
|
+
```
|
26
|
+
|
27
|
+
## Requirements
|
28
|
+
|
29
|
+
This code requires Ruby version 2.3 or higher. Older versions may work, but
|
30
|
+
are not supported.
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
Patches and pull requests are encouraged. Please include unit tests
|
35
|
+
whenever possible.
|
36
|
+
|
37
|
+
## Support
|
38
|
+
|
39
|
+
Please report all issues with this code using the [GitHub issue
|
40
|
+
tracker](https://github.com/maxmind/MaxMind-DB-Reader-ruby/issues).
|
41
|
+
|
42
|
+
If you are having an issue with a MaxMind service that is not specific to the
|
43
|
+
client API, please see [our support page](https://www.maxmind.com/en/support).
|
44
|
+
|
45
|
+
## Versioning
|
46
|
+
|
47
|
+
This library uses [Semantic Versioning](https://semver.org/).
|
48
|
+
|
49
|
+
## Copyright and License
|
50
|
+
|
51
|
+
This software is Copyright (c) 2018 by MaxMind, Inc.
|
52
|
+
|
53
|
+
This is free software, licensed under the [Apache License, Version
|
54
|
+
2.0](LICENSE-APACHE) or the [MIT License](LICENSE-MIT), at your option.
|
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'maxmind/db'
|
4
|
+
|
5
|
+
def main
|
6
|
+
args = parse_args
|
7
|
+
return false if args.nil?
|
8
|
+
|
9
|
+
reader = MaxMind::DB.new(args[:database], mode: MaxMind::DB::MODE_MEMORY)
|
10
|
+
benchmark(reader, args[:ip_file])
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse_args
|
15
|
+
if ARGV.length != 2
|
16
|
+
print_usage
|
17
|
+
return nil
|
18
|
+
end
|
19
|
+
|
20
|
+
database = ARGV[0]
|
21
|
+
ip_file = ARGV[1]
|
22
|
+
|
23
|
+
{
|
24
|
+
database: database,
|
25
|
+
ip_file: ip_file,
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def print_usage
|
30
|
+
STDERR.puts "Usage: #{$PROGRAM_NAME} <MMDB file> [IP file]"
|
31
|
+
STDERR.puts
|
32
|
+
STDERR.puts 'Benchmark by reading IPs from the IP file and looking up each one in the MMDB file.'
|
33
|
+
end
|
34
|
+
|
35
|
+
def benchmark(reader, file)
|
36
|
+
n = 0
|
37
|
+
count = 200_000
|
38
|
+
start = Time.now.to_f
|
39
|
+
File.open(file) do |fh|
|
40
|
+
fh.each do |line|
|
41
|
+
n += 1
|
42
|
+
line.strip!
|
43
|
+
|
44
|
+
reader.get(line)
|
45
|
+
|
46
|
+
write_status(start, n) if n % 1000 == 0
|
47
|
+
break if n == count
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def write_status(start, count)
|
53
|
+
now = Time.now.to_f
|
54
|
+
elapsed = now - start
|
55
|
+
rate = 1.0 * count / elapsed
|
56
|
+
puts format('%d @ %.2f lookups per second (%d seconds elapsed)', count, rate, elapsed)
|
57
|
+
end
|
58
|
+
|
59
|
+
exit 0 if main
|
60
|
+
|
61
|
+
exit 1
|
data/lib/maxmind/db.rb
ADDED
@@ -0,0 +1,274 @@
|
|
1
|
+
require 'ipaddr'
|
2
|
+
require 'maxmind/db/decoder'
|
3
|
+
require 'maxmind/db/errors'
|
4
|
+
require 'maxmind/db/file_reader.rb'
|
5
|
+
require 'maxmind/db/memory_reader.rb'
|
6
|
+
require 'maxmind/db/metadata.rb'
|
7
|
+
|
8
|
+
module MaxMind # :nodoc:
|
9
|
+
# DB provides a way to read {MaxMind DB
|
10
|
+
# files}[http://maxmind.github.io/MaxMind-DB/].
|
11
|
+
#
|
12
|
+
# {MaxMind DB}[http://maxmind.github.io/MaxMind-DB/] is a binary file format
|
13
|
+
# that stores data indexed by IP address subnets (IPv4 or IPv6).
|
14
|
+
#
|
15
|
+
# This class is a pure Ruby implementation of a reader for the format.
|
16
|
+
#
|
17
|
+
# == Example
|
18
|
+
#
|
19
|
+
# require 'maxmind/db'
|
20
|
+
#
|
21
|
+
# reader = MaxMind::DB.new('GeoIP2-City.mmdb', mode: MaxMind::DB::MODE_MEMORY)
|
22
|
+
#
|
23
|
+
# record = reader.get('1.1.1.1')
|
24
|
+
# if record.nil?
|
25
|
+
# puts '1.1.1.1 was not found in the database'
|
26
|
+
# else
|
27
|
+
# puts record['country']['iso_code']
|
28
|
+
# puts record['country']['names']['en']
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# reader.close
|
32
|
+
#
|
33
|
+
# == Exceptions
|
34
|
+
#
|
35
|
+
# DB raises an InvalidDatabaseError if the database is corrupt or invalid. It
|
36
|
+
# can raise other exceptions, such as ArgumentError, if other errors occur.
|
37
|
+
class DB
|
38
|
+
# Choose the default method to open the database. Currently the default is
|
39
|
+
# MODE_FILE.
|
40
|
+
MODE_AUTO = :MODE_AUTO
|
41
|
+
# Open the database as a regular file and read on demand.
|
42
|
+
MODE_FILE = :MODE_FILE
|
43
|
+
# Read the database into memory. This is faster than MODE_FILE but causes
|
44
|
+
# increased memory use.
|
45
|
+
MODE_MEMORY = :MODE_MEMORY
|
46
|
+
# Treat the database parameter as containing a database already read into
|
47
|
+
# memory. It must be a binary string. This primarily exists for testing.
|
48
|
+
MODE_PARAM_IS_BUFFER = :MODE_PARAM_IS_BUFFER # :nodoc:
|
49
|
+
|
50
|
+
DATA_SECTION_SEPARATOR_SIZE = 16
|
51
|
+
private_constant :DATA_SECTION_SEPARATOR_SIZE
|
52
|
+
METADATA_START_MARKER = "\xAB\xCD\xEFMaxMind.com".freeze.b.freeze
|
53
|
+
private_constant :METADATA_START_MARKER
|
54
|
+
METADATA_START_MARKER_LENGTH = 14
|
55
|
+
private_constant :METADATA_START_MARKER_LENGTH
|
56
|
+
METADATA_MAX_SIZE = 131_072
|
57
|
+
private_constant :METADATA_MAX_SIZE
|
58
|
+
|
59
|
+
# Return the metadata associated with the {MaxMind
|
60
|
+
# DB}[http://maxmind.github.io/MaxMind-DB/] as a Metadata object.
|
61
|
+
attr_reader :metadata
|
62
|
+
|
63
|
+
# Create a DB. A DB provides a way to read {MaxMind DB
|
64
|
+
# files}[http://maxmind.github.io/MaxMind-DB/]. If you're performing
|
65
|
+
# multiple lookups, it's most efficient to create one DB and reuse it.
|
66
|
+
#
|
67
|
+
# Once created, the DB is safe to use for lookups from multiple threads. It
|
68
|
+
# is safe to use after forking only if you use MODE_MEMORY or if your
|
69
|
+
# version of Ruby supports IO#pread.
|
70
|
+
#
|
71
|
+
# Creating the DB may raise an exception if initialization fails.
|
72
|
+
#
|
73
|
+
# +database+ is a path to a {MaxMind
|
74
|
+
# DB}[http://maxmind.github.io/MaxMind-DB/].
|
75
|
+
#
|
76
|
+
# +options+ is an option hash where each key is a symbol. The options
|
77
|
+
# control the behaviour of the DB.
|
78
|
+
#
|
79
|
+
# The available options are:
|
80
|
+
#
|
81
|
+
# [+:mode+] defines how to open the database. It may be one of MODE_AUTO,
|
82
|
+
# MODE_FILE, or MODE_MEMORY. If you don't provide one, DB uses
|
83
|
+
# MODE_AUTO. Refer to the definition of those constants for an
|
84
|
+
# explanation of their meaning.
|
85
|
+
def initialize(database, options = {})
|
86
|
+
options[:mode] = MODE_AUTO unless options.key?(:mode)
|
87
|
+
|
88
|
+
case options[:mode]
|
89
|
+
when MODE_AUTO, MODE_FILE
|
90
|
+
@io = FileReader.new(database)
|
91
|
+
when MODE_MEMORY
|
92
|
+
@io = MemoryReader.new(database)
|
93
|
+
when MODE_PARAM_IS_BUFFER
|
94
|
+
@io = MemoryReader.new(database, is_buffer: true)
|
95
|
+
else
|
96
|
+
raise ArgumentError, 'Invalid mode'.freeze
|
97
|
+
end
|
98
|
+
|
99
|
+
begin
|
100
|
+
@size = @io.size
|
101
|
+
|
102
|
+
metadata_start = find_metadata_start
|
103
|
+
metadata_decoder = Decoder.new(@io, metadata_start)
|
104
|
+
metadata_map, = metadata_decoder.decode(metadata_start)
|
105
|
+
@metadata = Metadata.new(metadata_map)
|
106
|
+
@decoder = Decoder.new(@io, @metadata.search_tree_size +
|
107
|
+
DATA_SECTION_SEPARATOR_SIZE)
|
108
|
+
|
109
|
+
# Store copies as instance variables to reduce method calls.
|
110
|
+
@ip_version = @metadata.ip_version
|
111
|
+
@node_count = @metadata.node_count
|
112
|
+
@node_byte_size = @metadata.node_byte_size
|
113
|
+
@record_size = @metadata.record_size
|
114
|
+
@search_tree_size = @metadata.search_tree_size
|
115
|
+
|
116
|
+
@ipv4_start = nil
|
117
|
+
# Find @ipv4_start up front. If we don't, we either have a race to
|
118
|
+
# get/set it or have to synchronize access.
|
119
|
+
start_node(0)
|
120
|
+
rescue StandardError => e
|
121
|
+
@io.close
|
122
|
+
raise e
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Return the record for the +ip_address+ in the {MaxMind
|
127
|
+
# DB}[http://maxmind.github.io/MaxMind-DB/]. The record can be one of
|
128
|
+
# several types and depends on the contents of the database.
|
129
|
+
#
|
130
|
+
# If no record is found for +ip_address+, +get+ returns +nil+.
|
131
|
+
#
|
132
|
+
# +get+ raises an exception if there is an error performing the lookup.
|
133
|
+
#
|
134
|
+
# +ip_address+ is a string in the standard notation. It may be IPv4 or
|
135
|
+
# IPv6.
|
136
|
+
def get(ip_address)
|
137
|
+
ip = IPAddr.new(ip_address)
|
138
|
+
# We could check the IP has the correct prefix (32 or 128) but I do not
|
139
|
+
# for performance reasons.
|
140
|
+
|
141
|
+
ip_version = ip.ipv6? ? 6 : 4
|
142
|
+
if ip_version == 6 && @ip_version == 4
|
143
|
+
raise ArgumentError,
|
144
|
+
"Error looking up #{ip}. You attempted to look up an IPv6 address in an IPv4-only database."
|
145
|
+
end
|
146
|
+
|
147
|
+
pointer = find_address_in_tree(ip, ip_version)
|
148
|
+
return nil if pointer == 0
|
149
|
+
|
150
|
+
resolve_data_pointer(pointer)
|
151
|
+
end
|
152
|
+
|
153
|
+
private
|
154
|
+
|
155
|
+
IP_VERSION_TO_BIT_COUNT = {
|
156
|
+
4 => 32,
|
157
|
+
6 => 128,
|
158
|
+
}.freeze
|
159
|
+
private_constant :IP_VERSION_TO_BIT_COUNT
|
160
|
+
|
161
|
+
def find_address_in_tree(ip_address, ip_version)
|
162
|
+
packed = ip_address.hton
|
163
|
+
|
164
|
+
bit_count = IP_VERSION_TO_BIT_COUNT[ip_version]
|
165
|
+
node = start_node(bit_count)
|
166
|
+
|
167
|
+
node_count = @node_count
|
168
|
+
bit_count.times do |i|
|
169
|
+
break if node >= node_count
|
170
|
+
c = packed[i >> 3].ord
|
171
|
+
bit = 1 & (c >> 7 - (i % 8))
|
172
|
+
node = read_node(node, bit)
|
173
|
+
end
|
174
|
+
|
175
|
+
return 0 if node == node_count
|
176
|
+
|
177
|
+
return node if node > node_count
|
178
|
+
|
179
|
+
raise InvalidDatabaseError, 'Invalid node in search tree'.freeze
|
180
|
+
end
|
181
|
+
|
182
|
+
def start_node(length)
|
183
|
+
return 0 if @ip_version != 6 || length == 128
|
184
|
+
|
185
|
+
return @ipv4_start if @ipv4_start
|
186
|
+
|
187
|
+
node = 0
|
188
|
+
96.times do
|
189
|
+
break if node >= @metadata.node_count
|
190
|
+
node = read_node(node, 0)
|
191
|
+
end
|
192
|
+
|
193
|
+
@ipv4_start = node
|
194
|
+
end
|
195
|
+
|
196
|
+
# Read a record from the indicated node. Index indicates whether it's the
|
197
|
+
# left (0) or right (1) record.
|
198
|
+
#
|
199
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
200
|
+
def read_node(node_number, index)
|
201
|
+
base_offset = node_number * @node_byte_size
|
202
|
+
|
203
|
+
if @record_size == 24
|
204
|
+
offset = index == 0 ? base_offset : base_offset + 3
|
205
|
+
buf = @io.read(offset, 3)
|
206
|
+
node_bytes = "\x00".freeze.b << buf
|
207
|
+
# When we support only Ruby 2.4+, we can change String#unpack calls
|
208
|
+
# that take the first element to String#unpack1.
|
209
|
+
return node_bytes.unpack('N'.freeze)[0]
|
210
|
+
end
|
211
|
+
|
212
|
+
if @record_size == 28
|
213
|
+
if index == 0
|
214
|
+
buf = @io.read(base_offset, 4)
|
215
|
+
n = buf.unpack('N'.freeze)[0]
|
216
|
+
last24 = n >> 8
|
217
|
+
first4 = (n & 0xf0) << 20
|
218
|
+
return first4 | last24
|
219
|
+
end
|
220
|
+
buf = @io.read(base_offset + 3, 4)
|
221
|
+
return buf.unpack('N'.freeze)[0] & 0x0fffffff
|
222
|
+
end
|
223
|
+
|
224
|
+
if @record_size == 32
|
225
|
+
offset = index == 0 ? base_offset : base_offset + 4
|
226
|
+
node_bytes = @io.read(offset, 4)
|
227
|
+
return node_bytes.unpack('N'.freeze)[0]
|
228
|
+
end
|
229
|
+
|
230
|
+
raise InvalidDatabaseError, "Unsupported record size: #{@record_size}"
|
231
|
+
end
|
232
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
233
|
+
|
234
|
+
def resolve_data_pointer(pointer)
|
235
|
+
offset_in_file = pointer - @node_count + @search_tree_size
|
236
|
+
|
237
|
+
if offset_in_file >= @size
|
238
|
+
raise InvalidDatabaseError,
|
239
|
+
'The MaxMind DB file\'s search tree is corrupt'.freeze
|
240
|
+
end
|
241
|
+
|
242
|
+
data, = @decoder.decode(offset_in_file)
|
243
|
+
data
|
244
|
+
end
|
245
|
+
|
246
|
+
def find_metadata_start
|
247
|
+
metadata_max_size = @size < METADATA_MAX_SIZE ? @size : METADATA_MAX_SIZE
|
248
|
+
|
249
|
+
stop_index = @size - metadata_max_size
|
250
|
+
index = @size - METADATA_START_MARKER_LENGTH
|
251
|
+
while index >= stop_index
|
252
|
+
return index + METADATA_START_MARKER_LENGTH if at_metadata?(index)
|
253
|
+
index -= 1
|
254
|
+
end
|
255
|
+
|
256
|
+
raise InvalidDatabaseError,
|
257
|
+
'Metadata section not found. Is this a valid MaxMind DB file?'.freeze
|
258
|
+
end
|
259
|
+
|
260
|
+
def at_metadata?(index)
|
261
|
+
@io.read(index, METADATA_START_MARKER_LENGTH) == METADATA_START_MARKER
|
262
|
+
end
|
263
|
+
|
264
|
+
public
|
265
|
+
|
266
|
+
# Close the DB and return resources to the system.
|
267
|
+
#
|
268
|
+
# There is no useful return value. #close raises an exception if there is
|
269
|
+
# an error.
|
270
|
+
def close
|
271
|
+
@io.close
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|