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 */
|
/** 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
|
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 = {
|
export const WA_CERT_DETAILS = {
|
||||||
SERIAL: 0
|
SERIAL: 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import type { LIDMapping, SignalAuthState, SignalKeyStoreWithTransaction } from
|
|||||||
import type { SignalRepositoryWithLIDStore } from '../Types/Signal'
|
import type { SignalRepositoryWithLIDStore } from '../Types/Signal'
|
||||||
import { generateSignalPubKey } from '../Utils'
|
import { generateSignalPubKey } from '../Utils'
|
||||||
import type { ILogger } from '../Utils/logger'
|
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 type { SenderKeyStore } from './Group/group_cipher'
|
||||||
import { SenderKeyName } from './Group/sender-key-name'
|
import { SenderKeyName } from './Group/sender-key-name'
|
||||||
import { SenderKeyRecord } from './Group/sender-key-record'
|
import { SenderKeyRecord } from './Group/sender-key-record'
|
||||||
@@ -181,10 +181,10 @@ export function makeLibSignalRepository(
|
|||||||
toJid: string
|
toJid: string
|
||||||
): Promise<{ migrated: number; skipped: number; total: number }> {
|
): Promise<{ migrated: number; skipped: number; total: number }> {
|
||||||
// TODO: use usync to handle this entire mess
|
// 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
|
// Only support PN to LID migration
|
||||||
if (!fromJid.includes('@s.whatsapp.net')) {
|
if (!isPnUser(fromJid) && !isHostedPnUser(fromJid)) {
|
||||||
return { migrated: 0, skipped: 0, total: 1 }
|
return { migrated: 0, skipped: 0, total: 1 }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,7 +222,10 @@ export function makeLibSignalRepository(
|
|||||||
const deviceStr = sessionKey.split('.')[1]
|
const deviceStr = sessionKey.split('.')[1]
|
||||||
if (!deviceStr) continue
|
if (!deviceStr) continue
|
||||||
const deviceNum = parseInt(deviceStr)
|
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)
|
deviceJids.push(jid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1165,6 +1165,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
awaitingSyncTimeout = setTimeout(() => {
|
awaitingSyncTimeout = setTimeout(() => {
|
||||||
if (syncState === SyncState.AwaitingInitialSync) {
|
if (syncState === SyncState.AwaitingInitialSync) {
|
||||||
|
// TODO: investigate
|
||||||
logger.warn('Timeout in AwaitingInitialSync, forcing state to Online and flushing buffer')
|
logger.warn('Timeout in AwaitingInitialSync, forcing state to Online and flushing buffer')
|
||||||
syncState = SyncState.Online
|
syncState = SyncState.Online
|
||||||
ev.flush()
|
ev.flush()
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ import {
|
|||||||
jidDecode,
|
jidDecode,
|
||||||
jidEncode,
|
jidEncode,
|
||||||
jidNormalizedUser,
|
jidNormalizedUser,
|
||||||
|
type JidServer,
|
||||||
type JidWithDevice,
|
type JidWithDevice,
|
||||||
S_WHATSAPP_NET
|
S_WHATSAPP_NET
|
||||||
} from '../WABinary'
|
} from '../WABinary'
|
||||||
@@ -246,7 +247,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
})
|
})
|
||||||
.filter(jid => jid !== null)
|
.filter(jid => jid !== null)
|
||||||
|
|
||||||
let mgetDevices: undefined | Record<string, JidWithDevice[] | undefined>
|
let mgetDevices: undefined | Record<string, FullJid[] | undefined>
|
||||||
|
|
||||||
if (useCache && userDevicesCache.mget) {
|
if (useCache && userDevicesCache.mget) {
|
||||||
const usersToFetch = jidsWithUser.map(j => j?.user).filter(Boolean) as string[]
|
const usersToFetch = jidsWithUser.map(j => j?.user).filter(Boolean) as string[]
|
||||||
@@ -257,12 +258,11 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
if (useCache) {
|
if (useCache) {
|
||||||
const devices =
|
const devices =
|
||||||
mgetDevices?.[user!] ||
|
mgetDevices?.[user!] ||
|
||||||
(userDevicesCache.mget ? undefined : ((await userDevicesCache.get(user!)) as JidWithDevice[]))
|
(userDevicesCache.mget ? undefined : ((await userDevicesCache.get(user!)) as FullJid[]))
|
||||||
if (devices) {
|
if (devices) {
|
||||||
const isLidJid = jid.includes('@lid')
|
|
||||||
const devicesWithJid = devices.map(d => ({
|
const devicesWithJid = devices.map(d => ({
|
||||||
...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)
|
deviceResults.push(...devicesWithJid)
|
||||||
|
|
||||||
@@ -322,10 +322,9 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
// Process all devices for this user
|
// Process all devices for this user
|
||||||
for (const item of userDevices) {
|
for (const item of userDevices) {
|
||||||
const deterministicServer = getServerFromDomainType(item.server, item.domainType)
|
|
||||||
const finalJid = isLidUser
|
const finalJid = isLidUser
|
||||||
? jidEncode(user, deterministicServer, item.device)
|
? jidEncode(user, item.server, item.device)
|
||||||
: jidEncode(item.user, deterministicServer, item.device)
|
: jidEncode(item.user, item.server, item.device)
|
||||||
|
|
||||||
deviceResults.push({
|
deviceResults.push({
|
||||||
...item,
|
...item,
|
||||||
|
|||||||
+12
-7
@@ -16,6 +16,7 @@ import {
|
|||||||
getBinaryNodeChildBuffer,
|
getBinaryNodeChildBuffer,
|
||||||
getBinaryNodeChildren,
|
getBinaryNodeChildren,
|
||||||
getBinaryNodeChildUInt,
|
getBinaryNodeChildUInt,
|
||||||
|
getServerFromDomainType,
|
||||||
jidDecode,
|
jidDecode,
|
||||||
S_WHATSAPP_NET,
|
S_WHATSAPP_NET,
|
||||||
WAJIDDomains
|
WAJIDDomains
|
||||||
@@ -142,7 +143,7 @@ export const extractDeviceJids = (
|
|||||||
|
|
||||||
for (const userResult of result) {
|
for (const userResult of result) {
|
||||||
const { devices, id } = userResult as { devices: ParsedDeviceInfo; id: string }
|
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[]
|
const deviceList = devices?.deviceList as DeviceListData[]
|
||||||
if (Array.isArray(deviceList)) {
|
if (Array.isArray(deviceList)) {
|
||||||
for (const { id: device, keyIndex, isHosted } of 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
|
((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
|
(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({
|
extracted.push({
|
||||||
user,
|
user,
|
||||||
device,
|
device,
|
||||||
domainType: isHosted
|
domainType,
|
||||||
? domainType === WAJIDDomains.LID
|
server: getServerFromDomainType(server, domainType)
|
||||||
? WAJIDDomains.HOSTED_LID
|
|
||||||
: WAJIDDomains.HOSTED
|
|
||||||
: domainType,
|
|
||||||
server
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user