-
Notifications
You must be signed in to change notification settings - Fork 0
/
System.ts
168 lines (143 loc) · 5.03 KB
/
System.ts
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
import { Page, launch, Browser } from "puppeteer";
import { invisibleCaptchaSolver } from "./CaptchaSolver";
import { accountCreationRequest } from "./Fetch";
import { readEmailsSince } from "./EmailReader";
export class System {
browser?: Browser;
page?: Page;
EMAIL_CREDENTIALS: {
user: string;
password: string;
host: string;
port: number;
tls: boolean;
};
TWO_CAPTCHA_API_KEY: string;
DOMAIN: string;
constructor(){
const config = require('./config.json');
this.EMAIL_CREDENTIALS = config.EMAIL_CREDENTIALS
this.TWO_CAPTCHA_API_KEY = config.TWO_CAPTCHA_API_KEY
this.DOMAIN = config.DOMAIN;
}
launchBrowser = async () => {
this.browser = await launch({ headless: true });
[this.page] = await this.browser.pages();
};
createAccount = async ({
email,
username,
password,
date
}: {
email?: string;
username: string;
password: string;
date: { day: number; month: number; year: number };
}) => {
// This token is so that only recent mails are analyzed and not all the inbox.
const dateSince = new Date(new Date().getTime());
console.log("Creating account with params", {
email,
username,
password,
date
});
if (!this.page || !this.browser) {
return;
}
if (!username.length) {
}
if (!email || (email && !email.length)) {
if (!this.DOMAIN) {
console.log(
red(
"No email address was specified and no domain was found in config. Add a domain in config or specify the email address to create an account."
)
);
}
email = username + "@" + this.DOMAIN;
}
await this.page.goto("https://www.dofus.com/fr/creer-un-compte");
await this.page.waitForSelector("#userlogin");
await this.page.type("#userlogin", username, { delay: 50 });
await this.page.waitForSelector("#user_password");
await this.page.type("#user_password", password, { delay: 50 });
await this.page.waitForSelector("#user_mail");
await this.page.type("#user_mail", email, { delay: 50 });
await this.page.waitFor(1000);
await this.page.select("#ak_field_1", date.day.toString());
await this.page.waitFor(1000);
await this.page.select("#ak_field_2", date.month.toString());
await this.page.waitFor(1000);
await this.page.select("#ak_field_3", date.year.toString());
// To remove focus from the mail field so that the error event can be triggered.
await this.page.click("#userlogin");
await this.page.waitFor(500);
let errorFields = await this.checkFields(this.page);
if (errorFields.length) {
errorFields.map(error => {
console.log(red("Error creating account: " + error.value))
})
return;
}
console.log("No errors. Proceeding to captcha solving & account creation.");
// Page is loaded, start fetching captcha
const captchaKey = await this.page.$eval(".grecaptcha-logo", element => {
const elementHTML = element.innerHTML;
return elementHTML.substr(elementHTML.indexOf("k=") + 2, 300).split("&")[0];
});
console.log("Obtained captcha key", captchaKey);
const solvedCaptcha = await invisibleCaptchaSolver(captchaKey, this.TWO_CAPTCHA_API_KEY);
console.log(solvedCaptcha.length > 100 ? "✔" : "❌", "Obtained captcha response");
await accountCreationRequest({
username,
password,
email,
solvedCaptcha,
date
});
// Wait for a few seconds for the email to be sent
await this.page.waitFor(10_000);
const links = await readEmailsSince({ dateSince, subject: "ADDRESS_CONFIRMATION", emailCredentials: this.EMAIL_CREDENTIALS });
if (links.length > 0) {
await this.page.goto(links[0]);
} else {
console.log(`❌ Account was ${red(bold("not"))} successfully created. Possible reasons: the captcha was badly resolved or your IP is flagged.`);
return;
}
// Wait for a few seconds for the email to be sent
await this.page.waitFor(10_000);
const confirmationEmail = await readEmailsSince({ dateSince, subject: "SUCCESSFUL_ACCOUNT_CREATION", emailCredentials: this.EMAIL_CREDENTIALS });
if (confirmationEmail.length > 0) {
console.log("✔", green("Account was successfully created."));
} else {
console.log(`❌ Account was ${red(bold("not"))} successfully created. Possible reasons: the captcha was badly resolved or your IP is flagged.`);
}
};
async checkFields(page: Page) {
const elements = await page.$$eval(".error", elements => {
return elements
.map(element => {
return { type: (element as HTMLLabelElement).htmlFor, value: element.innerHTML };
})
.filter(element => element.value !== "");
});
return elements;
}
}
export const red = (text: string): string => `\x1b[31m${text}\x1b[0m`;
export const green = (text: string): string => `\x1b[32m${text}\x1b[0m`;
export const yellow = (text: string): string => `\x1b[33m${text}\x1b[0m`;
export const bold = (text: string): string => `\x1b[1m${text}\x1b[0m`;
// (async () => {
// const system = new System();
// await system.launchBrowser();
// await system.createAccount({
// email: "",
// username: "Hdeyuidti",
// password: "yessai123!",
// date: { day: 9, month: 3, year: 1997 }
// });
// console.log(`✔ ${green("Done.")}`);
// })();