Skip to content

Commit 234cf8b

Browse files
committed
Improve ip_database code style
1 parent 03af719 commit 234cf8b

2 files changed

Lines changed: 83 additions & 85 deletions

File tree

proxy/include/proxy/ip_database.hpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@
55
// Email: jack.wgm at gmail dot com
66
//
77

8-
#ifndef INCLUDE__2019_10_18__IPIP_HPP
9-
#define INCLUDE__2019_10_18__IPIP_HPP
8+
#ifndef INCLUDE__2019_10_18__IP_DATABASE_HPP
9+
#define INCLUDE__2019_10_18__IP_DATABASE_HPP
1010

1111
#include <boost/filesystem.hpp>
1212
#include <boost/asio/ip/address.hpp>
1313

1414
#include <vector>
1515
#include <map>
1616
#include <string>
17-
#include <exception>
1817

1918

2019
namespace proxy {
2120
namespace net = boost::asio;
2221

22+
struct ip_result
23+
{
24+
std::vector<std::string> regions;
25+
std::string isp;
26+
};
27+
2328
class ip_database
2429
{
2530
public:
@@ -31,8 +36,7 @@ namespace proxy {
3136
// 返回值: <地址信息, 运营商>, 其中 std::vector<std::string> 为地址
3237
// 信息, 按地区从大到小排列.
3338
// 如: ["中国", "北京", "北京", "联通"], "联通" 为运营商.
34-
virtual std::tuple<std::vector<std::string>, std::string>
35-
lookup(net::ip::address ip) = 0;
39+
virtual ip_result lookup(net::ip::address ip) = 0;
3640
};
3741

3842
// IPIP数据文件格式: datx.
@@ -47,11 +51,10 @@ namespace proxy {
4751
virtual ~ip_datx() = default;
4852

4953
// 打开ipip数据文件.
50-
virtual bool load(const std::string& filename) override;
54+
bool load(const std::string& filename) override;
5155

5256
// 根据一个IP查询对应地址信息.
53-
virtual std::tuple<std::vector<std::string>, std::string>
54-
lookup(net::ip::address ip) override;
57+
ip_result lookup(net::ip::address ip) override;
5558

5659
private:
5760
std::vector<unsigned char> m_data;
@@ -72,11 +75,10 @@ namespace proxy {
7275
virtual ~ip_ipdb() = default;
7376

7477
// 打开ipip数据文件.
75-
virtual bool load(const std::string& filename) override;
78+
bool load(const std::string& filename) override;
7679

7780
// 根据一个IP查询对应地址信息.
78-
virtual std::tuple<std::vector<std::string>, std::string>
79-
lookup(net::ip::address ip) override;
81+
ip_result lookup(net::ip::address ip) override;
8082

8183
private:
8284
void parse_meta(const std::string& json);
@@ -86,21 +88,21 @@ namespace proxy {
8688
int guess_isp_index();
8789

8890
private:
89-
bool loaded_ = false;
90-
std::vector<uint8_t> data_;
91+
bool m_loaded = false;
92+
std::vector<uint8_t> m_data;
9193

9294
// 元数据信息
93-
int nodeCount_ = 0;
94-
int totalSize_ = 0;
95-
uint16_t ipVersion_ = 0;
96-
std::vector<std::string> fieldNames_;
97-
std::map<std::string, int> languages_;
95+
int m_node_count = 0;
96+
int m_total_size = 0;
97+
uint16_t m_ip_version = 0;
98+
std::vector<std::string> m_field_names;
99+
std::map<std::string, int> m_languages;
98100

99101
// 运行时属性
100-
int v4offset_ = 0;
101-
int ispIdx_ = -1;
102-
std::string currentLang_;
102+
int m_v4offset = 0;
103+
int m_isp_idx = -1;
104+
std::string m_current_lang;
103105
};
104106
}
105107

106-
#endif // INCLUDE__2019_10_18__IPIP_HPP
108+
#endif // INCLUDE__2019_10_18__IP_DATABASE_HPP

proxy/src/ip_database.cpp

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ namespace proxy {
7575
return true;
7676
}
7777

78-
std::tuple<std::vector<std::string>, std::string>
79-
ip_datx::lookup(boost::asio::ip::address ip)
78+
ip_result ip_datx::lookup(boost::asio::ip::address ip)
8079
{
81-
std::vector<std::string> ret;
80+
ip_result ret;
8281

8382
if (m_data.empty() || ip.is_v6())
84-
return {};
83+
return ret;
8584

8685
std::string result;
8786

@@ -114,20 +113,18 @@ namespace proxy {
114113
index_length);
115114

116115
// segment result into peaces.
117-
boost::split(ret, result, boost::is_space());
116+
boost::split(ret.regions, result, boost::is_space());
118117

119-
std::string isp;
120-
121-
if (ret.size() == 5)
118+
if (ret.regions.size() == 5)
122119
{
123-
isp = *ret.rbegin();
124-
ret.pop_back();
120+
ret.isp = *ret.regions.rbegin();
121+
ret.regions.pop_back();
125122
}
126123

127-
if (ret.back().empty())
128-
ret.pop_back();
124+
if (ret.regions.back().empty())
125+
ret.regions.pop_back();
129126

130-
return std::make_tuple(ret, isp);
127+
return ret;
131128
}
132129

133130

@@ -157,81 +154,80 @@ namespace proxy {
157154
if (!fs) throw ErrFileOpen;
158155

159156
// 1. 读取 4 字节元数据长度
160-
uint32_t metaLenBE = 0;
161-
fs.read(reinterpret_cast<char*>(&metaLenBE), 4);
162-
uint32_t metaLen = be32_to_host(metaLenBE);
157+
uint32_t meta_len_be = 0;
158+
fs.read(reinterpret_cast<char*>(&meta_len_be), 4);
159+
uint32_t meta_len = be32_to_host(meta_len_be);
163160

164161
// 2. 读取并解析 JSON 元数据
165-
std::string metaJson(metaLen, '\0');
166-
fs.read(&metaJson[0], metaLen);
162+
std::string meta_json(meta_len, '\0');
163+
fs.read(&meta_json[0], meta_len);
167164
if (!fs) throw ErrFileSize;
168-
parse_meta(metaJson);
165+
parse_meta(meta_json);
169166

170167
// 3. 校验并读取数据库主体
171168
fs.seekg(0, std::ios::end);
172-
if ((int64_t)fs.tellg() != (int64_t)4 + metaLen + totalSize_) throw ErrFileSize;
169+
if ((int64_t)fs.tellg() != (int64_t)4 + meta_len + m_total_size) throw ErrFileSize;
173170

174-
data_.resize((size_t)totalSize_);
175-
fs.seekg(4 + metaLen, std::ios::beg);
176-
fs.read(reinterpret_cast<char*>(data_.data()), totalSize_);
171+
m_data.resize((size_t)m_total_size);
172+
fs.seekg(4 + meta_len, std::ios::beg);
173+
fs.read(reinterpret_cast<char*>(m_data.data()), m_total_size);
177174

178175
// 4. 预计算 IPv4 偏移 (Trie 树 96 位深度处)
179-
v4offset_ = 0;
180-
for (int i = 0; i < 96 && v4offset_ < nodeCount_; ++i) {
181-
v4offset_ = read_node(v4offset_, (i >= 80) ? 1 : 0);
176+
m_v4offset = 0;
177+
for (int i = 0; i < 96 && m_v4offset < m_node_count; ++i) {
178+
m_v4offset = read_node(m_v4offset, (i >= 80) ? 1 : 0);
182179
}
183180

184181
// 5. 自动配置默认语言和 ISP 字段索引
185-
currentLang_ = "CN";
186-
if (languages_.find(currentLang_) == languages_.end() && !languages_.empty()) {
187-
currentLang_ = languages_.begin()->first;
182+
m_current_lang = "CN";
183+
if (m_languages.find(m_current_lang) == m_languages.end() && !m_languages.empty()) {
184+
m_current_lang = m_languages.begin()->first;
188185
}
189-
ispIdx_ = guess_isp_index();
186+
m_isp_idx = guess_isp_index();
190187

191-
loaded_ = true;
188+
m_loaded = true;
192189

193190
} catch (...) {
194-
loaded_ = false;
191+
m_loaded = false;
195192
return false;
196193
}
197194

198195
return true;
199196
}
200197

201-
std::tuple<std::vector<std::string>, std::string>
202-
ip_ipdb::lookup(net::ip::address ip)
198+
ip_result ip_ipdb::lookup(net::ip::address ip)
203199
{
204-
if (!loaded_) throw Error("ipdb: not loaded");
200+
if (!m_loaded) throw Error("ipdb: not loaded");
205201

206202
// 1. 根据 IP 类型执行树搜索
207203
int node = 0;
208204
if (ip.is_v4()) {
209-
if (!(ipVersion_ & 0x01)) throw Error("ipdb: no ipv4 support");
205+
if (!(m_ip_version & 0x01)) throw Error("ipdb: no ipv4 support");
210206
auto bytes = ip.to_v4().to_bytes();
211-
node = search_tree(bytes.data(), 32, v4offset_);
207+
node = search_tree(bytes.data(), 32, m_v4offset);
212208
} else {
213-
if (!(ipVersion_ & 0x02)) throw Error("ipdb: no ipv6 support");
209+
if (!(m_ip_version & 0x02)) throw Error("ipdb: no ipv6 support");
214210
auto bytes = ip.to_v6().to_bytes();
215211
node = search_tree(bytes.data(), 128, 0);
216212
}
217213

218214
// 2. 解析数据区内容
219-
std::string rawStr = resolve_content(node);
220-
std::vector<std::string> parts = split_tab(rawStr);
215+
std::string raw_str = resolve_content(node);
216+
std::vector<std::string> parts = split_tab(raw_str);
221217

222218
// 3. 根据语言提取字段
223-
int langBase = languages_[currentLang_];
219+
int lang_base = m_languages[m_current_lang];
224220
std::vector<std::string> fields;
225-
for (size_t i = 0; i < fieldNames_.size(); ++i) {
226-
size_t pIdx = (size_t)langBase + i;
227-
fields.push_back(pIdx < parts.size() ? parts[pIdx] : "");
221+
for (size_t i = 0; i < m_field_names.size(); ++i) {
222+
size_t p_idx = (size_t)lang_base + i;
223+
fields.push_back(p_idx < parts.size() ? parts[p_idx] : "");
228224
}
229225

230226
// 4. 整理返回格式
231227
std::vector<std::string> region;
232228

233229
for (int i = 0; i < (int)fields.size(); ++i) {
234-
if (i != ispIdx_ && !fields[i].empty()) region.push_back(fields[i]);
230+
if (i != m_isp_idx && !fields[i].empty()) region.push_back(fields[i]);
235231
}
236232

237233
return {region, ""};
@@ -241,56 +237,56 @@ namespace proxy {
241237
{
242238
auto v = boost::json::parse(json);
243239
auto const& obj = v.as_object();
244-
nodeCount_ = (int)obj.at("node_count").as_int64();
245-
totalSize_ = (int)obj.at("total_size").as_int64();
246-
ipVersion_ = (uint16_t)obj.at("ip_version").as_int64();
240+
m_node_count = (int)obj.at("node_count").as_int64();
241+
m_total_size = (int)obj.at("total_size").as_int64();
242+
m_ip_version = (uint16_t)obj.at("ip_version").as_int64();
247243

248-
fieldNames_.clear();
244+
m_field_names.clear();
249245
for (auto const& f : obj.at("fields").as_array())
250-
fieldNames_.emplace_back(f.as_string().c_str());
246+
m_field_names.emplace_back(f.as_string().c_str());
251247

252-
languages_.clear();
248+
m_languages.clear();
253249
for (auto const& kv : obj.at("languages").as_object())
254-
languages_[std::string(kv.key())] = (int)kv.value().as_int64();
250+
m_languages[std::string(kv.key())] = (int)kv.value().as_int64();
255251
}
256252

257253
int ip_ipdb::read_node(int node, int bit) const
258254
{
259255
size_t offset = (size_t)node * 8 + (size_t)bit * 4;
260-
if (offset + 4 > data_.size()) throw ErrDatabase;
256+
if (offset + 4 > m_data.size()) throw ErrDatabase;
261257
uint32_t val;
262-
std::memcpy(&val, &data_[offset], 4);
258+
std::memcpy(&val, &m_data[offset], 4);
263259
return (int)be32_to_host(val);
264260
}
265261

266262
int ip_ipdb::search_tree(const uint8_t* ip, int bits, int startNode) const
267263
{
268264
int node = startNode;
269265
for (int i = 0; i < bits; ++i) {
270-
if (node >= nodeCount_) break;
266+
if (node >= m_node_count) break;
271267
int bit = (ip[i >> 3] >> (7 - (i & 7))) & 1;
272268
node = read_node(node, bit);
273269
}
274-
if (node >= nodeCount_) return node;
270+
if (node >= m_node_count) return node;
275271
throw ErrDataEmpty;
276272
}
277273

278274
std::string ip_ipdb::resolve_content(int node) const
279275
{
280276
// 数据区偏移 = 节点位置 - 节点总数 + (节点总数 * 8字节)
281-
size_t pos = (size_t)node - nodeCount_ + (size_t)nodeCount_ * 8;
282-
if (pos + 2 > data_.size()) throw ErrDatabase;
277+
size_t pos = (size_t)node - m_node_count + (size_t)m_node_count * 8;
278+
if (pos + 2 > m_data.size()) throw ErrDatabase;
283279

284-
size_t len = ((size_t)data_[pos] << 8) | (size_t)data_[pos + 1];
285-
if (pos + 2 + len > data_.size()) throw ErrDatabase;
280+
size_t len = ((size_t)m_data[pos] << 8) | (size_t)m_data[pos + 1];
281+
if (pos + 2 + len > m_data.size()) throw ErrDatabase;
286282

287-
return std::string((const char*)data_.data() + pos + 2, len);
283+
return std::string((const char*)m_data.data() + pos + 2, len);
288284
}
289285

290286
int ip_ipdb::guess_isp_index()
291287
{
292-
for (size_t i = 0; i < fieldNames_.size(); ++i) {
293-
std::string n = fieldNames_[i];
288+
for (size_t i = 0; i < m_field_names.size(); ++i) {
289+
std::string n = m_field_names[i];
294290
std::transform(n.begin(), n.end(), n.begin(), ::tolower);
295291
if (n.find("isp") != std::string::npos || n.find("operator") != std::string::npos)
296292
return (int)i;

0 commit comments

Comments
 (0)