-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathnfe.fazenda.gov.br.js
224 lines (169 loc) · 6.78 KB
/
nfe.fazenda.gov.br.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
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
/*
fazenda.gov.br bypass example with Anti Captcha.
IMPORTANT!
1. Make sure "const userAgent" is same as in your Chromium instance
2. Set your API key ("apiKey")
3. Set correct form password value ("formPassword")
4. The page is using Hcaptcha Enterprise. This is why we do several attempts to bypass the screen (howManyTestsToMake value).
5. If you want to see what's happening, replace "headless: true" with "headless: false".
Install dependencies:
npm install @antiadmin/anticaptchaofficial puppeteer
* */
const pup = require('puppeteer')
const anticaptcha = require("@antiadmin/anticaptchaofficial");
//screen with captcha
const captchaUrl = 'https://www.nfe.fazenda.gov.br/portal/consultaRecaptcha.aspx?tipoConsulta=resumo&tipoConteudo=7PhJ+gAVw2g=';
//address behind captcha
const checkUrl = 'https://www.nfe.fazenda.gov.br/portal/consultaResumo.aspx?tipoConteudo=7PhJ+gAVw2g=';
//Anti-captcha.com API key
const apiKey = 'API_KEY_HERE';
const formPassword = '44_DIGIT_CODE_HERE_44_DIGIT_CODE_HERE_44_DIG';
const howManyTestsToMake = 10;
let browser = null;
let page = null;
let success = 0;
let fail = 0;
let userAgent = '';
// STOP! IMPORTANT! Shared proxy services won't work!
// Use ONLY self-installed proxies on your own infrastructure! Instruction: https://anti-captcha.com/apidoc/articles/how-to-install-squid
const proxyAddress = '1.2.3.4';
const proxyPort = 1234;
const proxyLogin = 'login';
const proxyPassword = 'pass';
(async () => {
anticaptcha.setAPIKey(apiKey);
anticaptcha.shutUp(); //comment for verbose captcha recognition
const balance = await anticaptcha.getBalance();
if (balance <= 0) {
console.log('Topup your anti-captcha.com balance!');
return;
} else {
console.log('API key balance is '+balance+', continuing');
}
for (let i=0;i<howManyTestsToMake;i++) {
console.log("\nSolving HCaptcha with Anti-Captcha.Com ..");
let hcaptchaResponse = null;
try {
hcaptchaResponse = await anticaptcha.solveHCaptchaProxyOn(
captchaUrl,
'e72d2f82-9594-4448-a875-47ded9a1898a',
'http',
proxyAddress,
proxyPort,
proxyLogin,
proxyPassword,
'',
'',
{}
);
userAgent = anticaptcha.getHcaptchaUserAgent();
} catch (e) {
console.error("could not solve captcha: " + e.toString());
return;
}
console.log('hcaptchaResponse:', hcaptchaResponse);
try {
console.log('opening browser ..');
let options = {
headless: false,
ignoreDefaultArgs: ["--disable-extensions", "--enable-automation"],
devtools: true,
args: [
`--proxy-server=${proxyAddress}:${proxyPort}`,
'--disable-web-security',
'--disable-features=IsolateOrigins,site-per-process',
'--allow-running-insecure-content',
'--disable-blink-features=AutomationControlled',
'--no-sandbox',
'--mute-audio',
'--no-zygote',
'--no-xshm',
'--window-size=1920,1080',
'--no-first-run',
'--no-default-browser-check',
'--disable-dev-shm-usage',
'--disable-gpu',
'--enable-webgl',
'--ignore-certificate-errors',
'--lang=en-US,en;q=0.9',
'--password-store=basic',
'--disable-gpu-sandbox',
'--disable-software-rasterizer',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
'--disable-infobars',
'--disable-breakpad',
'--disable-canvas-aa',
'--disable-2d-canvas-clip-aa',
'--disable-gl-drawing-for-tests',
'--enable-low-end-device-mode',
'--no-sandbox'
]
};
browser = await pup.launch(options);
console.log('creating new page ..');
page = await browser.newPage();
console.log('setting browser user agent to ',userAgent);
await page.setUserAgent(userAgent);
await page.evaluateOnNewDocument(() => {
delete navigator.__proto__.webdriver;
});
if (proxyPassword && proxyLogin) {
console.log(`setting proxy authentication ${proxyLogin}:${proxyPassword}`);
await page.authenticate({
username: proxyLogin,
password: proxyPassword,
});
}
} catch (e) {
console.error("could not open browser: " + e);
return false;
}
//screen size
await page.setViewport({width: 1360, height: 1000});
try {
await page.goto(captchaUrl, {
waitUntil: "networkidle0",
timeout: 300000
});
} catch (e) {
console.log('err while loading the page: ' + e);
}
console.log('Filling h-captcha-response');
await page.evaluate(async (hcaptchaResponse) => {
document.getElementsByName('h-captcha-response')[0].value = hcaptchaResponse;
}, hcaptchaResponse);
console.log('Filling password ',formPassword);
await page.type('#ctl00_ContentPlaceHolder1_txtChaveAcessoResumo', formPassword);
console.log('Waiting 3 seconds...');
await delay(3000);
console.log('Submitting form');
await page.click('#ctl00_ContentPlaceHolder1_btnConsultarHCaptcha');
console.log('Waiting 3 seconds...');
await delay(3000);
console.log('Checking if we are on correct page');
let currentUrl = await page.evaluate(async () => {
return new Promise((resolve => {
resolve(document.location.href);
}))
});
if (currentUrl === checkUrl) {
console.log('SUCCESS: We are on the target page ' + checkUrl);
success++;
} else {
console.log('FAIL: We are not on the target page: ' + currentUrl);
await anticaptcha.reportIncorrectHcaptcha();
fail++;
}
console.log('success = ',success);
console.log('fail = ',fail);
await delay(3000);
await browser.close();
}
})();
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}