-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQQWry.cc
233 lines (195 loc) · 5.61 KB
/
QQWry.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "IPInfo.h"
#define USING_STATIC_LIBICONV
#include "iconv.h"
#pragma comment(lib, "libiconvStatic.lib")
#ifndef ICONV_CONST
# define ICONV_CONST const
#endif
////////////////////////////////Essentials Above//////////////////////////////////////////
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
using namespace node;
using namespace v8;
iconv_t conv_;
namespace{
class QQWry: ObjectWrap
{
private:
static Persistent<FunctionTemplate> constructor_template;
CIPInfo m_IPInfo;
public:
static void Init(Handle<Object> target);
QQWry(Handle<Object> wrapper, CIPInfo qqwry): m_IPInfo(qqwry)
{
Wrap(wrapper);
}
~QQWry()
{
m_IPInfo.Close();
}
static Handle<Value> New(const Arguments& args);
static Handle<Value> QueryIP(const Arguments& args);
Handle<Value> QueryIP(char *ipAddress);
};
Persistent<FunctionTemplate> QQWry::constructor_template;
Handle<Value> QQWry::New(const Arguments& args)
{
HandleScope scope;
String::AsciiValue path(args[0]->ToString());
CIPInfo qqwry;
if (FALSE == qqwry.OpenA(*path)) {
return ThrowException(Exception::Error(String::New(
"QQWry: File not Found.")));
}
new QQWry(args.This(), qqwry);
return args.This();
}
void FreeMemory(char *data, void *hint) {
free(data);
V8::AdjustAmountOfExternalAllocatedMemory(-(sizeof(Buffer) + (size_t) hint));
}
int grow(char** output, size_t* outlen, char** outbuf, size_t* outbufsz) {
size_t newlen;
char *newptr;
newlen = *outlen ? (*outlen * 2) : 16;
if ((newptr = (char*) realloc(*output, newlen))) {
*outbufsz = newlen - *outlen;
*outbuf = newptr + (*outbuf - *output);
*outlen = newlen;
*output = newptr;
return 1;
}
return 0;
}
/**
* This function will clobber `output` and `outlen` on both success and error.
*/
int convert(iconv_t iv, char* input, size_t inlen, char** output, size_t* outlen) {
char* inbuf;
char* outbuf;
size_t outbufsz;
size_t inbufsz;
size_t rv;
inbufsz = inlen;
inbuf = input;
*outlen = outbufsz = 0;
*output = outbuf = 0;
// reset to initial state
iconv(iv, 0, 0, 0, 0);
// convert input
do {
if (grow(output, outlen, &outbuf, &outbufsz)) {
rv = iconv(iv, (ICONV_CONST char **) &inbuf, &inbufsz, &outbuf, &outbufsz);
}
else {
goto error;
}
}
while (rv == (size_t) -1 && errno == E2BIG);
if (rv == (size_t) -1) {
goto error;
}
// write out shift sequence
rv = iconv(iv, 0, 0, &outbuf, &outbufsz);
if (rv == (size_t) -1) {
if (errno != E2BIG) {
goto error;
}
if (!grow(output, outlen, &outbuf, &outbufsz)) {
goto error;
}
if (iconv(iv, 0, 0, &outbuf, &outbufsz) == (size_t) -1) {
goto error;
}
}
// store length
*outlen = outbuf - *output;
// release unused trailing memory; this can't conceivably fail
// because newlen <= oldlen but let's take the safe route anyway
//
// realloc() may free the memory and return NULL if *outlen == 0
// but that's not an error, the caller should (and does) handle it
// graciously
//
if ((outbuf = (char*) realloc(*output, *outlen)) || *outlen == 0) {
*output = outbuf;
}
return 1;
error:
free(*output);
*output = 0;
*outlen = 0;
return 0;
}
Handle<Value> QQWry::QueryIP(char* ipAddress){
char szLocation[512]={0};
//char *szLocation = new char[512];
//memset(szLocation, 0, sizeof(szLocation));
if(m_IPInfo.QueryIPA(ipAddress, szLocation, sizeof(szLocation))){
//printf("%s\n", szLocation);
/*int len = strlen(szLocation);
for(int i=0;i<len;i++){
printf("%2x ", (byte)szLocation[i]);
}
printf("\n%d\n", len);
V8::AdjustAmountOfExternalAllocatedMemory(sizeof(Buffer) + len);
return Buffer::New(szLocation, len, FreeMemory, (void *) len)->handle_;
*/
char* p = strstr(szLocation, "CZ88.NET");
if (p != NULL) p[-1] = '\0';
int inlen = strlen(szLocation);
size_t outlen;
char *output;
outlen = 0;
output = 0;
if (convert(conv_, szLocation, inlen, &output, &outlen)) {
V8::AdjustAmountOfExternalAllocatedMemory(sizeof(Buffer) + outlen);
return Buffer::New(output, outlen, FreeMemory, (void *) outlen)->handle_;
}
else if (errno == EINVAL) {
return ThrowException(ErrnoException(EINVAL, "iconv", "Incomplete character sequence."));
}
else if (errno == EILSEQ) {
return ThrowException(ErrnoException(errno, "iconv", "Illegal character sequence."));
}
else if (errno == ENOMEM) {
V8::LowMemoryNotification();
return ThrowException(ErrnoException(errno, "iconv", "Out of memory."));
}
else {
return ThrowException(ErrnoException(errno, "iconv"));
}
}
return Undefined();
}
Handle<Value> QQWry::QueryIP(const Arguments& args)
{
HandleScope scope;
QQWry* self = ObjectWrap::Unwrap<QQWry>(args.This());
Local<Value> arg = args[0];
if (arg->IsString()) {
String::Utf8Value string(arg->ToString());
return self->QueryIP(*string);
}
return Undefined();
}
void QQWry::Init(Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(QQWry::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("QQWry"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "QueryIP", QueryIP);
target->Set(String::NewSymbol("QQWry"),
constructor_template->GetFunction());
//printf("%s\n", String::AsciiValue(constructor_template->GetFunction()->ToString()));
}
static void init (Handle<Object> target)
{
QQWry::Init(target);
conv_ = iconv_open("UTF-8", "GBK");
}
NODE_MODULE(QQWry, init);
}