-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
373 lines (317 loc) · 11 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const axios = require('axios');
const stream = require('stream');
const request = require('request');
const BPromise = require("bluebird");
const { get, sample } = require('lodash');
const moment = require('moment-timezone');
moment.tz.setDefault("Asia/Ho_Chi_Minh");
class ProxyServiceError extends Error {
constructor(message) {
super(message);
// Ensure the name of this error is the same as the class name
this.name = this.constructor.name;
// This clips the constructor invocation from the stack trace.
// It's not absolutely essential, but it does make the stack trace a little nicer.
// @see Node.js reference (bottom)
Error.captureStackTrace(this, this.constructor);
}
}
class TinSoftProxy {
constructor(options = {}) {
this.Stream = new stream.Stream();
this.user_api_key = get(options, 'user_api_key', '');
this.api_key = get(options, 'api_key', '');
this.apiEndpoint = 'http://proxy.tinsoftsv.com';
this.requestTimeout = 20000;
this.service = axios.create({
baseURL: this.apiEndpoint,
timeout: this.requestTimeout,
});
this.proxy = '';
this.waitProxyLiveTimeout = 30000;
// Request intercepter
this.service.interceptors.request.use(
(config) => {
const newConfig = config;
if (Object.prototype.hasOwnProperty.call(newConfig, 'params')) {
if (!newConfig.params.key) newConfig.params.key = this.user_api_key || this.api_key;
} else {
newConfig.params = {
key: this.user_api_key || this.api_key,
};
}
return newConfig;
},
(error) => {
Promise.reject(error);
},
);
// response pre-processing
this.service.interceptors.response.use(
(response) => {
// const data = get(response, 'data', {});
const rpSuccess = get(response, 'data.success', false);
const rpMessage = get(response, 'data.description', 'Không có thông tin từ server.');
if (rpSuccess === false) {
return Promise.reject(new ProxyServiceError(`${rpMessage}`));
}
return get(response, 'data.data', get(response, 'data', {}));
},
(error) => Promise.reject(new ProxyServiceError(error)),
);
}
isAdmin() {
if (!this.user_api_key) throw new Error('Pls input user_api_key');
}
rp_custom(options = {}) {
return new Promise((resolve, reject) => {
const proxyOption = (this.proxy !== '') ? { proxy: `http://${this.proxy}` } : {};
const requestOptions = {
timeout: this.requestTimeout,
...options,
...proxyOption
};
request(requestOptions, (error, response, body) => {
const requestURL = get(requestOptions, 'url', '');
const statusCode = get(response, 'statusCode', '');
if (!error && statusCode == 200) {
resolve(response);
} else if (error && !statusCode) {
reject(error);
} else {
reject(new Error(`Request failed with statusCode: ${statusCode}, requestURL: ${requestURL}`));
}
});
});
}
async ipconfig() {
const response = await this.rp_custom({
url: 'http://ipconfig.top/api/ip',
method: 'get',
json: true,
time : true,
timeout: 2000,
});
const elapsedTime = get(response, 'elapsedTime', -1);
const currentIP = get(response, 'body.data.ip');
const ipInfo = get(response, 'body.data', {});
ipInfo['speed'] = elapsedTime;
ipInfo['service'] = 'ipconfig.top';
this.Stream.emit('log', `Current IP: ${currentIP}, Speed: ${elapsedTime}ms`);
return ipInfo;
}
async amazonIP() {
const response = await this.rp_custom({
url: 'http://checkip.amazonaws.com',
method: 'get',
time : true,
timeout: 2000,
});
const elapsedTime = get(response, 'elapsedTime', -1);
const currentIP = get(response, 'body', '').trim();
const ipInfo = { ip: currentIP };
ipInfo['speed'] = elapsedTime;
ipInfo['service'] = 'checkip.amazonaws.com';
this.Stream.emit('log', `Current IP: ${currentIP}, Speed: ${elapsedTime}ms`);
return ipInfo;
}
async FPTIP() {
const response = await this.rp_custom({
url: 'https://checkip.fptplay.net',
method: 'get',
time : true,
timeout: 2000,
});
const elapsedTime = get(response, 'elapsedTime', -1);
const currentIP = get(response, 'body', '').trim();
const ipInfo = { ip: currentIP };
ipInfo['speed'] = elapsedTime;
ipInfo['service'] = 'checkip.fptplay.net';
this.Stream.emit('log', `Current IP: ${currentIP}, Speed: ${elapsedTime}ms`);
return ipInfo;
}
async getCurrentIP() {
var result = {}
try {
result = await this.ipconfig();;
} catch (e) {
result = await this.amazonIP();;
} finally {
return result;
}
}
async userInfo() {
this.isAdmin();
const rps = await this.service.request({
url: '/api/getUserInfo.php',
method: 'get',
});
return rps;
}
async balance() {
this.isAdmin();
const rps = await this.service.request({
url: '/api/getUserInfo.php',
method: 'get',
});
return parseInt(`${get(rps, 'balance', 0)}`);
}
async userKeys() {
this.isAdmin();
const rps = await this.service.request({
url: '/api/getUserKeys.php',
method: 'get',
});
rps.map(item => {
const date_expired = get(item, 'date_expired');
if (!date_expired) throw new Error('user keys is wrong struct');
item.expireAt = moment(date_expired, 'DD-MM-YYYY HH:mm:ss');
item.isExp = () => !item.expireAt.isAfter(moment(Date.now()));
return item;
})
return rps;
}
async keyInfo(api_key) {
const rps = await this.service.request({
url: '/api/getKeyInfo.php',
method: 'get',
params: { key: api_key || this.api_key }
});
return rps;
}
async getLocations() {
const rps = await this.service.request({
url: '/api/getLocations.php',
method: 'get'
});
return rps;
}
async getProxy(options = {}) {
const api_key = get(options, 'api_key', '');
const random = get(options, 'random', true);
let ranDomKey = '';
if (random) {
const currentUserKeys = await this.userKeys();
const availableKeys = currentUserKeys.filter(item => !item.isExp());
if (availableKeys.length === 0) throw new Error('Do not have any key exp');
const pickupRandomKey = sample(availableKeys);
ranDomKey = get(pickupRandomKey, 'key', '');
}
const key = ranDomKey || api_key || this.api_key;
const rps = await this.service.request({
url: '/api/getProxy.php',
method: 'get',
params: { key }
});
const proxy = get(rps, 'proxy', '');
this.Stream.emit('log', `Got proxy: ${proxy}, api_key: ${key}`);
return { ...rps, api_key: key };
}
async changeProxy(options = {}) {
const api_key = get(options, 'api_key', '');
const location_id = get(options, 'api_key', '0');
if (!api_key && !this.api_key) throw new Error('Pls input api_key');
const key = api_key || this.api_key;
const rps = await this.service.request({
url: '/api/changeProxy.php',
method: 'get',
params: { key, location: `${location_id}` }
});
const proxy = get(rps, 'proxy', '');
this.Stream.emit('log', `Changed proxy: ${proxy}, api_key: ${key}`);
return { ...rps, api_key: key };
// {
// success: true,
// proxy: '171.229.89.231:20400',
// location: '2',
// next_change: '120',
// timeout: 1188
// }
}
async waitUntilProxyOK(startTime = Date.now()) {
try {
const ipResult = await this.getCurrentIP();
return ipResult;
} catch (e) {
if (e.message.includes('ECONNREFUSED')) {
const passTime = Date.now() - startTime;
if (passTime >= this.waitProxyLiveTimeout) throw e;
this.Stream.emit('log', `Waited ${passTime}ms until proxy ready`);
return this.waitUntilProxyOK(startTime);
}
throw e;
}
}
async changeIP(options = {}) {
let result = { isChanged: false, message: '' };
const currentProxy = await this.pickupProxy(options);
const next_change = get(currentProxy, 'next_change', -1);
if (next_change === 0) {
await this.changeProxy(options);
result.isChanged = true;
} else {
if (next_change === 120) {
// await BPromise.delay(150);
result.isChanged = true;
}
result.message = `Wait ${next_change}s`;
};
const changedProxy = await this.pickupProxy(options);
result = { ...result, ...changedProxy};
this.proxy = get(result, 'proxy', this.proxy);
if (result.isChanged) await this.waitUntilProxyOK();
return result;
}
async pickup(options = {}) {
let result = { isChanged: false, message: '' };
let api_key = get(options, 'api_key', '');
const location_id = get(options, 'api_key', '0');
const random = (api_key === '');
if (!api_key && random) {
const currentUserKeys = await this.userKeys();
const availableKeys = currentUserKeys.filter(item => !item.isExp());
if (availableKeys.length === 0) throw new Error('Do not have any key exp');
const pickupRandomKey = sample(availableKeys);
api_key = get(pickupRandomKey, 'key', '');
this.Stream.emit('log', `Pickup api_key: ${api_key}`);
}
try {
result = await this.getProxy({ api_key, random: false });
} catch (e) {
if (e.name === 'ProxyServiceError' && e.message.includes('proxy not found!')) {
await this.changeProxy({ api_key, location_id });
result = await this.getProxy({ api_key, random: false });
result.isChanged = true;
} else {
throw e;
}
}
let next_change = get(result, 'next_change', -1);
if (next_change === 0) {
await this.changeProxy({ api_key, location_id });
result = await this.getProxy({ api_key, random: false });
result.isChanged = true;
} else if (next_change === 120) {
result.isChanged = true;
};
next_change = get(result, 'next_change', -1);
result.message = `Wait ${next_change}s`;
this.proxy = get(result, 'proxy', this.proxy);
if (result.isChanged) await this.waitUntilProxyOK();
return result;
}
async pickupProxy(options = {}) {
const api_key = get(options, 'api_key', '');
const location_id = get(options, 'api_key', '0');
const random = get(options, 'random', false);
let result = {};
try {
result = await this.getProxy(options);
} catch (e) {
await this.changeProxy(options);
result = await this.getProxy(options);
}
return result;
}
}
module.exports = TinSoftProxy;