Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: registration + add captcha support #227

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions Example/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import NodeCache from 'node-cache'
import readline from 'readline'
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, getAggregateVotesInPollMessage, makeCacheableSignalKeyStore, makeInMemoryStore, PHONENUMBER_MCC, proto, useMultiFileAuthState, WAMessageContent, WAMessageKey } from '../src'
import MAIN_LOGGER from '../src/Utils/logger'
import open from 'open'
import fs from 'fs'

const logger = MAIN_LOGGER.child({})
logger.level = 'trace'
Expand Down Expand Up @@ -92,21 +94,38 @@ const startSock = async() => {
}
}

async function enterCaptcha() {
const response = await sock.requestRegistrationCode({ ...registration, method: 'captcha' })
const path = __dirname + '/captcha.png'
fs.writeFileSync(path, Buffer.from(response.image_blob!, 'base64'))

open(path)
const code = await question('Please enter the captcha code:\n')
fs.unlinkSync(path)
registration.captcha = code.replace(/["']/g, '').trim().toLowerCase()
}

async function askForOTP() {
let code = await question('How would you like to receive the one time code for registration? "sms" or "voice"\n')
code = code.replace(/["']/g, '').trim().toLowerCase()
if (!registration.method) {
let code = await question('How would you like to receive the one time code for registration? "sms" or "voice"\n')
code = code.replace(/["']/g, '').trim().toLowerCase()
if(code !== 'sms' && code !== 'voice') {
return await askForOTP()
}

if(code !== 'sms' && code !== 'voice') {
return await askForOTP()
registration.method = code
}

registration.method = code

try {
await sock.requestRegistrationCode(registration)
await enterCode()
} catch(error) {
console.error('Failed to request registration code. Please try again.\n', error)

if(error?.reason === 'code_checkpoint') {
await enterCaptcha()
}

await askForOTP()
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"jest": "^27.0.6",
"jimp": "^0.16.1",
"link-preview-js": "^3.0.0",
"open": "^8.4.2",
"qrcode-terminal": "^0.12.0",
"release-it": "^15.10.3",
"sharp": "^0.30.5",
Expand Down
4 changes: 2 additions & 2 deletions src/Defaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export const PHONE_CONNECTION_CB = 'CB:Pong'

export const WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60

export const MOBILE_TOKEN = Buffer.from('0a1mLfGUIBVrMKF1RdvLI5lkRBvof6vn0fD2QRSM4174c0243f5277a5d7720ce842cc4ae6')
export const MOBILE_TOKEN = Buffer.from('0a1mLfGUIBVrMKF1RdvLI5lkRBvof6vn0fD2QRSMc46ab726e8fb8c66811b0b95c014e262')
export const MOBILE_REGISTRATION_ENDPOINT = 'https://v.whatsapp.net/v2'
export const MOBILE_USERAGENT = 'WhatsApp/2.22.24.81 iOS/15.3.1 Device/Apple-iPhone_7'
export const MOBILE_USERAGENT = 'WhatsApp/2.23.12.75 iOS/15.3.1 Device/Apple-iPhone_7'
export const REGISTRATION_PUBLIC_KEY = Buffer.from([
5, 142, 140, 15, 116, 195, 235, 197, 215, 166, 134, 92, 108, 60, 132, 56, 86, 176, 97, 33, 204, 232, 234, 119, 77,
34, 251, 111, 18, 37, 18, 48, 45,
Expand Down
6 changes: 3 additions & 3 deletions src/Signal/libsignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GroupCipher, GroupSessionBuilder, SenderKeyDistributionMessage, SenderK
import { SignalAuthState } from '../Types'
import { SignalRepository } from '../Types/Signal'
import { generateSignalPubKey } from '../Utils'
import { jidDecode } from '../WABinary'

export function makeLibSignalRepository(auth: SignalAuthState): SignalRepository {
const storage = signalStorage(auth)
Expand Down Expand Up @@ -76,10 +77,9 @@ export function makeLibSignalRepository(auth: SignalAuthState): SignalRepository
}
}

const jidToSignalAddress = (jid: string) => jid.split('@')[0]

const jidToSignalProtocolAddress = (jid: string) => {
return new libsignal.ProtocolAddress(jidToSignalAddress(jid), 0)
const { user, device } = jidDecode(jid)!
return new libsignal.ProtocolAddress(user, device || 0)
}

const jidToSignalSenderKeyName = (group: string, user: string): string => {
Expand Down
18 changes: 11 additions & 7 deletions src/Socket/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export interface RegistrationOptions {
/**
* How to send the one time code
*/
method?: 'sms' | 'voice'
method?: 'sms' | 'voice' | 'captcha'
/**
* The captcha code if it was requested
*/
captcha?: string
}

export type RegistrationParams = RegistrationData & RegistrationOptions
Expand Down Expand Up @@ -136,6 +140,7 @@ export function registrationParams(params: RegistrationParams) {
id: convertBufferToUrlHex(params.identityId),
backup_token: convertBufferToUrlHex(params.backupToken),
token: md5(Buffer.concat([MOBILE_TOKEN, Buffer.from(params.phoneNumberNationalNumber)])).toString('hex'),
fraud_checkpoint_code: params.captcha,
}
}

Expand Down Expand Up @@ -196,13 +201,10 @@ export async function mobileRegisterFetch(path: string, opts: AxiosRequestConfig
const parameter = [] as string[]

for(const param in opts.params) {
if (opts.params[param] == null) continue;
parameter.push(param + '=' + urlencode(opts.params[param]))
}

console.log('parameter', opts.params, parameter)

// const params = urlencode(mobileRegisterEncrypt(parameter.join('&')))
// url += `?ENC=${params}`
url += `?${parameter.join('&')}`
delete opts.params
}
Expand Down Expand Up @@ -230,16 +232,18 @@ export async function mobileRegisterFetch(path: string, opts: AxiosRequestConfig


export interface ExistsResponse {
status: 'fail'
status: 'fail' | 'sent'
voice_length?: number
voice_wait?: number
sms_length?: number
sms_wait?: number
reason?: 'incorrect' | 'missing_param'
reason?: 'incorrect' | 'missing_param' | 'code_checkpoint'
login?: string
flash_type?: number
ab_hash?: string
ab_key?: string
exp_cfg?: string
lid?: string
image_blob?: string
audio_blob?: string
}