@@ -62,6 +62,7 @@ type Client struct {
62
62
socket * socket.NoiseSocket
63
63
socketLock sync.RWMutex
64
64
socketWait chan struct {}
65
+ wsDialer * websocket.Dialer
65
66
66
67
isLoggedIn atomic.Bool
67
68
expectedDisconnect atomic.Bool
@@ -181,8 +182,9 @@ type Client struct {
181
182
}
182
183
183
184
type MessengerConfig struct {
184
- UserAgent string
185
- BaseURL string
185
+ UserAgent string
186
+ BaseURL string
187
+ WebsocketURL string
186
188
}
187
189
188
190
// Size of buffer for the channel that all incoming XML nodes go through.
@@ -380,6 +382,9 @@ func (cli *Client) closeSocketWaitChan() {
380
382
}
381
383
382
384
func (cli * Client ) getOwnID () types.JID {
385
+ if cli == nil {
386
+ return types .EmptyJID
387
+ }
383
388
id := cli .Store .ID
384
389
if id == nil {
385
390
return types .EmptyJID
@@ -388,6 +393,9 @@ func (cli *Client) getOwnID() types.JID {
388
393
}
389
394
390
395
func (cli * Client ) WaitForConnection (timeout time.Duration ) bool {
396
+ if cli == nil {
397
+ return false
398
+ }
391
399
timeoutChan := time .After (timeout )
392
400
cli .socketLock .RLock ()
393
401
for cli .socket == nil || ! cli .socket .IsConnected () || ! cli .IsLoggedIn () {
@@ -404,9 +412,16 @@ func (cli *Client) WaitForConnection(timeout time.Duration) bool {
404
412
return true
405
413
}
406
414
415
+ func (cli * Client ) SetWSDialer (dialer * websocket.Dialer ) {
416
+ cli .wsDialer = dialer
417
+ }
418
+
407
419
// Connect connects the client to the WhatsApp web websocket. After connection, it will either
408
420
// authenticate if there's data in the device store, or emit a QREvent to set up a new link.
409
421
func (cli * Client ) Connect () error {
422
+ if cli == nil {
423
+ return ErrClientIsNil
424
+ }
410
425
cli .socketLock .Lock ()
411
426
defer cli .socketLock .Unlock ()
412
427
if cli .socket != nil {
@@ -418,8 +433,10 @@ func (cli *Client) Connect() error {
418
433
}
419
434
420
435
cli .resetExpectedDisconnect ()
421
- wsDialer := websocket.Dialer {}
422
- if ! cli .proxyOnlyLogin || cli .Store .ID == nil {
436
+ var wsDialer websocket.Dialer
437
+ if cli .wsDialer != nil {
438
+ wsDialer = * cli .wsDialer
439
+ } else if ! cli .proxyOnlyLogin || cli .Store .ID == nil {
423
440
if cli .proxy != nil {
424
441
wsDialer .Proxy = cli .proxy
425
442
} else if cli .socksProxy != nil {
@@ -432,12 +449,14 @@ func (cli *Client) Connect() error {
432
449
}
433
450
fs := socket .NewFrameSocket (cli .Log .Sub ("Socket" ), wsDialer )
434
451
if cli .MessengerConfig != nil {
435
- fs .URL = "wss://web-chat-e2ee.facebook.com/ws/chat"
452
+ fs .URL = cli . MessengerConfig . WebsocketURL
436
453
fs .HTTPHeaders .Set ("Origin" , cli .MessengerConfig .BaseURL )
437
454
fs .HTTPHeaders .Set ("User-Agent" , cli .MessengerConfig .UserAgent )
438
- fs .HTTPHeaders .Set ("Sec-Fetch-Dest" , "empty" )
439
- fs .HTTPHeaders .Set ("Sec-Fetch-Mode" , "websocket" )
440
- fs .HTTPHeaders .Set ("Sec-Fetch-Site" , "cross-site" )
455
+ fs .HTTPHeaders .Set ("Cache-Control" , "no-cache" )
456
+ fs .HTTPHeaders .Set ("Pragma" , "no-cache" )
457
+ //fs.HTTPHeaders.Set("Sec-Fetch-Dest", "empty")
458
+ //fs.HTTPHeaders.Set("Sec-Fetch-Mode", "websocket")
459
+ //fs.HTTPHeaders.Set("Sec-Fetch-Site", "cross-site")
441
460
}
442
461
if err := fs .Connect (); err != nil {
443
462
fs .Close (0 )
@@ -453,7 +472,7 @@ func (cli *Client) Connect() error {
453
472
454
473
// IsLoggedIn returns true after the client is successfully connected and authenticated on WhatsApp.
455
474
func (cli * Client ) IsLoggedIn () bool {
456
- return cli .isLoggedIn .Load ()
475
+ return cli != nil && cli .isLoggedIn .Load ()
457
476
}
458
477
459
478
func (cli * Client ) onDisconnect (ns * socket.NoiseSocket , remote bool ) {
@@ -517,6 +536,9 @@ func (cli *Client) autoReconnect() {
517
536
// IsConnected checks if the client is connected to the WhatsApp web websocket.
518
537
// Note that this doesn't check if the client is authenticated. See the IsLoggedIn field for that.
519
538
func (cli * Client ) IsConnected () bool {
539
+ if cli == nil {
540
+ return false
541
+ }
520
542
cli .socketLock .RLock ()
521
543
connected := cli .socket != nil && cli .socket .IsConnected ()
522
544
cli .socketLock .RUnlock ()
@@ -528,7 +550,7 @@ func (cli *Client) IsConnected() bool {
528
550
// This will not emit any events, the Disconnected event is only used when the
529
551
// connection is closed by the server or a network error.
530
552
func (cli * Client ) Disconnect () {
531
- if cli .socket == nil {
553
+ if cli == nil || cli .socket == nil {
532
554
return
533
555
}
534
556
cli .socketLock .Lock ()
@@ -553,7 +575,9 @@ func (cli *Client) unlockedDisconnect() {
553
575
// Note that this will not emit any events. The LoggedOut event is only used for external logouts
554
576
// (triggered by the user from the main device or by WhatsApp servers).
555
577
func (cli * Client ) Logout () error {
556
- if cli .MessengerConfig != nil {
578
+ if cli == nil {
579
+ return ErrClientIsNil
580
+ } else if cli .MessengerConfig != nil {
557
581
return errors .New ("can't logout with Messenger credentials" )
558
582
}
559
583
ownID := cli .getOwnID ()
@@ -737,6 +761,9 @@ func (cli *Client) handlerQueueLoop(ctx context.Context) {
737
761
}
738
762
739
763
func (cli * Client ) sendNodeAndGetData (node waBinary.Node ) ([]byte , error ) {
764
+ if cli == nil {
765
+ return nil , ErrClientIsNil
766
+ }
740
767
cli .socketLock .RLock ()
741
768
sock := cli .socket
742
769
cli .socketLock .RUnlock ()
0 commit comments