This commit is contained in:
spiduler
2020-11-05 11:26:18 +09:00
parent 443a568a86
commit 294ca78b71
2 changed files with 90 additions and 54 deletions

View File

@@ -0,0 +1,87 @@
const logger = require("logops")
, { By } = require('selenium-webdriver')
, url = require('url')
, fetch = require('node-fetch')
class CaptchaComp {
constructor() {
this.api = {
in: 'https://2captcha.com/in.php',
res: 'https://2captcha.com/res.php',
key: "62fc5ef52fd4befe266d224796a7ea6f",
retryTimeout: 5000
}
this.params = {
in: {
key: this.api.key,
method: 'userrecaptcha',
googlekey: null,
pageurl: null,
json: 1
},
res: {
key: this.api.key,
json: 1,
action: 'get',
id: null
}
}
}
isCaptchaPage(html) {
return html.indexOf('grecaptcha') > -1 || html.indexOf('www.google.com/recaptcha/api2/anchor') > -1
}
getUrl(params) {
if (params.id) {
let params = Object.assign(this.params.res, { id: id })
let o = new url.URL(this.api.res)
o.search = new url.URLSearchParams(params).toString();
return o
}
let params = Object.assign(this.params.in, { googlekey: params.googlekey, pageurl: params.pageurl })
let o = new url.URL(this.api.in)
o.search = new url.URLSearchParams(params).toString();
return o
}
resolveCaptcha(driver) {
return driver.findElement(By.css('#grecaptcha iframe')).getAttribute('src')
.then(src => url.parse(src, true).query.k)
.then(googlekey => {
return driver.getCurrentUrl().then(pageurl => {
return fetch(this.getUrl({ googlekey: googlekey, pageurl: pageurl }), {
method: 'GET',
headers: { "Content-Type": "application/json" }
}).then(res => res.json()).then(res => {
logger.debug(res, '2captcha IN-request response')
if (!res.hasOwnProperty('request')) {
throw new Error('2captcha IN-request response has no data request id : ' + JSON.stringify(res))
}
return res.request
})
})
})
.then(async id => {
logger.info('Request 2captcha solving result after 20 senconds')
await this.sleep(1000 * 20)
let res = await fetch(this.getUrl({ id: id })).then(res => res.json())
let maxRetry = 10
while (res.request == 'CAPCHA_NOT_READY') {
await this.sleep(this.api.retryTimeout)
maxRetry = maxRetry - 1
logger.info('2captcha still working so request solving result after 5 senconds')
res = await fetch(this.getUrl({ id: id })).then(res => res.json())
if (maxRetry == 0) break
}
return res
})
.then(res => {
return driver.executeScript('captchaCallback("' + res.request + '")').then(() => this.sleep(1000 * 5))
})
}
}
module.exports = new CaptchaComp

View File

@@ -1,9 +1,7 @@
const logger = require("logops")
, config = require('../../config')
, { By, until } = require('selenium-webdriver')
, url = require('url')
, captcha = require('../component/CaptchaComp')
, poolingService = require('../service/PoolingService')
, fetch = require('node-fetch')
class SeleniumBrowser {
@@ -21,7 +19,7 @@ class SeleniumBrowser {
let html = await driver.get(uri)
.then(() => driver.findElement(By.css('body')))
.then(body => body.getAttribute('innerHTML'))
let htmlPromise = this.isCaptchaPage(html) ? this.resolveCaptcha(driver) : this.getHtml(driver, html, params)
let htmlPromise = captcha.isCaptchaPage(html) ? captcha.resolveCaptcha(driver) : this.getHtml(driver, html, params)
html = await htmlPromise
this.pool.release(driver)
return html
@@ -31,14 +29,6 @@ class SeleniumBrowser {
}
}
isCaptchaPage(html) {
return html.indexOf('grecaptcha') > -1 || html.indexOf('www.google.com/recaptcha/api2/anchor') > -1
}
getCaptchaRequestUri(id) {
return 'https://2captcha.com/res.php?key=62fc5ef52fd4befe266d224796a7ea6f&json=1&action=get&id=' + id
}
getHtml(driver, html, params) {
if (params.waitElements) {
return driver.get(params.uri)
@@ -53,47 +43,6 @@ class SeleniumBrowser {
}
}
resolveCaptcha(driver) {
return driver.findElement(By.css('#grecaptcha iframe')).getAttribute('src')
.then(async src => {
return {
"key": "62fc5ef52fd4befe266d224796a7ea6f",
"method": "userrecaptcha",
"googlekey": url.parse(src, true).query.k,
"pageurl": await driver.getCurrentUrl(),
"json": 1
}
})
.then(params => {
let requestUrl = new url.URL('https://2captcha.com/in.php')
requestUrl.search = new url.URLSearchParams(params).toString();
return fetch(requestUrl).then(res => res.json()).then(res => {
logger.debug(res, '2captcha IN-request response')
if (!res.hasOwnProperty('request')) {
throw new Error('2captcha IN-request response has no data request id : ' + JSON.stringify(res))
}
return res.request
})
})
.then(async id => {
logger.info('Request 2captcha solving result after 20 senconds')
await this.sleep(1000 * 20)
let res = await fetch(this.getCaptchaRequestUri(id)).then(res => res.json())
let maxRetry = 10
while (res.request == 'CAPCHA_NOT_READY') {
await this.sleep(1000 * 5)
maxRetry = maxRetry - 1
logger.info('2captcha still working so request solving result after 5 senconds')
res = await fetch(this.getCaptchaRequestUri(id)).then(res => res.json())
if (maxRetry == 0) break
}
return res
})
.then(res => {
return driver.executeScript('captchaCallback("' + res.request + '")').then(() => this.sleep(1000 * 5))
})
}
close() {
return this.pool.drain()
}