-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): new qrcode session generator
- Loading branch information
1 parent
70f722e
commit 5299d53
Showing
8 changed files
with
304 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"EverythingSuckz/fsb/pkg/qrlogin" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var sessionCmd = &cobra.Command{ | ||
Use: "session", | ||
Short: "Generate a string session.", | ||
DisableSuggestions: false, | ||
Run: generateSession, | ||
} | ||
|
||
func init() { | ||
sessionCmd.Flags().StringP("login-type", "T", "qr", "The login type to use. Can be either 'qr' or 'phone'") | ||
sessionCmd.Flags().Int32P("api-id", "I", 0, "The API ID to use for the session (required).") | ||
sessionCmd.Flags().StringP("api-hash", "H", "", "The API hash to use for the session (required).") | ||
sessionCmd.MarkFlagRequired("api-id") | ||
sessionCmd.MarkFlagRequired("api-hash") | ||
} | ||
|
||
func generateSession(cmd *cobra.Command, args []string) { | ||
loginType, _ := cmd.Flags().GetString("login-type") | ||
apiId, _ := cmd.Flags().GetInt32("api-id") | ||
apiHash, _ := cmd.Flags().GetString("api-hash") | ||
if loginType == "qr" { | ||
qrlogin.GenerateQRSession(int(apiId), apiHash) | ||
} else if loginType == "phone" { | ||
generatePhoneSession() | ||
} else { | ||
fmt.Println("Invalid login type. Please use either 'qr' or 'phone'") | ||
} | ||
} | ||
|
||
func generatePhoneSession() { | ||
fmt.Println("Phone session is not implemented yet.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// This file is a part of EverythingSuckz/TG-FileStreamBot | ||
// And is licenced under the Affero General Public License. | ||
// Any distributions of this code MUST be accompanied by a copy of the AGPL | ||
// with proper attribution to the original author(s). | ||
|
||
package qrlogin | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"encoding/binary" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/gotd/td/session" | ||
) | ||
|
||
func EncodeToPyrogramSession(data *session.Data, appID int32) (string, error) { | ||
buf := new(bytes.Buffer) | ||
if err := buf.WriteByte(byte(data.DC)); err != nil { | ||
return "", err | ||
} | ||
if err := binary.Write(buf, binary.BigEndian, appID); err != nil { | ||
return "", err | ||
} | ||
var testMode byte | ||
if data.Config.TestMode { | ||
testMode = 1 | ||
} | ||
if err := buf.WriteByte(testMode); err != nil { | ||
return "", err | ||
} | ||
if len(data.AuthKey) != 256 { | ||
return "", errors.New("auth key must be 256 bytes long") | ||
} | ||
if _, err := buf.Write(data.AuthKey); err != nil { | ||
return "", err | ||
} | ||
if len(data.AuthKeyID) != 8 { | ||
return "", errors.New("auth key ID must be 8 bytes long") | ||
} | ||
if _, err := buf.Write(data.AuthKeyID); err != nil { | ||
return "", err | ||
} | ||
if err := buf.WriteByte(0); err != nil { | ||
return "", err | ||
} | ||
// Convert the bytes buffer to a base64 string | ||
encodedString := base64.URLEncoding.EncodeToString(buf.Bytes()) | ||
trimmedEncoded := strings.TrimRight(encodedString, "=") | ||
return trimmedEncoded, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// This file is a part of EverythingSuckz/TG-FileStreamBot | ||
// And is licenced under the Affero General Public License. | ||
// Any distributions of this code MUST be accompanied by a copy of the AGPL | ||
// with proper attribution to the original author(s). | ||
|
||
package qrlogin | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gotd/td/session" | ||
"github.com/gotd/td/telegram" | ||
"github.com/gotd/td/telegram/auth/qrlogin" | ||
"github.com/gotd/td/tg" | ||
"github.com/gotd/td/tgerr" | ||
"github.com/mdp/qrterminal" | ||
) | ||
|
||
type CustomWriter struct { | ||
LineLength int | ||
} | ||
|
||
func (w *CustomWriter) Write(p []byte) (n int, err error) { | ||
for _, c := range p { | ||
if c == '\n' { | ||
w.LineLength++ | ||
} | ||
} | ||
return os.Stdout.Write(p) | ||
} | ||
|
||
func printQrCode(data string, writer *CustomWriter) { | ||
qrterminal.GenerateHalfBlock(data, qrterminal.L, writer) | ||
} | ||
|
||
func clearQrCode(writer *CustomWriter) { | ||
for i := 0; i < writer.LineLength; i++ { | ||
fmt.Printf("\033[F\033[K") | ||
} | ||
writer.LineLength = 0 | ||
} | ||
|
||
func GenerateQRSession(apiId int, apiHash string) error { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
fmt.Println("Generating QR session...") | ||
reader := bufio.NewReader(os.Stdin) | ||
dispatcher := tg.NewUpdateDispatcher() | ||
loggedIn := qrlogin.OnLoginToken(dispatcher) | ||
sessionStorage := &session.StorageMemory{} | ||
client := telegram.NewClient(apiId, apiHash, telegram.Options{ | ||
UpdateHandler: dispatcher, | ||
SessionStorage: sessionStorage, | ||
Device: telegram.DeviceConfig{ | ||
DeviceModel: "Pyrogram", | ||
SystemVersion: runtime.GOOS, | ||
AppVersion: "2.0", | ||
}, | ||
}) | ||
var stringSession string | ||
qrWriter := &CustomWriter{} | ||
tickerCtx, cancelTicker := context.WithCancel(context.Background()) | ||
err := client.Run(ctx, func(ctx context.Context) error { | ||
authorization, err := client.QR().Auth(ctx, loggedIn, func(ctx context.Context, token qrlogin.Token) error { | ||
if qrWriter.LineLength == 0 { | ||
fmt.Printf("\033[F\033[K") | ||
} | ||
clearQrCode(qrWriter) | ||
printQrCode(token.URL(), qrWriter) | ||
qrWriter.Write([]byte("\nTo log in, Open your Telegram app and go to Settings > Devices > Scan QR and scan the QR code.\n")) | ||
go func(ctx context.Context) { | ||
ticker := time.NewTicker(1 * time.Second) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
expiresIn := time.Until(token.Expires()) | ||
if expiresIn <= 0 { | ||
return | ||
} | ||
fmt.Printf("\rThis code expires in %s", expiresIn.Truncate(time.Second)) | ||
} | ||
} | ||
}(tickerCtx) | ||
return nil | ||
}) | ||
if err != nil { | ||
if tgerr.Is(err, "SESSION_PASSWORD_NEEDED") { | ||
cancelTicker() | ||
fmt.Println("\n2FA password is required, enter it below: ") | ||
passkey, _ := reader.ReadString('\n') | ||
strippedPasskey := strings.TrimSpace(passkey) | ||
authorization, err = client.Auth().Password(ctx, strippedPasskey) | ||
if err != nil { | ||
if err.Error() == "invalid password" { | ||
fmt.Println("Invalid password, please try again.") | ||
} | ||
fmt.Println("Error while logging in: ", err) | ||
return nil | ||
} | ||
} | ||
} | ||
if authorization == nil { | ||
cancel() | ||
return errors.New("authorization is nil") | ||
} | ||
user, err := client.Self(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if user.Username == "" { | ||
fmt.Println("Logged in as ", user.FirstName, user.LastName) | ||
} else { | ||
fmt.Println("Logged in as @", user.Username) | ||
} | ||
res, _ := sessionStorage.LoadSession(ctx) | ||
type jsonDataStruct struct { | ||
Version int | ||
Data session.Data | ||
} | ||
var jsonData jsonDataStruct | ||
json.Unmarshal(res, &jsonData) | ||
stringSession, err = EncodeToPyrogramSession(&jsonData.Data, int32(apiId)) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("Your pyrogram session string:", stringSession) | ||
client.API().MessagesSendMessage( | ||
ctx, | ||
&tg.MessagesSendMessageRequest{ | ||
NoWebpage: true, | ||
Peer: &tg.InputPeerSelf{}, | ||
Message: "Your pyrogram session string: " + stringSession, | ||
}, | ||
) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |