-
Notifications
You must be signed in to change notification settings - Fork 349
/
index.js
146 lines (130 loc) · 4.22 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
const actionFunc = async (
username, password, cookies, recipient, message, captchaToken) => {
console.log('TextNow bot start...');
const path = require('path');
const fs = require('fs').promises;
const puppeteer = require('puppeteer-extra');
const recaptchaPlugin = require('puppeteer-extra-plugin-recaptcha');
const stealthPlugin = require('puppeteer-extra-plugin-stealth');
const textNowHelper = require('./utils/helper');
let browser = null;
let page = null;
let md5Username = textNowHelper.md5(username);
// Make cache folder
const cacheFolder = path.resolve(__dirname, '.cache');
try {
await fs.access(cacheFolder);
} catch (error) {
await fs.mkdir(cacheFolder);
}
try {
puppeteer.use(stealthPlugin());
if (captchaToken) {
puppeteer.use(
recaptchaPlugin({
provider: {
id: '2captcha',
token: captchaToken,
},
visualFeedback: true,
}),
);
}
browser = await puppeteer.launch({
headless: true,
args: [
process.env.HTTP_PROXY ?
`--proxy-server=${process.env.HTTP_PROXY}` :
'',
'--disable-features=IsolateOrigins,site-per-process',
'--flag-switches-begin --disable-site-isolation-trials --flag-switches-end',
],
});
page = await browser.newPage();
const client = await page.target().createCDPSession();
try {
console.log('Importing cookies from environment...');
cookies = JSON.parse(Buffer.from(cookies, 'base64').toString());
} catch (error) {
console.log(`Environment cookies is invalid format: ${error}`);
try {
console.log('Importing existing cookies from file...');
const cookiesString = await fs.readFile(
path.resolve(cacheFolder, `${md5Username}.cookies.json`),
);
cookies = JSON.parse(cookiesString.toString());
} catch (error) {
console.log(`Failed to import existing cookies: ${error}`);
}
}
// Log into TextNow and get cookies
try {
console.log('Logging in with existing cookies');
await page.setCookie(...cookies);
cookies = await textNowHelper.logIn(page, client);
} catch (error) {
console.log(`Failed to log in with existing cookies: ${error}`);
console.log('Logging in with account credentials...');
cookies = await textNowHelper.logIn(page, client, username, password);
}
try {
console.log('Successfully logged into TextNow!');
// Save cookies to file
await fs.writeFile(
path.resolve(cacheFolder, `${md5Username}.cookies.json`),
JSON.stringify(cookies),
);
} catch (error) {
console.log(`Failed to save cookies to file: ${error}`);
}
// Select a conversation using recipient info
console.log('Selecting conversation...');
await textNowHelper.selectConversation(page, recipient);
// Send a message to the current recipient
console.log('Sending message...');
await textNowHelper.sendMessage(page, message);
console.log('Message sent!');
await browser.close();
} catch (error) {
console.log(error);
if (page) {
await page.screenshot({
path: path.resolve(cacheFolder, 'error-screenshot.jpg'),
type: 'jpeg',
});
}
if (browser) {
await browser.close();
}
process.exit(1);
}
};
(async () => {
console.log('Start...');
const config = require('./config');
const {
username,
password,
cookies,
recipient,
message,
captchaToken,
} = config;
const arrUsername = username.split('|');
const arrPassword = password.split('|');
const arrCookies = cookies.split('|');
if (arrUsername.length === arrPassword.length) {
for (let i = 0, length = arrUsername.length; i < length; i++) {
const strUsername = arrUsername[i];
const strPassword = arrPassword[i];
const strCookies = arrCookies[i];
console.log(`User: ${strUsername} start...`);
await actionFunc(strUsername, strPassword, strCookies,
recipient, message, captchaToken);
console.log(`User: ${strUsername} end...`);
}
} else {
console.log('Multi user information is error.');
}
console.log('End...');
})();