forked from kuno/GeoIP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
203 lines (173 loc) · 5.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var fs = require('fs'),
path = require('path'),
buf2long = require('./lib/utils.js').buf2long,
matchFingerPrint = require('./lib/utils.js').matchFingerPrint,
DATA = require('./include/models.js').DATA,
CONST = Object.freeze(require('./include/constants.js'));
/******************************************************************************
* Inner Functions
*****************************************************************************/
function _setup_segments(data) {
var seg, delim, i, j,
buf = data.buffer, offset = buf.length - 3;
// Set data type and segments
for (i = 0; i < CONST.STRUCTURE_INFO_MAX_SIZE; i++) {
delim = buf2long(buf.slice(offset, offset + 3));
offset += 3;
if (delim === CONST.DELIMETER_NUMBER) {
data.db_type = parseInt(buf[offset], 10);
offset++;
if (data.db_type === CONST.REGION_EDITION_REV0) {
data.db_segments = CONST.STATE_BEGIN_REV0;
} else if (data.db_type === CONST.REGION_EDITION_REV1) {
data.db_segments = CONST.STATE_BEGIN_REV1;
} else if (data.db_type === CONST.CITY_EDITION_REV0 ||
data.db_type === CONST.CITY_EDITION_REV1 ||
data.db_type === CONST.ORG_EDITION ||
data.db_type === CONST.DOMAIN_EDITION ||
data.db_type === CONST.ISP_EDITION ||
data.db_type === CONST.LOCATIONA_EDITION ||
data.db_type === CONST.ACCURACYRADIUS_EDITION ||
data.db_type === CONST.ASNUM_EDITION) {
data.db_segments = 0;
seg = buf.slice(offset, offset + CONST.SEGMENT_RECORD_LENGTH);
for (j = 0; j < CONST.SEGMENT_RECORD_LENGTH; j++) {
data.db_segments += (parseInt(seg[j], 10) << (j * 8));
}
} else if (data.db_type === CONST.PROXY_EDITION ||
data.db_type === CONST.NETSPEED_EDITION) {
data.db_segments = CONST.COUNTRY_BEGIN;
}
break;
} else {
offset -= 4;
}
// Set default data type to country edition
if (data.db_type === undefined) {
if (matchFingerPrint(data)) {
data.db_type = CONST.COUNTRY_EDITION;
data.db_segments = CONST.COUNTRY_BEGIN;
}
}
}
// Set record length
if (data.db_type === CONST.ORG_EDITION ||
data.db_type === CONST.DOMAIN_EDITION ||
data.db_type === CONST.ISP_EDITION) {
data.record_length = CONST.ORG_RECORD_LENGTH;
} else {
data.record_length = CONST.STANDARD_RECORD_LENGTH;
}
return data;
}
/******************************************************************************
* Exprots Functions
*****************************************************************************/
exports.open = function(file) {
var stats, bytesRead;
var data = new DATA();
data.file_descriptor = fs.openSync(file, 'r');
stats = fs.fstatSync(data.file_descriptor);
data.buffer = new Buffer(stats.size);
bytesRead = fs.readSync(data.file_descriptor, data.buffer, 0, stats.size, 0);
if (bytesRead >= 0) {
return _setup_segments(data);
} else {
return false;
}
};
exports.check = function(data) {
var code, type;
code = data.db_type;
switch(code)
{
case CONST.COUNTRY_EDITION:
type = 'country';
break;
case CONST.CITY_EDITION_REV0:
type = 'city';
break;
case CONST.CITY_EDITION_REV1:
type = 'city';
break;
case CONST.REGION_EDITION_REV0:
type = 'region';
break;
case CONST.REGION_EDITION_REV1:
type = 'region';
break;
case CONST.ORG_EDITION:
type = 'org';
break;
case CONST.ASNUM_EDITION:
type = 'asnumber';
break;
case CONST.NETSPEED_EDITION:
type = 'netspeed';
break;
default:
type = null;
break;
}
return type;
};
exports.filter = function(file, callback) {
var error, code, type, data = new DATA();
fs.open(file, 'r', function(err, fd) {
if (err) {throw err;}
data.file_descriptor = fd;
fs.fstat(data.file_descriptor, function(err, stats) {
if (err) {throw err;}
data.buffer = new Buffer(stats.size);
fs.read(data.file_descriptor, data.buffer, 0, stats.size, 0, function(err, bytesRead) {
if (err) {throw err;}
data = _setup_segments(data);
code = data.db_type;
switch(code)
{
case CONST.COUNTRY_EDITION:
type = 'country';
break;
case CONST.CITY_EDITION_REV0:
type = 'city';
break;
case CONST.CITY_EDITION_REV1:
type = 'city';
break;
case CONST.REGION_EDITION_REV0:
type = 'region';
break;
case CONST.REGION_EDITION_REV1:
type = 'region';
break;
case CONST.ORG_EDITION:
type = 'org';
break;
case CONST.ASNUM_EDITION:
type = 'asnumber';
break;
case CONST.NETSPEED_EDITION:
type = 'netspeed';
break;
default:
error = new Error('Unkown data type');
break;
}
callback(error, type, data);
});
});
});
};
exports.close = function(data) {
var keys;
fs.closeSync(data.file_descriptor);
keys = Object.keys(data);
keys.forEach(function(k) {
delete data[k];
});
};
exports.NetSpeed = require('./lib/netspeed.js');
exports.Country = require('./lib/country.js');
exports.Region = require('./lib/region.js');
exports.City = require('./lib/city.js');
exports.Org = require('./lib/org.js');