-
Notifications
You must be signed in to change notification settings - Fork 1
/
vastmaxmind.cc
155 lines (119 loc) · 4.34 KB
/
vastmaxmind.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
#include <node.h>
#include <node_buffer.h>
#include <string.h>
#include "vastmaxmind.h"
using namespace v8;
struct iprequest {
v8::Persistent<v8::Function> cb;
char *addr;
uint ipnum;
VastMaxmind *vmm;
GeoIPRecord *gir;
};
int VastMaxmind::translateIp(char *ipaddr) {
char *str = new char[16];
char *tok;
int *parts = new int[4];
int ipnum = 0;
strncpy(str, ipaddr, 15);
tok = strtok(str, "."); // should be 4 groups of digits
for(int i=0; i < 4; i++) {
if (tok != NULL) {
parts[i] = atoi(tok);
tok = strtok(NULL, ".");
}
}
ipnum = 16777216*parts[0] + 65536*parts[1] + 256*parts[2] + parts[3];
delete [] parts;
delete [] str;
return ipnum;
}
void VastMaxmind::locationWorker(uv_work_t *req) {
iprequest *ipreq = (iprequest *)(req->data);
VastMaxmind *vmm = ipreq->vmm;
ipreq->ipnum = VastMaxmind::translateIp(ipreq->addr);
if (ipreq->ipnum > 0 && vmm && vmm->gi) {
ipreq->gir = GeoIP_record_by_ipnum(vmm->gi, ipreq->ipnum);
}
else {
ThrowException(Exception::Error(String::New("Maxmind db not opened.")));
}
}
void VastMaxmind::locationAfter(uv_work_t *req, int status) {
HandleScope scope;
Handle<Object> ret = Object::New();
iprequest *ipreq = (iprequest *)(req->data);
char *na = (char *)"N/A";
GeoIPRecord *gir = ipreq->gir;
// return the searched ip
ret->Set( NODE_PSYMBOL("ip"), String::New(ipreq->addr) );
ret->Set( NODE_PSYMBOL("ipnum"), Number::New(ipreq->ipnum) );
if (gir) {
ret->Set( NODE_PSYMBOL("country"), String::New(gir->country_code ? gir->country_code : na) );
ret->Set( NODE_PSYMBOL("state"), String::New(gir->region ? gir->region : na) );
ret->Set( NODE_PSYMBOL("city"), String::New(gir->city ? gir->city : na) );
ret->Set( NODE_PSYMBOL("zip"), String::New(gir->postal_code ? gir->postal_code : na) );
ret->Set( NODE_PSYMBOL("latitude"), Number::New( gir->latitude) );
ret->Set( NODE_PSYMBOL("longitude"), Number::New( gir->longitude) );
ret->Set( NODE_PSYMBOL("areacode"), Number::New( gir->area_code) );
GeoIPRecord_delete(gir);
}
Local<Value> argv[1];
argv[0] = Local<Value>::Local(ret->ToObject());
TryCatch try_catch;
ipreq->cb->Call(Context::GetCurrent()->Global(), 1, argv);
if (try_catch.HasCaught())
node::FatalException(try_catch);
ipreq->cb.Dispose();
delete ipreq->addr;
delete ipreq;
delete req;
}
// Constuctor
VastMaxmind::VastMaxmind(char *_db) : db(_db) {
this->gi = GeoIP_open(this->db, GEOIP_MEMORY_CACHE); //GEOIP_MEMORY_CACHE | GEOIP_INDEX_CACHE);
if (this->gi == NULL) {
ThrowException(Exception::Error(String::New("Could not open maxmind db.")));
}
}
// Destructor
VastMaxmind::~VastMaxmind() {
GeoIP_delete(this->gi);
GeoIP_cleanup();
}
void VastMaxmind::Init(Handle<Object> target) {
// Prepare constructor template
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->SetClassName(String::NewSymbol("VastMaxmind"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
tpl->PrototypeTemplate()->Set(String::NewSymbol("location"),
FunctionTemplate::New(getLocation)->GetFunction());
v8::Persistent<Function> constructor = v8::Persistent<Function>::New(tpl->GetFunction());
target->Set(String::NewSymbol("VastMaxmind"), constructor);
}
Handle<Value> VastMaxmind::New(const Arguments& args) {
HandleScope scope;
String::Utf8Value db(args[0]->ToString());
VastMaxmind *vmm = new VastMaxmind(*db);
vmm->Wrap(args.This());
return args.This();
}
Handle<Value> VastMaxmind::getLocation(const Arguments& args) {
HandleScope scope;
String::Utf8Value ipaddr(args[0]);
Local<Function> cb = Local<Function>::Cast(args[1]);
VastMaxmind *vmm = ObjectWrap::Unwrap<VastMaxmind>(args.Holder());
iprequest *request = new iprequest;
request->cb = Persistent<Function>::New(cb);
request->vmm = vmm;
char *strNew = new char[ strlen(*ipaddr) + 1 ];
strcpy(strNew, *ipaddr);
request->addr = strNew;
//eio_custom(EIO_location, EIO_PRI_DEFAULT, EIO_locationAfter, req);
uv_work_t* req = new uv_work_t();
req->data = request;
uv_queue_work(uv_default_loop(), req, VastMaxmind::locationWorker, (uv_after_work_cb)VastMaxmind::locationAfter);
scope.Close(Undefined());
return Undefined(); //Handle<String>(args[0]->ToString());
}