-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathsmsbower_probe.js
More file actions
388 lines (344 loc) · 11.1 KB
/
Copy pathsmsbower_probe.js
File metadata and controls
388 lines (344 loc) · 11.1 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env node
'use strict';
const {
callApi: callSmsbowerApi,
extractCountriesForService,
getCatalog,
getServicePriceSheet,
resolveService,
} = require('./src/lib/providers/smsbower');
function printHelp() {
console.log(`
Usage:
node smsbower_probe.js prices --service <code|id> [--country <id[,id]>] [--iso <iso[,iso]>] [--all] [--json]
node smsbower_probe.js probe --api-key <key> --service <code|id> [--country <id[,id]>] [--iso <iso[,iso]>] [--all] [--json]
Commands:
prices Query current SMSBower price tiers for a service.
probe Buy a number with getNumberV2, read canGetAnotherSms, then cancel immediately.
Options:
--api-key <key> SMSBower API key. Required for probe.
--service <value> Service code, id, slug, or title. Example: dr, tg, 247.
--country <list> Country id list. Example: 53,52,1
--iso <list> Country ISO list. Example: SA,TH,US
--all Use all countries currently listed for the service.
--json Print JSON instead of a text table.
--include-empty Keep price tiers whose count is 0.
--show-attempts Include every provider attempt in text output for probe.
--help Show this help.
Examples:
node smsbower_probe.js prices --service dr --iso SA
node smsbower_probe.js prices --service tg --country 53,52 --json
node smsbower_probe.js probe --api-key YOUR_KEY --service dr --iso SA
node smsbower_probe.js probe --api-key YOUR_KEY --service tg --country 53,52 --show-attempts
`.trim());
}
function parseArgs(argv) {
const args = {
command: '',
service: '',
apiKey: '',
countryIds: [],
isoCodes: [],
all: false,
json: false,
includeEmpty: false,
showAttempts: false,
help: false,
};
const positional = [];
for (let i = 0; i < argv.length; i += 1) {
const token = String(argv[i] || '').trim();
if (!token) continue;
if (!token.startsWith('--')) {
positional.push(token);
continue;
}
const key = token.slice(2);
const next = argv[i + 1];
switch (key) {
case 'api-key':
args.apiKey = String(next || '').trim();
i += 1;
break;
case 'service':
args.service = String(next || '').trim();
i += 1;
break;
case 'country':
args.countryIds = splitCsv(next).map((value) => Number.parseInt(value, 10)).filter(Number.isFinite);
i += 1;
break;
case 'iso':
args.isoCodes = splitCsv(next).map((value) => String(value || '').trim().toUpperCase()).filter(Boolean);
i += 1;
break;
case 'all':
args.all = true;
break;
case 'json':
args.json = true;
break;
case 'include-empty':
args.includeEmpty = true;
break;
case 'show-attempts':
args.showAttempts = true;
break;
case 'help':
args.help = true;
break;
default:
throw new Error(`Unknown option: --${key}`);
}
}
args.command = positional[0] || '';
return args;
}
function splitCsv(value) {
return String(value || '')
.split(/[,\s]+/)
.map((entry) => String(entry || '').trim())
.filter(Boolean);
}
function getCountriesForService(sheet, serviceId, includeEmpty) {
const countries = extractCountriesForService(sheet, serviceId).map((country) => ({
...country,
tiers: country.tiers
.map((tier, index) => ({
price: tier.priceOriginal,
count: tier.stock,
rankId: index,
rank: `tier-${index + 1}`,
providerIds: String(tier.providerRef || '')
.split(',')
.map((value) => Number.parseInt(value, 10))
.filter(Number.isFinite),
}))
.filter((tier) => includeEmpty || tier.count > 0),
}));
return countries.sort((left, right) => left.id - right.id);
}
function selectCountries(countries, args) {
if (args.all || (!args.countryIds.length && !args.isoCodes.length)) {
return countries;
}
const byId = new Map();
const byIso = new Map(countries.map((country) => [country.iso, country]));
for (const country of countries) {
byId.set(country.id, country);
if (Number.isFinite(country.apiCountryCode)) {
byId.set(country.apiCountryCode, country);
}
}
const selected = [];
const seen = new Set();
for (const id of args.countryIds) {
const country = byId.get(id);
if (!country || seen.has(country.id)) continue;
seen.add(country.id);
selected.push(country);
}
for (const iso of args.isoCodes) {
const country = byIso.get(iso);
if (!country || seen.has(country.id)) continue;
seen.add(country.id);
selected.push(country);
}
return selected;
}
async function callApi(apiKey, params) {
return callSmsbowerApi(apiKey, params);
}
function parseCanGetAnotherSms(value) {
if (value === true || value === 1) return true;
const normalized = String(value ?? '').trim().toLowerCase();
if (normalized === '1' || normalized === 'true' || normalized === 'yes') return true;
if (normalized === '0' || normalized === 'false' || normalized === 'no') return false;
return null;
}
async function probeCountry(apiKey, service, country) {
const attempts = [];
for (const tier of country.tiers) {
if (!tier.providerIds.length) continue;
for (const providerId of tier.providerIds) {
const response = await callApi(apiKey, {
action: 'getNumberV2',
service: service.activate_org_code,
country: country.apiCountryCode,
maxPrice: tier.price,
providerIds: providerId,
});
const attempt = {
providerId,
price: tier.price,
rank: tier.rank,
rankId: tier.rankId,
response,
};
attempts.push(attempt);
if (typeof response === 'string') {
if (response === 'NO_NUMBERS') continue;
if (/^(BAD_KEY|BAD_ACTION|BAD_SERVICE|BAD_COUNTRY|NO_BALANCE)/.test(response)) {
return {
status: 'error',
country,
attempts,
error: response,
};
}
continue;
}
if (!response || typeof response !== 'object' || !response.activationId || !response.phoneNumber) {
continue;
}
let cancelResponse = '';
try {
cancelResponse = await callApi(apiKey, {
action: 'setStatus',
status: 8,
id: response.activationId,
});
} catch (error) {
cancelResponse = `CANCEL_FAILED: ${error.message}`;
}
return {
status: 'success',
country,
attempts,
match: {
activationId: String(response.activationId),
phoneNumber: String(response.phoneNumber),
activationCost: Number(response.activationCost || tier.price),
canGetAnotherSms: parseCanGetAnotherSms(response.canGetAnotherSms),
rawCanGetAnotherSms: response.canGetAnotherSms,
providerId,
rank: tier.rank,
rankId: tier.rankId,
cancelResponse,
},
};
}
}
return {
status: 'no-number',
country,
attempts,
};
}
function formatTier(tier) {
return `${tier.rank} price=${tier.price} count=${tier.count} providerIds=${tier.providerIds.join(',')}`;
}
function printPricesText(service, countries) {
console.log(`service=${service.activate_org_code} serviceId=${service.id} title=${service.title}`);
for (const country of countries) {
console.log('');
console.log(`[${country.iso}] ${country.title} countryId=${country.id} apiCountry=${country.apiCountryCode} minPrice=${country.minPrice} totalCount=${country.count}`);
if (!country.tiers.length) {
console.log(' no tiers');
continue;
}
for (const tier of country.tiers) {
console.log(` ${formatTier(tier)}`);
}
}
}
function printProbeText(service, results, showAttempts) {
console.log(`service=${service.activate_org_code} serviceId=${service.id} title=${service.title}`);
for (const result of results) {
const country = result.country;
console.log('');
console.log(`[${country.iso}] ${country.title} countryId=${country.id} apiCountry=${country.apiCountryCode}`);
for (const tier of country.tiers) {
console.log(` tier ${formatTier(tier)}`);
}
if (result.status === 'success') {
console.log(` probe success providerId=${result.match.providerId} activationCost=${result.match.activationCost} canGetAnotherSms=${result.match.canGetAnotherSms} raw=${result.match.rawCanGetAnotherSms} cancel=${result.match.cancelResponse}`);
} else if (result.status === 'no-number') {
console.log(' probe no-number');
} else {
console.log(` probe error=${result.error}`);
}
if (showAttempts && result.attempts.length) {
for (const attempt of result.attempts) {
const response = typeof attempt.response === 'string'
? attempt.response
: JSON.stringify(attempt.response);
console.log(` attempt providerId=${attempt.providerId} price=${attempt.price} rank=${attempt.rank} response=${response}`);
}
}
}
}
async function runPricesCommand(args) {
const catalog = await getCatalog();
const service = resolveService(args.service, catalog);
const sheet = await getServicePriceSheet(service.id);
const countries = selectCountries(getCountriesForService(sheet, service.id, args.includeEmpty), args);
if (!countries.length) {
throw new Error('No countries matched the given filters.');
}
if (args.json) {
console.log(JSON.stringify({
service: {
id: service.id,
title: service.title,
code: service.activate_org_code,
slug: service.slug,
},
countries,
}, null, 2));
return;
}
printPricesText(service, countries);
}
async function runProbeCommand(args) {
if (!args.apiKey) {
throw new Error('Missing --api-key');
}
if (!args.all && !args.countryIds.length && !args.isoCodes.length) {
throw new Error('Probe requires --country, --iso, or --all.');
}
const catalog = await getCatalog();
const service = resolveService(args.service, catalog);
const sheet = await getServicePriceSheet(service.id);
const countries = selectCountries(getCountriesForService(sheet, service.id, args.includeEmpty), args);
if (!countries.length) {
throw new Error('No countries matched the given filters.');
}
const results = [];
for (const country of countries) {
results.push(await probeCountry(args.apiKey, service, country));
}
if (args.json) {
console.log(JSON.stringify({
service: {
id: service.id,
title: service.title,
code: service.activate_org_code,
slug: service.slug,
},
results,
}, null, 2));
return;
}
printProbeText(service, results, args.showAttempts);
}
async function main() {
const args = parseArgs(process.argv.slice(2));
if (args.help || !args.command) {
printHelp();
return;
}
if (args.command === 'prices') {
await runPricesCommand(args);
return;
}
if (args.command === 'probe') {
await runProbeCommand(args);
return;
}
throw new Error(`Unknown command: ${args.command}`);
}
main().catch((error) => {
console.error(`Error: ${error.message}`);
process.exitCode = 1;
});