/
ceoip.cc
212 lines (172 loc) · 5.41 KB
/
ceoip.cc
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
204
205
206
207
208
209
210
211
212
// Copyright 2010 Gustaf Sjöberg <gs@distrop.com>
#include <GeoIP.h>
#include <GeoIPCity.h>
#include <v8.h>
#include <node.h>
#include <node_events.h>
#include <assert.h>
using namespace v8;
using namespace node;
static Persistent<String> connected_symbol;
static Persistent<String> closed_symbol;
static Persistent<String> error_symbol;
static Persistent<String> result_symbol;
class Connection : public EventEmitter {
public:
static void
Initialize(v8::Handle<v8::Object> target) {
Local<FunctionTemplate> t = FunctionTemplate::New(Connection::New);
t->Inherit(EventEmitter::constructor_template);
t->InstanceTemplate()->SetInternalFieldCount(1);
closed_symbol = NODE_PSYMBOL("closed");
connected_symbol = NODE_PSYMBOL("connected");
error_symbol = NODE_PSYMBOL("error");
result_symbol = NODE_PSYMBOL("result");
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
NODE_SET_PROTOTYPE_METHOD(t, "query", Query);
NODE_DEFINE_CONSTANT(target, GEOIP_STANDARD);
NODE_DEFINE_CONSTANT(target, GEOIP_MEMORY_CACHE);
NODE_DEFINE_CONSTANT(target, GEOIP_CHECK_CACHE);
NODE_DEFINE_CONSTANT(target, GEOIP_INDEX_CACHE);
NODE_DEFINE_CONSTANT(target, GEOIP_MMAP_CACHE);
target->Set(String::NewSymbol("Connection"), t->GetFunction());
}
void Connect(const char *dbpath, int db_opts_bitmask) {
HandleScope scope;
if(db_opts_bitmask == 0){
db_opts_bitmask = 0 | GEOIP_STANDARD;
}
gi = GeoIP_open(dbpath, db_opts_bitmask);
Emit((gi ? connected_symbol : error_symbol), 0, NULL);
}
void Close() {
HandleScope scope;
if (gi != NULL) {
GeoIP_delete(gi);
gi = NULL;
}
Emit(closed_symbol, 0, NULL);
}
void Query(const char *query) {
HandleScope scope;
assert(gi);
GeoIPRecord *record;
record = GeoIP_record_by_addr(gi, query);
if (record) {
Local<Value> result = BuildResult(record);
GeoIPRecord_delete(record);
Emit(result_symbol, 1, &result);
} else {
Emit(result_symbol, 0, NULL);
}
}
protected:
static Handle<Value> New(const Arguments& args) {
HandleScope scope;
Connection *connection = new Connection();
connection->Wrap(args.This());
return args.This();
}
static Handle<Value> Connect(const Arguments &args) {
HandleScope scope;
if (args.Length() < 1 || !args[0]->IsString()) {
return ThrowException(
Exception::TypeError(
String::New("Required argument: path to database.")));
}
if (args.Length() == 2 && !args[1]->IsNumber()){
return ThrowException(
Exception::TypeError(
String::New("Second argument must be a bitmask")));
}
String::Utf8Value dbpath(args[0]->ToString());
Connection *connection = ObjectWrap::Unwrap<Connection>(args.This());
connection->Connect(*dbpath, args[1]->ToUint32()->Value());
return Undefined();
}
static Handle<Value> Close(const Arguments &args) {
HandleScope scope;
Connection *connection = ObjectWrap::Unwrap<Connection>(args.This());
connection->Close();
return Undefined();
}
static Handle<Value> Query(const Arguments &args) {
HandleScope scope;
if (args.Length() < 1 || !args[0]->IsString()) {
return ThrowException(
Exception::TypeError(
String::New("Required argument: ip address.")));
}
String::Utf8Value query(args[0]->ToString());
Connection *connection = ObjectWrap::Unwrap<Connection>(args.This());
connection->Query(*query);
return Undefined();
}
private:
Local<Value>BuildResult(GeoIPRecord *record) {
HandleScope scope;
Local<Array> result = Array::New();
if (record->longitude != NULL) {
result->Set(
String::New("longitude"),
Number::New(record->longitude)
);
}
if (record->latitude != NULL) {
result->Set(
String::New("latitude"),
Number::New(record->latitude)
);
}
if (record->country_code != NULL) {
result->Set(
String::New("country_code"),
String::New(record->country_code)
);
}
if (record->continent_code != NULL) {
result->Set(
String::New("continent_code"),
String::New(record->continent_code)
);
}
if (record->metro_code != NULL) {
result->Set(
String::New("metro_code"),
Number::New(record->metro_code)
);
}
if (record->country_name != NULL) {
result->Set(
String::New("country"),
String::New(record->country_name)
);
}
if (record->city != NULL) {
result->Set(
String::New("city"),
String::New(record->city)
);
}
if (record->area_code != NULL) {
result->Set(
String::New("area_code"),
Number::New(record->area_code)
);
}
if (record->region != NULL) {
result->Set(
String::New("region"),
String::New(record->region)
);
}
return scope.Close(result);
}
GeoIP *gi;
};
extern "C" void
init(Handle<Object> target) {
HandleScope scope;
Connection::Initialize(target);
}