From ca06088900164c96806fb2e12230404742ce8932 Mon Sep 17 00:00:00 2001 From: divyam234 Date: Fri, 25 Aug 2023 16:00:46 +0530 Subject: [PATCH] fixed nonauth client --- services/auth.service.go | 187 +++++++++++++++++++-------------------- utils/tgclient.go | 24 +++-- 2 files changed, 109 insertions(+), 102 deletions(-) diff --git a/services/auth.service.go b/services/auth.service.go index 0b17306..6578c4e 100644 --- a/services/auth.service.go +++ b/services/auth.service.go @@ -246,103 +246,102 @@ func (as *AuthService) HandleMultipleLogin(c *gin.Context) { dispatcher := tg.NewUpdateDispatcher() loggedIn := qrlogin.OnLoginToken(dispatcher) sessionStorage := &session.StorageMemory{} - tgClient := utils.GetNonAuthClient(dispatcher, sessionStorage) + tgClient, stop, _ := utils.StartNonAuthClient(dispatcher, sessionStorage) - tgClient.Run(c, func(ctx context.Context) error { + defer stop() - for { - message := &SocketMessage{} - err := conn.ReadJSON(message) + for { + message := &SocketMessage{} + err := conn.ReadJSON(message) - if err != nil { - log.Println(err) - return err - } - if message.AuthType == "qr" { - go func() { - authorization, err := tgClient.QR().Auth(c, loggedIn, func(ctx context.Context, token qrlogin.Token) error { - conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": map[string]string{"token": token.URL()}}) - return nil - }) - - if tgerr.Is(err, "SESSION_PASSWORD_NEEDED") { - conn.WriteJSON(map[string]interface{}{"type": "auth", "message": "2FA required"}) - return - } - - if err != nil { - conn.WriteJSON(map[string]interface{}{"type": "error", "message": err.Error()}) - return - } - user, ok := authorization.User.AsNotEmpty() - if !ok { - conn.WriteJSON(map[string]interface{}{"type": "error", "message": errors.New("auth failed")}) - return - } - res, _ := sessionStorage.LoadSession(c) - sessionData := &SessionData{} - json.Unmarshal(res, sessionData) - session := prepareSession(user, &sessionData.Data) - conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": session, "message": "success"}) - }() - } - if message.AuthType == "phone" && message.Message == "sendcode" { - go func() { - res, err := tgClient.Auth().SendCode(c, message.PhoneNo, tgauth.SendCodeOptions{}) - if err != nil { - conn.WriteJSON(map[string]interface{}{"type": "error", "message": err.Error()}) - return - } - code := res.(*tg.AuthSentCode) - conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": map[string]string{"phoneCodeHash": code.PhoneCodeHash}}) - }() - } - if message.AuthType == "phone" && message.Message == "signin" { - go func() { - auth, err := tgClient.Auth().SignIn(c, message.PhoneNo, message.PhoneCode, message.PhoneCodeHash) - - if errors.Is(err, tgauth.ErrPasswordAuthNeeded) { - conn.WriteJSON(map[string]interface{}{"type": "auth", "message": "2FA required"}) - return - } - - if err != nil { - conn.WriteJSON(map[string]interface{}{"type": "error", "message": err.Error()}) - return - } - user, ok := auth.User.AsNotEmpty() - if !ok { - conn.WriteJSON(map[string]interface{}{"type": "error", "message": errors.New("auth failed")}) - return - } - res, _ := sessionStorage.LoadSession(c) - sessionData := &SessionData{} - json.Unmarshal(res, sessionData) - session := prepareSession(user, &sessionData.Data) - conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": session, "message": "success"}) - }() - } - - if message.AuthType == "2fa" && message.Password != "" { - go func() { - auth, err := tgClient.Auth().Password(c, message.Password) - if err != nil { - conn.WriteJSON(map[string]interface{}{"type": "error", "message": err.Error()}) - return - } - user, ok := auth.User.AsNotEmpty() - if !ok { - conn.WriteJSON(map[string]interface{}{"type": "error", "message": errors.New("auth failed")}) - return - } - res, _ := sessionStorage.LoadSession(c) - sessionData := &SessionData{} - json.Unmarshal(res, sessionData) - session := prepareSession(user, &sessionData.Data) - conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": session, "message": "success"}) - }() - } + if err != nil { + log.Println(err) + return } - }) + if message.AuthType == "qr" { + go func() { + authorization, err := tgClient.QR().Auth(c, loggedIn, func(ctx context.Context, token qrlogin.Token) error { + conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": map[string]string{"token": token.URL()}}) + return nil + }) + + if tgerr.Is(err, "SESSION_PASSWORD_NEEDED") { + conn.WriteJSON(map[string]interface{}{"type": "auth", "message": "2FA required"}) + return + } + + if err != nil { + conn.WriteJSON(map[string]interface{}{"type": "error", "message": err.Error()}) + return + } + user, ok := authorization.User.AsNotEmpty() + if !ok { + conn.WriteJSON(map[string]interface{}{"type": "error", "message": errors.New("auth failed")}) + return + } + res, _ := sessionStorage.LoadSession(c) + sessionData := &SessionData{} + json.Unmarshal(res, sessionData) + session := prepareSession(user, &sessionData.Data) + conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": session, "message": "success"}) + }() + } + if message.AuthType == "phone" && message.Message == "sendcode" { + go func() { + res, err := tgClient.Auth().SendCode(c, message.PhoneNo, tgauth.SendCodeOptions{}) + if err != nil { + conn.WriteJSON(map[string]interface{}{"type": "error", "message": err.Error()}) + return + } + code := res.(*tg.AuthSentCode) + conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": map[string]string{"phoneCodeHash": code.PhoneCodeHash}}) + }() + } + if message.AuthType == "phone" && message.Message == "signin" { + go func() { + auth, err := tgClient.Auth().SignIn(c, message.PhoneNo, message.PhoneCode, message.PhoneCodeHash) + + if errors.Is(err, tgauth.ErrPasswordAuthNeeded) { + conn.WriteJSON(map[string]interface{}{"type": "auth", "message": "2FA required"}) + return + } + + if err != nil { + conn.WriteJSON(map[string]interface{}{"type": "error", "message": err.Error()}) + return + } + user, ok := auth.User.AsNotEmpty() + if !ok { + conn.WriteJSON(map[string]interface{}{"type": "error", "message": errors.New("auth failed")}) + return + } + res, _ := sessionStorage.LoadSession(c) + sessionData := &SessionData{} + json.Unmarshal(res, sessionData) + session := prepareSession(user, &sessionData.Data) + conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": session, "message": "success"}) + }() + } + + if message.AuthType == "2fa" && message.Password != "" { + go func() { + auth, err := tgClient.Auth().Password(c, message.Password) + if err != nil { + conn.WriteJSON(map[string]interface{}{"type": "error", "message": err.Error()}) + return + } + user, ok := auth.User.AsNotEmpty() + if !ok { + conn.WriteJSON(map[string]interface{}{"type": "error", "message": errors.New("auth failed")}) + return + } + res, _ := sessionStorage.LoadSession(c) + sessionData := &SessionData{} + json.Unmarshal(res, sessionData) + session := prepareSession(user, &sessionData.Data) + conn.WriteJSON(map[string]interface{}{"type": "auth", "payload": session, "message": "success"}) + }() + } + } } diff --git a/utils/tgclient.go b/utils/tgclient.go index 7447cef..1315178 100644 --- a/utils/tgclient.go +++ b/utils/tgclient.go @@ -115,17 +115,25 @@ func GetAuthClient(ctx context.Context, sessionStr string, userId int64) (*teleg return client, nil } -func GetNonAuthClient(handler telegram.UpdateHandler, storage telegram.SessionStorage) *telegram.Client { +func StartNonAuthClient(handler telegram.UpdateHandler, storage telegram.SessionStorage) (*telegram.Client, bg.StopFunc, error) { + middlewares := []telegram.Middleware{} + if config.RateLimit { + middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*100), 5)) + } client := telegram.NewClient(config.AppId, config.AppHash, telegram.Options{ - SessionStorage: storage, - Device: getDeviceConfig(), - UpdateHandler: handler, - ReconnectionBackoff: reconnectionBackoff, - RetryInterval: 5 * time.Second, - MaxRetries: 5, + SessionStorage: storage, + Middlewares: middlewares, + Device: getDeviceConfig(), + UpdateHandler: handler, }) - return client + stop, err := bg.Connect(client) + + if err != nil { + return nil, nil, err + } + + return client, stop, nil } func startBotClient(ctx context.Context, client *telegram.Client, token string) (bg.StopFunc, error) {