ip2region 0.1.0

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
+ SHA256:
3
+ metadata.gz: 4cf7641a9cdffd70afad1721300c25a25e43aa60c4fc123be5bf3aa8d088f2a1
4
+ data.tar.gz: 6449f691a5d3c7549b7373b39e6e65f59634bf612554f7fe05014b42e81fb0d8
5
+ SHA512:
6
+ metadata.gz: 6cad346e866c41bfaec9e776a82661f6fcd9530104427f22064ea21285ff318c45ea3a5a42b92e67ade6d04f83de5761cddc4dedec3edc420be9c85befc05792
7
+ data.tar.gz: effd14501f9988ee5c5d2e5c00b059f73fd292429fb512a17c7f43542b9e3f032697cab8c00bf07ae7493c86ea5f186797cf214b11af47c1f4085e5f593e8e1c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-04-14
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at atpking@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ip2region.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ip2region (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ rake (13.0.6)
11
+ rspec (3.12.0)
12
+ rspec-core (~> 3.12.0)
13
+ rspec-expectations (~> 3.12.0)
14
+ rspec-mocks (~> 3.12.0)
15
+ rspec-core (3.12.1)
16
+ rspec-support (~> 3.12.0)
17
+ rspec-expectations (3.12.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.12.0)
20
+ rspec-mocks (3.12.5)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.12.0)
23
+ rspec-support (3.12.0)
24
+
25
+ PLATFORMS
26
+ arm64-darwin-22
27
+
28
+ DEPENDENCIES
29
+ ip2region!
30
+ rake (~> 13.0)
31
+ rspec (~> 3.0)
32
+
33
+ BUNDLED WITH
34
+ 2.4.8
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 atpking
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,40 @@
1
+ # ruby-Ip2region
2
+
3
+ ## 介绍
4
+
5
+ 这是一个ruby 版本的 ip2region, 基于 [lionsoul2014/ip2region](https://github.com/lionsoul2014/ip2region) v2.0 版本开发
6
+
7
+ 此版本是完全仿照 python, 通过 copilot 进行的编写
8
+
9
+ 为了方便直接搞成 gem 包, 故单独开了个项目
10
+
11
+ ## 安装
12
+ gem install ip2region
13
+ ## 使用
14
+
15
+ **需要外挂 xdb 文件**
16
+
17
+
18
+ ```ruby
19
+ require 'ip2region'
20
+
21
+ # 使用前必须设置 xdb 文件路径
22
+ Ip2region.ip_2_region_path="./ip2region.xdb"
23
+
24
+ # 查询
25
+ Ip2region.search("114.114.114.114“)
26
+ # => "中国|0|江苏省|南京市|0"
27
+
28
+ Ip2region.search "127.0.0.1"
29
+ => "0|0|0|内网IP|内网IP"
30
+
31
+ ```
32
+
33
+ ## 如何下载外挂 xdb 文件
34
+
35
+ 下载这个文件 [https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.xdb](https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.xdb)
36
+
37
+
38
+ ## 协议
39
+
40
+ MIT 协议
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ip2region
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,138 @@
1
+ require 'ipaddr'
2
+ require 'singleton'
3
+
4
+
5
+ module Ip2region
6
+ class XdbSearcher
7
+ include ::Singleton
8
+
9
+ # xdb默认参数
10
+ HeaderInfoLength = 256
11
+ VectorIndexRows = 256
12
+ VectorIndexCols = 256
13
+ VectorIndexSize = 8
14
+ SegmentIndexSize = 14
15
+
16
+ attr_accessor :vector_index, :content_buff, :f
17
+
18
+ def self.xdb_path= (path)
19
+ @@xdb_path = path
20
+ end
21
+
22
+ def self.xdb_path
23
+ @@xdb_path
24
+ end
25
+
26
+ def initialize()
27
+ init_database(@@xdb_path, nil, nil)
28
+ end
29
+
30
+ def init_database(dbfile, vi, cb)
31
+ begin
32
+ if cb
33
+ @f = nil
34
+ @vector_index = nil
35
+ @content_buff= cb
36
+ else
37
+ @f = File.open(dbfile, "rb")
38
+ @vector_index = vi
39
+ end
40
+ rescue IOError => e
41
+ puts "[Error]: #{e}"
42
+ exit
43
+ end
44
+ end
45
+
46
+ def search(ip)
47
+ if ip.is_a?(String)
48
+
49
+ ip = ip_to_long(ip).to_i unless ip =~ /\A\d+\z/
50
+ return search_by_ip_long(ip)
51
+ else
52
+ return search_by_ip_long(ip)
53
+ end
54
+ end
55
+
56
+ def ip_to_long(ip)
57
+ IPAddr.new(ip).to_i
58
+ end
59
+
60
+ def get_long(b, offset)
61
+ return 0 if b[offset, 4].length != 4
62
+
63
+ b[offset, 4].unpack("I")[0]
64
+ end
65
+
66
+ def get_int2(b, offset)
67
+ (b[offset].ord & 0x000000FF) | (b[offset + 1].ord & 0x0000FF00)
68
+ end
69
+
70
+ def read_buffer(offset, length)
71
+ buffer = nil
72
+ if self.content_buff != nil
73
+ buffer = self.content_buff[offset, length]
74
+ else
75
+ f.seek(offset)
76
+ buffer = f.read(length)
77
+ end
78
+ end
79
+
80
+ def search_by_ip_long(ip)
81
+ #locate the segment index block based on the vector index
82
+ s_ptr = e_ptr = 0
83
+ il0 = (ip >> 24) & 0xFF
84
+ il1 = (ip >> 16) & 0xFF
85
+ idx = il0 * VectorIndexCols * VectorIndexSize + il1 * VectorIndexSize
86
+
87
+
88
+ if self.vector_index != nil
89
+ s_ptr = self.get_long(self.vector_index, idx)
90
+ e_ptr = self.get_long(self.vector_index, idx + 4)
91
+ elsif self.content_buff != nil
92
+ s_ptr = self.get_long(self.content_buff,HeaderInfoLength + idx)
93
+ e_ptr = self.get_long(self.content_buff, HeaderInfoLength + idx + 4)
94
+ else
95
+ f.seek(HeaderInfoLength + idx)
96
+ buffer_ptr = f.read(8)
97
+ s_ptr = self.get_long(buffer_ptr, 0)
98
+ e_ptr = self.get_long(buffer_ptr, 4)
99
+ end
100
+
101
+ data_len = data_ptr = -1
102
+
103
+ l = 0
104
+ h = (e_ptr - s_ptr) / SegmentIndexSize
105
+
106
+ while l <= h
107
+ m = (l + h) >> 1
108
+ p = s_ptr + m * SegmentIndexSize
109
+
110
+ # read the segment index
111
+ buffer_sip = self.read_buffer(p, SegmentIndexSize)
112
+ sip = self.get_long(buffer_sip, 0)
113
+ if ip < sip
114
+ h = m-1
115
+ else
116
+ eip = self.get_long(buffer_sip, 4)
117
+ if ip > eip
118
+ l = m + 1
119
+ else
120
+ data_len = self.get_int2(buffer_sip, 8)
121
+ data_ptr = self.get_long(buffer_sip, 10)
122
+ break
123
+ end
124
+ end
125
+ end
126
+
127
+ if data_ptr == -1
128
+ return nil
129
+ end
130
+
131
+ buffer_string = self.read_buffer(data_ptr, data_len)
132
+ # to utf8
133
+ buffer_string.force_encoding("UTF-8")
134
+
135
+ end
136
+ end
137
+
138
+ end
data/lib/ip2region.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ip2region/version"
4
+ require_relative "ip2region/xdb_searcher"
5
+
6
+ module Ip2region
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+
10
+ def self.ip_2_region_path=(path)
11
+ @@ip_2_region_path = path
12
+ end
13
+
14
+ def self.search(ip)
15
+
16
+ if @@ip_2_region_path.nil?
17
+ throw "需要先运行 Ip2region.ip_2_region_path = XDB_PATH"
18
+ end
19
+
20
+ XdbSearcher.xdb_path = @@ip_2_region_path
21
+
22
+ searcher = XdbSearcher.instance
23
+ searcher.search(ip)
24
+ end
25
+ end
data/sig/ip2region.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ip2region
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ip2region
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - atpking
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ip2region ruby version.
14
+ email:
15
+ - atpking@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/ip2region.rb
29
+ - lib/ip2region/version.rb
30
+ - lib/ip2region/xdb_searcher.rb
31
+ - sig/ip2region.rbs
32
+ homepage:
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ homepage_uri: https://github.com/jicheng1014/ruby-ip2region
37
+ source_code_uri: https://github.com/jicheng1014/ruby-ip2region
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.4.6
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: ip2region ruby version
57
+ test_files: []