added official tg client info

This commit is contained in:
divyam234 2023-08-16 02:32:08 +05:30
parent fb44d215b0
commit 2f242944dc
6 changed files with 34 additions and 13 deletions

1
.gitignore vendored
View file

@ -21,6 +21,7 @@
go.work
sessions
certs
*.env
*.env.example

View file

@ -45,5 +45,6 @@ func main() {
routes.GetRoutes(router)
//router.RunTLS(":8080", "./certs/cert.pem", "./certs/key.pem")
router.Run(":8080")
}

View file

@ -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
}

View file

@ -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

View file

@ -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}
}

View file

@ -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,
})