Skip to content

Commit

Permalink
feat: reflactor session
Browse files Browse the repository at this point in the history
  • Loading branch information
wubenqi committed May 15, 2024
1 parent 7b1759c commit 8bc277d
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 324 deletions.
249 changes: 1 addition & 248 deletions app/interface/session/internal/dao/auth_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,18 @@ package dao

import (
"context"
"errors"
"fmt"
"strconv"
"time"

"github.com/teamgram/marmota/pkg/hack"
"github.com/teamgram/proto/mtproto"
"github.com/teamgram/teamgram-server/app/service/authsession/authsession"

"github.com/zeromicro/go-zero/core/logx"
)

type cacheAuthValue struct {
UserId int64
Layer int32
pushSessionId int64
client string
langpack string
SaltList []*mtproto.TLFutureSalt
PermAuthKeyId int64
SaltList []*mtproto.TLFutureSalt
}

// Size Impl cache.Value interface
Expand All @@ -61,151 +53,6 @@ func (d *Dao) getCacheValue(authKeyId int64) *cacheAuthValue {
}
}

func (d *Dao) GetCacheUserID(ctx context.Context, authKeyId int64) (int64, bool) {
cv := d.getCacheValue(authKeyId)
if cv.UserId == 0 {
id, err := d.AuthsessionClient.AuthsessionGetUserId(ctx, &authsession.TLAuthsessionGetUserId{
AuthKeyId: authKeyId,
})
if err != nil {
logx.WithContext(ctx).Error(err.Error())
if errors.Is(err, mtproto.ErrAuthKeyPermEmpty) {
return 0, true
} else {
return 0, false
}
}

// update to cache
cv.UserId = id.GetV()
}
return cv.UserId, true
}

func (d *Dao) GetCachePushSessionID(ctx context.Context, userId int64, authKeyId int64) (int64, bool) {
cv := d.getCacheValue(authKeyId)
if cv.pushSessionId == 0 {
id, err := d.AuthsessionClient.AuthsessionGetPushSessionId(ctx, &authsession.TLAuthsessionGetPushSessionId{
UserId: userId,
AuthKeyId: authKeyId,
TokenType: 7,
})
if err != nil {
logx.WithContext(ctx).Error(err.Error())
return 0, false
}
cv.pushSessionId = id.GetV()
}

return cv.pushSessionId, true
}

func (d *Dao) GetCacheApiLayer(ctx context.Context, authKeyId int64) (int32, bool) {
cv := d.getCacheValue(authKeyId)
if cv.Layer == 0 {
id, err := d.AuthsessionClient.AuthsessionGetLayer(ctx, &authsession.TLAuthsessionGetLayer{
AuthKeyId: authKeyId,
})
if err != nil {
logx.WithContext(ctx).Errorf(err.Error())
return 0, false
}
//if r.Result != 0 {
// log.Errorf("queryAuthKey err: {%v}", r)
// return 0, false
//}

// update to cache
cv.Layer = id.GetV()
}

return cv.Layer, true
}

func (d *Dao) GetCacheClient(ctx context.Context, authKeyId int64) string {
cv := d.getCacheValue(authKeyId)
if cv.client == "" {
r, err := d.AuthsessionClient.AuthsessionGetClient(ctx, &authsession.TLAuthsessionGetClient{
AuthKeyId: authKeyId,
})
if err != nil {
logx.WithContext(ctx).Error(err.Error())
return ""
}

// update to cache
cv.client = r.GetV()
}

return cv.client
}

func (d *Dao) GetCacheLangpack(ctx context.Context, authKeyId int64) string {
cv := d.getCacheValue(authKeyId)
if cv.langpack == "" {
r, err := d.AuthsessionClient.AuthsessionGetLangPack(ctx, &authsession.TLAuthsessionGetLangPack{
AuthKeyId: authKeyId,
})
if err != nil {
logx.WithContext(ctx).Errorf(err.Error())
return ""
}

// update to cache
cv.langpack = r.GetV()
}

return cv.langpack
}

func (d *Dao) GetCachePermAuthKeyId(ctx context.Context, authKeyId int64) int64 {
cv := d.getCacheValue(authKeyId)
if cv.PermAuthKeyId == 0 {
r, err := d.AuthsessionClient.AuthsessionGetPermAuthKeyId(ctx, &authsession.TLAuthsessionGetPermAuthKeyId{
AuthKeyId: authKeyId,
})
if err != nil {
logx.WithContext(ctx).Errorf(err.Error())
return 0
}

// update to cache
cv.PermAuthKeyId = r.GetV()
}

return cv.PermAuthKeyId
}

func (d *Dao) PutCacheApiLayer(ctx context.Context, authKeyId int64, layer int32) {
cv := d.getCacheValue(authKeyId)
cv.Layer = layer
}

func (d *Dao) PutCacheClient(ctx context.Context, authKeyId int64, v string) {
cv := d.getCacheValue(authKeyId)
cv.client = v
}

func (d *Dao) PutCacheLangpack(ctx context.Context, authKeyId int64, v string) {
cv := d.getCacheValue(authKeyId)
cv.langpack = v
}

func (d *Dao) PutCacheUserId(ctx context.Context, authKeyId int64, userId int64) {
cv := d.getCacheValue(authKeyId)
cv.UserId = userId
}

func (d *Dao) PutCachePushSessionId(ctx context.Context, authKeyId, sessionId int64) {
cv := d.getCacheValue(authKeyId)
cv.pushSessionId = sessionId
}

func (d *Dao) PutCachePermAuthKeyId(ctx context.Context, authKeyId, kId int64) {
cv := d.getCacheValue(authKeyId)
cv.PermAuthKeyId = kId
}

func (d *Dao) getFutureSaltList(ctx context.Context, authKeyId int64) ([]*mtproto.TLFutureSalt, bool) {
var (
cv = d.getCacheValue(authKeyId)
Expand Down Expand Up @@ -251,94 +98,6 @@ func (d *Dao) getFutureSaltList(ctx context.Context, authKeyId int64) ([]*mtprot
return nil, false
}

func (d *Dao) PutLayer(ctx context.Context, authKeyId int64, layer int32, ip string) error {
_, err := d.AuthsessionClient.AuthsessionSetLayer(
ctx,
&authsession.TLAuthsessionSetLayer{
AuthKeyId: authKeyId,
Ip: ip,
Layer: layer,
})

if err != nil {
logx.WithContext(ctx).Errorf("PutLayer - error: %v", err)
return err
}

return nil
}

func (d *Dao) PutInitConnection(ctx context.Context, authKeyId int64, ip string, initConnection *mtproto.TLInitConnection) error {
var (
proxy string
params string
)

if initConnection.GetProxy() != nil {
proxy = hack.String(mtproto.TLObjectToJson(initConnection.Proxy))
}
if initConnection.GetParams() != nil {
params = hack.String(mtproto.TLObjectToJson(initConnection.Params))
}

_, err := d.AuthsessionClient.AuthsessionSetInitConnection(
ctx,
&authsession.TLAuthsessionSetInitConnection{
AuthKeyId: authKeyId,
Ip: ip,
ApiId: initConnection.GetApiId(),
DeviceModel: initConnection.GetDeviceModel(),
SystemVersion: initConnection.GetSystemVersion(),
AppVersion: initConnection.GetAppVersion(),
SystemLangCode: initConnection.GetSystemLangCode(),
LangPack: initConnection.GetLangPack(),
LangCode: initConnection.GetLangCode(),
Proxy: proxy,
Params: params,
})

if err != nil {
logx.WithContext(ctx).Errorf("PutInitConnection - error: %v", err)
return err
}

return nil
}

func (d *Dao) PutUploadInitConnection(ctx context.Context, authKeyId int64, layer int32, ip string, initConnection *mtproto.TLInitConnection) error {
session := authsession.MakeTLClientSession(&authsession.ClientSession{
AuthKeyId: authKeyId,
Ip: ip,
Layer: layer,
ApiId: initConnection.GetApiId(),
DeviceModel: initConnection.GetDeviceModel(),
SystemVersion: initConnection.GetSystemVersion(),
AppVersion: initConnection.GetAppVersion(),
SystemLangCode: initConnection.GetSystemLangCode(),
LangPack: initConnection.GetLangPack(),
LangCode: initConnection.GetLangCode(),
Proxy: "",
Params: "",
}).To_ClientSession()

if initConnection.GetProxy() != nil {
session.Proxy = hack.String(mtproto.TLObjectToJson(initConnection.Proxy))
}
if initConnection.GetParams() != nil {
session.Params = hack.String(mtproto.TLObjectToJson(initConnection.Params))
}

_, err := d.AuthsessionClient.AuthsessionSetClientSessionInfo(ctx, &authsession.TLAuthsessionSetClientSessionInfo{
Data: session,
})

if err != nil {
logx.WithContext(ctx).Error(err.Error())
}

return err
}

func (d *Dao) GetOrFetchNewSalt(ctx context.Context, authKeyId int64) (salt, lastInvalidSalt *mtproto.TLFutureSalt, err error) {
cacheSalts, _ := d.getFutureSaltList(ctx, authKeyId)
//TODO(@benqi): check len(cacheSalts) > 0
Expand All @@ -359,9 +118,3 @@ func (d *Dao) GetFutureSalts(ctx context.Context, authKeyId int64, num int32) ([

return cacheSalts, nil
}

func (d *Dao) GetKeyStateData(ctx context.Context, authKeyId int64) (*authsession.AuthKeyStateData, error) {
return d.AuthsessionClient.AuthsessionGetAuthStateData(ctx, &authsession.TLAuthsessionGetAuthStateData{
AuthKeyId: authKeyId,
})
}
8 changes: 0 additions & 8 deletions app/interface/session/internal/dao/gateway_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ import (
"github.com/zeromicro/go-zero/zrpc"
)

// eGateOptions comet options.
type gatewayOptions struct {
RoutineSize uint64
RoutineChan uint64
}

// Gateway eGateClient is a gateway.
type Gateway struct {
serverId string
Expand All @@ -48,8 +42,6 @@ type Gateway struct {
func NewGateway(c zrpc.RpcClientConf) (*Gateway, error) {
g := &Gateway{
serverId: c.Endpoints[0],
// client: make([]chan interface{}, options.RoutineSize),
// options: options,
}

cli, err := zrpc.NewClient(c)
Expand Down
8 changes: 5 additions & 3 deletions app/interface/session/internal/sess/check_api_request_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ import (
func checkRpcWithoutLogin(tl mtproto.TLObject) bool {
switch tl.(type) {
// account
// case *mtproto.TLAccountGetPassword:
// return true
case *mtproto.TLAccountGetPassword:
return true

// auth
case *mtproto.TLAuthSendCode,
Expand All @@ -80,7 +80,9 @@ func checkRpcWithoutLogin(tl mtproto.TLObject) bool {
*mtproto.TLAuthExportLoginToken,
*mtproto.TLAuthAcceptLoginToken,
*mtproto.TLAuthLogOut, // TODO: before process, try fetch usrId
*mtproto.TLAuthBindTempAuthKey:
*mtproto.TLAuthBindTempAuthKey,
*mtproto.TLAuthCheckPassword:

return true

// help
Expand Down
Loading

0 comments on commit 8bc277d

Please sign in to comment.