ip_location_cn 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ip_location_cn.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rspec'
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 calebx
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,29 @@
1
+ # IpLocationCn
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ip_location_cn'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ip_location_cn
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ip_location_cn/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["calebx"]
6
+ gem.email = ["caleb.xiang@gmail.com"]
7
+ gem.description = %q{根据IP地址判断所在地}
8
+ gem.summary = %q{使用的是QQWRY纯真IP地址库2012-12}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "ip_location_cn"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = IpLocationCn::VERSION
17
+ end
data/lib/.DS_Store ADDED
Binary file
Binary file
@@ -0,0 +1,110 @@
1
+ require 'singleton'
2
+ module IpLocationCn
3
+ class QqWry
4
+ include Singleton
5
+
6
+ def initialize
7
+ data_file_path = File.expand_path("../../data/qqwry.dat", __FILE__)
8
+ @data_file = File.open data_file_path, 'r'
9
+ @index_first, @index_last = @data_file.read(8).unpack('L2')
10
+ @index_total = (@index_last - @index_first) / 7 + 1
11
+ @location = {}
12
+ end
13
+
14
+ #读取偏移值
15
+ def read_offset(position)
16
+ @data_file.seek position
17
+ chars = @data_file.read(3).unpack('C3')
18
+ (chars[2] << 16) + (chars[1] << 8) + chars[0]
19
+ end
20
+
21
+ #读取记录中的4字节作为一个long值
22
+ def read_long(position)
23
+ @data_file.seek position
24
+ @data_file.read(4).unpack('L')[0]
25
+ end
26
+
27
+ #读取模式信息,1和2为正常,其他值异常 position:字符串偏移量
28
+ def read_mode(position)
29
+ @data_file.seek position #前4位为IP值
30
+ @data_file.read(1).unpack('C')[0]
31
+ end
32
+
33
+ #读取记录中的地址信息
34
+ def read_origin(position)
35
+ mode = read_mode(position)
36
+ if mode == 1 || mode == 2
37
+ offset = read_offset(position + 1)
38
+ return '' if offset == 0
39
+ return read_str(offset)
40
+ else
41
+ return read_str(position)
42
+ end
43
+ end
44
+
45
+ #根据IP在索引中查找具体位置
46
+ def find_str_offset(ip_long)
47
+ offset_min, offset_max = @index_first, @index_last
48
+ while offset_min <= offset_max
49
+ offset_mid = offset_min + (offset_max - offset_min) / 14 * 7
50
+ mid = read_long(offset_mid)
51
+ if ip_long < mid
52
+ offset_max = offset_mid - 7
53
+ elsif ip_long == mid
54
+ return read_offset(offset_mid+4)
55
+ else
56
+ offset_min = offset_mid + 7
57
+ end
58
+ end
59
+
60
+ return read_offset(offset_max + 4)
61
+ end
62
+
63
+ #读取字符串
64
+ def read_str(position)
65
+ @data_file.seek position
66
+ str = []
67
+ while c = @data_file.getc
68
+ break if str.size > 60 #地址不会太长,防止有异常数据
69
+ break if c == "\0" #地址字符串以\0结尾
70
+ str << c
71
+ end
72
+ str.join ''
73
+ end
74
+
75
+ #把IP转换为长整形
76
+ def ip2long(ip)
77
+ long = 0
78
+ ip.split(/\./).each_with_index do |b, i|
79
+ long += b.to_i << 8 * (3 - i)
80
+ end
81
+ long
82
+ end
83
+
84
+ #根据IP查找地址
85
+ def find_ip_location(ip)
86
+ offset = find_str_offset(ip2long(ip))#读取具体数据在记录区的偏移
87
+ @location = {}
88
+ case read_mode(offset + 4)
89
+ when 1
90
+ str_offset = read_offset(offset + 4 + 1) #读取字符串存储位置偏移(4是IP值,1是模式)
91
+ if read_mode(str_offset) == 2 then
92
+ location_offset = read_offset(str_offset+1)
93
+ @location[:location] = read_str location_offset
94
+ @location[:origin] = read_origin(str_offset+4)
95
+ else
96
+ @location[:location] = read_str str_offset
97
+ @location[:origin] = read_origin(@data_file.pos)
98
+ end
99
+ when 2
100
+ str_offset = read_offset(offset + 4 + 1) #读取字符串存储位置偏移(4是IP值,1是模式)
101
+ @location[:location] = read_str(str_offset)
102
+ @location[:origin] = read_origin(offset + 8)
103
+ else
104
+ @location[:location] = read_str(offset)
105
+ @location[:origin] = read_str(@data_file.pos)
106
+ end
107
+ @location
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,3 @@
1
+ module IpLocationCn
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ require "ip_location_cn/version"
2
+ require "ip_location_cn/qq_wry"
3
+
4
+ module IpLocationCn
5
+ class << self
6
+ def valid_ip(ip)
7
+ ip =~ /(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9])[.]){3}(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9]))/i
8
+ end
9
+
10
+ def qqwry
11
+ @qqwry ||= QqWry.instance
12
+ end
13
+
14
+ def location_of(ip)
15
+ return 'invalid ip address' unless valid_ip(ip)
16
+ result = qqwry.find_ip_location(ip)
17
+ ec = Encoding::Converter.new("GB2312", "UTF-8")
18
+ result[:location] = ec.convert result[:location]
19
+ result[:origin] = ec.convert result[:origin]
20
+ result
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ #encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe IpLocationCn do
5
+ it "should testing valid ip" do
6
+ IpLocationCn.valid_ip("192.168.12.1").should_not == nil
7
+ end
8
+
9
+ it "should works" do
10
+ IpLocationCn.location_of("115.236.64.78").should == { location: "浙江省杭州市", origin: "电信" }
11
+ end
12
+
13
+ it "should return nothing if ip is not valid" do
14
+ IpLocationCn.location_of("192.168.12.e").should == 'invalid ip address'
15
+ end
16
+
17
+ it "works fine" do
18
+ list = %w[66.249.71.153 202.8.15.255.2 61.157.175.233 58.19.176.201 61.178.12.170 61.191.187.113 121.14.133.169 58.222.234.230 202.198.184.136 121.12.116.58 203.191.148.55]
19
+ list.each do |ip|
20
+ t_result = IpLocationCn.location_of(ip)
21
+ puts t_result
22
+ t_result.should_not == nil
23
+ end
24
+ end
25
+ end
@@ -0,0 +1 @@
1
+ require 'ip_location_cn.rb'
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ip_location_cn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - calebx
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: 根据IP地址判断所在地
15
+ email:
16
+ - caleb.xiang@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .DS_Store
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - ip_location_cn.gemspec
28
+ - lib/.DS_Store
29
+ - lib/data/qqwry.dat
30
+ - lib/ip_location_cn.rb
31
+ - lib/ip_location_cn/qq_wry.rb
32
+ - lib/ip_location_cn/version.rb
33
+ - spec/ip_location_cn_spec.rb
34
+ - spec/spec_helper.rb
35
+ homepage: ''
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: 使用的是QQWRY纯真IP地址库2012-12
59
+ test_files:
60
+ - spec/ip_location_cn_spec.rb
61
+ - spec/spec_helper.rb