You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

115 lines
3.3 KiB

  1. import axios from 'axios'
  2. export class TwoCaptcha {
  3. private readonly CAPTCHA_KEY: string = 'TWO_CAPTCHA_KEY'
  4. private readonly CAPTCHA_CREATE: string = 'http://2captcha.com/in.php'
  5. private readonly CAPTCHA_RESULT: string = 'http://2captcha.com/res.php'
  6. private readonly CAPTCHA_BALANCE: string = 'https://2captcha.com/res.php'
  7. async resolveCaptcha (imageData: string) {
  8. const captchaId = await this._sendCaptcha(imageData)
  9. const captchaResolved = await this._getResponseCaptcha(captchaId)
  10. return captchaResolved
  11. }
  12. public async temSaldoDisponivel (): Promise<boolean> {
  13. try {
  14. const balanceData = {
  15. key: this.CAPTCHA_KEY,
  16. action: 'getbalance',
  17. json: 1
  18. }
  19. const { data: { request } } = await axios.get(this.CAPTCHA_BALANCE, { params: balanceData });
  20. if ((request || '').includes('ERROR_')) {
  21. console.log(`[consultar-saldo-captcha] - ${request}`);
  22. return false;
  23. }
  24. return Number(request) > 0;
  25. } catch (error) {
  26. console.log(`[consultar-saldo-captcha] - ${error.message}`);
  27. return false;
  28. }
  29. }
  30. private async _sendCaptcha (imageData: string): Promise<string> {
  31. try {
  32. const captchaResponse = await axios.post(
  33. this.CAPTCHA_CREATE,
  34. {
  35. method: 'base64',
  36. key: this.CAPTCHA_KEY,
  37. body: imageData,
  38. json: 1
  39. },
  40. { headers: { 'Content-Type': 'multipart/form-data' } }
  41. )
  42. let response: string = ''
  43. if (captchaResponse.data && typeof captchaResponse.data === 'object') {
  44. if (captchaResponse.data.status === 0)
  45. throw new Error(captchaResponse.data.request)
  46. response = captchaResponse.data.request
  47. } else if (
  48. captchaResponse.data &&
  49. typeof captchaResponse.data === 'string'
  50. ) {
  51. if (captchaResponse.data.indexOf('OK') === -1)
  52. throw new Error(captchaResponse.data)
  53. response = captchaResponse.data.split('|')[1]
  54. }
  55. return response
  56. } catch (error) {
  57. return new Promise((resolve, reject) => reject(error))
  58. }
  59. }
  60. private async _getResponseCaptcha (captchaId: string): Promise<string> {
  61. return new Promise((resolve, reject) => {
  62. const interval = setInterval(async () => {
  63. try {
  64. const serverResponse = await axios.get(this.CAPTCHA_RESULT, {
  65. params: {
  66. key: this.CAPTCHA_KEY,
  67. action: 'get',
  68. id: captchaId,
  69. json: 1
  70. }
  71. })
  72. if (serverResponse.data.request !== 'CAPCHA_NOT_READY') {
  73. clearInterval(interval)
  74. let response = ''
  75. if (
  76. serverResponse.data &&
  77. typeof serverResponse.data === 'object'
  78. ) {
  79. if (serverResponse.data.status === 0)
  80. throw new Error(serverResponse.data.request)
  81. response = serverResponse.data.request
  82. } else if (
  83. serverResponse.data &&
  84. typeof serverResponse.data === 'string'
  85. ) {
  86. if (serverResponse.data.indexOf('OK') === -1)
  87. throw new Error(serverResponse.data)
  88. response = serverResponse.data.split('|')[1]
  89. }
  90. resolve(response)
  91. }
  92. } catch (e) {
  93. reject(e)
  94. }
  95. }, 10 * 1000)
  96. })
  97. }
  98. }