Spaces:
Sleeping
Sleeping
Delete pkg
Browse files- pkg/qrlogin/encoder.go +0 -52
- pkg/qrlogin/qrcode.go +0 -152
pkg/qrlogin/encoder.go
DELETED
|
@@ -1,52 +0,0 @@
|
|
| 1 |
-
// This file is a part of EverythingSuckz/TG-FileStreamBot
|
| 2 |
-
// And is licenced under the Affero General Public License.
|
| 3 |
-
// Any distributions of this code MUST be accompanied by a copy of the AGPL
|
| 4 |
-
// with proper attribution to the original author(s).
|
| 5 |
-
|
| 6 |
-
package qrlogin
|
| 7 |
-
|
| 8 |
-
import (
|
| 9 |
-
"bytes"
|
| 10 |
-
"encoding/base64"
|
| 11 |
-
"encoding/binary"
|
| 12 |
-
"errors"
|
| 13 |
-
"strings"
|
| 14 |
-
|
| 15 |
-
"github.com/gotd/td/session"
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
func EncodeToPyrogramSession(data *session.Data, appID int32) (string, error) {
|
| 19 |
-
buf := new(bytes.Buffer)
|
| 20 |
-
if err := buf.WriteByte(byte(data.DC)); err != nil {
|
| 21 |
-
return "", err
|
| 22 |
-
}
|
| 23 |
-
if err := binary.Write(buf, binary.BigEndian, appID); err != nil {
|
| 24 |
-
return "", err
|
| 25 |
-
}
|
| 26 |
-
var testMode byte
|
| 27 |
-
if data.Config.TestMode {
|
| 28 |
-
testMode = 1
|
| 29 |
-
}
|
| 30 |
-
if err := buf.WriteByte(testMode); err != nil {
|
| 31 |
-
return "", err
|
| 32 |
-
}
|
| 33 |
-
if len(data.AuthKey) != 256 {
|
| 34 |
-
return "", errors.New("auth key must be 256 bytes long")
|
| 35 |
-
}
|
| 36 |
-
if _, err := buf.Write(data.AuthKey); err != nil {
|
| 37 |
-
return "", err
|
| 38 |
-
}
|
| 39 |
-
if len(data.AuthKeyID) != 8 {
|
| 40 |
-
return "", errors.New("auth key ID must be 8 bytes long")
|
| 41 |
-
}
|
| 42 |
-
if _, err := buf.Write(data.AuthKeyID); err != nil {
|
| 43 |
-
return "", err
|
| 44 |
-
}
|
| 45 |
-
if err := buf.WriteByte(0); err != nil {
|
| 46 |
-
return "", err
|
| 47 |
-
}
|
| 48 |
-
// Convert the bytes buffer to a base64 string
|
| 49 |
-
encodedString := base64.URLEncoding.EncodeToString(buf.Bytes())
|
| 50 |
-
trimmedEncoded := strings.TrimRight(encodedString, "=")
|
| 51 |
-
return trimmedEncoded, nil
|
| 52 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pkg/qrlogin/qrcode.go
DELETED
|
@@ -1,152 +0,0 @@
|
|
| 1 |
-
// This file is a part of EverythingSuckz/TG-FileStreamBot
|
| 2 |
-
// And is licenced under the Affero General Public License.
|
| 3 |
-
// Any distributions of this code MUST be accompanied by a copy of the AGPL
|
| 4 |
-
// with proper attribution to the original author(s).
|
| 5 |
-
|
| 6 |
-
package qrlogin
|
| 7 |
-
|
| 8 |
-
import (
|
| 9 |
-
"bufio"
|
| 10 |
-
"context"
|
| 11 |
-
"encoding/json"
|
| 12 |
-
"errors"
|
| 13 |
-
"fmt"
|
| 14 |
-
"os"
|
| 15 |
-
"runtime"
|
| 16 |
-
"strings"
|
| 17 |
-
"time"
|
| 18 |
-
|
| 19 |
-
"github.com/gotd/td/session"
|
| 20 |
-
"github.com/gotd/td/telegram"
|
| 21 |
-
"github.com/gotd/td/telegram/auth/qrlogin"
|
| 22 |
-
"github.com/gotd/td/tg"
|
| 23 |
-
"github.com/gotd/td/tgerr"
|
| 24 |
-
"github.com/mdp/qrterminal"
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
type CustomWriter struct {
|
| 28 |
-
LineLength int
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
func (w *CustomWriter) Write(p []byte) (n int, err error) {
|
| 32 |
-
for _, c := range p {
|
| 33 |
-
if c == '\n' {
|
| 34 |
-
w.LineLength++
|
| 35 |
-
}
|
| 36 |
-
}
|
| 37 |
-
return os.Stdout.Write(p)
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
func printQrCode(data string, writer *CustomWriter) {
|
| 41 |
-
qrterminal.GenerateHalfBlock(data, qrterminal.L, writer)
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
func clearQrCode(writer *CustomWriter) {
|
| 45 |
-
for i := 0; i < writer.LineLength; i++ {
|
| 46 |
-
fmt.Printf("\033[F\033[K")
|
| 47 |
-
}
|
| 48 |
-
writer.LineLength = 0
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
func GenerateQRSession(apiId int, apiHash string) error {
|
| 52 |
-
ctx, cancel := context.WithCancel(context.Background())
|
| 53 |
-
defer cancel()
|
| 54 |
-
fmt.Println("Generating QR session...")
|
| 55 |
-
reader := bufio.NewReader(os.Stdin)
|
| 56 |
-
dispatcher := tg.NewUpdateDispatcher()
|
| 57 |
-
loggedIn := qrlogin.OnLoginToken(dispatcher)
|
| 58 |
-
sessionStorage := &session.StorageMemory{}
|
| 59 |
-
client := telegram.NewClient(apiId, apiHash, telegram.Options{
|
| 60 |
-
UpdateHandler: dispatcher,
|
| 61 |
-
SessionStorage: sessionStorage,
|
| 62 |
-
Device: telegram.DeviceConfig{
|
| 63 |
-
DeviceModel: "Pyrogram",
|
| 64 |
-
SystemVersion: runtime.GOOS,
|
| 65 |
-
AppVersion: "2.0",
|
| 66 |
-
},
|
| 67 |
-
})
|
| 68 |
-
var stringSession string
|
| 69 |
-
qrWriter := &CustomWriter{}
|
| 70 |
-
tickerCtx, cancelTicker := context.WithCancel(context.Background())
|
| 71 |
-
err := client.Run(ctx, func(ctx context.Context) error {
|
| 72 |
-
authorization, err := client.QR().Auth(ctx, loggedIn, func(ctx context.Context, token qrlogin.Token) error {
|
| 73 |
-
if qrWriter.LineLength == 0 {
|
| 74 |
-
fmt.Printf("\033[F\033[K")
|
| 75 |
-
}
|
| 76 |
-
clearQrCode(qrWriter)
|
| 77 |
-
printQrCode(token.URL(), qrWriter)
|
| 78 |
-
qrWriter.Write([]byte("\nTo log in, Open your Telegram app and go to Settings > Devices > Scan QR and scan the QR code.\n"))
|
| 79 |
-
go func(ctx context.Context) {
|
| 80 |
-
ticker := time.NewTicker(1 * time.Second)
|
| 81 |
-
defer ticker.Stop()
|
| 82 |
-
for {
|
| 83 |
-
select {
|
| 84 |
-
case <-ctx.Done():
|
| 85 |
-
return
|
| 86 |
-
case <-ticker.C:
|
| 87 |
-
expiresIn := time.Until(token.Expires())
|
| 88 |
-
if expiresIn <= 0 {
|
| 89 |
-
return
|
| 90 |
-
}
|
| 91 |
-
fmt.Printf("\rThis code expires in %s", expiresIn.Truncate(time.Second))
|
| 92 |
-
}
|
| 93 |
-
}
|
| 94 |
-
}(tickerCtx)
|
| 95 |
-
return nil
|
| 96 |
-
})
|
| 97 |
-
if err != nil {
|
| 98 |
-
if tgerr.Is(err, "SESSION_PASSWORD_NEEDED") {
|
| 99 |
-
cancelTicker()
|
| 100 |
-
fmt.Println("\n2FA password is required, enter it below: ")
|
| 101 |
-
passkey, _ := reader.ReadString('\n')
|
| 102 |
-
strippedPasskey := strings.TrimSpace(passkey)
|
| 103 |
-
authorization, err = client.Auth().Password(ctx, strippedPasskey)
|
| 104 |
-
if err != nil {
|
| 105 |
-
if err.Error() == "invalid password" {
|
| 106 |
-
fmt.Println("Invalid password, please try again.")
|
| 107 |
-
}
|
| 108 |
-
fmt.Println("Error while logging in: ", err)
|
| 109 |
-
return nil
|
| 110 |
-
}
|
| 111 |
-
}
|
| 112 |
-
}
|
| 113 |
-
if authorization == nil {
|
| 114 |
-
cancel()
|
| 115 |
-
return errors.New("authorization is nil")
|
| 116 |
-
}
|
| 117 |
-
user, err := client.Self(ctx)
|
| 118 |
-
if err != nil {
|
| 119 |
-
return err
|
| 120 |
-
}
|
| 121 |
-
if user.Username == "" {
|
| 122 |
-
fmt.Println("Logged in as ", user.FirstName, user.LastName)
|
| 123 |
-
} else {
|
| 124 |
-
fmt.Println("Logged in as @", user.Username)
|
| 125 |
-
}
|
| 126 |
-
res, _ := sessionStorage.LoadSession(ctx)
|
| 127 |
-
type jsonDataStruct struct {
|
| 128 |
-
Version int
|
| 129 |
-
Data session.Data
|
| 130 |
-
}
|
| 131 |
-
var jsonData jsonDataStruct
|
| 132 |
-
json.Unmarshal(res, &jsonData)
|
| 133 |
-
stringSession, err = EncodeToPyrogramSession(&jsonData.Data, int32(apiId))
|
| 134 |
-
if err != nil {
|
| 135 |
-
return err
|
| 136 |
-
}
|
| 137 |
-
fmt.Println("Your pyrogram session string:", stringSession)
|
| 138 |
-
client.API().MessagesSendMessage(
|
| 139 |
-
ctx,
|
| 140 |
-
&tg.MessagesSendMessageRequest{
|
| 141 |
-
NoWebpage: true,
|
| 142 |
-
Peer: &tg.InputPeerSelf{},
|
| 143 |
-
Message: "Your pyrogram session string: " + stringSession,
|
| 144 |
-
},
|
| 145 |
-
)
|
| 146 |
-
return nil
|
| 147 |
-
})
|
| 148 |
-
if err != nil {
|
| 149 |
-
return err
|
| 150 |
-
}
|
| 151 |
-
return nil
|
| 152 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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