mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Add session id always cached in temp directory (to prevent #30)
This commit is contained in:
parent
192a85afb8
commit
afa975c459
2 changed files with 79 additions and 63 deletions
|
|
@ -5,9 +5,7 @@ import (
|
||||||
"github.com/ByteDream/crunchyroll-go/v2"
|
"github.com/ByteDream/crunchyroll-go/v2"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -21,11 +19,11 @@ var loginCmd = &cobra.Command{
|
||||||
Short: "Login to crunchyroll",
|
Short: "Login to crunchyroll",
|
||||||
Args: cobra.RangeArgs(1, 2),
|
Args: cobra.RangeArgs(1, 2),
|
||||||
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if loginSessionIDFlag {
|
if loginSessionIDFlag {
|
||||||
loginSessionID(args[0])
|
return loginSessionID(args[0])
|
||||||
} else {
|
} else {
|
||||||
loginCredentials(args[0], args[1])
|
return loginCredentials(args[0], args[1])
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -46,12 +44,31 @@ func init() {
|
||||||
|
|
||||||
func loginCredentials(user, password string) error {
|
func loginCredentials(user, password string) error {
|
||||||
out.Debug("Logging in via credentials")
|
out.Debug("Logging in via credentials")
|
||||||
if _, err := crunchyroll.LoginWithCredentials(user, password, systemLocale(false), client); err != nil {
|
c, err := crunchyroll.LoginWithCredentials(user, password, systemLocale(false), client)
|
||||||
out.Err(err.Error())
|
if err != nil {
|
||||||
os.Exit(1)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return os.WriteFile(loginStorePath(), []byte(fmt.Sprintf("%s\n%s", user, password)), 0600)
|
if loginPersistentFlag {
|
||||||
|
if configDir, err := os.UserConfigDir(); err != nil {
|
||||||
|
return fmt.Errorf("could not save credentials persistent: %w", err)
|
||||||
|
} else {
|
||||||
|
os.MkdirAll(filepath.Join(configDir, "crunchyroll-go"), 0755)
|
||||||
|
if err = os.WriteFile(filepath.Join(configDir, "crunchyroll-go", "crunchy"), []byte(fmt.Sprintf("%s\n%s", user, password)), 0600); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
out.Info("The login information will be stored permanently UNENCRYPTED on your drive (%s)", filepath.Join(configDir, "crunchyroll-go", "crunchy"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err = os.WriteFile(filepath.Join(os.TempDir(), ".crunchy"), []byte(c.SessionID), 0600); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !loginPersistentFlag {
|
||||||
|
out.Info("Due to security reasons, you have to login again on the next reboot")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func loginSessionID(sessionID string) error {
|
func loginSessionID(sessionID string) error {
|
||||||
|
|
@ -61,21 +78,25 @@ func loginSessionID(sessionID string) error {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return os.WriteFile(loginStorePath(), []byte(sessionID), 0600)
|
var err error
|
||||||
}
|
|
||||||
|
|
||||||
func loginStorePath() string {
|
|
||||||
path := filepath.Join(os.TempDir(), ".crunchy")
|
|
||||||
if loginPersistentFlag {
|
if loginPersistentFlag {
|
||||||
if runtime.GOOS != "windows" {
|
if configDir, err := os.UserConfigDir(); err != nil {
|
||||||
usr, _ := user.Current()
|
return fmt.Errorf("could not save credentials persistent: %w", err)
|
||||||
path = filepath.Join(usr.HomeDir, ".config/crunchy")
|
} else {
|
||||||
|
os.MkdirAll(filepath.Join(configDir, "crunchyroll-go"), 0755)
|
||||||
|
if err = os.WriteFile(filepath.Join(configDir, "crunchyroll-go", "crunchy"), []byte(sessionID), 0600); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
out.Info("The login information will be stored permanently UNENCRYPTED on your drive (%s)", filepath.Join(configDir, "crunchyroll-go", "crunchy"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err = os.WriteFile(filepath.Join(os.TempDir(), ".crunchy"), []byte(sessionID), 0600); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
out.Info("The login information will be stored permanently UNENCRYPTED on your drive (%s)", path)
|
if !loginPersistentFlag {
|
||||||
} else if runtime.GOOS != "windows" {
|
|
||||||
out.Info("Due to security reasons, you have to login again on the next reboot")
|
out.Info("Due to security reasons, you have to login again on the next reboot")
|
||||||
}
|
}
|
||||||
|
|
||||||
return path
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/user"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
@ -141,57 +140,53 @@ func freeFileName(filename string) (string, bool) {
|
||||||
func loadCrunchy() {
|
func loadCrunchy() {
|
||||||
out.SetProgress("Logging in")
|
out.SetProgress("Logging in")
|
||||||
|
|
||||||
files := []string{filepath.Join(os.TempDir(), ".crunchy")}
|
if configDir, err := os.UserConfigDir(); err == nil {
|
||||||
|
persistentFilePath := filepath.Join(configDir, "crunchyroll-go", "crunchy")
|
||||||
if runtime.GOOS != "windows" {
|
if _, statErr := os.Stat(persistentFilePath); statErr == nil {
|
||||||
usr, _ := user.Current()
|
body, err := os.ReadFile(persistentFilePath)
|
||||||
files = append(files, filepath.Join(usr.HomeDir, ".config/crunchy"))
|
if err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
for _, file := range files {
|
|
||||||
if _, err = os.Stat(file); os.IsNotExist(err) {
|
|
||||||
err = nil
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var body []byte
|
|
||||||
if body, err = os.ReadFile(file); err != nil {
|
|
||||||
out.StopProgress("Failed to read login information: %v", err)
|
out.StopProgress("Failed to read login information: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else if body == nil {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
split := strings.SplitN(string(body), "\n", 2)
|
split := strings.SplitN(string(body), "\n", 2)
|
||||||
if len(split) == 1 || split[1] == "" {
|
if len(split) == 1 || split[1] == "" {
|
||||||
if crunchy, err = crunchyroll.LoginWithSessionID(split[0], systemLocale(true), client); err == nil {
|
split[0] = url.QueryEscape(split[0])
|
||||||
out.Debug("Logged in with session id %s. BLANK THIS LINE OUT IF YOU'RE ASKED TO POST THE DEBUG OUTPUT SOMEWHERE", split[0])
|
if crunchy, err = crunchyroll.LoginWithSessionID(split[0], systemLocale(true), client); err != nil {
|
||||||
}
|
out.StopProgress(err.Error())
|
||||||
} else {
|
|
||||||
if crunchy, err = crunchyroll.LoginWithCredentials(split[0], split[1], systemLocale(true), client); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
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])
|
|
||||||
if file != filepath.Join(os.TempDir(), ".crunchy") {
|
|
||||||
// the session id is written to a temp file to reduce the amount of re-logging in.
|
|
||||||
// it seems like that crunchyroll has also a little cooldown if a user logs in too often in a short time
|
|
||||||
if err = os.WriteFile(filepath.Join(os.TempDir(), ".crunchy"), []byte(crunchy.SessionID), 0600); err != nil {
|
|
||||||
out.StopProgress("Failed to write session id to temp file")
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
out.Debug("Wrote session id to temp file")
|
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 {
|
||||||
|
if crunchy, err = crunchyroll.LoginWithCredentials(split[0], split[1], systemLocale(true), client); err != nil {
|
||||||
|
out.StopProgress(err.Error())
|
||||||
|
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", crunchy.SessionID)
|
||||||
|
// the session id is written to a temp file to reduce the amount of re-logging in.
|
||||||
|
// it seems like that crunchyroll has also a little cooldown if a user logs in too often in a short time
|
||||||
|
os.WriteFile(filepath.Join(os.TempDir(), ".crunchy"), []byte(crunchy.SessionID), 0600)
|
||||||
}
|
}
|
||||||
|
|
||||||
out.StopProgress("Logged in")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
out.StopProgress(err.Error())
|
|
||||||
} else {
|
|
||||||
out.StopProgress("To use this command, login first. Type `%s login -h` to get help", os.Args[0])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tmpFilePath := filepath.Join(os.TempDir(), ".crunchy")
|
||||||
|
if _, statErr := os.Stat(tmpFilePath); !os.IsNotExist(statErr) {
|
||||||
|
body, err := os.ReadFile(tmpFilePath)
|
||||||
|
if err != nil {
|
||||||
|
out.StopProgress("Failed to read login information: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if crunchy, err = crunchyroll.LoginWithSessionID(url.QueryEscape(string(body)), systemLocale(true), client); err != nil {
|
||||||
|
out.StopProgress(err.Error())
|
||||||
|
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", body)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
out.StopProgress("To use this command, login first. Type `%s login -h` to get help", os.Args[0])
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue