mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
unify web + mobile and use tcp socket
This commit is contained in:
@@ -564,7 +564,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
if(msg.messageStubType === proto.WebMessageInfo.StubType.CIPHERTEXT) {
|
||||
retryMutex.mutex(
|
||||
async() => {
|
||||
if(ws.readyState === ws.OPEN) {
|
||||
if(ws.isOpen) {
|
||||
const encNode = getBinaryNodeChild(node, 'enc')
|
||||
await sendRetryRequest(node, !encNode)
|
||||
if(retryRequestDelayMs) {
|
||||
|
||||
@@ -6,10 +6,6 @@ export class MobileSocket extends Socket {
|
||||
constructor(public config: SocketConfig) {
|
||||
super()
|
||||
|
||||
if(config.auth.creds.registered) {
|
||||
this.connect()
|
||||
}
|
||||
|
||||
this.on('data', (d) => {
|
||||
this.emit('message', d)
|
||||
})
|
||||
|
||||
@@ -8,7 +8,6 @@ import { addTransactionCapability, bindWaitForConnectionUpdate, configureSuccess
|
||||
import { makeEventBuffer } from '../Utils/event-buffer'
|
||||
import { assertNodeErrorFree, BinaryNode, binaryNodeToString, encodeBinaryNode, getBinaryNodeChild, getBinaryNodeChildren, S_WHATSAPP_NET } from '../WABinary'
|
||||
import { MobileSocket } from './mobile-socket'
|
||||
import { WebSocket } from './web-socket'
|
||||
|
||||
/**
|
||||
* Connects to WA servers and performs:
|
||||
@@ -32,9 +31,17 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
} = config
|
||||
|
||||
config.mobile = config.mobile || config.auth.creds.registered
|
||||
const ws = config.mobile ? new MobileSocket(config) : new WebSocket(config)
|
||||
const ws = new MobileSocket(config)
|
||||
ws.setMaxListeners(0)
|
||||
|
||||
if(!config.mobile) {
|
||||
// if not mobile -> auto connect
|
||||
ws.connect()
|
||||
} else if(config.auth.creds.registered) {
|
||||
// if mobile and registered -> auto connect
|
||||
ws.connect()
|
||||
}
|
||||
|
||||
const ev = makeEventBuffer(logger)
|
||||
/** ephemeral key pair used to encrypt/decrypt communication. Unique for each connection */
|
||||
const ephemeralKeyPair = Curve.generateKeyPair()
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import { WebSocket as WS } from 'ws'
|
||||
import { DEFAULT_ORIGIN } from '../Defaults'
|
||||
import { SocketConfig } from '../Types'
|
||||
|
||||
export class WebSocket extends WS {
|
||||
constructor(public config: SocketConfig) {
|
||||
super(config.waWebSocketUrl, undefined, {
|
||||
origin: DEFAULT_ORIGIN,
|
||||
headers: config.options.headers as Record<string, string>,
|
||||
handshakeTimeout: config.connectTimeoutMs,
|
||||
timeout: config.connectTimeoutMs,
|
||||
agent: config.agent,
|
||||
})
|
||||
}
|
||||
|
||||
get isOpen() {
|
||||
return this.readyState === WS.OPEN
|
||||
}
|
||||
|
||||
get isClosed() {
|
||||
return this.readyState === WS.CLOSED
|
||||
}
|
||||
|
||||
get isClosing() {
|
||||
return this.readyState === WS.CLOSING
|
||||
}
|
||||
|
||||
get isConnecting() {
|
||||
return this.readyState === WS.CONNECTING
|
||||
}
|
||||
}
|
||||
@@ -213,6 +213,6 @@ export const initAuthCreds = (): AuthenticationCreds => {
|
||||
identityId: randomBytes(20),
|
||||
registered: false,
|
||||
backupToken: randomBytes(20),
|
||||
registration: {} as RegistrationOptions
|
||||
registration: {} as never
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ const getUserAgent = (config: SocketConfig): proto.ClientPayload.IUserAgent => {
|
||||
const version = config.mobile ? [2, 22, 24] : config.version
|
||||
const device = config.mobile ? 'iPhone_7' : 'Desktop'
|
||||
const manufacturer = config.mobile ? 'Apple' : ''
|
||||
const platform = config.mobile ? proto.ClientPayload.UserAgent.Platform.IOS : proto.ClientPayload.UserAgent.Platform.WEB
|
||||
const platform = config.mobile ? proto.ClientPayload.UserAgent.Platform.IOS : proto.ClientPayload.UserAgent.Platform.MACOS
|
||||
const phoneId = config.mobile ? { phoneId: config.auth.creds.phoneId } : {}
|
||||
|
||||
return {
|
||||
@@ -36,20 +36,6 @@ const getUserAgent = (config: SocketConfig): proto.ClientPayload.IUserAgent => {
|
||||
}
|
||||
}
|
||||
|
||||
const PLATFORM_MAP = {
|
||||
'Mac OS': proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
|
||||
'Windows': proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
|
||||
}
|
||||
|
||||
const getWebInfo = (config: SocketConfig): proto.ClientPayload.IWebInfo => {
|
||||
let webSubPlatform = proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER
|
||||
if(config.syncFullHistory && PLATFORM_MAP[config.browser[0]]) {
|
||||
webSubPlatform = PLATFORM_MAP[config.browser[0]]
|
||||
}
|
||||
|
||||
return { webSubPlatform }
|
||||
}
|
||||
|
||||
const getClientPayload = (config: SocketConfig) => {
|
||||
const payload: proto.IClientPayload = {
|
||||
connectType: proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
|
||||
@@ -57,10 +43,6 @@ const getClientPayload = (config: SocketConfig) => {
|
||||
userAgent: getUserAgent(config),
|
||||
}
|
||||
|
||||
if(!config.mobile) {
|
||||
payload.webInfo = getWebInfo(config)
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user