iptoasn 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +73 -0
  3. data/lib/iptoasn.rb +3 -0
  4. data/lib/main.rb +29 -4
  5. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f9becad4d91cd66d645e228fb998106ed1832b1d70eb633107106621be50262e
4
- data.tar.gz: 560094583c0b251f3dff6e5c24fb1220df7475c461d3a996a7c854a6dea32afc
3
+ metadata.gz: e7d28863be637ca0ad4c3b7b7ac648828fb963f3fed53bc237e695c83876737f
4
+ data.tar.gz: ac59b82846fcacd8e5d584ca7d10804dfe9097cd43d889bd53339c503686c011
5
5
  SHA512:
6
- metadata.gz: af385e1b8401f25a6b3ee8259496d9beaa3a5a8513c0d3ee5d5a135575fae1b4b69c18367254ffdddff03b271d79549f6531b539ad9f57086a0442b04d923825
7
- data.tar.gz: 7264f892662f1fb6b63d709bb6bf7ffa57ea10cfbe38fd268b0460f9d916d8658463a51d0e7acf93e7a5451bf6f14136a7676649e2733ddc6f3e958f9611d1aa
6
+ metadata.gz: 5bfdd00ff5e6d10bea42810af1861de3bcaf0eafebf933788ccac9b0056a5974bc9e457bd5cc3077b6b4d0952f9a46ed758512891beaa59c7fcec977a42f2ca1
7
+ data.tar.gz: b0f0ad77ee6e089c3dfa0143523f8829ddf5fe25d3c38063fc383038a9a1350c8de00847925e3cff7a2c2c9439e797f93ac021bbb67f1904d10f5c62dbd1f7d7
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # iptoasn
2
+
3
+ `iptoasn` is a Ruby gem that provides a simple way to query Autonomous System (AS) information for a given IP address using the [iptoasn.com](https://iptoasn.com) dataset. This gem directly contains the iptoasn dataset broken up into chunks; at build time an index is created so individual chunks can be lazy loaded.
4
+
5
+ ## Features
6
+
7
+ - Query AS information by IP address.
8
+ - Efficient lazy loading to minimize memory usage.
9
+ - Compatible with IPv4 addresses.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'iptoasn'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```bash
22
+ bundle install
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```bash
28
+ gem install iptoasn
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Here is a basic example of how to use the `iptoasn` gem:
34
+
35
+ ```ruby
36
+ require 'iptoasn'
37
+
38
+ finder = IpToAsn.new
39
+ ip_address = ARGV[0]
40
+
41
+ response = finder.lookup(ip_address)
42
+ if response.nil?
43
+ puts "Couldn't locate #{ip_address}!"
44
+ exit 1
45
+ end
46
+
47
+ puts "#{ip_address} is in #{response[:country_code]} and belongs to #{response[:as_name]}"
48
+ ```
49
+
50
+ ## Building
51
+
52
+ ```shell
53
+ $ make fetch # grab the latest copy of the dataset
54
+ $ make process # break it up into chunks
55
+ $ make index # build indexes
56
+ $ make clean # clean up
57
+ ```
58
+
59
+ ## Dataset
60
+
61
+ The gem wraps the [iptoasn.com](https://iptoasn.com) dataset, which contains information about:
62
+ - IP address ranges (start and end IPs)
63
+ - Autonomous System Numbers (ASNs)
64
+ - Country codes
65
+ - Autonomous System names
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
70
+
71
+ ## Acknowledgments
72
+
73
+ This gem utilizes the [iptoasn.com](https://iptoasn.com) dataset. Special thanks to them for providing this valuable resource.
data/lib/iptoasn.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'main'
data/lib/main.rb CHANGED
@@ -1,14 +1,31 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ipaddr'
2
- require_relative 'index.rb'
4
+ require_relative 'index'
3
5
 
4
6
  class IpToAsn
5
7
  def initialize
6
8
  @index = chunk_index
7
9
  @cache = {}
8
- @chunk_dir = "./chunks"
10
+ @chunk_dir = './chunks'
11
+ @reserved_ranges = {
12
+ IPAddr.new('10.0.0.0/8') => 'RFC1918 Private',
13
+ IPAddr.new('172.16.0.0/12') => 'RFC1918 Private',
14
+ IPAddr.new('192.168.0.0/16') => 'RFC1918 Private',
15
+ IPAddr.new('0.0.0.0/8') => 'Current Network',
16
+ IPAddr.new('127.0.0.0/8') => 'Loopback',
17
+ IPAddr.new('169.254.0.0/16') => 'Link Local'
18
+ }
9
19
  end
10
20
 
11
- def lookup(ip_str)
21
+ def lookup(ip_str) # rubocop:disable Metrics/AbcSize
22
+ reserved_range_name = find_range(
23
+ ranges: @reserved_ranges,
24
+ ip_address: ip_str
25
+ )
26
+
27
+ return { as_number: 0, country_code: 'XX', as_name: reserved_range_name } unless reserved_range_name.nil?
28
+
12
29
  chunk_name = binary_search(@index, ip_str).first
13
30
  return nil if chunk_name.nil?
14
31
 
@@ -18,11 +35,19 @@ class IpToAsn
18
35
  end
19
36
 
20
37
  result = binary_search(@cache[chunk_name], ip_str)
21
- nil if result.empty?
38
+ return nil if result.empty?
22
39
 
23
40
  { as_number: result[0], country_code: result[1], as_name: result[2] }
24
41
  end
25
42
 
43
+ private
44
+
45
+ def find_range(ranges:, ip_address:)
46
+ ranges.filter_map do |k, v|
47
+ v if k.include? ip_address
48
+ end.first
49
+ end
50
+
26
51
  def binary_search(ranges, ip_str)
27
52
  ip_int = IPAddr.new(ip_str).to_i
28
53
  low = 0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iptoasn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OMAR
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-28 00:00:00.000000000 Z
11
+ date: 2025-01-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem wraps the IP to ASN dataset. It uses lazy loading so it doesn't
14
14
  load the entire ~25M dataset all at once.
@@ -17,6 +17,7 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - README.md
20
21
  - lib/chunks/chunk_aa.rb
21
22
  - lib/chunks/chunk_ab.rb
22
23
  - lib/chunks/chunk_ac.rb
@@ -42,6 +43,7 @@ files:
42
43
  - lib/chunks/chunk_aw.rb
43
44
  - lib/chunks/chunk_ax.rb
44
45
  - lib/index.rb
46
+ - lib/iptoasn.rb
45
47
  - lib/main.rb
46
48
  homepage: https://github.com/ancat/iptoasn
47
49
  licenses: