search_apnic 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28666d6d9e1437dc2da807e1441b7299d9eb64dd
4
+ data.tar.gz: 3e65d4a8a824a0648c4f71c5f39303ba8900e3b5
5
+ SHA512:
6
+ metadata.gz: b26631fe6f47bdc0bbda771dd1768a451b617ba8cf00fc8e22f162e4624d66554ff70a36411834e01604dac07d5d739ef5fe9660ce79995189969634c7ef2137
7
+ data.tar.gz: e169705cc6fb74aa7fc79de0bbca2ad7009f2eab43708e63c8c59831569f8e0743614fc2208e55802c6454cd50a4e1e9f5cb04ac95ee05a03ccda9e66054096f
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at kobayashi@ans-web.co.jp. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in search_apnic.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+ directories %w(lib test) \
8
+ .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
9
+
10
+ ## Note: if you are using the `directories` clause above and you are not
11
+ ## watching the project directory ('.'), then you will want to move
12
+ ## the Guardfile to a watched dir and symlink it back, e.g.
13
+ #
14
+ # $ mkdir config
15
+ # $ mv Guardfile config/
16
+ # $ ln -s config/Guardfile .
17
+ #
18
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
19
+
20
+ guard :minitest do
21
+ watch(%r{^test/(.*)\/?test_(.*)\.rb$})
22
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
23
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
24
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 hiroshi kobayashi
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,41 @@
1
+ # SearchApnic
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/search_apnic`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'search_apnic'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install search_apnic
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/search_apnic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "search_apnic"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/search_apnic ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: ft=ruby
3
+ require 'search_apnic'
4
+ require 'optparse'
5
+ opt = OptionParser.new
6
+ @opts = {
7
+ dump: false,
8
+ ipaddr: nil,
9
+ country: nil
10
+ }
11
+ opt.on('-c country', '--country=Serach-Country') {|v| @opts[:country] = v}
12
+ opt.on('-i ipaddr', '--ipaddr=IP-Address', 'Search addr in Country'){|v| @opts[:ipaddr] = v}
13
+ opt.on('-D', '--dump', 'Dump all IP Adrress') {|v| @opts[:dump] = true }
14
+ opt.parse!(ARGV)
15
+
16
+ unless @opts[:country]
17
+ puts "Nothing Country. `search_apnic -c JP -i 192.168.1.1`"
18
+ puts "Choose #{SearchApnic::Search.countries.join(" ")}"
19
+ exit 1
20
+ end
21
+
22
+ by_country = SearchApnic::Search.new.by_country(@opts[:country])
23
+
24
+ if @opts[:dump]
25
+ by_country.print
26
+ exit 1
27
+ end
28
+
29
+ if @opts[:ipaddr]
30
+ if ip_and_mask = by_country.include?(@opts[:ipaddr])
31
+ puts ip_and_mask
32
+ exit 0
33
+ else
34
+ puts 'Not Match'
35
+ exit 1
36
+ end
37
+ end
38
+
@@ -0,0 +1,50 @@
1
+ require 'net/http'
2
+ require 'open-uri'
3
+ require "awesome_print"
4
+ module SearchApnic
5
+ class File
6
+ HOST = 'ftp.apnic.net'
7
+ PATH = '/stats/apnic/delegated-apnic-latest'
8
+ CACHE_FILE = '/tmp/apnic-latest.txt'
9
+
10
+ def get_path
11
+ file = cache_file
12
+ fresh?(file) ? file : get
13
+ end
14
+
15
+ def head
16
+ Net::HTTP.new(HOST).head(PATH)
17
+ end
18
+
19
+ private
20
+
21
+ def url
22
+ "http://#{HOST}#{PATH}"
23
+ end
24
+
25
+ def get
26
+ file = cache_file
27
+ open(file, 'wb') do |file|
28
+ open(url) do |data|
29
+ file.write(data.read)
30
+ end
31
+ end
32
+ file
33
+ end
34
+
35
+ def fresh?(file)
36
+ ::FileTest.exist?(file) ? true : false
37
+ end
38
+
39
+ def cache_file
40
+ "#{CACHE_FILE}.#{last_modified.to_i}"
41
+ end
42
+
43
+ def last_modified
44
+ unless @last_modified
45
+ @last_modified = Time.parse(head['last-modified'])
46
+ end
47
+ @last_modified
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,133 @@
1
+ require 'ipaddr'
2
+ module SearchApnic
3
+ class Search
4
+
5
+ # File Format http://ftp.apnic.net/apnic/stats/apnic/README.TXT
6
+ def by_country(country, protocol_version: 'ipv4')
7
+ ::File.foreach(SearchApnic::File.new.get_path) do |l|
8
+ next if l[0] == "#" # Comment Line
9
+
10
+ row = l.strip.split("|")
11
+
12
+ unless header
13
+ set_header(row)
14
+ next
15
+ end
16
+
17
+ if(row[5] == 'summary')
18
+ add_summary(row)
19
+ next
20
+ end
21
+
22
+ record = parse_record(row)
23
+ next if record[:type] == 'asn' # Autonomous System number
24
+ next if record[:cc] != country
25
+ next if record[:type] != protocol_version # ipv4 or ipv6
26
+ add_record record
27
+ end
28
+ self
29
+ end
30
+
31
+ def print
32
+ records.each do|l|
33
+ puts l
34
+ end
35
+ end
36
+
37
+ def include?(ip)
38
+ search_first_number = ip.split(".").first
39
+ records.select{|ip_and_mask| search_first_number == ip_and_mask.split(".").first}.each do |ip_and_mask|
40
+ if IPAddr.new(ip_and_mask).include? ip
41
+ return ip_and_mask
42
+ end
43
+ end
44
+ return false
45
+ end
46
+
47
+ def self.countries
48
+ %w{
49
+ AP AS AU BD BN BR BT
50
+ CA CK CN CO
51
+ DE
52
+ ES
53
+ FJ FM FR
54
+ GB GU
55
+ HK
56
+ ID IN IO IR
57
+ JP
58
+ KE KH KI KP KR
59
+ LA LK
60
+ MH MM MN MO MP MU MV MY
61
+ NC NF NL NP NR NU NZ
62
+ PF PG PH PK PW
63
+ SA SB SC SE SG
64
+ TH TK TL TO TR TV TW
65
+ US
66
+ VN VU
67
+ WF WS
68
+ ZA
69
+ }
70
+ end
71
+
72
+ def records
73
+ @records
74
+ end
75
+
76
+ def summary
77
+ @summary
78
+ end
79
+
80
+ def header
81
+ @header
82
+ end
83
+
84
+ private
85
+
86
+ def add_record(record)
87
+ @records ||= []
88
+ number_of_ip = record[:value].to_i
89
+ start = record[:start]
90
+ mask = 33 - ("%b" % number_of_ip).length + 1
91
+ ip = "#{start}/#{mask}"
92
+
93
+ @records << ip
94
+ end
95
+
96
+ def parse_record(row)
97
+ {
98
+ registry: row.shift,
99
+ cc: row.shift,
100
+ type: row.shift,
101
+ start: row.shift,
102
+ value: row.shift,
103
+ date: row.shift,
104
+ status: row.shift,
105
+ extensions: row
106
+ }
107
+ end
108
+
109
+ def add_summary(row)
110
+ @summary ||= []
111
+ @summary << {
112
+ registry: row.shift,
113
+ noop1: row.shift,
114
+ type: row.shift,
115
+ noop2: row.shift,
116
+ count: row.shift,
117
+ summary: row.shift,
118
+ }
119
+ end
120
+
121
+ def set_header(row)
122
+ @header = {
123
+ version: row.shift,
124
+ registry: row.shift,
125
+ serial: row.shift,
126
+ records: row.shift,
127
+ startdate: row.shift,
128
+ enddate: row.shift,
129
+ utcoffset: row.shift
130
+ }
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,3 @@
1
+ module SearchApnic
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "search_apnic/version"
2
+ require "search_apnic/file"
3
+ require "search_apnic/search"
4
+
5
+ module SearchApnic
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'search_apnic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "search_apnic"
8
+ spec.version = SearchApnic::VERSION
9
+ spec.authors = ["hiroshi kobayashi"]
10
+ spec.email = ["koba.shipbuilding@gmail.com"]
11
+
12
+ spec.summary = %q{Search IP Adress by country from APNIC.}
13
+ spec.description = %q{Dump or Search IP Adress from APNIC.}
14
+ spec.homepage = "https://github.com/kminiatures/search_apnic"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ spec.add_development_dependency "guard"
26
+ spec.add_development_dependency "guard-minitest"
27
+ spec.add_development_dependency "awesome_print"
28
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search_apnic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hiroshi kobayashi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
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
+ - !ruby/object:Gem::Dependency
70
+ name: guard-minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Dump or Search IP Adress from APNIC.
98
+ email:
99
+ - koba.shipbuilding@gmail.com
100
+ executables:
101
+ - search_apnic
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - Guardfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - exe/search_apnic
116
+ - lib/search_apnic.rb
117
+ - lib/search_apnic/file.rb
118
+ - lib/search_apnic/search.rb
119
+ - lib/search_apnic/version.rb
120
+ - search_apnic.gemspec
121
+ homepage: https://github.com/kminiatures/search_apnic
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.5.1
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Search IP Adress by country from APNIC.
145
+ test_files: []