Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Promise } from 'es6-promise';
- import * as request from 'request-promise';
- import * as md5 from 'md5';
- import { URL } from 'url';
- // TODO: TRY ADDING PROXIES
- export class TextNowRegistration {
- private readonly API_BASE = 'https://api.textnow.me/api2.0';
- private readonly API_SECRET =
- '851a9f2bad15843c1a922890c061830e8ce50e86840575d17e61ef0becdc3576';
- private readonly SITE_KEY = '6LcU4gkTAAAAAM4SyYTmXlKvqwWLuaFLy-30rzBn';
- private readonly AREA_CODE = 540;
- private readonly CAPTCHA_SERVICE_KEY = '7c8145707915f4ba0cb32db0747b75d0';
- private username: string;
- private password: string;
- private email: string;
- private clientId: string;
- private captchaToken: string;
- private captchaLink: string;
- // private onCaptchaReceived: (siteKey: string) => Promise<string>;
- constructor(
- username: string,
- password: string,
- email: string,
- // onCaptchaReceived: (siteKey: string) => Promise<string>,
- ) {
- this.username = username;
- this.password = password;
- this.email = email;
- // this.onCaptchaReceived = onCaptchaReceived;
- this.clientId = null;
- this.captchaToken = null;
- this.captchaLink = null;
- }
- private buildSignature(method: string, uri: string, json: any): string {
- // strip base uri
- let path = uri.replace(this.API_BASE + '/', '');
- // prepare json payload
- let stringified = '';
- if (json) {
- stringified = JSON.stringify(json);
- }
- // hash the payload
- return md5(this.API_SECRET + method + path + stringified);
- }
- private buildUrl(path: string): string {
- return this.API_BASE + path;
- }
- public register(): Promise<string> {
- return new Promise<string>((resolve, reject) => {
- this.checkUsername()
- .then((usernameExists: boolean) => {
- // check fif username already exists
- if (usernameExists)
- throw new Error(
- 'the user could not be created as the username is not available',
- );
- return this.checkEmail();
- })
- .then((emailExists: boolean) => {
- // check if email already exists
- if (emailExists)
- throw new Error(
- 'the user could not be created as the email is already registered',
- );
- return this.signUp();
- })
- .then(() => {
- return this.validateCaptcha();
- })
- .then(() => {
- return this.claimPhoneNumber(this.AREA_CODE);
- })
- .then((phoneNumber: string) => {
- return resolve(phoneNumber);
- })
- .catch(reject);
- });
- }
- private checkUsername(): Promise<boolean> {
- return new Promise<boolean>((resolve, reject) => {
- // prepare json request
- this.makeRequest({
- uri: this.buildUrl(
- `/users/${
- this.username
- }?idfa=&idfv=&client_type=TN_IOS_FREE`,
- ),
- method: 'HEAD',
- })
- .then((data: any) => {
- console.log('Username is taken!');
- return resolve(true);
- })
- .catch((err: any) => {
- console.log('Username is not taken.');
- console.log(err)
- return resolve(false);
- });
- });
- }
- private sleep(ms) {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
- private onCaptchaReceived(siteKey): Promise<string> {
- return new Promise<string>((resolve, reject) => {
- // TODO: ADD PROXY SUPPORT
- let getURL = `http://2captcha.com/in.php?key=${
- this.CAPTCHA_SERVICE_KEY
- }&method=userrecaptcha&googlekey=${siteKey}&pageurl=${
- this.captchaLink
- }`;
- this.makeRequest({
- uri: getURL,
- method: 'GET',
- })
- .then((tokenKey: string) => {
- if (tokenKey.substr(0, 2) != 'OK') {
- return resolve('CAPTCHA NOT RECEIVED');
- }
- let captchaID = tokenKey.substr(3);
- let curToken = undefined;
- for (let i = 0; i < 24; i++) {
- this.makeRequest({
- uri: `http://2captcha.com/res.php?key=${
- this.CAPTCHA_SERVICE_KEY
- }&action=get&id=${captchaID}`,
- method: 'GET',
- }).then((token: string) => {
- if (token.substr(0, 2) == 'OK') {
- curToken = token.substring(3);
- console.log(
- 'CAPTCHA TOKEN RECEIVED: ' +
- token.substr(0, 10),
- );
- resolve();
- }
- });
- this.sleep(5);
- }
- return resolve(curToken);
- })
- .catch(reject);
- });
- }
- private checkEmail(): Promise<boolean> {
- return new Promise<boolean>((resolve, reject) => {
- // prepare json request
- this.makeRequest({
- uri: this.buildUrl(
- `/emails/${this.email.replace(
- '@',
- '%40'
- )}?idfa=&idfv=&client_type=TN_IOS_FREE`,
- ),
- method: 'HEAD',
- })
- .then((data: any) => {
- console.log('Email is taken!');
- return resolve(true);
- })
- .catch((err: any) => {
- console.log('Email is not taken.');
- // console.log(err)
- return resolve(false);
- });
- });
- }
- private signUp(): Promise<string> {
- return new Promise<string>((resolve, reject) => {
- // prepare json request
- let bonusInfo = {
- TimeZone: '-25200',
- time: 1530670138,
- Language: 'en-US',
- ga: 'GA1.2.196167121.1528432309',
- };
- let body = {
- dob: '2018-07-05',
- password: this.password,
- email: this.email,
- bonus_info: JSON.stringify(bonusInfo),
- };
- console.log('Made signup request...');
- // make the code verification request
- this.makeRequest({
- uri: this.buildUrl(
- `/users/${
- this.username
- }?idfa=&idfv=&client_type=TN_IOS_FREE`,
- ),
- method: 'PUT',
- body: body,
- })
- // TODO: CONVERT TO PYTHON CODE
- .then((data: any) => {
- // check if registration was successfull
- if (!data.result || !data.result.id) {
- console.log('Could not register user!');
- throw new Error(
- 'there was an error trying to register the user',
- );
- }
- // the user was successfully created
- this.clientId = data.result.id;
- this.captchaLink = data.result.captcha_link;
- let url = new URL(data.result.captcha_link);
- this.captchaToken = url.searchParams.get('token');
- console.log('Signed up!');
- return resolve(this.clientId);
- })
- .catch(reject => {
- console.log(reject);
- });
- });
- }
- private claimPhoneNumber(areaCode: number): Promise<string> {
- return new Promise<string>((resolve, reject) => {
- // make the phone number claim request
- this.makeRequest({
- uri: this.buildUrl(
- `/users/${this.username}/phone?idfa=&idfv=&client_id=${
- this.clientId
- }&client_type=TN_IOS_FREE`,
- ),
- method: 'PUT',
- body: {
- area_code: areaCode,
- },
- })
- .then((phone: any) => {
- const phoneNumber: string = '+1' + phone;
- console.log('Phone number claimed: ' + phoneNumber);
- return resolve(phoneNumber);
- })
- .catch(reject);
- });
- }
- private validateCaptcha(): Promise<void> {
- return new Promise<void>((resolve, reject) => {
- // wait for the captcha to be returned
- this.onCaptchaReceived(this.SITE_KEY)
- .then((token: string) => {
- // prepare json payload
- let body = {
- u: this.captchaToken,
- c: token,
- };
- // prepare request
- return request({
- uri: 'https://www.textnow.com/api/identity/validate',
- method: 'POST',
- form: {
- json: JSON.stringify(body),
- },
- json: true,
- headers: {
- 'User-Agent':
- 'TextNowSwift-Free/8.29.0 (iPhone; iOS 12.0; Scale/2.00)',
- },
- });
- })
- .then((data: any) => {
- // check if the validation was successful
- if (data.error_code !== null) {
- console.log('Could not validate captcha!');
- throw new Error(
- 'there was an error validating the captcha',
- );
- }
- console.log('Successfully validated captcha!');
- // the validation was successful
- return resolve();
- })
- .catch(reject);
- });
- }
- private makeRequest(
- options: request.OptionsWithUri,
- ): request.RequestPromise {
- // modify and add required headers
- //options.proxy = 'http://localhost:8888';
- options.json = true;
- options.headers = {
- 'User-Agent':
- 'TextNowSwift-Free/8.29.0 (iPhone; iOS 12.0; Scale/2.00)',
- 'Accept-Language': 'en-US;q=1',
- };
- // add signature
- options.uri =
- options.uri +
- '&signature=' +
- this.buildSignature(
- options.method,
- options.uri as string,
- options.body,
- );
- // make request
- return request(options);
- }
- }
- new TextNowRegistration(
- 'USERNAsdfOSNGFDS',
- 'abc1ffg23df',
- 'EMAILsdfdsfGFDS@gmail.com',
- ).register();
Add Comment
Please, Sign In to add comment