330 lines
8.7 KiB
JavaScript
330 lines
8.7 KiB
JavaScript
import fs from 'fs';
|
|
import request from 'async-request';
|
|
import rp from 'request-promise';
|
|
import prompts from 'prompts';
|
|
import faker from 'faker';
|
|
|
|
import { MASTER_BEARER_TOKEN, CREATOR_SETTINGS, HEADERS, SERVER_URL } from './config';
|
|
|
|
export default class Creator {
|
|
constructor() {
|
|
this.apiKey = CREATOR_SETTINGS.API_KEY;
|
|
}
|
|
|
|
async run() {
|
|
if (!this.apiKey) {
|
|
console.log('No API Key defined... reverting to manual mode.');
|
|
await this.runManual();
|
|
return;
|
|
}
|
|
|
|
const getChoice = async () => {
|
|
const response = await prompts([
|
|
{
|
|
type: 'number',
|
|
name: 'count',
|
|
message: 'Number to Generate:',
|
|
},
|
|
{
|
|
type: 'text',
|
|
name: 'referral',
|
|
message: 'Referral Code:',
|
|
},
|
|
]);
|
|
|
|
return response;
|
|
};
|
|
|
|
const response = await getChoice();
|
|
const { count, referral } = response;
|
|
|
|
const priceCheckRequest = await request(`https://cloudsms.io/api/number/price?key=${this.apiKey}&service=7&tier=0`);
|
|
|
|
const { balance, price } = JSON.parse(priceCheckRequest.body);
|
|
const totalPrice = price * count;
|
|
console.log(`This will cost you $${totalPrice}. You have $${balance} in your account.`);
|
|
|
|
if (totalPrice > balance) {
|
|
console.log(`Your account doesn't have enough money to cover this.`);
|
|
return;
|
|
}
|
|
|
|
const getConfirm = async () => {
|
|
const response = await prompts([
|
|
{
|
|
type: 'text',
|
|
name: 'confirm',
|
|
message: 'Confirm (y/n):',
|
|
},
|
|
]);
|
|
|
|
return response;
|
|
};
|
|
|
|
const confirmation = await getConfirm();
|
|
if (confirmation.confirm.toLowerCase() !== 'y') {
|
|
return;
|
|
}
|
|
|
|
const result = await this.register(count, referral);
|
|
}
|
|
|
|
async runManual() {
|
|
const getChoice = async () => {
|
|
const response = await prompts([
|
|
{
|
|
type: 'text',
|
|
name: 'phone',
|
|
message: 'Phone Number:',
|
|
},
|
|
{
|
|
type: 'text',
|
|
name: 'referral',
|
|
message: 'Referral Code:',
|
|
},
|
|
]);
|
|
|
|
return response;
|
|
};
|
|
|
|
const getCode = async () => {
|
|
const response = await prompts([
|
|
{
|
|
type: 'text',
|
|
name: 'code',
|
|
message: 'SMS Code:',
|
|
},
|
|
]);
|
|
|
|
return response;
|
|
};
|
|
|
|
const response = await getChoice();
|
|
const { phone, referral } = response;
|
|
|
|
console.log(`Using ${phone}! Verifying with Trivia Server...`);
|
|
|
|
const verificationId = await this.verifyNumber(phone);
|
|
if (!verificationId) return;
|
|
|
|
console.log(`Successfully verified with id ${verificationId}! Checking for confirmation code...`);
|
|
|
|
const codeResponse = await getCode();
|
|
const { code } = codeResponse;
|
|
if (!code) return;
|
|
|
|
console.log(`Found confirmation code ${code}! Confirming phone number with Trivia Server...`);
|
|
|
|
const authExists = await this.confirmNumber(verificationId, code);
|
|
if (authExists) return;
|
|
|
|
console.log(`Successfully confirmed and no account exists! Creating account now...`)
|
|
|
|
const account = await this.createAccount(verificationId, referral);
|
|
if (!account) return;
|
|
|
|
console.log(`Account created with username ${account.username} and token\n${account.accessToken}`);
|
|
|
|
fs.readFile('accounts.json', 'utf8', (err, data) => {
|
|
if (err) {
|
|
this.logger.logSilent('Error reading accounts for writing: ', err);
|
|
} else {
|
|
const { accounts } = JSON.parse(data);
|
|
accounts.push(account);
|
|
|
|
const accountsJSON = JSON.stringify({accounts}, null, 2);
|
|
|
|
fs.writeFile('accounts.json', accountsJSON, 'utf8', (err, data) => {
|
|
if (err) {
|
|
this.logger.logSilent('Error saving accounts: ', err);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
async register(count, referral) {
|
|
for (let i = 0; i < count; i++) {
|
|
console.log(`Generating phone number for account #${i + 1}...`);
|
|
const newNumberRequest = await request(`https://cloudsms.io/api/number?key=${this.apiKey}&service=7&tier=0`);
|
|
const { status, error, number } = JSON.parse(newNumberRequest.body);
|
|
|
|
if (status === 'error') {
|
|
console.log(`Error: ${error}`);
|
|
return;
|
|
}
|
|
|
|
console.log(`Reserved ${number}! Verifying with Trivia Server...`);
|
|
|
|
const verificationId = await this.verifyNumber(number);
|
|
if (!verificationId) return;
|
|
|
|
console.log(`Successfully verified with id ${verificationId}! Waiting for 10 seconds then checking for confirmation code...`);
|
|
|
|
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
await delay(10000);
|
|
|
|
const confirmationCode = await this.readSMS(number);
|
|
if (!confirmationCode) return;
|
|
|
|
console.log(`Found confirmation code ${confirmationCode}! Confirming phone number with Trivia Server...`);
|
|
|
|
const authExists = await this.confirmNumber(verificationId, confirmationCode);
|
|
if (authExists) return;
|
|
|
|
console.log(`Successfully confirmed and no account exists! Creating account now...`)
|
|
|
|
const account = await this.createAccount(verificationId, referral);
|
|
if (!account) return;
|
|
|
|
console.log(`Account created with username ${account.username} token\n${account.accessToken}`);
|
|
|
|
fs.readFile('accounts.json', 'utf8', (err, data) => {
|
|
if (err) {
|
|
this.logger.logSilent('Error reading accounts for writing: ', err);
|
|
} else {
|
|
const { accounts } = JSON.parse(data);
|
|
accounts.push(account);
|
|
|
|
const accountsJSON = JSON.stringify({accounts}, null, 2);
|
|
|
|
fs.writeFile('accounts.json', accountsJSON, 'utf8', (err, data) => {
|
|
if (err) {
|
|
this.logger.logSilent('Error saving accounts: ', err);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
async verifyNumber(number) {
|
|
const body = {
|
|
method: 'sms',
|
|
phone: `+1${number}`,
|
|
};
|
|
|
|
const options = {
|
|
method: 'POST',
|
|
uri: `https://${SERVER_URL}/verifications`,
|
|
headers: {
|
|
...HEADERS,
|
|
'content-length': JSON.stringify(body).length,
|
|
},
|
|
body,
|
|
json: true,
|
|
};
|
|
|
|
try {
|
|
const response = await rp(options);
|
|
const { error, verificationId } = response;
|
|
if (error && error !== '') {
|
|
return Promise.reject(error);
|
|
}
|
|
|
|
return Promise.resolve(verificationId);
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
}
|
|
|
|
async readSMS(number) {
|
|
const messageRequest = await request(`https://cloudsms.io/api/number/messages?key=${this.apiKey}&number=${number}`);
|
|
|
|
const { status, error, messages } = JSON.parse(messageRequest.body);
|
|
if (status === 'error') {
|
|
console.log(`Error: ${error}`);
|
|
return;
|
|
}
|
|
|
|
const firstMessage = messages.length > 0 ? messages[0] : {};
|
|
const { text } = firstMessage;
|
|
const smsCode = parseInt(text);
|
|
|
|
return smsCode;
|
|
}
|
|
|
|
async confirmNumber(verificationId, confirmationCode) {
|
|
const body = {
|
|
code: confirmationCode,
|
|
};
|
|
|
|
const options = {
|
|
method: 'POST',
|
|
uri: `https://${SERVER_URL}/verifications/${verificationId}`,
|
|
headers: {
|
|
...HEADERS,
|
|
'content-length': JSON.stringify(body).length,
|
|
},
|
|
body,
|
|
json: true,
|
|
};
|
|
|
|
try {
|
|
const response = await rp(options);
|
|
const { error, auth } = response;
|
|
if (error && error !== '') {
|
|
return Promise.reject(error);
|
|
}
|
|
|
|
if (auth) {
|
|
return Promise.reject(`User already exists: ${auth}`);
|
|
}
|
|
|
|
return Promise.resolve(null);
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
}
|
|
|
|
async createAccount(verificationId, referral) {
|
|
let username;
|
|
while (!username || username.length > 10) {
|
|
username = faker.internet.userName();
|
|
if (username.length <= 10) {
|
|
const checkUser = await request(`https://${SERVER_URL}/users/?q=${username}`, {
|
|
headers: {
|
|
...HEADERS,
|
|
Authorization: `Bearer ${MASTER_BEARER_TOKEN}`,
|
|
},
|
|
});
|
|
const { data } = JSON.parse(checkUser.body);
|
|
if (data && data.length > 0) {
|
|
username = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
const body = {
|
|
country: 'US',
|
|
language: 'en',
|
|
referringUsername: referral,
|
|
username,
|
|
verificationId,
|
|
};
|
|
|
|
const options = {
|
|
method: 'POST',
|
|
uri: `https://${SERVER_URL}/users`,
|
|
headers: {
|
|
...HEADERS,
|
|
'content-length': JSON.stringify(body).length,
|
|
},
|
|
body,
|
|
json: true,
|
|
};
|
|
|
|
try {
|
|
const response = await rp(options);
|
|
const { error } = response;
|
|
if (error && error !== '') {
|
|
return Promise.reject(error);
|
|
}
|
|
|
|
return Promise.resolve(response);
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
}
|
|
}
|