mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
refactor: cleanup MD socket initial connect code + log more
This commit is contained in:
@@ -9,6 +9,8 @@ import { AuthenticationCreds, BaileysEventEmitter, DisconnectReason, SocketConfi
|
||||
import { addTransactionCapability, bindWaitForConnectionUpdate, configureSuccessfulPairing, Curve, encodeBigEndian, generateLoginNode, generateOrGetPreKeys, generateRegistrationNode, getPreKeys, makeNoiseHandler, printQRIfNecessaryListener, promiseTimeout, useSingleFileAuthState, xmppPreKey, xmppSignedPreKey } from '../Utils'
|
||||
import { assertNodeErrorFree, BinaryNode, encodeBinaryNode, getBinaryNodeChild, S_WHATSAPP_NET } from '../WABinary'
|
||||
|
||||
const INITIAL_PREKEY_COUNT = 30
|
||||
|
||||
/**
|
||||
* Connects to WA servers and performs:
|
||||
* - simple queries (no retry mechanism, wait for connection establishment)
|
||||
@@ -172,30 +174,34 @@ export const makeSocket = ({
|
||||
|
||||
/** connection handshake */
|
||||
const validateConnection = async() => {
|
||||
logger.info({ browser }, 'connected to WA Web')
|
||||
|
||||
const init = proto.HandshakeMessage.encode({
|
||||
const helloMsg: proto.IHandshakeMessage = {
|
||||
clientHello: { ephemeral: ephemeralKeyPair.public }
|
||||
}).finish()
|
||||
}
|
||||
|
||||
logger.info({ browser, helloMsg }, 'connected to WA Web')
|
||||
|
||||
const init = proto.HandshakeMessage.encode(helloMsg).finish()
|
||||
|
||||
const result = await awaitNextMessage(init)
|
||||
const handshake = proto.HandshakeMessage.decode(result)
|
||||
|
||||
logger.debug('handshake recv from WA Web')
|
||||
logger.trace({ handshake }, 'handshake recv from WA Web')
|
||||
|
||||
const keyEnc = noise.processHandshake(handshake, creds.noiseKey)
|
||||
logger.info('handshake complete')
|
||||
|
||||
let node: Uint8Array
|
||||
let node: proto.IClientPayload
|
||||
if(!creds.me) {
|
||||
logger.info('not logged in, attempting registration...')
|
||||
node = generateRegistrationNode(creds, { version, browser })
|
||||
logger.info({ node }, 'not logged in, attempting registration...')
|
||||
} else {
|
||||
logger.info('logging in...')
|
||||
node = generateLoginNode(creds.me!.id, { version, browser })
|
||||
logger.info({ node }, 'logging in...')
|
||||
}
|
||||
|
||||
const payloadEnc = noise.encrypt(node)
|
||||
const payloadEnc = noise.encrypt(
|
||||
proto.ClientPayload.encode(node).finish()
|
||||
)
|
||||
await sendRawMessage(
|
||||
proto.HandshakeMessage.encode({
|
||||
clientFinish: {
|
||||
@@ -234,7 +240,7 @@ export const makeSocket = ({
|
||||
|
||||
/** generates and uploads a set of pre-keys */
|
||||
const uploadPreKeys = async() => {
|
||||
await assertingPreKeys(30, async preKeys => {
|
||||
await assertingPreKeys(INITIAL_PREKEY_COUNT, async preKeys => {
|
||||
const node: BinaryNode = {
|
||||
tag: 'iq',
|
||||
attrs: {
|
||||
|
||||
@@ -7,6 +7,8 @@ import { Curve, hmacSign } from './crypto'
|
||||
import { encodeInt } from './generics'
|
||||
import { createSignalIdentity } from './signal'
|
||||
|
||||
type ClientPayloadConfig = Pick<SocketConfig, 'version' | 'browser'>
|
||||
|
||||
const getUserAgent = ({ version }: Pick<SocketConfig, 'version'>): proto.IUserAgent => ({
|
||||
appVersion: {
|
||||
primary: version[0],
|
||||
@@ -29,23 +31,29 @@ const getWebInfo = (): proto.IWebInfo => ({
|
||||
webSubPlatform: proto.WebInfo.WebInfoWebSubPlatform.WEB_BROWSER
|
||||
})
|
||||
|
||||
export const generateLoginNode = (userJid: string, config: Pick<SocketConfig, 'version' | 'browser'>) => {
|
||||
const { user, device } = jidDecode(userJid)
|
||||
const payload: proto.IClientPayload = {
|
||||
const getClientPayload = (config: ClientPayloadConfig): proto.IClientPayload => {
|
||||
return {
|
||||
passive: true,
|
||||
connectType: proto.ClientPayload.ClientPayloadConnectType.WIFI_UNKNOWN,
|
||||
connectReason: proto.ClientPayload.ClientPayloadConnectReason.USER_ACTIVATED,
|
||||
userAgent: getUserAgent(config),
|
||||
webInfo: getWebInfo(),
|
||||
}
|
||||
}
|
||||
|
||||
export const generateLoginNode = (userJid: string, config: ClientPayloadConfig): proto.IClientPayload => {
|
||||
const { user, device } = jidDecode(userJid)
|
||||
const payload: proto.IClientPayload = {
|
||||
...getClientPayload(config),
|
||||
username: +user,
|
||||
device: device,
|
||||
}
|
||||
return proto.ClientPayload.encode(payload).finish()
|
||||
return payload
|
||||
}
|
||||
|
||||
export const generateRegistrationNode = (
|
||||
{ registrationId, signedPreKey, signedIdentityKey }: SignalCreds,
|
||||
config: Pick<SocketConfig, 'version' | 'browser'>
|
||||
config: ClientPayloadConfig
|
||||
) => {
|
||||
// the app version needs to be md5 hashed
|
||||
// and passed in
|
||||
@@ -68,9 +76,7 @@ export const generateRegistrationNode = (
|
||||
const companionProto = proto.CompanionProps.encode(companion).finish()
|
||||
|
||||
const registerPayload: proto.IClientPayload = {
|
||||
connectReason: proto.ClientPayload.ClientPayloadConnectReason.USER_ACTIVATED,
|
||||
connectType: proto.ClientPayload.ClientPayloadConnectType.WIFI_UNKNOWN,
|
||||
passive: false,
|
||||
...getClientPayload(config),
|
||||
regData: {
|
||||
buildHash: appVersionBuf,
|
||||
companionProps: companionProto,
|
||||
@@ -81,11 +87,9 @@ export const generateRegistrationNode = (
|
||||
eSkeyVal: signedPreKey.keyPair.public,
|
||||
eSkeySig: signedPreKey.signature,
|
||||
},
|
||||
userAgent: getUserAgent(config),
|
||||
webInfo: getWebInfo(),
|
||||
}
|
||||
|
||||
return proto.ClientPayload.encode(registerPayload).finish()
|
||||
return registerPayload
|
||||
}
|
||||
|
||||
export const configureSuccessfulPairing = (
|
||||
|
||||
Reference in New Issue
Block a user