From 2f242944dcd03b37c63e2ec51a3988a01e42a371 Mon Sep 17 00:00:00 2001 From: divyam234 Date: Wed, 16 Aug 2023 02:32:08 +0530 Subject: [PATCH] added official tg client info --- .gitignore | 1 + main.go | 1 + services/auth.service.go | 15 ++++++++++----- services/file.service.go | 2 +- services/upload.service.go | 2 +- utils/tgclient.go | 26 ++++++++++++++++++++------ 6 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index b7565dd..1d42a2f 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ go.work sessions +certs *.env *.env.example diff --git a/main.go b/main.go index 05ffcd4..6973486 100644 --- a/main.go +++ b/main.go @@ -45,5 +45,6 @@ func main() { routes.GetRoutes(router) + //router.RunTLS(":8080", "./certs/cert.pem", "./certs/key.pem") router.Run(":8080") } diff --git a/services/auth.service.go b/services/auth.service.go index 6feb53f..826905d 100644 --- a/services/auth.service.go +++ b/services/auth.service.go @@ -138,8 +138,9 @@ func (as *AuthService) LogIn(c *gin.Context) (*schemas.Message, *types.AppError) }).Create(&user).Error; err != nil { return nil, &types.AppError{Error: errors.New("failed to create or update user"), Code: http.StatusInternalServerError} } - - c.SetCookie(GetUserSessionCookieName(c), jweToken, as.SessionMaxAge, "/", c.Request.Host, false, false) + isHttps := c.Request.URL.Scheme == "https" + c.SetSameSite(2) + c.SetCookie(GetUserSessionCookieName(c), jweToken, as.SessionMaxAge, "/", c.Request.Host, isHttps, true) return &schemas.Message{Status: true, Message: "login success"}, nil } @@ -172,7 +173,9 @@ func (as *AuthService) GetSession(c *gin.Context) *types.Session { if err != nil { return nil } - c.SetCookie(GetUserSessionCookieName(c), jweToken, as.SessionMaxAge, "/", c.Request.Host, false, false) + isHttps := c.Request.URL.Scheme == "https" + c.SetSameSite(2) + c.SetCookie(GetUserSessionCookieName(c), jweToken, as.SessionMaxAge, "/", c.Request.Host, isHttps, true) return session } @@ -180,7 +183,7 @@ func (as *AuthService) Logout(c *gin.Context) (*schemas.Message, *types.AppError val, _ := c.Get("jwtUser") jwtUser := val.(*types.JWTClaims) userId, _ := strconv.Atoi(jwtUser.Subject) - tgClient, err, stop := utils.GetAuthClient(jwtUser.TgSession, userId) + tgClient, stop, err := utils.GetAuthClient(jwtUser.TgSession, userId) if err != nil { return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} @@ -188,7 +191,9 @@ func (as *AuthService) Logout(c *gin.Context) (*schemas.Message, *types.AppError tgClient.Tg.API().AuthLogOut(c) utils.StopClient(stop, userId) - c.SetCookie(GetUserSessionCookieName(c), "", -1, "/", c.Request.Host, false, false) + isHttps := c.Request.URL.Scheme == "https" + c.SetSameSite(2) + c.SetCookie(GetUserSessionCookieName(c), "", -1, "/", c.Request.Host, isHttps, true) return &schemas.Message{Status: true, Message: "logout success"}, nil } diff --git a/services/file.service.go b/services/file.service.go index fa2fdaf..aa4c0a0 100644 --- a/services/file.service.go +++ b/services/file.service.go @@ -274,7 +274,7 @@ func (fs *FileService) GetFileStream(c *gin.Context) { val, _ := c.Get("jwtUser") jwtUser := val.(*types.JWTClaims) userId, _ := strconv.Atoi(jwtUser.Subject) - tgClient, err, _ = utils.GetAuthClient(jwtUser.TgSession, userId) + tgClient, _, err = utils.GetAuthClient(jwtUser.TgSession, userId) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/services/upload.service.go b/services/upload.service.go index b60eec0..e19f287 100644 --- a/services/upload.service.go +++ b/services/upload.service.go @@ -74,7 +74,7 @@ func (us *UploadService) UploadFile(c *gin.Context) (*schemas.UploadPartOut, *ty val, _ := c.Get("jwtUser") jwtUser := val.(*types.JWTClaims) userId, _ := strconv.Atoi(jwtUser.Subject) - tgClient, err, _ = utils.GetAuthClient(jwtUser.TgSession, userId) + tgClient, _, err = utils.GetAuthClient(jwtUser.TgSession, userId) if err != nil { return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } diff --git a/utils/tgclient.go b/utils/tgclient.go index 92cd201..d12ae6f 100644 --- a/utils/tgclient.go +++ b/utils/tgclient.go @@ -26,6 +26,17 @@ type Client struct { var clients map[int]*Client +func getDeviceConfig() telegram.DeviceConfig { + config := telegram.DeviceConfig{ + DeviceModel: "Desktop", + SystemVersion: "Windows 11", + AppVersion: "4.9.1 x64", + SystemLangCode: "en-US", + LangPack: "tdesktop", + LangCode: "en", + } + return config +} func getBotClient(appID int, appHash, clientName, sessionDir string) *telegram.Client { sessionStorage := &telegram.FileSessionStorage{ @@ -36,6 +47,7 @@ func getBotClient(appID int, appHash, clientName, sessionDir string) *telegram.C // Middlewares: []telegram.Middleware{ // ratelimit.New(rate.Every(time.Millisecond*100), 5), // }, + Device: getDeviceConfig(), NoUpdates: true, } @@ -104,7 +116,7 @@ func StartBotTgClients() { } -func GetAuthClient(sessionStr string, userId int) (*Client, error, bg.StopFunc) { +func GetAuthClient(sessionStr string, userId int) (*Client, bg.StopFunc, error) { if client, ok := clients[userId]; ok { return client, nil, nil @@ -115,7 +127,7 @@ func GetAuthClient(sessionStr string, userId int) (*Client, error, bg.StopFunc) data, err := session.TelethonSession(sessionStr) if err != nil { - return nil, err, nil + return nil, nil, err } var ( @@ -124,7 +136,7 @@ func GetAuthClient(sessionStr string, userId int) (*Client, error, bg.StopFunc) ) if err := loader.Save(ctx, data); err != nil { - return nil, err, nil + return nil, nil, err } client := telegram.NewClient(config.AppId, config.AppHash, telegram.Options{ @@ -132,19 +144,20 @@ func GetAuthClient(sessionStr string, userId int) (*Client, error, bg.StopFunc) Middlewares: []telegram.Middleware{ ratelimit.New(rate.Every(time.Millisecond*100), 5), }, + Device: getDeviceConfig(), NoUpdates: true, }) stop, err := bg.Connect(client) if err != nil { - return nil, err, nil + return nil, nil, err } tguser, err := client.Self(ctx) if err != nil { - return nil, err, nil + return nil, nil, err } Logger.Info("started Client", zap.String("user", tguser.Username)) @@ -153,7 +166,7 @@ func GetAuthClient(sessionStr string, userId int) (*Client, error, bg.StopFunc) clients[int(tguser.GetID())] = tgClient - return tgClient, nil, stop + return tgClient, stop, nil } func GetBotClient() *Client { @@ -173,6 +186,7 @@ func GetNonAuthClient(handler telegram.UpdateHandler, storage telegram.SessionSt Middlewares: []telegram.Middleware{ ratelimit.New(rate.Every(time.Millisecond*100), 5), }, + Device: getDeviceConfig(), UpdateHandler: handler, })