-
-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(login): support import desktop client session
- Loading branch information
Showing
3 changed files
with
84 additions
and
3 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
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 | ||
} |
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 |
---|---|---|
@@ -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") | ||
} |