@x4r1s3/baileys 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1262 -2
- package/SECURITY.md +47 -0
- package/WAProto/V +1 -0
- package/WAProto/WAProto.proto +8892 -0
- package/WAProto/index.d.ts +104128 -0
- package/WAProto/index.js +334920 -0
- package/assets/rolling-circle.svg +133 -0
- package/assets/x4r1s3-banner.svg +32 -0
- package/engine-requirements.js +27 -0
- package/lib/Builders/A2UI.js +245 -0
- package/lib/Builders/AIRich.js +179 -0
- package/lib/Builders/Button.js +108 -0
- package/lib/Builders/CFC.js +284 -0
- package/lib/Builders/Carousel.js +81 -0
- package/lib/Call/THIRD_PARTY_LICENSE.txt +21 -0
- package/lib/Call/audio-feeder.mjs +179 -0
- package/lib/Call/audio-recorder.mjs +250 -0
- package/lib/Call/fetch-wasm-resources.mjs +104 -0
- package/lib/Call/index.mjs +680 -0
- package/lib/Call/relay-transport.mjs +563 -0
- package/lib/Call/signaling.mjs +641 -0
- package/lib/Call/types.mjs +17 -0
- package/lib/Call/video-feeder.mjs +94 -0
- package/lib/Call/wasm-engine.mjs +1372 -0
- package/lib/Call/worker-bootstrap.mjs +1042 -0
- package/lib/Defaults/ban-checker.js +56 -0
- package/lib/Defaults/index.js +170 -0
- package/lib/Signal/Group/ciphertext-message.js +23 -0
- package/lib/Signal/Group/group-session-builder.js +41 -0
- package/lib/Signal/Group/group_cipher.js +93 -0
- package/lib/Signal/Group/index.js +23 -0
- package/lib/Signal/Group/keyhelper.js +29 -0
- package/lib/Signal/Group/sender-chain-key.js +37 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +74 -0
- package/lib/Signal/Group/sender-key-message.js +77 -0
- package/lib/Signal/Group/sender-key-name.js +59 -0
- package/lib/Signal/Group/sender-key-record.js +52 -0
- package/lib/Signal/Group/sender-key-state.js +95 -0
- package/lib/Signal/Group/sender-message-key.js +37 -0
- package/lib/Signal/libsignal.js +440 -0
- package/lib/Signal/lid-mapping.js +292 -0
- package/lib/Socket/Client/index.js +14 -0
- package/lib/Socket/Client/types.js +22 -0
- package/lib/Socket/Client/websocket.js +65 -0
- package/lib/Socket/aigroups.js +327 -0
- package/lib/Socket/ban-checker.js +165 -0
- package/lib/Socket/business.js +419 -0
- package/lib/Socket/chats.js +1921 -0
- package/lib/Socket/communities.js +443 -0
- package/lib/Socket/graphql.js +911 -0
- package/lib/Socket/groups.js +561 -0
- package/lib/Socket/index.js +49 -0
- package/lib/Socket/interactive-handler.js +612 -0
- package/lib/Socket/interop.js +575 -0
- package/lib/Socket/managed-account.js +220 -0
- package/lib/Socket/messages-recv.js +2191 -0
- package/lib/Socket/messages-send.js +4104 -0
- package/lib/Socket/mex.js +53 -0
- package/lib/Socket/newsletter.js +291 -0
- package/lib/Socket/privacy.js +458 -0
- package/lib/Socket/registration.js +440 -0
- package/lib/Socket/socket.js +1029 -0
- package/lib/Socket/status-actions.js +287 -0
- package/lib/Socket/username.js +241 -0
- package/lib/Store/index.js +16 -0
- package/lib/Store/make-cache-manager-store.js +101 -0
- package/lib/Store/make-in-memory-store.js +432 -0
- package/lib/Store/make-ordered-dictionary.js +90 -0
- package/lib/Store/object-repository.js +35 -0
- package/lib/Types/Auth.js +13 -0
- package/lib/Types/Bussines.js +13 -0
- package/lib/Types/Call.js +13 -0
- package/lib/Types/Chat.js +19 -0
- package/lib/Types/Contact.js +13 -0
- package/lib/Types/Events.js +13 -0
- package/lib/Types/GroupMetadata.js +13 -0
- package/lib/Types/Label.js +36 -0
- package/lib/Types/LabelAssociation.js +18 -0
- package/lib/Types/Message.js +30 -0
- package/lib/Types/Mex.js +53 -0
- package/lib/Types/Product.js +13 -0
- package/lib/Types/RichType.js +34 -0
- package/lib/Types/Signal.js +13 -0
- package/lib/Types/Socket.js +14 -0
- package/lib/Types/State.js +60 -0
- package/lib/Types/USync.js +13 -0
- package/lib/Types/index.js +37 -0
- package/lib/Utils/auth-utils.js +313 -0
- package/lib/Utils/ban-checker.js +48 -0
- package/lib/Utils/bot-capabilities.js +96 -0
- package/lib/Utils/bot-planning-replay.js +177 -0
- package/lib/Utils/browser-utils.js +40 -0
- package/lib/Utils/business.js +242 -0
- package/lib/Utils/c2582.js +147 -0
- package/lib/Utils/chat-utils.js +905 -0
- package/lib/Utils/companion-reg-client-utils.js +44 -0
- package/lib/Utils/consumer-application.js +114 -0
- package/lib/Utils/crypto.js +129 -0
- package/lib/Utils/decode-wa-message.js +496 -0
- package/lib/Utils/direct-chat-theme.js +164 -0
- package/lib/Utils/event-buffer.js +601 -0
- package/lib/Utils/experimental-proto.js +206 -0
- package/lib/Utils/games/miniapp.js +17 -0
- package/lib/Utils/games/slot-machine.js +270 -0
- package/lib/Utils/games/website-preview.js +306 -0
- package/lib/Utils/generics.js +402 -0
- package/lib/Utils/group-history.js +68 -0
- package/lib/Utils/group-wallpaper.js +168 -0
- package/lib/Utils/history.js +162 -0
- package/lib/Utils/identity-change-handler.js +60 -0
- package/lib/Utils/index.js +61 -0
- package/lib/Utils/jid-display-normalization.js +221 -0
- package/lib/Utils/link-preview.js +96 -0
- package/lib/Utils/logger.js +14 -0
- package/lib/Utils/lt-hash.js +19 -0
- package/lib/Utils/make-mutex.js +44 -0
- package/lib/Utils/message-composer.js +709 -0
- package/lib/Utils/message-inspect.js +79 -0
- package/lib/Utils/message-retry-manager.js +253 -0
- package/lib/Utils/message-security.js +167 -0
- package/lib/Utils/messages-media.js +1027 -0
- package/lib/Utils/messages.js +2467 -0
- package/lib/Utils/meta-ai-msmsg.js +215 -0
- package/lib/Utils/meta-compositing.js +239 -0
- package/lib/Utils/native-flow.js +158 -0
- package/lib/Utils/newsletter-live-update-utils.js +74 -0
- package/lib/Utils/newsletter-status-utils.js +152 -0
- package/lib/Utils/noise-handler.js +212 -0
- package/lib/Utils/offline-node-processor.js +51 -0
- package/lib/Utils/poll-manager.js +206 -0
- package/lib/Utils/portable-compat.js +12 -0
- package/lib/Utils/pre-key-manager.js +117 -0
- package/lib/Utils/process-message.js +610 -0
- package/lib/Utils/reporting-utils.js +269 -0
- package/lib/Utils/rich-message-utils.js +2841 -0
- package/lib/Utils/scheduled-message.js +107 -0
- package/lib/Utils/signal.js +212 -0
- package/lib/Utils/source-grep.js +57 -0
- package/lib/Utils/stanza-ack.js +49 -0
- package/lib/Utils/sticker-annotation.js +87 -0
- package/lib/Utils/sync-action-utils.js +59 -0
- package/lib/Utils/tc-token-utils.js +174 -0
- package/lib/Utils/use-multi-file-auth-state.js +132 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.js +120 -0
- package/lib/Utils/validate-connection.js +220 -0
- package/lib/Utils/view-once-utils.js +218 -0
- package/lib/Utils/voip-rekey.js +32 -0
- package/lib/Utils/website-preview.js +27 -0
- package/lib/Utils/welcome-flow.js +228 -0
- package/lib/Utils/x4r1s3.js +144 -0
- package/lib/WABinary/constants.js +1475 -0
- package/lib/WABinary/decode.js +273 -0
- package/lib/WABinary/encode.js +231 -0
- package/lib/WABinary/generic-utils.js +242 -0
- package/lib/WABinary/index.js +17 -0
- package/lib/WABinary/jid-utils.js +107 -0
- package/lib/WABinary/types.js +13 -0
- package/lib/WAM/BinaryInfo.js +21 -0
- package/lib/WAM/constants.js +22864 -0
- package/lib/WAM/encode.js +161 -0
- package/lib/WAM/index.js +15 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +108 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +68 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +82 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +40 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +37 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +49 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +46 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +36 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +40 -0
- package/lib/WAUSync/Protocols/index.js +24 -0
- package/lib/WAUSync/USyncQuery.js +213 -0
- package/lib/WAUSync/USyncUser.js +66 -0
- package/lib/WAUSync/index.js +15 -0
- package/lib/antiban.cjs +4726 -0
- package/lib/index.d.ts +1285 -0
- package/lib/index.js +30 -0
- package/package.json +125 -4
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Utils/validate-connection.js
|
|
5
|
+
* About: Validate connection module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: generateLoginNode, generateRegistrationNode, configureSuccessfulPairing, encodeSignedDeviceIdentity
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import { Boom } from '@hapi/boom';
|
|
14
|
+
import { createHash } from 'crypto';
|
|
15
|
+
import { proto } from '../../WAProto/index.js';
|
|
16
|
+
import { KEY_BUNDLE_TYPE, WA_ADV_ACCOUNT_SIG_PREFIX, WA_ADV_DEVICE_SIG_PREFIX, WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX } from '../Defaults/index.js';
|
|
17
|
+
import { getBinaryNodeChild, jidDecode, S_WHATSAPP_NET } from '../WABinary/index.js';
|
|
18
|
+
import { Curve, hmacSign } from './crypto.js';
|
|
19
|
+
import { encodeBigEndian } from './generics.js';
|
|
20
|
+
import { createSignalIdentity } from './signal.js';
|
|
21
|
+
const getUserAgent = (config) => {
|
|
22
|
+
return {
|
|
23
|
+
appVersion: {
|
|
24
|
+
primary: config.version[0],
|
|
25
|
+
secondary: config.version[1],
|
|
26
|
+
tertiary: config.version[2]
|
|
27
|
+
},
|
|
28
|
+
platform: config.browser[1].toLocaleLowerCase().includes('android')
|
|
29
|
+
? proto.ClientPayload.UserAgent.Platform.ANDROID
|
|
30
|
+
: proto.ClientPayload.UserAgent.Platform.WEB,
|
|
31
|
+
releaseChannel: proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
|
|
32
|
+
osVersion: '0.1',
|
|
33
|
+
device: 'Desktop',
|
|
34
|
+
osBuildNumber: '0.1',
|
|
35
|
+
localeLanguageIso6391: 'en',
|
|
36
|
+
mnc: '000',
|
|
37
|
+
mcc: '000',
|
|
38
|
+
localeCountryIso31661Alpha2: config.countryCode
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const PLATFORM_MAP = {
|
|
42
|
+
'Mac OS': proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
|
|
43
|
+
Windows: proto.ClientPayload.WebInfo.WebSubPlatform.WIN_HYBRID
|
|
44
|
+
};
|
|
45
|
+
const getWebInfo = (config) => {
|
|
46
|
+
let webSubPlatform = proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
|
|
47
|
+
if (config.syncFullHistory &&
|
|
48
|
+
PLATFORM_MAP[config.browser[0]] &&
|
|
49
|
+
config.browser[1] === 'Desktop') {
|
|
50
|
+
webSubPlatform = PLATFORM_MAP[config.browser[0]];
|
|
51
|
+
}
|
|
52
|
+
return { webSubPlatform };
|
|
53
|
+
};
|
|
54
|
+
const getClientPayload = (config) => {
|
|
55
|
+
const payload = {
|
|
56
|
+
connectType: proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
|
|
57
|
+
connectReason: proto.ClientPayload.ConnectReason.USER_ACTIVATED,
|
|
58
|
+
userAgent: getUserAgent(config)
|
|
59
|
+
};
|
|
60
|
+
if (!config.browser[1].toLocaleLowerCase().includes('android')) {
|
|
61
|
+
payload.webInfo = getWebInfo(config);
|
|
62
|
+
}
|
|
63
|
+
return payload;
|
|
64
|
+
};
|
|
65
|
+
export const generateLoginNode = (userJid, config) => {
|
|
66
|
+
const { user, device } = jidDecode(userJid);
|
|
67
|
+
const payload = {
|
|
68
|
+
...getClientPayload(config),
|
|
69
|
+
passive: true,
|
|
70
|
+
pull: true,
|
|
71
|
+
username: +user,
|
|
72
|
+
device: device,
|
|
73
|
+
// TODO: investigate (hard set as false atm)
|
|
74
|
+
lidDbMigrated: false
|
|
75
|
+
};
|
|
76
|
+
return proto.ClientPayload.fromObject(payload);
|
|
77
|
+
};
|
|
78
|
+
const getPlatformType = (platform) => {
|
|
79
|
+
const platformType = platform.toUpperCase();
|
|
80
|
+
if (platformType === 'ANDROID') {
|
|
81
|
+
return proto.DeviceProps.PlatformType.ANDROID_PHONE;
|
|
82
|
+
}
|
|
83
|
+
return (proto.DeviceProps.PlatformType[platformType] ||
|
|
84
|
+
proto.DeviceProps.PlatformType.CHROME);
|
|
85
|
+
};
|
|
86
|
+
export const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentityKey }, config) => {
|
|
87
|
+
// the app version needs to be md5 hashed
|
|
88
|
+
// and passed in
|
|
89
|
+
const appVersionBuf = createHash('md5')
|
|
90
|
+
.update(config.version.join('.')) // join as string
|
|
91
|
+
.digest();
|
|
92
|
+
const companion = {
|
|
93
|
+
os: config.browser[0],
|
|
94
|
+
platformType: getPlatformType(config.browser[1]),
|
|
95
|
+
requireFullSync: config.syncFullHistory,
|
|
96
|
+
historySyncConfig: {
|
|
97
|
+
storageQuotaMb: 10240,
|
|
98
|
+
inlineInitialPayloadInE2EeMsg: true,
|
|
99
|
+
recentSyncDaysLimit: undefined,
|
|
100
|
+
supportCallLogHistory: false,
|
|
101
|
+
supportBotUserAgentChatHistory: true,
|
|
102
|
+
supportCagReactionsAndPolls: true,
|
|
103
|
+
supportBizHostedMsg: true,
|
|
104
|
+
supportRecentSyncChunkMessageCountTuning: true,
|
|
105
|
+
supportHostedGroupMsg: true,
|
|
106
|
+
supportFbidBotChatHistory: true,
|
|
107
|
+
supportAddOnHistorySyncMigration: undefined,
|
|
108
|
+
supportMessageAssociation: true,
|
|
109
|
+
supportGroupHistory: false,
|
|
110
|
+
onDemandReady: undefined,
|
|
111
|
+
supportGuestChat: undefined,
|
|
112
|
+
supportOmittedConversationIndex: true,
|
|
113
|
+
supportUniversalReachChat: true
|
|
114
|
+
},
|
|
115
|
+
version: {
|
|
116
|
+
primary: 10,
|
|
117
|
+
secondary: 15,
|
|
118
|
+
tertiary: 7
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const companionProto = proto.DeviceProps.encode(companion).finish();
|
|
122
|
+
const registerPayload = {
|
|
123
|
+
...getClientPayload(config),
|
|
124
|
+
passive: false,
|
|
125
|
+
pull: false,
|
|
126
|
+
devicePairingData: {
|
|
127
|
+
buildHash: appVersionBuf,
|
|
128
|
+
deviceProps: companionProto,
|
|
129
|
+
eRegid: encodeBigEndian(registrationId),
|
|
130
|
+
eKeytype: KEY_BUNDLE_TYPE,
|
|
131
|
+
eIdent: signedIdentityKey.public,
|
|
132
|
+
eSkeyId: encodeBigEndian(signedPreKey.keyId, 3),
|
|
133
|
+
eSkeyVal: signedPreKey.keyPair.public,
|
|
134
|
+
eSkeySig: signedPreKey.signature
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
return proto.ClientPayload.fromObject(registerPayload);
|
|
138
|
+
};
|
|
139
|
+
export const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, signalIdentities }) => {
|
|
140
|
+
const msgId = stanza.attrs.id;
|
|
141
|
+
const pairSuccessNode = getBinaryNodeChild(stanza, 'pair-success');
|
|
142
|
+
const deviceIdentityNode = getBinaryNodeChild(pairSuccessNode, 'device-identity');
|
|
143
|
+
const platformNode = getBinaryNodeChild(pairSuccessNode, 'platform');
|
|
144
|
+
const deviceNode = getBinaryNodeChild(pairSuccessNode, 'device');
|
|
145
|
+
const businessNode = getBinaryNodeChild(pairSuccessNode, 'biz');
|
|
146
|
+
if (!deviceIdentityNode || !deviceNode) {
|
|
147
|
+
throw new Boom('Missing device-identity or device in pair success node', { data: stanza });
|
|
148
|
+
}
|
|
149
|
+
const bizName = businessNode?.attrs.name;
|
|
150
|
+
const jid = deviceNode.attrs.jid;
|
|
151
|
+
const lid = deviceNode.attrs.lid;
|
|
152
|
+
const { details, hmac, accountType } = proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
|
|
153
|
+
let hmacPrefix = Buffer.from([]);
|
|
154
|
+
if (accountType !== undefined && accountType === proto.ADVEncryptionType.HOSTED) {
|
|
155
|
+
hmacPrefix = WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX;
|
|
156
|
+
}
|
|
157
|
+
const advSign = hmacSign(Buffer.concat([hmacPrefix, details]), Buffer.from(advSecretKey, 'base64'));
|
|
158
|
+
if (Buffer.compare(hmac, advSign) !== 0) {
|
|
159
|
+
throw new Boom('Invalid account signature');
|
|
160
|
+
}
|
|
161
|
+
const account = proto.ADVSignedDeviceIdentity.decode(details);
|
|
162
|
+
const { accountSignatureKey, accountSignature, details: deviceDetails } = account;
|
|
163
|
+
const deviceIdentity = proto.ADVDeviceIdentity.decode(deviceDetails);
|
|
164
|
+
const accountSignaturePrefix = deviceIdentity.deviceType === proto.ADVEncryptionType.HOSTED
|
|
165
|
+
? WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX
|
|
166
|
+
: WA_ADV_ACCOUNT_SIG_PREFIX;
|
|
167
|
+
const accountMsg = Buffer.concat([accountSignaturePrefix, deviceDetails, signedIdentityKey.public]);
|
|
168
|
+
if (!Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
|
|
169
|
+
throw new Boom('Failed to verify account signature');
|
|
170
|
+
}
|
|
171
|
+
const deviceMsg = Buffer.concat([
|
|
172
|
+
WA_ADV_DEVICE_SIG_PREFIX,
|
|
173
|
+
deviceDetails,
|
|
174
|
+
signedIdentityKey.public,
|
|
175
|
+
accountSignatureKey
|
|
176
|
+
]);
|
|
177
|
+
account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg);
|
|
178
|
+
const identity = createSignalIdentity(lid, accountSignatureKey);
|
|
179
|
+
const accountEnc = encodeSignedDeviceIdentity(account, false);
|
|
180
|
+
const reply = {
|
|
181
|
+
tag: 'iq',
|
|
182
|
+
attrs: {
|
|
183
|
+
to: S_WHATSAPP_NET,
|
|
184
|
+
type: 'result',
|
|
185
|
+
id: msgId
|
|
186
|
+
},
|
|
187
|
+
content: [
|
|
188
|
+
{
|
|
189
|
+
tag: 'pair-device-sign',
|
|
190
|
+
attrs: {},
|
|
191
|
+
content: [
|
|
192
|
+
{
|
|
193
|
+
tag: 'device-identity',
|
|
194
|
+
attrs: { 'key-index': deviceIdentity.keyIndex.toString() },
|
|
195
|
+
content: accountEnc
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
}
|
|
199
|
+
]
|
|
200
|
+
};
|
|
201
|
+
const authUpdate = {
|
|
202
|
+
account,
|
|
203
|
+
me: { id: jid, name: bizName, lid },
|
|
204
|
+
signalIdentities: [...(signalIdentities || []), identity],
|
|
205
|
+
platform: platformNode?.attrs.name
|
|
206
|
+
};
|
|
207
|
+
return {
|
|
208
|
+
creds: authUpdate,
|
|
209
|
+
reply
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
export const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
|
|
213
|
+
account = { ...account };
|
|
214
|
+
// set to null if we are not to include the signature key
|
|
215
|
+
// or if we are including the signature key but it is empty
|
|
216
|
+
if (!includeSignatureKey || !account.accountSignatureKey?.length) {
|
|
217
|
+
account.accountSignatureKey = null;
|
|
218
|
+
}
|
|
219
|
+
return proto.ADVSignedDeviceIdentity.encode(account).finish();
|
|
220
|
+
};
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Utils/view-once-utils.js
|
|
5
|
+
* About: View once utils module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: isViewOnceMessage, extractViewOnceContent, getViewOnceMediaType, getViewOnceMediaContent, downloadViewOnceMedia, createRegularMessageFromViewOnce, shouldAutoDecryptViewOnce, shouldDeleteViewOnce; …
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* View-Once Message Utilities
|
|
15
|
+
*
|
|
16
|
+
* Provides auto-detection, decryption, and handling of view-once messages.
|
|
17
|
+
* - autoVV: Automatically decrypt view-once messages and re-emit as regular messages
|
|
18
|
+
* - antiVV: Automatically detect and delete/view view-once messages
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { downloadContentFromMessage } from './messages-media.js';
|
|
22
|
+
import { extractMessageContent, getContentType } from './messages.js';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Detect if a message is a view-once message
|
|
26
|
+
* @param {object} message - The message object
|
|
27
|
+
* @returns {boolean}
|
|
28
|
+
*/
|
|
29
|
+
export const isViewOnceMessage = (message) => {
|
|
30
|
+
if (!message) return false;
|
|
31
|
+
const content = message.message || message;
|
|
32
|
+
return !!(
|
|
33
|
+
content.viewOnceMessage ||
|
|
34
|
+
content.viewOnceMessageV2 ||
|
|
35
|
+
content.viewOnceMessageV2Extension
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Extract the inner content from a view-once message wrapper
|
|
41
|
+
* @param {object} message - The message object with viewOnce wrapper
|
|
42
|
+
* @returns {object|null} The inner message content or null
|
|
43
|
+
*/
|
|
44
|
+
export const extractViewOnceContent = (message) => {
|
|
45
|
+
const content = message.message || message;
|
|
46
|
+
const viewOnceWrapper =
|
|
47
|
+
content.viewOnceMessage ||
|
|
48
|
+
content.viewOnceMessageV2 ||
|
|
49
|
+
content.viewOnceMessageV2Extension;
|
|
50
|
+
|
|
51
|
+
if (!viewOnceWrapper) return null;
|
|
52
|
+
return viewOnceWrapper.message || viewOnceWrapper;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get the media type from a view-once message
|
|
57
|
+
* @param {object} message - The message object
|
|
58
|
+
* @returns {string|null} The media type (image, video, audio, document) or null
|
|
59
|
+
*/
|
|
60
|
+
export const getViewOnceMediaType = (message) => {
|
|
61
|
+
const innerContent = extractViewOnceContent(message);
|
|
62
|
+
if (!innerContent) return null;
|
|
63
|
+
|
|
64
|
+
if (innerContent.imageMessage) return 'image';
|
|
65
|
+
if (innerContent.videoMessage) return 'video';
|
|
66
|
+
if (innerContent.audioMessage) return 'audio';
|
|
67
|
+
if (innerContent.documentMessage) return 'document';
|
|
68
|
+
if (innerContent.stickerMessage) return 'sticker';
|
|
69
|
+
|
|
70
|
+
return null;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Get the media content from a view-once message
|
|
75
|
+
* @param {object} message - The message object
|
|
76
|
+
* @returns {object|null} The media content object with url, mediaKey, etc.
|
|
77
|
+
*/
|
|
78
|
+
export const getViewOnceMediaContent = (message) => {
|
|
79
|
+
const innerContent = extractViewOnceContent(message);
|
|
80
|
+
if (!innerContent) return null;
|
|
81
|
+
|
|
82
|
+
return innerContent.imageMessage ||
|
|
83
|
+
innerContent.videoMessage ||
|
|
84
|
+
innerContent.audioMessage ||
|
|
85
|
+
innerContent.documentMessage ||
|
|
86
|
+
innerContent.stickerMessage ||
|
|
87
|
+
null;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Download and decrypt a view-once message's media content
|
|
92
|
+
* @param {object} message - The full message object
|
|
93
|
+
* @param {object} options - Options for download
|
|
94
|
+
* @returns {Promise<Buffer>} The decrypted media buffer
|
|
95
|
+
*/
|
|
96
|
+
export const downloadViewOnceMedia = async (message, options = {}) => {
|
|
97
|
+
const mediaContent = getViewOnceMediaContent(message);
|
|
98
|
+
if (!mediaContent) {
|
|
99
|
+
throw new Error('No media content found in view-once message');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const mediaType = getViewOnceMediaType(message);
|
|
103
|
+
if (!mediaType) {
|
|
104
|
+
throw new Error('Could not determine media type');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const stream = await downloadContentFromMessage(mediaContent, mediaType, options);
|
|
108
|
+
const chunks = [];
|
|
109
|
+
for await (const chunk of stream) {
|
|
110
|
+
chunks.push(chunk);
|
|
111
|
+
}
|
|
112
|
+
return Buffer.concat(chunks);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Create a regular message from a decrypted view-once message
|
|
117
|
+
* @param {object} originalMessage - The original view-once message
|
|
118
|
+
* @param {Buffer} mediaBuffer - The decrypted media buffer
|
|
119
|
+
* @returns {object} A regular message object with the media
|
|
120
|
+
*/
|
|
121
|
+
export const createRegularMessageFromViewOnce = (originalMessage, mediaBuffer) => {
|
|
122
|
+
const innerContent = extractViewOnceContent(originalMessage);
|
|
123
|
+
if (!innerContent) return null;
|
|
124
|
+
|
|
125
|
+
const mediaType = getViewOnceMediaType(originalMessage);
|
|
126
|
+
const mediaContent = getViewOnceMediaContent(originalMessage);
|
|
127
|
+
|
|
128
|
+
// Create a new message with the decrypted content
|
|
129
|
+
const newMessage = {
|
|
130
|
+
key: originalMessage.key,
|
|
131
|
+
messageTimestamp: originalMessage.messageTimestamp,
|
|
132
|
+
pushName: originalMessage.pushName,
|
|
133
|
+
participant: originalMessage.participant,
|
|
134
|
+
// Store the decrypted media
|
|
135
|
+
decryptedMedia: {
|
|
136
|
+
type: mediaType,
|
|
137
|
+
buffer: mediaBuffer,
|
|
138
|
+
mimeType: mediaContent?.mimetype,
|
|
139
|
+
fileName: mediaContent?.fileName,
|
|
140
|
+
caption: mediaContent?.caption,
|
|
141
|
+
fileLength: mediaContent?.fileLength,
|
|
142
|
+
},
|
|
143
|
+
// Keep original message for reference
|
|
144
|
+
originalViewOnceMessage: originalMessage.message,
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
return newMessage;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Check if a message should be auto-decrypted based on configuration
|
|
152
|
+
* @param {object} message - The message object
|
|
153
|
+
* @param {object} config - Configuration object
|
|
154
|
+
* @returns {boolean}
|
|
155
|
+
*/
|
|
156
|
+
export const shouldAutoDecryptViewOnce = (message, config = {}) => {
|
|
157
|
+
if (!isViewOnceMessage(message)) return false;
|
|
158
|
+
|
|
159
|
+
// Check if auto-decryption is enabled
|
|
160
|
+
if (config.autoVV === false) return false;
|
|
161
|
+
|
|
162
|
+
// Check if it's from self (don't decrypt own messages)
|
|
163
|
+
if (message.key?.fromMe) return false;
|
|
164
|
+
|
|
165
|
+
return true;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Check if a message should be deleted based on anti-VV configuration
|
|
170
|
+
* @param {object} message - The message object
|
|
171
|
+
* @param {object} config - Configuration object
|
|
172
|
+
* @returns {boolean}
|
|
173
|
+
*/
|
|
174
|
+
export const shouldDeleteViewOnce = (message, config = {}) => {
|
|
175
|
+
if (!isViewOnceMessage(message)) return false;
|
|
176
|
+
|
|
177
|
+
// Check if anti-VV is enabled
|
|
178
|
+
if (config.antiVV === false) return false;
|
|
179
|
+
|
|
180
|
+
// Check if it's from self (don't delete own messages)
|
|
181
|
+
if (message.key?.fromMe) return false;
|
|
182
|
+
|
|
183
|
+
return true;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Parse view-once message info for logging/display
|
|
188
|
+
* @param {object} message - The message object
|
|
189
|
+
* @returns {object} Parsed view-once info
|
|
190
|
+
*/
|
|
191
|
+
export const parseViewOnceInfo = (message) => {
|
|
192
|
+
const mediaType = getViewOnceMediaType(message);
|
|
193
|
+
const mediaContent = getViewOnceMediaContent(message);
|
|
194
|
+
|
|
195
|
+
return {
|
|
196
|
+
isViewOnce: isViewOnceMessage(message),
|
|
197
|
+
mediaType,
|
|
198
|
+
mimeType: mediaContent?.mimetype,
|
|
199
|
+
fileName: mediaContent?.fileName,
|
|
200
|
+
caption: mediaContent?.caption,
|
|
201
|
+
fileLength: mediaContent?.fileLength,
|
|
202
|
+
hasUrl: !!mediaContent?.url,
|
|
203
|
+
hasDirectPath: !!mediaContent?.directPath,
|
|
204
|
+
hasMediaKey: !!mediaContent?.mediaKey,
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export default {
|
|
209
|
+
isViewOnceMessage,
|
|
210
|
+
extractViewOnceContent,
|
|
211
|
+
getViewOnceMediaType,
|
|
212
|
+
getViewOnceMediaContent,
|
|
213
|
+
downloadViewOnceMedia,
|
|
214
|
+
createRegularMessageFromViewOnce,
|
|
215
|
+
shouldAutoDecryptViewOnce,
|
|
216
|
+
shouldDeleteViewOnce,
|
|
217
|
+
parseViewOnceInfo,
|
|
218
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Utils/voip-rekey.js
|
|
5
|
+
* About: Voip rekey module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: decodeE2eRekeyPayload
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import { proto } from '../../WAProto/index.js';
|
|
14
|
+
|
|
15
|
+
const REKEY_TYPE_NAMES = {
|
|
16
|
+
[proto.RekeyKeyType.REKEY_KEY_AUDIO]: 'REKEY_KEY_AUDIO',
|
|
17
|
+
[proto.RekeyKeyType.REKEY_KEY_VIDEO]: 'REKEY_KEY_VIDEO',
|
|
18
|
+
[proto.RekeyKeyType.REKEY_KEY_APPDATA]: 'REKEY_KEY_APPDATA'
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const decodeE2eRekeyPayload = (buffer) => {
|
|
22
|
+
if (!Buffer.isBuffer(buffer) && !(buffer instanceof Uint8Array)) {
|
|
23
|
+
throw new TypeError('decodeE2eRekeyPayload: buffer must be Buffer or Uint8Array');
|
|
24
|
+
}
|
|
25
|
+
const decoded = proto.E2eRekeyPayload.decode(buffer);
|
|
26
|
+
return {
|
|
27
|
+
keys: (decoded.keys || []).map(entry => ({
|
|
28
|
+
type: REKEY_TYPE_NAMES[entry.type] ?? entry.type,
|
|
29
|
+
key: entry.key ? Buffer.from(entry.key) : Buffer.alloc(0)
|
|
30
|
+
}))
|
|
31
|
+
};
|
|
32
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const escapeHtml = (value) => String(value ?? '').replace(/[&<>"']/g, (character) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[character]));
|
|
2
|
+
|
|
3
|
+
const validateUrl = (value) => {
|
|
4
|
+
const parsed = new URL(value);
|
|
5
|
+
if (!['http:', 'https:'].includes(parsed.protocol)) throw new TypeError('only HTTP(S) preview URLs are supported');
|
|
6
|
+
return parsed;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const generateLinkPreviewHtml = (url, options = {}) => {
|
|
10
|
+
const parsed = validateUrl(url);
|
|
11
|
+
const title = options.title || parsed.hostname;
|
|
12
|
+
const description = options.description || `Open ${parsed.hostname}`;
|
|
13
|
+
const image = options.image ? `<img src="${escapeHtml(options.image)}" alt="" loading="lazy">` : '<div class="placeholder">🌐</div>';
|
|
14
|
+
return `<!doctype html><html><head><meta charset="utf-8"><style>body{margin:0;background:transparent;font:14px system-ui,sans-serif}.card{overflow:hidden;border:1px solid #333;border-radius:12px;background:#1a1a2e;color:#fff}.card img,.placeholder{display:block;width:100%;height:160px;object-fit:cover}.placeholder{display:grid;place-items:center;font-size:48px;background:linear-gradient(135deg,#1a1a2e,#2d2d44)}.body{padding:14px}.title{font-weight:700;margin-bottom:5px}.desc{color:#bbb;line-height:1.4}.url{margin-top:8px;color:#55c878;font-size:11px}</style></head><body><a href="${escapeHtml(parsed.href)}" target="_blank" rel="noopener noreferrer" style="text-decoration:none"><div class="card">${image}<div class="body"><div class="title">${escapeHtml(title)}</div><div class="desc">${escapeHtml(description)}</div><div class="url">${escapeHtml(parsed.hostname)}</div></div></div></a></body></html>`;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const generateWebsitePreviewHtml = async (url, options = {}) => {
|
|
18
|
+
const parsed = validateUrl(url);
|
|
19
|
+
const response = await (options.fetch || globalThis.fetch)(parsed.href, { signal: options.signal, headers: options.headers || {} });
|
|
20
|
+
if (!response?.ok) throw new Error(`website preview request failed with status ${response?.status ?? 'unknown'}`);
|
|
21
|
+
const html = await response.text();
|
|
22
|
+
const title = html.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1] || parsed.hostname;
|
|
23
|
+
const description = html.match(/<meta[^>]+name=["']description["'][^>]+content=["']([^"']*)["']/i)?.[1] || `Preview of ${parsed.hostname}`;
|
|
24
|
+
return generateLinkPreviewHtml(parsed.href, { ...options, title, description });
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default generateWebsitePreviewHtml;
|