package telegram
import (
"context"
"strings"
"time"
"github.com/go-faster/errors"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"github.com/gotd/log"
"github.com/gotd/td/bin"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgerr"
)
type AuthLostKind int
const (
AuthLostUnknown AuthLostKind = iota
AuthLostBanned
AuthLostDeleted
AuthLostRevoked
AuthLostUnregistered
AuthLostExpired
AuthLostKeyInvalid
)
func (k AuthLostKind ) String () string {
switch k {
case AuthLostBanned :
return "banned"
case AuthLostDeleted :
return "deleted"
case AuthLostRevoked :
return "revoked"
case AuthLostUnregistered :
return "unregistered"
case AuthLostExpired :
return "expired"
case AuthLostKeyInvalid :
return "key_invalid"
default :
return "unknown"
}
}
func AuthLostKindOf (err error ) AuthLostKind {
rpcErr , ok := tgerr .As (err )
if !ok {
return AuthLostUnknown
}
switch rpcErr .Type {
case "USER_DEACTIVATED_BAN" :
return AuthLostBanned
case "USER_DEACTIVATED" :
return AuthLostDeleted
case "SESSION_REVOKED" :
return AuthLostRevoked
case "AUTH_KEY_UNREGISTERED" :
return AuthLostUnregistered
case "SESSION_EXPIRED" :
return AuthLostExpired
case "AUTH_KEY_INVALID" , "AUTH_KEY_DUPLICATED" :
return AuthLostKeyInvalid
}
if strings .HasPrefix (rpcErr .Type , authKeyDropPrefix ) {
return AuthLostRevoked
}
return AuthLostUnknown
}
func IsAccountBanned (err error ) bool {
return tgerr .Is (err , "USER_DEACTIVATED_BAN" , tg .ErrPhoneNumberBanned )
}
func (e *AuthLostError ) Kind () AuthLostKind {
return AuthLostKindOf (e .Err )
}
const authKeyDropPrefix = "AUTH_KEY_DROP_"
func AuthKeyDropError (u *tg .UpdateServiceNotification ) error {
return &tgerr .Error {
Code : 401 ,
Type : u .Type ,
Message : u .Message ,
}
}
func (c *Client ) handleServiceNotification (u *tg .UpdateServiceNotification ) {
ctx := context .Background ()
if strings .HasPrefix (u .Type , authKeyDropPrefix ) {
c .log .Warn (ctx , "Server asked to drop the auth key" ,
log .String ("type" , u .Type ),
log .String ("message" , u .Message ),
)
c .authLostDetected (AuthKeyDropError (u ))
return
}
if u .Popup {
c .log .Info (ctx , "Service notification" ,
log .String ("type" , u .Type ),
log .String ("message" , u .Message ),
)
return
}
c .log .Debug (ctx , "Service notification" , log .String ("type" , u .Type ))
}
type FreezeState struct {
Since time .Time
Until time .Time
AppealURL string
}
func (s FreezeState ) Frozen () bool {
return !s .Since .IsZero ()
}
func FreezeStateFrom (cfg tg .JSONValueClass ) FreezeState {
var s FreezeState
obj , ok := cfg .(*tg .JSONObject )
if !ok {
return s
}
unix := func (v tg .JSONValueClass ) time .Time {
n , ok := v .(*tg .JSONNumber )
if !ok || n .Value <= 0 {
return time .Time {}
}
return time .Unix (int64 (n .Value ), 0 )
}
for _ , kv := range obj .Value {
switch kv .Key {
case "freeze_since_date" :
s .Since = unix (kv .Value )
case "freeze_until_date" :
s .Until = unix (kv .Value )
case "freeze_appeal_url" :
if str , ok := kv .Value .(*tg .JSONString ); ok {
s .AppealURL = str .Value
}
}
}
return s
}
func (c *Client ) FreezeState (ctx context .Context ) (FreezeState , error ) {
cfg , err := c .API ().HelpGetAppConfig (ctx , 0 )
if err != nil {
return FreezeState {}, errors .Wrap (err , "get app config" )
}
app , ok := cfg .(*tg .HelpAppConfig )
if !ok {
return FreezeState {}, errors .Errorf ("unexpected app config %T" , cfg )
}
return FreezeStateFrom (app .Config ), nil
}
func IsFrozen (err error ) bool {
return tgerr .Is (err , tg .ErrFrozenMethodInvalid , tg .ErrFrozenParticipantMissing )
}
const freezeCheckInterval = time .Hour
func (c *Client ) observeRestriction (input bin .Encoder , err error ) {
rpcErr , ok := tgerr .As (err )
if !ok {
return
}
frozen := IsFrozen (err )
if !frozen && !rpcErr .IsType (tg .ErrPhoneNumberBanned ) {
return
}
ctx := context .Background ()
c .meters ().restricted .Add (ctx , 1 , metric .WithAttributes (
attribute .String ("reason" , rpcErr .Type ),
))
method := methodName (input )
if !frozen {
c .log .Error (ctx , "Phone number is banned by Telegram. " +
"If this is a mistake, log in from an official mobile app and follow " +
"its instructions to recover the account" ,
log .String ("method" , method ),
)
return
}
c .log .Debug (ctx , "Request refused: account is frozen" ,
log .String ("reason" , rpcErr .Type ),
log .String ("method" , method ),
)
now := c .clock .Now ()
c .freezeMux .Lock ()
if !c .freezeChecked .IsZero () && now .Sub (c .freezeChecked ) < freezeCheckInterval {
c .freezeMux .Unlock ()
return
}
c .freezeChecked = now
c .freezeMux .Unlock ()
runCtx := c .ctx
if runCtx == nil || runCtx .Err () != nil {
return
}
go c .checkFreeze (runCtx , method )
}
func (c *Client ) checkFreeze (ctx context .Context , method string ) {
state , err := c .FreezeState (ctx )
if err != nil {
if ctx .Err () == nil {
c .log .Warn (ctx , "Failed to check freeze state" , log .Error (err ))
}
return
}
if !state .Frozen () {
c .log .Warn (ctx , "Frozen error, but app config has no freeze state" ,
log .String ("method" , method ),
)
return
}
c .log .Warn (ctx , "Account is frozen" ,
log .Time ("since" , state .Since ),
log .Time ("until" , state .Until ),
log .String ("appeal_url" , state .AppealURL ),
log .String ("method" , method ),
)
if c .onFrozen != nil {
c .onFrozen (ctx , state )
}
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .