mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Added support for storing login credential permanently
This commit is contained in:
parent
252762f410
commit
733f4a97ea
2 changed files with 81 additions and 40 deletions
|
|
@ -1,13 +1,20 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/ByteDream/crunchyroll-go"
|
"github.com/ByteDream/crunchyroll-go"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
sessionIDFlag bool
|
loginSessionIDFlag bool
|
||||||
|
|
||||||
|
loginPersistentFlag bool
|
||||||
)
|
)
|
||||||
|
|
||||||
var loginCmd = &cobra.Command{
|
var loginCmd = &cobra.Command{
|
||||||
|
|
@ -15,36 +22,55 @@ var loginCmd = &cobra.Command{
|
||||||
Short: "Login to crunchyroll",
|
Short: "Login to crunchyroll",
|
||||||
Args: cobra.RangeArgs(1, 2),
|
Args: cobra.RangeArgs(1, 2),
|
||||||
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if sessionIDFlag {
|
if loginSessionIDFlag {
|
||||||
return loginSessionID(args[0], false)
|
loginSessionID(args[0])
|
||||||
} else {
|
} else {
|
||||||
return loginCredentials(args[0], args[1])
|
loginCredentials(args[0], args[1])
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
loginCmd.Flags().BoolVar(&loginSessionIDFlag, "session-id", false, "Use a session id to login instead of username and password")
|
||||||
|
|
||||||
|
loginCmd.Flags().BoolVar(&loginPersistentFlag, "persistent", false, "If the given credential should be stored persistent")
|
||||||
|
|
||||||
rootCmd.AddCommand(loginCmd)
|
rootCmd.AddCommand(loginCmd)
|
||||||
loginCmd.Flags().BoolVar(&sessionIDFlag, "session-id", false, "session id")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginCredentials(email, password string) error {
|
func loginCredentials(user, password string) error {
|
||||||
out.Debug("Logging in via credentials")
|
out.Debug("Logging in via credentials")
|
||||||
session, err := crunchyroll.LoginWithCredentials(email, password, locale, client)
|
if _, err := crunchyroll.LoginWithCredentials(user, password, systemLocale(), client); err != nil {
|
||||||
if err != nil {
|
out.Err(err.Error())
|
||||||
return err
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return loginSessionID(session.SessionID, true)
|
|
||||||
|
return ioutil.WriteFile(loginStorePath(), []byte(fmt.Sprintf("%s\n%s", user, password)), 0600)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginSessionID(sessionID string, alreadyChecked bool) error {
|
func loginSessionID(sessionID string) error {
|
||||||
if !alreadyChecked {
|
out.Debug("Logging in via session id")
|
||||||
out.Debug("Logging in via session id")
|
if _, err := crunchyroll.LoginWithSessionID(sessionID, systemLocale(), client); err != nil {
|
||||||
if _, err := crunchyroll.LoginWithSessionID(sessionID, locale, client); err != nil {
|
out.Err(err.Error())
|
||||||
return err
|
os.Exit(1)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
out.Info("Due to security reasons, you have to login again on the next reboot")
|
|
||||||
return ioutil.WriteFile(sessionIDPath, []byte(sessionID), 0777)
|
return ioutil.WriteFile(loginStorePath(), []byte(sessionID), 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loginStorePath() string {
|
||||||
|
path := filepath.Join(os.TempDir(), ".crunchy")
|
||||||
|
if loginPersistentFlag {
|
||||||
|
if runtime.GOOS != "windows" {
|
||||||
|
usr, _ := user.Current()
|
||||||
|
path = filepath.Join(usr.HomeDir, ".config/crunchyroll-go")
|
||||||
|
}
|
||||||
|
|
||||||
|
out.Info("The login information will be stored permanently UNENCRYPTED on your drive (%s)", path)
|
||||||
|
} else {
|
||||||
|
out.Info("Due to security reasons, you have to login again on the next reboot")
|
||||||
|
}
|
||||||
|
|
||||||
|
return path
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ByteDream/crunchyroll-go"
|
"github.com/ByteDream/crunchyroll-go"
|
||||||
"github.com/ByteDream/crunchyroll-go/utils"
|
"github.com/ByteDream/crunchyroll-go/utils"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
@ -19,8 +19,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var sessionIDPath = filepath.Join(os.TempDir(), ".crunchy")
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
invalidWindowsChars = []string{"<", ">", ":", "\"", "/", "|", "\\", "?", "*"}
|
invalidWindowsChars = []string{"<", ">", ":", "\"", "/", "|", "\\", "?", "*"}
|
||||||
invalidLinuxChars = []string{"/"}
|
invalidLinuxChars = []string{"/"}
|
||||||
|
|
@ -95,32 +93,49 @@ func freeFileName(filename string) (string, bool) {
|
||||||
return filename, j != 0
|
return filename, j != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSessionID() (string, error) {
|
|
||||||
if _, stat := os.Stat(sessionIDPath); os.IsNotExist(stat) {
|
|
||||||
out.Err("To use this command, login first. Type `%s login -h` to get help", os.Args[0])
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
body, err := ioutil.ReadFile(sessionIDPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return strings.ReplaceAll(string(body), "\n", ""), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadCrunchy() {
|
func loadCrunchy() {
|
||||||
out.SetProgress("Logging in")
|
out.SetProgress("Logging in")
|
||||||
sessionID, err := loadSessionID()
|
|
||||||
if err == nil {
|
files := []string{filepath.Join(os.TempDir(), ".crunchy")}
|
||||||
if crunchy, err = crunchyroll.LoginWithSessionID(sessionID, systemLocale(), client); err != nil {
|
|
||||||
|
if runtime.GOOS != "windows" {
|
||||||
|
usr, _ := user.Current()
|
||||||
|
files = append(files, filepath.Join(usr.HomeDir, ".config/crunchyroll-go"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var body []byte
|
||||||
|
var err error
|
||||||
|
for _, file := range files {
|
||||||
|
if _, err = os.Stat(file); os.IsNotExist(err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
body, err = os.ReadFile(file)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if body == nil {
|
||||||
|
out.Err("To use this command, login first. Type `%s login -h` to get help", os.Args[0])
|
||||||
|
os.Exit(1)
|
||||||
|
} else if err != nil {
|
||||||
|
out.Err("Failed to read login information: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
split := strings.SplitN(string(body), "\n", 2)
|
||||||
|
if len(split) == 1 || split[2] == "" {
|
||||||
|
if crunchy, err = crunchyroll.LoginWithSessionID(split[0], systemLocale(), client); err != nil {
|
||||||
out.StopProgress(err.Error())
|
out.StopProgress(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
out.Debug("Logged in with session id %s. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", split[0])
|
||||||
} else {
|
} else {
|
||||||
out.StopProgress(err.Error())
|
if crunchy, err = crunchyroll.LoginWithCredentials(split[0], split[1], systemLocale(), client); err != nil {
|
||||||
os.Exit(1)
|
out.StopProgress(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
out.Debug("Logged in with username '%s' and password '%s'. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", split[0], split[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
out.StopProgress("Logged in")
|
out.StopProgress("Logged in")
|
||||||
out.Debug("Logged in with session id %s", sessionID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func hasFFmpeg() bool {
|
func hasFFmpeg() bool {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue