ip2cityisp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b71a86858729619a02b44e2f51ba479787fe86b
4
+ data.tar.gz: 3bd769d106b4e9c4bf7f2414569a6ce20b5f2fef
5
+ SHA512:
6
+ metadata.gz: 1f8c5ce55a302572eab4a182bf0fe72d4b86cdb9e2e2b0dbf0a544b6752610c8bab648c69c323ac817442c2ba0c1415d438fa8077c6d4b6bfc6f7127b3f002af
7
+ data.tar.gz: a4efaffdc9fc7fca2fac3e14bde427bc8dd0b66fa1f93633c9bdd5b03d0456788324e0d95cef9c37fb843ad4d073367162dc90020429c7eb8ce26b386a64306a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ip2cityisp.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 geda
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Ip2cityisp
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ip2cityisp'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ip2cityisp
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/ip2cityisp/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ip2cityisp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ip2cityisp"
8
+ spec.version = Ip2cityisp::VERSION
9
+ spec.authors = ["geda"]
10
+ spec.email = ["beidou77@gmail.com"]
11
+ spec.summary = %q{query ip from which city and isp}
12
+ spec.description = %q{return isp and city}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 0.9"
23
+ spec.add_development_dependency "bindata", "~> 2.1"
24
+ end
@@ -0,0 +1,97 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'bindata'
4
+ module Ip2cityisp
5
+ class Header < BinData::Record
6
+ SIZE = 8
7
+ endian :little
8
+ string :index_version , :read_length => 4
9
+ uint32 :total_nums
10
+ end
11
+
12
+ class Index < BinData::Record
13
+ SIZE = 12
14
+ endian :little
15
+ uint32 :ip_start
16
+ uint32 :country_offset
17
+ uint32 :isp_offset
18
+ uint32 :ip_end
19
+ end
20
+
21
+ class Database
22
+ attr_reader :area, :isp
23
+ def initialize(file = 'ipinfo.data')
24
+ if file.respond_to?(:to_str)
25
+ @file = File.new(file.to_str)
26
+ else
27
+ @file = file
28
+ @file.seek(0)
29
+ end
30
+ @header = Ip2cityisp::Header.read(@file)
31
+ $start = Ip2cityisp::Header::SIZE
32
+ $high = @header.total_nums
33
+ $off1 = @header.total_nums*12 + 20
34
+ $version = @header.index_version
35
+ #get end off2
36
+ @file.seek($high*12+8)
37
+ index = Ip2cityisp::Index.read(@file)
38
+ $off2 = $off1 + index.country_offset
39
+ $create_time= index.isp_offset
40
+ @file.seek(0)
41
+ end
42
+
43
+ def version
44
+ $version + ":CREATE TIME:" + Time.at($create_time).to_date.to_s
45
+ end
46
+
47
+ def query(ip)
48
+ if ip.respond_to?(:to_str)
49
+ #way 1
50
+ ips = ip.to_str.split('.')
51
+ ip = ips[0].to_i * 16777216 + ips[1].to_i * 65536 + ips[2].to_i * 256 + ips[3].to_i
52
+ #p ip
53
+ #way 2
54
+ #ip = ip.to_str.split('.').collect{|i| i.to_i}.pack('C4').unpack('N')[0]
55
+ end
56
+ raise ArgumentError, "invalid ip" unless (0...(1 << 32)).include?(ip)
57
+ low = 0
58
+ high = $high - 1
59
+ start = $start
60
+ info = {"area"=>"","isp"=>""}
61
+ while(low <= high)
62
+ middle = (low + high) >> 1
63
+ #p middle
64
+ @file.seek(start + middle * Ip2cityisp::Index::SIZE)
65
+ index = Ip2cityisp::Index.read(@file)
66
+ ip_start = index.ip_start
67
+ country_offset = index.country_offset
68
+ isp_offset = index.isp_offset
69
+ ip_end = index.ip_end
70
+ if ip < index.ip_start
71
+ high = middle - 1
72
+ elsif ip >= index.ip_end
73
+ low = middle + 1
74
+ else
75
+ if country_offset >= 0
76
+ country_off = $off1 + country_offset
77
+ if country_off < @file.size
78
+ @file.seek(country_off)
79
+ vlen = @file.read(1).unpack('C')[0]
80
+ info["area"] = @file.read(vlen).force_encoding("UTF-8")
81
+ end
82
+ end
83
+ isp_off = $off2 + isp_offset
84
+ if isp_offset >= 0
85
+ isp_off = $off2 + isp_offset
86
+ if isp_off < @file.size
87
+ @file.seek(isp_off)
88
+ vlen = @file.read(1).unpack('C')[0]
89
+ info["isp"] = @file.read(vlen).force_encoding("UTF-8")
90
+ end
91
+ end
92
+ return info
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module Ip2cityisp
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ip2cityisp.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "ip2cityisp/version"
2
+ require "ip2cityisp/database"
3
+
data/tt.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'ip2cityisp'
4
+ ip = ARGV[0]
5
+ @db = Ip2cityisp::Database.new('/tmp/ipinfo.data')
6
+ r = @db.query('8.8.8.8')
7
+ rip = r["area"]+r["isp"]
8
+ p rip
9
+ p @db.version
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ip2cityisp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - geda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2014-09-02 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: "1.7"
22
+ type: :development
23
+ version_requirements: *id001
24
+ - !ruby/object:Gem::Dependency
25
+ name: rake
26
+ prerelease: false
27
+ requirement: &id002 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: "0.9"
32
+ type: :development
33
+ version_requirements: *id002
34
+ - !ruby/object:Gem::Dependency
35
+ name: bindata
36
+ prerelease: false
37
+ requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: "2.1"
42
+ type: :development
43
+ version_requirements: *id003
44
+ description: return isp and city
45
+ email:
46
+ - beidou77@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files: []
52
+
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - ip2cityisp.gemspec
60
+ - lib/ip2cityisp.rb
61
+ - lib/ip2cityisp/database.rb
62
+ - lib/ip2cityisp/version.rb
63
+ - tt.rb
64
+ homepage: ""
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - &id004
77
+ - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - *id004
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 2.4.1
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: query ip from which city and isp
90
+ test_files: []
91
+