Skip to content

Commit 545e6cb

Browse files
committed
validate connect session with another whatsapp number
1 parent 7f7aa2d commit 545e6cb

File tree

8 files changed

+41
-18
lines changed

8 files changed

+41
-18
lines changed

__tests__/services/client_baileys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { Store } from '../../src/services/store'
88
import {
99
connect,
1010
Status,
11-
SendError,
1211
sendMessage,
1312
readMessages,
1413
rejectCall,
@@ -25,6 +24,7 @@ import { Incoming } from '../../src/services/incoming'
2524
import { dataStores } from '../../src/services/data_store'
2625
import logger from '../../src/services/logger'
2726
import { SessionStore } from '../../src/services/session_store'
27+
import { SendError } from '../../src/services/send_error'
2828

2929
const mockConnect = connect as jest.MockedFunction<typeof connect>
3030

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unoapi-cloud",
3-
"version": "2.0.10",
3+
"version": "2.1.0",
44
"description": "Unoapi Cloud",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
"pairing_code": "Open your WhatsApp and go to: Connected Devices > Connect a new Device > Connect using phone number > And put your connection code > %s",
2525
"restart": "Restarting session",
2626
"standby": "Standby session, waiting for time configured to try connect again, %s error in %s seconds",
27-
"proxy_error": "Error on connect to proxy: %s"
27+
"proxy_error": "Error on connect to proxy: %s",
28+
"session_conflict": "The session number is %s but the configured number %s"
2829
}

src/locales/pt.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
"pairing_code": "Informe o código para conectar no whatsapp > %s",
2525
"restart": "Reiniciando sessão",
2626
"standby": "Sessão colocada em standby, esperando pelo tempo configurado para tentar conectar novamente: %s",
27-
"proxy_error": "Erro ao conectar no proxy: %s"
27+
"proxy_error": "Erro ao conectar no proxy: %s",
28+
"session_conflict": "O número the sessão usado é o %s mas o número da configuração é %s"
2829
}

src/locales/pt_BR.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
"pairing_code": "Informe o código para conectar no whatsapp: %s",
2525
"restart": "Reiniciando sessão",
2626
"standby": "Sessão colocada em standby, esperando pelo tempo configurado para tentar conectar novamente: %s",
27-
"proxy_error": "Erro ao conectar no proxy: %s"
27+
"proxy_error": "Erro ao conectar no proxy: %s",
28+
"session_conflict": "O número the sessão usado é o %s mas o número da configuração é %s"
2829
}

src/services/client_baileys.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Listener } from './listener'
44
import { Store } from './store'
55
import {
66
connect,
7-
SendError,
87
sendMessage,
98
readMessages,
109
rejectCall,
@@ -29,6 +28,8 @@ import logger from './logger'
2928
import { FETCH_TIMEOUT_MS, VALIDATE_MEDIA_LINK_BEFORE_SEND } from '../defaults'
3029
import { t } from '../i18n'
3130
import { ClientForward } from './client_forward'
31+
import { SendError } from './send_error'
32+
3233
const attempts = 3
3334

3435
interface Delay {

src/services/send_error.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class SendError extends Error {
2+
readonly code: number
3+
readonly title: string
4+
constructor(code: number, title: string) {
5+
super(`${code}: ${title}`)
6+
this.code = code
7+
this.title = title
8+
}
9+
}

src/services/socket.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ import MAIN_LOGGER from 'baileys/lib/Utils/logger'
1717
import { Config, defaultConfig } from './config'
1818
import { Store } from './store'
1919
import NodeCache from 'node-cache'
20-
import { isIndividualJid, isValidPhoneNumber } from './transformer'
20+
import { isIndividualJid, isValidPhoneNumber, jidToPhoneNumber } from './transformer'
2121
import logger from './logger'
2222
import { Level } from 'pino'
2323
import { SocksProxyAgent } from 'socks-proxy-agent'
2424
import { HttpsProxyAgent } from 'https-proxy-agent'
2525
import { useVoiceCallsBaileys } from 'voice-calls-baileys/lib/services/transport.model'
2626
import { DEFAULT_BROWSER, WHATSAPP_VERSION, LOG_LEVEL, CONNECTING_TIMEOUT_MS, MAX_CONNECT_TIME, MAX_CONNECT_RETRY } from '../defaults'
2727
import { t } from '../i18n'
28+
import { SendError } from './send_error'
2829

2930
const EVENTS = [
3031
'connection.update',
@@ -61,16 +62,6 @@ export type OnDisconnected = (phone: string, payload: any) => Promise<void>
6162
export type OnNewLogin = (phone: string) => Promise<void>
6263
export type OnReconnect = (time: number) => Promise<void>
6364

64-
export class SendError extends Error {
65-
readonly code: number
66-
readonly title: string
67-
constructor(code: number, title: string) {
68-
super(`${code}: ${title}`)
69-
this.code = code
70-
this.title = title
71-
}
72-
}
73-
7465
export interface sendMessage {
7566
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7667
(_phone: string, _message: AnyMessageContent, _options: unknown): Promise<any>
@@ -134,6 +125,25 @@ export const connect = async ({
134125
let sock: WASocket | undefined = undefined
135126
const msgRetryCounterCache = new NodeCache()
136127
const { dataStore, state, saveCreds, sessionStore } = store
128+
const firstSaveCreds = async () => {
129+
if (state?.creds?.me?.id) {
130+
const phoneCreds = jidToPhoneNumber(state?.creds?.me?.id, '')
131+
logger.info(`First save creds with number is ${phoneCreds} and configured number ${phone}`)
132+
if (phoneCreds != phone) {
133+
await logout()
134+
currentSaveCreds = async () => {
135+
const message = t('session_conflict', phoneCreds, phone)
136+
logger.error(message)
137+
await onNotification(message, true)
138+
}
139+
} else {
140+
logger.info(`Correct save creds with number is ${phoneCreds} and configured number ${phone}`)
141+
currentSaveCreds = saveCreds
142+
}
143+
}
144+
}
145+
let currentSaveCreds = firstSaveCreds
146+
const verifyAndSaveCreds = async () => currentSaveCreds()
137147
let connectingTimeout
138148

139149
const status: Status = {
@@ -519,7 +529,7 @@ export const connect = async ({
519529
}
520530
if (sock) {
521531
dataStore.bind(sock.ev)
522-
event('creds.update', saveCreds)
532+
event('creds.update', verifyAndSaveCreds)
523533
logger.info('Connection type %s already creds %s', config.connectionType, sock?.authState?.creds?.registered)
524534
if (config.connectionType == 'pairing_code' && !sock?.authState?.creds?.registered) {
525535
logger.info(`Requesting pairing code ${phone}`)

0 commit comments

Comments
 (0)