captcha.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import axios, { AxiosInstance } from 'axios'
  2. import dotenv from 'dotenv'
  3. import polly from 'polly-js'
  4. import { solveChallenge, solveChallengeWorkers } from 'shawnlu96-altcha-lib'
  5. import * as os from 'node:os'
  6. dotenv.config()
  7. const cpus = os.cpus()
  8. console.log('current cores', cpus.length)
  9. const googlekey = '6LdLhtYpAAAAAOe1xmceNR-i6MTtzq7N6AYztoVI'
  10. const hCaptchaKey = '04d28d90-d5b9-4a90-94e5-a12c595bd4e2'
  11. const captchaApiKey = process.env.CAPTCHA_API_KEY
  12. export async function getRecaptcha(address: string) {
  13. return await polly()
  14. .waitAndRetry(2)
  15. .executeForPromise(async () => {
  16. const res = await axios.post('https://2captcha.com/in.php', {
  17. method: 'userrecaptcha',
  18. key: captchaApiKey,
  19. googlekey,
  20. pageUrl: `https://faucet.testnet.initia.xyz/?address=${address}`,
  21. json: 1,
  22. action: 'verify',
  23. })
  24. const request = res.data.request
  25. return await polly()
  26. .waitAndRetry([10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000])
  27. .executeForPromise(async () => {
  28. const captchaRes = await axios.get(
  29. `https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${request}&json=1`,
  30. )
  31. const token = captchaRes.data.request
  32. console.log(token)
  33. if (token === 'CAPCHA_NOT_READY') throw new Error('CAPCHA_NOT_READY')
  34. if (!token) throw new Error('no result')
  35. return token
  36. })
  37. })
  38. }
  39. export async function getAltchaPayload(client: AxiosInstance) {
  40. return await polly()
  41. .waitAndRetry(5)
  42. .executeForPromise(async info => {
  43. console.log('altcha retry:', info.count)
  44. const challengeResp = await client.get(
  45. 'https://faucet-api.initiation-1.initia.xyz/create_challenge',
  46. )
  47. const challenge = challengeResp.data
  48. // console.log(challenge)
  49. const solution = await solveChallengeWorkers(
  50. './build/src/faucet/worker.js',
  51. cpus.length / 2,
  52. challenge.challenge,
  53. challenge.salt,
  54. challenge.max,
  55. challenge.start,
  56. )
  57. const payload = {
  58. algorithm: 'SHA-256',
  59. challenge: challenge.challenge,
  60. number: solution.number,
  61. salt: challenge.salt,
  62. signature: challenge.signature,
  63. took: solution.took,
  64. }
  65. console.log('challenge resolved')
  66. return btoa(JSON.stringify(payload))
  67. })
  68. }
  69. export async function solveHCaptcha(address: string) {
  70. return await polly()
  71. .waitAndRetry(2)
  72. .executeForPromise(async () => {
  73. const res = await axios.post('https://2captcha.com/in.php', {
  74. method: 'hcaptcha',
  75. sitekey: hCaptchaKey,
  76. key: captchaApiKey,
  77. pageUrl: `https://faucet.testnet.initia.xyz/?address=${address}`,
  78. json: 1,
  79. })
  80. const request = res.data.request
  81. return await polly()
  82. .waitAndRetry([10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000])
  83. .executeForPromise(async () => {
  84. const captchaRes = await axios.get(
  85. `https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${request}&json=1`,
  86. )
  87. const token = captchaRes.data.request
  88. // console.log(token)
  89. if (token === 'CAPCHA_NOT_READY') throw new Error('CAPCHA_NOT_READY')
  90. if (!token) throw new Error('no result')
  91. console.log('hCaptcha solved')
  92. return token
  93. })
  94. })
  95. }