messages-send, signal: improve hosted device support + patch cache
This commit is contained in:
@@ -29,6 +29,7 @@ export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]) // last is
|
||||
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
|
||||
export const URL_REGEX = /https:\/\/(?![^:@\/\s]+:[^:@\/\s]+@)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?/g
|
||||
|
||||
// TODO: Add WA root CA
|
||||
export const WA_CERT_DETAILS = {
|
||||
SERIAL: 0
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { LIDMapping, SignalAuthState, SignalKeyStoreWithTransaction } from
|
||||
import type { SignalRepositoryWithLIDStore } from '../Types/Signal'
|
||||
import { generateSignalPubKey } from '../Utils'
|
||||
import type { ILogger } from '../Utils/logger'
|
||||
import { jidDecode, transferDevice, WAJIDDomains } from '../WABinary'
|
||||
import { isHostedLidUser, isHostedPnUser, isLidUser, isPnUser, jidDecode, transferDevice, WAJIDDomains } from '../WABinary'
|
||||
import type { SenderKeyStore } from './Group/group_cipher'
|
||||
import { SenderKeyName } from './Group/sender-key-name'
|
||||
import { SenderKeyRecord } from './Group/sender-key-record'
|
||||
@@ -181,10 +181,10 @@ export function makeLibSignalRepository(
|
||||
toJid: string
|
||||
): Promise<{ migrated: number; skipped: number; total: number }> {
|
||||
// TODO: use usync to handle this entire mess
|
||||
if (!fromJid || !toJid.includes('@lid')) return { migrated: 0, skipped: 0, total: 0 }
|
||||
if (!fromJid || (!isLidUser(toJid) && !isHostedLidUser(toJid))) return { migrated: 0, skipped: 0, total: 0 }
|
||||
|
||||
// Only support PN to LID migration
|
||||
if (!fromJid.includes('@s.whatsapp.net')) {
|
||||
if (!isPnUser(fromJid) && !isHostedPnUser(fromJid)) {
|
||||
return { migrated: 0, skipped: 0, total: 1 }
|
||||
}
|
||||
|
||||
@@ -222,7 +222,10 @@ export function makeLibSignalRepository(
|
||||
const deviceStr = sessionKey.split('.')[1]
|
||||
if (!deviceStr) continue
|
||||
const deviceNum = parseInt(deviceStr)
|
||||
const jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`
|
||||
let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`
|
||||
if (deviceNum == 99) {
|
||||
jid = `${user}:99@hosted`
|
||||
}
|
||||
deviceJids.push(jid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1165,6 +1165,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
|
||||
awaitingSyncTimeout = setTimeout(() => {
|
||||
if (syncState === SyncState.AwaitingInitialSync) {
|
||||
// TODO: investigate
|
||||
logger.warn('Timeout in AwaitingInitialSync, forcing state to Online and flushing buffer')
|
||||
syncState = SyncState.Online
|
||||
ev.flush()
|
||||
|
||||
@@ -51,6 +51,7 @@ import {
|
||||
jidDecode,
|
||||
jidEncode,
|
||||
jidNormalizedUser,
|
||||
type JidServer,
|
||||
type JidWithDevice,
|
||||
S_WHATSAPP_NET
|
||||
} from '../WABinary'
|
||||
@@ -246,7 +247,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
})
|
||||
.filter(jid => jid !== null)
|
||||
|
||||
let mgetDevices: undefined | Record<string, JidWithDevice[] | undefined>
|
||||
let mgetDevices: undefined | Record<string, FullJid[] | undefined>
|
||||
|
||||
if (useCache && userDevicesCache.mget) {
|
||||
const usersToFetch = jidsWithUser.map(j => j?.user).filter(Boolean) as string[]
|
||||
@@ -257,12 +258,11 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
if (useCache) {
|
||||
const devices =
|
||||
mgetDevices?.[user!] ||
|
||||
(userDevicesCache.mget ? undefined : ((await userDevicesCache.get(user!)) as JidWithDevice[]))
|
||||
(userDevicesCache.mget ? undefined : ((await userDevicesCache.get(user!)) as FullJid[]))
|
||||
if (devices) {
|
||||
const isLidJid = jid.includes('@lid')
|
||||
const devicesWithJid = devices.map(d => ({
|
||||
...d,
|
||||
jid: isLidJid ? jidEncode(d.user, 'lid', d.device) : jidEncode(d.user, 's.whatsapp.net', d.device)
|
||||
jid: jidEncode(d.user, d.server, d.device)
|
||||
}))
|
||||
deviceResults.push(...devicesWithJid)
|
||||
|
||||
@@ -322,10 +322,9 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
|
||||
// Process all devices for this user
|
||||
for (const item of userDevices) {
|
||||
const deterministicServer = getServerFromDomainType(item.server, item.domainType)
|
||||
const finalJid = isLidUser
|
||||
? jidEncode(user, deterministicServer, item.device)
|
||||
: jidEncode(item.user, deterministicServer, item.device)
|
||||
? jidEncode(user, item.server, item.device)
|
||||
: jidEncode(item.user, item.server, item.device)
|
||||
|
||||
deviceResults.push({
|
||||
...item,
|
||||
|
||||
+12
-7
@@ -16,6 +16,7 @@ import {
|
||||
getBinaryNodeChildBuffer,
|
||||
getBinaryNodeChildren,
|
||||
getBinaryNodeChildUInt,
|
||||
getServerFromDomainType,
|
||||
jidDecode,
|
||||
S_WHATSAPP_NET,
|
||||
WAJIDDomains
|
||||
@@ -142,7 +143,7 @@ export const extractDeviceJids = (
|
||||
|
||||
for (const userResult of result) {
|
||||
const { devices, id } = userResult as { devices: ParsedDeviceInfo; id: string }
|
||||
const { user, domainType, server } = jidDecode(id)!
|
||||
let { user, domainType, server } = jidDecode(id)!
|
||||
const deviceList = devices?.deviceList as DeviceListData[]
|
||||
if (Array.isArray(deviceList)) {
|
||||
for (const { id: device, keyIndex, isHosted } of deviceList) {
|
||||
@@ -151,15 +152,19 @@ export const extractDeviceJids = (
|
||||
((myUser !== user && myLid !== user) || myDevice !== device) && // either different user or if me user, not this device
|
||||
(device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
|
||||
) {
|
||||
if (isHosted) {
|
||||
if (domainType === WAJIDDomains.LID) {
|
||||
domainType = WAJIDDomains.HOSTED_LID
|
||||
} else {
|
||||
domainType = WAJIDDomains.HOSTED
|
||||
}
|
||||
}
|
||||
|
||||
extracted.push({
|
||||
user,
|
||||
device,
|
||||
domainType: isHosted
|
||||
? domainType === WAJIDDomains.LID
|
||||
? WAJIDDomains.HOSTED_LID
|
||||
: WAJIDDomains.HOSTED
|
||||
: domainType,
|
||||
server
|
||||
domainType,
|
||||
server: getServerFromDomainType(server, domainType)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user