Skip to content

Commit

Permalink
feat(login): support import desktop client session
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Sep 17, 2022
1 parent e63f8bf commit 2aa6c25
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
66 changes: 66 additions & 0 deletions app/login/desktop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package login

import (
"context"
"fmt"
"github.com/dpastoor/go-input"
"github.com/fatih/color"
"github.com/gotd/td/session"
"github.com/gotd/td/session/tdesktop"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/storage"
"path/filepath"
"strconv"
)

const tdata = "tdata"

func Desktop(ctx context.Context, ns, desktop string) error {
kvd, err := kv.New(kv.Options{
Path: consts.KVPath,
NS: ns,
})

color.Blue("Importing session from desktop client: %s", desktop)

if filepath.Base(desktop) != tdata {
desktop = filepath.Join(desktop, tdata)
}
accounts, err := tdesktop.Read(desktop, nil)
if err != nil {
return err
}

infos := make([]string, 0, len(accounts))
infoMap := make(map[string]tdesktop.Account)
for _, acc := range accounts {
id := strconv.FormatUint(acc.Authorization.UserID, 10)
infos = append(infos, id)
infoMap[id] = acc
}

color.Blue("You can get user id from @userinfobot")
fmt.Println()

acc, err := input.DefaultUI().Select(color.BlueString("Select a user id:"), infos, &input.Options{
Loop: true,
Required: true,
})
if err != nil {
return err
}

data, err := session.TDesktopSession(infoMap[acc])
if err != nil {
return err
}

loader := &session.Loader{Storage: storage.NewSession(kvd, true)}
if err = loader.Save(ctx, data); err != nil {
return err
}

color.Green("Import %s successfully to '%s' namespace!", acc, ns)
return nil
}
3 changes: 1 addition & 2 deletions app/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/iyear/tdl/pkg/kv"
)

func Run(ctx context.Context, ns, proxy string) error {
func Code(ctx context.Context, ns, proxy string) error {
kvd, err := kv.New(kv.Options{
Path: consts.KVPath,
NS: ns,
Expand All @@ -26,7 +26,6 @@ func Run(ctx context.Context, ns, proxy string) error {
}

color.Blue("Login...")
color.Yellow("WARN: If data exists in the namespace, data will be overwritten")

flow := auth.NewFlow(termAuth{}, auth.SendCodeOptions{})
if err := c.Auth().IfNecessary(ctx, flow); err != nil {
Expand Down
18 changes: 17 additions & 1 deletion cmd/login/login.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package login

import (
"github.com/fatih/color"
"github.com/iyear/tdl/app/login"
"github.com/spf13/cobra"
)

var (
desktop string
)

var Cmd = &cobra.Command{
Use: "login",
Short: "Login to Telegram",
Example: "tdl login -n iyear --proxy socks5://localhost:1080",
RunE: func(cmd *cobra.Command, args []string) error {
return login.Run(cmd.Context(), cmd.Flag("ns").Value.String(), cmd.Flag("proxy").Value.String())
ns := cmd.Flag("ns").Value.String()

color.Yellow("WARN: If data exists in the namespace, data will be overwritten")

if desktop != "" {
return login.Desktop(cmd.Context(), ns, desktop)
}
return login.Code(cmd.Context(), ns, cmd.Flag("proxy").Value.String())
},
}

func init() {
Cmd.Flags().StringVarP(&desktop, "desktop", "d", "", "Official desktop client path, import session from it")
}

0 comments on commit 2aa6c25

Please sign in to comment.