diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-09-01 00:26:35 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-09-01 00:26:35 +0300 |
commit | 4588170d1e43db780c551177f5996598fe25bc6e (patch) | |
tree | bc9ec31b03fd179cb1b812df5c1493ab816f59fb /telegram/connect.go | |
parent | aa561c5be606c14cfd211df694ef0be856195df7 (diff) |
Harden the authorizer access to prevent crashesv1.8.1
Diffstat (limited to 'telegram/connect.go')
-rw-r--r-- | telegram/connect.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/telegram/connect.go b/telegram/connect.go index ab9c19c..6c49aa9 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -110,6 +110,7 @@ func (c *Client) Connect(resource string) error { log.Warn("Connecting to Telegram network...") + c.locks.authorizerWriteLock.Lock() c.authorizer = &clientAuthorizer{ TdlibParameters: make(chan *client.SetTdlibParametersRequest, 1), PhoneNumber: make(chan string, 1), @@ -123,6 +124,7 @@ func (c *Client) Connect(resource string) error { go c.interactor() c.authorizer.TdlibParameters <- c.parameters + c.locks.authorizerWriteLock.Unlock() tdlibClient, err := client.NewClient(c.authorizer, c.options...) if err != nil { @@ -178,6 +180,9 @@ func (c *Client) TryLogin(resource string, login string) error { time.Sleep(1e5) } + c.locks.authorizerReadLock.Lock() + defer c.locks.authorizerReadLock.Unlock() + if c.authorizer == nil { return errors.New(TelegramNotInitialized) } @@ -190,6 +195,9 @@ func (c *Client) TryLogin(resource string, login string) error { } func (c *Client) SetPhoneNumber(login string) error { + c.locks.authorizerWriteLock.Lock() + defer c.locks.authorizerWriteLock.Unlock() + if c.authorizer == nil || c.authorizer.isClosed { return errors.New("Authorization not needed") } @@ -234,9 +242,16 @@ func (c *Client) Disconnect(resource string, quit bool) bool { func (c *Client) interactor() { for { + c.locks.authorizerReadLock.Lock() + if c.authorizer == nil { + log.Warn("Authorizer is lost, halting the interactor") + c.locks.authorizerReadLock.Unlock() + return + } state, ok := <-c.authorizer.State if !ok { log.Warn("Interactor is disconnected") + c.locks.authorizerReadLock.Unlock() return } @@ -266,18 +281,27 @@ func (c *Client) interactor() { log.Warn("Waiting for 2FA password...") gateway.SendServiceMessage(c.jid, "Please, enter 2FA passphrase via /password 12345", c.xmpp) } + c.locks.authorizerReadLock.Unlock() } } func (c *Client) forceClose() { + c.locks.authorizerReadLock.Lock() + c.locks.authorizerWriteLock.Lock() + defer c.locks.authorizerReadLock.Unlock() + defer c.locks.authorizerWriteLock.Unlock() + c.online = false c.authorizer = nil } func (c *Client) close() { + c.locks.authorizerWriteLock.Lock() if c.authorizer != nil && !c.authorizer.isClosed { c.authorizer.Close() } + c.locks.authorizerWriteLock.Unlock() + if c.client != nil { _, err := c.client.Close() if err != nil { |